]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
02a36bc9 DR |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2013 Dave Reisner | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
b5efdb8a | 21 | #include "alloc-util.h" |
cf0fbc49 | 22 | #include "string-util.h" |
02a36bc9 DR |
23 | #include "utf8.h" |
24 | #include "util.h" | |
25 | ||
7991ac34 DR |
26 | static void test_utf8_is_printable(void) { |
27 | assert_se(utf8_is_printable("ascii is valid\tunicode", 22)); | |
28 | assert_se(utf8_is_printable("\342\204\242", 3)); | |
29 | assert_se(!utf8_is_printable("\341\204", 2)); | |
a7176505 | 30 | assert_se(utf8_is_printable("ąę", 4)); |
7991ac34 DR |
31 | } |
32 | ||
02a36bc9 DR |
33 | static void test_utf8_is_valid(void) { |
34 | assert_se(utf8_is_valid("ascii is valid unicode")); | |
8f6ce71f | 35 | assert_se(utf8_is_valid("\342\204\242")); |
02a36bc9 DR |
36 | assert_se(!utf8_is_valid("\341\204")); |
37 | } | |
38 | ||
e7363c59 DR |
39 | static void test_ascii_is_valid(void) { |
40 | assert_se(ascii_is_valid("alsdjf\t\vbarr\nba z")); | |
41 | assert_se(!ascii_is_valid("\342\204\242")); | |
42 | assert_se(!ascii_is_valid("\341\204")); | |
43 | } | |
44 | ||
e7363c59 DR |
45 | static void test_utf8_encoded_valid_unichar(void) { |
46 | assert_se(utf8_encoded_valid_unichar("\342\204\242") == 3); | |
47 | assert_se(utf8_encoded_valid_unichar("\302\256") == 2); | |
48 | assert_se(utf8_encoded_valid_unichar("a") == 1); | |
49 | assert_se(utf8_encoded_valid_unichar("\341\204") < 0); | |
50 | assert_se(utf8_encoded_valid_unichar("\341\204\341\204") < 0); | |
e7363c59 DR |
51 | } |
52 | ||
550a40ec ZJS |
53 | static void test_utf8_escaping(void) { |
54 | _cleanup_free_ char *p1, *p2, *p3; | |
55 | ||
56 | p1 = utf8_escape_invalid("goo goo goo"); | |
57 | puts(p1); | |
58 | assert_se(utf8_is_valid(p1)); | |
59 | ||
60 | p2 = utf8_escape_invalid("\341\204\341\204"); | |
61 | puts(p2); | |
62 | assert_se(utf8_is_valid(p2)); | |
63 | ||
64 | p3 = utf8_escape_invalid("\341\204"); | |
65 | puts(p3); | |
66 | assert_se(utf8_is_valid(p3)); | |
67 | } | |
68 | ||
fec84576 | 69 | static void test_utf8_escaping_printable(void) { |
3c6d3052 | 70 | _cleanup_free_ char *p1, *p2, *p3, *p4, *p5, *p6; |
fec84576 WC |
71 | |
72 | p1 = utf8_escape_non_printable("goo goo goo"); | |
73 | puts(p1); | |
74 | assert_se(utf8_is_valid(p1)); | |
75 | ||
76 | p2 = utf8_escape_non_printable("\341\204\341\204"); | |
77 | puts(p2); | |
78 | assert_se(utf8_is_valid(p2)); | |
79 | ||
80 | p3 = utf8_escape_non_printable("\341\204"); | |
81 | puts(p3); | |
82 | assert_se(utf8_is_valid(p3)); | |
83 | ||
84 | p4 = utf8_escape_non_printable("ąę\n가너도루\n1234\n\341\204\341\204\n\001 \019\20\a"); | |
85 | puts(p4); | |
86 | assert_se(utf8_is_valid(p4)); | |
87 | ||
88 | p5 = utf8_escape_non_printable("\001 \019\20\a"); | |
89 | puts(p5); | |
90 | assert_se(utf8_is_valid(p5)); | |
3c6d3052 LP |
91 | |
92 | p6 = utf8_escape_non_printable("\xef\xbf\x30\x13"); | |
93 | puts(p6); | |
94 | assert_se(utf8_is_valid(p6)); | |
fec84576 WC |
95 | } |
96 | ||
04166cb7 TG |
97 | static void test_utf16_to_utf8(void) { |
98 | char *a = NULL; | |
502184de | 99 | const uint16_t utf16[] = { htole16('a'), htole16(0xd800), htole16('b'), htole16(0xdc00), htole16('c'), htole16(0xd801), htole16(0xdc37) }; |
04166cb7 TG |
100 | const char utf8[] = { 'a', 'b', 'c', 0xf0, 0x90, 0x90, 0xb7, 0 }; |
101 | ||
102 | a = utf16_to_utf8(utf16, 14); | |
103 | assert_se(a); | |
104 | assert_se(streq(a, utf8)); | |
105 | ||
106 | free(a); | |
107 | } | |
108 | ||
02a36bc9 DR |
109 | int main(int argc, char *argv[]) { |
110 | test_utf8_is_valid(); | |
7991ac34 | 111 | test_utf8_is_printable(); |
e7363c59 | 112 | test_ascii_is_valid(); |
e7363c59 | 113 | test_utf8_encoded_valid_unichar(); |
550a40ec | 114 | test_utf8_escaping(); |
fec84576 | 115 | test_utf8_escaping_printable(); |
04166cb7 | 116 | test_utf16_to_utf8(); |
7991ac34 DR |
117 | |
118 | return 0; | |
02a36bc9 | 119 | } |