chiark / gitweb /
machine: make sure unpriviliged "machinectl status" can show the machine's OS version
[elogind.git] / src / libsystemd-terminal / test-term-parser.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2 /***
3   This file is part of systemd.
4
5   Copyright (C) 2014 David Herrmann <dh.herrmann@gmail.com>
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
21 /*
22  * Terminal Parser Tests
23  */
24
25 #include <assert.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include "macro.h"
31 #include "term-internal.h"
32 #include "util.h"
33
34 static void test_term_utf8_invalid(void) {
35         term_utf8 p = { };
36         const uint32_t *res;
37         size_t len;
38
39         res = term_utf8_decode(NULL, NULL, 0);
40         assert_se(res == NULL);
41
42         res = term_utf8_decode(&p, NULL, 0);
43         assert_se(res != NULL);
44
45         len = 5;
46         res = term_utf8_decode(NULL, &len, 0);
47         assert_se(res == NULL);
48         assert_se(len == 0);
49
50         len = 5;
51         res = term_utf8_decode(&p, &len, 0);
52         assert_se(res != NULL);
53         assert_se(len == 1);
54
55         len = 5;
56         res = term_utf8_decode(&p, &len, 0xCf);
57         assert_se(res == NULL);
58         assert_se(len == 0);
59
60         len = 5;
61         res = term_utf8_decode(&p, &len, 0x0);
62         assert_se(res != NULL);
63         assert_se(len == 2);
64 }
65
66 static void test_term_utf8_range(void) {
67         term_utf8 p = { };
68         const uint32_t *res;
69         char u8[4];
70         uint32_t i, j;
71         size_t ulen, len;
72
73         /* Convert all ucs-4 chars to utf-8 and back */
74
75         for (i = 0; i < 0x10FFFF; ++i) {
76                 ulen = term_utf8_encode(u8, i);
77                 if (!ulen)
78                         continue;
79
80                 for (j = 0; j < ulen; ++j) {
81                         res = term_utf8_decode(&p, &len, u8[j]);
82                         if (!res) {
83                                 assert_se(j + 1 != ulen);
84                                 continue;
85                         }
86
87                         assert_se(j + 1 == ulen);
88                         assert_se(len == 1 && *res == i);
89                         assert_se(i <= 127 || ulen >= 2);
90                 }
91         }
92 }
93
94 static void test_term_utf8_mix(void) {
95         static const char source[] = {
96                 0x00,                           /* normal 0 */
97                 0xC0, 0x80,                     /* overlong 0 */
98                 0xC0, 0x81,                     /* overlong 1 */
99                 0xE0, 0x80, 0x81,               /* overlong 1 */
100                 0xF0, 0x80, 0x80, 0x81,         /* overlong 1 */
101                 0xC0, 0x00,                     /* invalid continuation */
102                 0xC0, 0xC0, 0x81,               /* invalid continuation with a following overlong 1 */
103                 0xF8, 0x80, 0x80, 0x80, 0x81,   /* overlong 1 with 5 bytes */
104                 0xE0, 0x80, 0xC0, 0x81,         /* invalid 3-byte followed by valid 2-byte */
105                 0xF0, 0x80, 0x80, 0xC0, 0x81,   /* invalid 4-byte followed by valid 2-byte */
106         };
107         static const uint32_t result[] = {
108                 0x0000,
109                 0x0000,
110                 0x0001,
111                 0x0001,
112                 0x0001,
113                 0x00C0, 0x0000,
114                 0x00C0, 0x0001,
115                 0x00F8, 0x0080, 0x0080, 0x0080, 0x0081,
116                 0x00E0, 0x0080, 0x0001,
117                 0x00F0, 0x0080, 0x0080, 0x0001,
118         };
119         term_utf8 p = { };
120         const uint32_t *res;
121         unsigned int i, j;
122         size_t len;
123
124         for (i = 0, j = 0; i < sizeof(source); ++i) {
125                 res = term_utf8_decode(&p, &len, source[i]);
126                 if (!res)
127                         continue;
128
129                 assert_se(j + len <= ELEMENTSOF(result));
130                 assert_se(!memcmp(res, &result[j], sizeof(uint32_t) * len));
131                 j += len;
132         }
133
134         assert_se(j == ELEMENTSOF(result));
135 }
136
137 int main(int argc, char *argv[]) {
138         test_term_utf8_invalid();
139         test_term_utf8_range();
140         test_term_utf8_mix();
141
142         return 0;
143 }