compose.c: fix untrusted source issue
[platform/upstream/libxkbcommon.git] / test / atom.c
1 /*
2  * Copyright © 2012 Ran Benita <ran234@gmail.com>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23
24 #include "config.h"
25
26 #include <time.h>
27
28 #include "test.h"
29 #include "atom.h"
30
31 #define INTERN_LITERAL(table, literal) \
32     atom_intern(table, literal, sizeof(literal) - 1, true)
33
34 #define LOOKUP_LITERAL(table, literal) \
35     atom_intern(table, literal, sizeof(literal) - 1, false)
36
37 static void
38 random_string(char **str_out, size_t *len_out)
39 {
40     /* Keep this small, so collisions might happen. */
41     static const char random_chars[] = {
42         'a', 'b', 'c', 'd', 'e', 'f', 'g'
43     };
44
45     size_t len;
46     char *str;
47     unsigned int seed = (unsigned int)time(NULL);
48
49     len = rand_r(&seed) % 15;
50     str = malloc(len + 1);
51     assert(str);
52
53     for (size_t i = 0; i < len; i++)
54         str[i] = random_chars[rand_r(&seed) % ARRAY_SIZE(random_chars)];
55     /* Don't always terminate it; should work without. */
56     if (rand_r(&seed) % 2 == 0)
57         str[len] = '\0';
58
59     *str_out = str;
60     *len_out = len;
61 }
62
63 static void
64 test_random_strings(void)
65 {
66     struct atom_string {
67         xkb_atom_t atom;
68         char *string;
69         size_t len;
70     };
71
72     struct atom_table *table;
73     struct atom_string *arr;
74     int N;
75     xkb_atom_t atom;
76     const char *string;
77
78     table = atom_table_new();
79     assert(table);
80
81     unsigned seed = (unsigned) clock();
82     srand(seed);
83
84     N = 1 + rand_r(&seed) % 100000;
85     arr = calloc(N, sizeof(*arr));
86     assert(arr);
87
88     for (int i = 0; i < N; i++) {
89         random_string(&arr[i].string, &arr[i].len);
90
91         atom = atom_intern(table, arr[i].string, arr[i].len, false);
92         if (atom != XKB_ATOM_NONE) {
93             string = atom_text(table, atom);
94             assert(string);
95
96             if (arr[i].len != strlen(string) ||
97                 strncmp(string, arr[i].string, arr[i].len) != 0) {
98                 fprintf(stderr, "got a collision, but strings don't match!\n");
99                 fprintf(stderr, "existing length %zu, string %s\n",
100                         strlen(string), string);
101                 fprintf(stderr, "new length %zu, string %.*s\n",
102                         arr[i].len, (int) arr[i].len, arr[i].string);
103                 fprintf(stderr, "seed: %u\n", seed);
104                 assert(false);
105             }
106
107             /* OK, got a real collision. */
108             free(arr[i].string);
109             i--;
110             continue;
111         }
112
113         arr[i].atom = atom_intern(table, arr[i].string, arr[i].len, true);
114         if (arr[i].atom == XKB_ATOM_NONE) {
115             fprintf(stderr, "failed to intern! len: %zu, string: %.*s\n",
116                     arr[i].len, (int) arr[i].len, arr[i].string);
117             fprintf(stderr, "seed: %u\n", seed);
118             assert(false);
119         }
120     }
121
122     for (int i = 0; i < N; i++) {
123         string = atom_text(table, arr[i].atom);
124         assert(string);
125
126         if (arr[i].len != strlen(string) ||
127             strncmp(string, arr[i].string, arr[i].len) != 0) {
128             fprintf(stderr, "looked-up string doesn't match!\n");
129             fprintf(stderr, "found length %zu, string %s\n",
130                     strlen(string), string);
131             fprintf(stderr, "expected length %zu, string %.*s\n",
132                     arr[i].len, (int) arr[i].len, arr[i].string);
133
134             /* Since this is random, we need to dump the failing data,
135              * so we might have some chance to reproduce. */
136             fprintf(stderr, "START dump of arr, N=%d\n", N);
137             for (int j = 0; j < N; j++) {
138                 fprintf(stderr, "%u\t\t%zu\t\t%.*s\n", arr[i].atom,
139                         arr[i].len, (int) arr[i].len, arr[i].string);
140             }
141             fprintf(stderr, "END\n");
142
143             fprintf(stderr, "seed: %u\n", seed);
144             assert(false);
145         }
146     }
147
148     for (int i = 0; i < N; i++)
149         free(arr[i].string);
150     free(arr);
151     atom_table_free(table);
152 }
153
154 int
155 main(void)
156 {
157     struct atom_table *table;
158     xkb_atom_t atom1, atom2, atom3;
159
160     table = atom_table_new();
161     assert(table);
162
163     assert(atom_text(table, XKB_ATOM_NONE) == NULL);
164     assert(atom_intern(table, NULL, 0, false) == XKB_ATOM_NONE);
165
166     atom1 = INTERN_LITERAL(table, "hello");
167     assert(atom1 != XKB_ATOM_NONE);
168     assert(atom1 == LOOKUP_LITERAL(table, "hello"));
169     assert(streq(atom_text(table, atom1), "hello"));
170
171     atom2 = atom_intern(table, "hello", 3, true);
172     assert(atom2 != XKB_ATOM_NONE);
173     assert(atom1 != atom2);
174     assert(streq(atom_text(table, atom2), "hel"));
175     assert(LOOKUP_LITERAL(table, "hel") == atom2);
176     assert(LOOKUP_LITERAL(table, "hell") == XKB_ATOM_NONE);
177     assert(LOOKUP_LITERAL(table, "hello") == atom1);
178
179     atom3 = atom_intern(table, "", 0, true);
180     assert(atom3 != XKB_ATOM_NONE);
181     assert(LOOKUP_LITERAL(table, "") == atom3);
182
183     atom_table_free(table);
184
185     test_random_strings();
186
187     return 0;
188 }