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