]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2011 New Dream Network | |
7 | * | |
8 | * This is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License version 2.1, as published by the Free Software | |
11 | * Foundation. See file COPYING. | |
12 | * | |
13 | */ | |
14 | #include "common/utf8.h" | |
15 | #include "gtest/gtest.h" | |
16 | #include <stdint.h> | |
17 | ||
18 | TEST(IsValidUtf8, SimpleAscii) { | |
19 | ASSERT_EQ(0, check_utf8_cstr("Ascii ftw.")); | |
20 | ASSERT_EQ(0, check_utf8_cstr("")); | |
21 | ASSERT_EQ(0, check_utf8_cstr("B")); | |
22 | ASSERT_EQ(0, check_utf8_cstr("Badgers badgers badgers badgers " | |
23 | "mushroom mushroom")); | |
24 | ASSERT_EQ(0, check_utf8("foo", strlen("foo"))); | |
25 | } | |
26 | ||
27 | TEST(IsValidUtf8, ControlChars) { | |
28 | // Sadly, control characters are valid utf8... | |
29 | uint8_t control_chars[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | |
30 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d }; | |
31 | ASSERT_EQ(0, check_utf8((char*)control_chars, sizeof(control_chars))); | |
32 | } | |
33 | ||
34 | TEST(IsValidUtf8, SimpleUtf8) { | |
35 | uint8_t funkystr[] = { 0x66, 0xd1, 0x86, 0xd1, 0x9d, 0xd2, 0xa0, 0xd3, | |
36 | 0xad, 0xd3, 0xae, 0x0a }; | |
37 | ASSERT_EQ(0, check_utf8((char*)funkystr, sizeof(funkystr))); | |
38 | ||
39 | uint8_t valid2[] = { 0xc3, 0xb1 }; | |
40 | ASSERT_EQ(0, check_utf8((char*)valid2, sizeof(valid2))); | |
41 | } | |
42 | ||
43 | TEST(IsValidUtf8, InvalidUtf8) { | |
44 | uint8_t inval[] = { 0xe2, 0x28, 0xa1 }; | |
45 | ASSERT_NE(0, check_utf8((char*)inval, sizeof(inval))); | |
46 | ||
47 | uint8_t invalid2[] = { 0xc3, 0x28 }; | |
48 | ASSERT_NE(0, check_utf8((char*)invalid2, sizeof(invalid2))); | |
49 | } | |
50 | ||
51 | TEST(HasControlChars, HasControlChars1) { | |
52 | uint8_t has_control_chars[] = { 0x41, 0x01, 0x00 }; | |
53 | ASSERT_NE(0, check_for_control_characters_cstr((const char*)has_control_chars)); | |
54 | uint8_t has_control_chars2[] = { 0x7f, 0x41, 0x00 }; | |
55 | ASSERT_NE(0, check_for_control_characters_cstr((const char*)has_control_chars2)); | |
56 | ||
57 | char has_newline[] = "blah blah\n"; | |
58 | ASSERT_NE(0, check_for_control_characters_cstr(has_newline)); | |
59 | ||
60 | char no_control_chars[] = "blah blah"; | |
61 | ASSERT_EQ(0, check_for_control_characters_cstr(no_control_chars)); | |
62 | ||
63 | uint8_t validutf[] = { 0x66, 0xd1, 0x86, 0xd1, 0x9d, 0xd2, 0xa0, 0xd3, | |
64 | 0xad, 0xd3, 0xae, 0x0 }; | |
65 | ASSERT_EQ(0, check_for_control_characters_cstr((const char*)validutf)); | |
66 | } |