Line data Source code
1 : #include "passgen/util/utf8.h"
2 : #include "tests.h"
3 :
4 : // test that we can decode an empty string.
5 1 : test_result test_utf8_can_decode_empty(void) {
6 1 : size_t input_len = 0;
7 1 : const uint8_t input[input_len + 1];
8 1 : size_t output_len = 0;
9 1 : uint32_t output[output_len + 1];
10 1 : const uint8_t *input_pos = &input[0];
11 1 : uint32_t *output_pos = &output[0];
12 :
13 1 : int ret = passgen_utf8_decode(
14 : &output_pos,
15 : output_len,
16 : NULL,
17 : &input_pos,
18 : input_len);
19 :
20 1 : assert(ret == PASSGEN_UTF8_SUCCESS);
21 1 : assert(output_pos == &output[0]);
22 1 : assert(input_pos == &input[0]);
23 :
24 1 : return test_ok;
25 : }
26 :
27 : // test that we can decode a simple utf-8 sequence.
28 1 : test_result test_utf8_can_decode_simple(void) {
29 : // üðÂÂÂõ
30 1 : const uint8_t input[] =
31 : {0xc3, 0xbc, 0xf0, 0x9f, 0x98, 0x82, 0xc2, 0xb5, 0x0a};
32 1 : size_t input_len = sizeof(input);
33 :
34 1 : size_t output_len = 100;
35 1 : uint32_t output[output_len];
36 :
37 1 : const uint8_t *input_pos = &input[0];
38 1 : uint32_t *output_pos = &output[0];
39 :
40 1 : int ret = passgen_utf8_decode(
41 : &output_pos,
42 : output_len,
43 : NULL,
44 : &input_pos,
45 : input_len);
46 :
47 : // succesful return
48 1 : assert(ret == PASSGEN_UTF8_SUCCESS);
49 :
50 : // parsed all data
51 1 : assert(input_pos == &input[input_len]);
52 1 : assert(output_pos == &output[4]);
53 :
54 : // parsed right characters
55 1 : assert(output[0] == 0xFC);
56 1 : assert(output[1] == 0x1F602);
57 1 : assert(output[2] == 0xB5);
58 1 : assert(output[3] == 0x0A);
59 :
60 1 : return test_ok;
61 : }
62 :
63 : // test that we can decode a simple utf-8 sequence with character widths.
64 1 : test_result test_utf8_can_decode_simple_widths(void) {
65 : // üðÂÂÂõ
66 1 : const uint8_t input[] =
67 : {0xc3, 0xbc, 0xf0, 0x9f, 0x98, 0x82, 0xc2, 0xb5, 0x0a};
68 1 : size_t input_len = sizeof(input);
69 :
70 1 : size_t output_len = 100;
71 1 : uint32_t output[output_len];
72 1 : uint8_t widths[output_len];
73 :
74 1 : const uint8_t *input_pos = &input[0];
75 1 : uint32_t *output_pos = &output[0];
76 :
77 1 : int ret = passgen_utf8_decode(
78 : &output_pos,
79 : output_len,
80 : &widths[0],
81 : &input_pos,
82 : input_len);
83 :
84 : // successful return
85 1 : assert(ret == PASSGEN_UTF8_SUCCESS);
86 :
87 : // processed all input data
88 1 : assert(input_pos == &input[input_len]);
89 1 : assert(output_pos == &output[4]);
90 :
91 : // parsed characters properly
92 1 : assert(output[0] == 0xFC);
93 1 : assert(output[1] == 0x1F602);
94 1 : assert(output[2] == 0xB5);
95 1 : assert(output[3] == 0x0A);
96 :
97 : // parsed widths correctly
98 1 : assert(widths[0] == 2);
99 1 : assert(widths[1] == 4);
100 1 : assert(widths[2] == 2);
101 1 : assert(widths[3] == 1);
102 :
103 1 : return test_ok;
104 : }
105 :
106 : // test that we can decode a simple UTF-8 string, if there is not enough
107 : // space in the output array, by restarting the decoding.
108 1 : test_result test_utf8_decode_short_output(void) {
109 : // üðÂÂÂõ
110 1 : const uint8_t input[] =
111 : {0xc3, 0xbc, 0xf0, 0x9f, 0x98, 0x82, 0xc2, 0xb5, 0x0a};
112 1 : size_t input_len = sizeof(input);
113 :
114 1 : size_t output_len = 3;
115 1 : uint32_t output[output_len];
116 :
117 1 : const uint8_t *input_pos = &input[0];
118 1 : uint32_t *output_pos = &output[0];
119 :
120 1 : int ret = passgen_utf8_decode(
121 : &output_pos,
122 : output_len,
123 : NULL,
124 : &input_pos,
125 : input_len);
126 :
127 : // on first run, it should only decode the first three characters. it
128 : // should return larger than zero because there is still content to be
129 : // decoded.
130 1 : assert(ret == PASSGEN_UTF8_OUTPUT_SIZE);
131 1 : assert(input_pos == &input[8]);
132 1 : assert(output_pos == &output[output_len]);
133 1 : assert(output[0] == 0xFC);
134 1 : assert(output[1] == 0x1F602);
135 1 : assert(output[2] == 0xB5);
136 :
137 : // reset where the current output pointer is at
138 1 : output_pos = &output[0];
139 :
140 : // continue parsing the rest of the utf-8 sequence.
141 1 : ret = passgen_utf8_decode(
142 : &output_pos,
143 : output_len,
144 : NULL,
145 : &input_pos,
146 : input_len - 8);
147 :
148 : // on second run, the final character is decoded.
149 1 : assert(ret == PASSGEN_UTF8_SUCCESS);
150 1 : assert(input_pos == &input[input_len]);
151 1 : assert(output_pos == &output[1]);
152 1 : assert(output[0] == 0x0A);
153 :
154 1 : return test_ok;
155 : }
156 :
157 1 : test_result test_utf8_encode_simple(void) {
158 : // test that we can decode a simple UTF-8 string.
159 :
160 : // üðÂÂÂõ
161 1 : const uint8_t expected[] =
162 : {0xc3, 0xbc, 0xf0, 0x9f, 0x98, 0x82, 0xc2, 0xb5, 0x0a};
163 :
164 1 : const uint32_t input[] = {0xFC, 0x1F602, 0xB5, 0x0A};
165 :
166 1 : size_t output_len = 15;
167 1 : uint8_t output[output_len];
168 :
169 1 : size_t in_pos = 0;
170 1 : size_t out_pos = 0;
171 :
172 1 : int ret = passgen_utf8_encode(
173 : output,
174 : output_len,
175 : &out_pos,
176 : input,
177 : sizeof(input) / sizeof(input[0]),
178 : &in_pos);
179 :
180 1 : assert(ret == PASSGEN_UTF8_SUCCESS);
181 1 : assert(in_pos == (sizeof(input) / sizeof(input[0])));
182 1 : assert(out_pos == (sizeof(expected) / sizeof(expected[0])));
183 10 : for(size_t i = 0; i < out_pos; i++) {
184 9 : assert(output[i] == expected[i]);
185 : }
186 :
187 1 : return test_ok;
188 : }
|