Merge branch 'maint-0.4.5' into release-0.4.5
[tor.git] / src / test / test_namemap.c
blobe93d3fbc3c6f1253f28af7ea081e117cc1e27946
1 /* Copyright (c) 2018-2020, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #include "test/test.h"
6 #include "lib/cc/torint.h"
7 #include "lib/container/namemap.h"
8 #include "lib/container/namemap_st.h"
9 #include "lib/malloc/malloc.h"
11 #include <stdio.h>
12 #include <string.h>
14 static void
15 test_namemap_empty(void *arg)
17 (void)arg;
19 namemap_t m;
20 namemap_init(&m);
21 namemap_t m2 = NAMEMAP_INIT();
23 tt_uint_op(0, OP_EQ, namemap_get_size(&m));
24 tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m, "hello"));
25 tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m, "hello"));
26 tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m, "hello128"));
27 tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m, ""));
28 tt_uint_op(0, OP_EQ, namemap_get_size(&m));
30 tt_uint_op(0, OP_EQ, namemap_get_size(&m2));
31 tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m2, "hello"));
32 tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m2, "hello"));
33 tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m2, "hello128"));
34 tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m2, ""));
35 tt_uint_op(0, OP_EQ, namemap_get_size(&m));
37 done:
38 namemap_clear(&m);
39 namemap_clear(&m2);
42 static void
43 test_namemap_toolong(void *arg)
45 (void)arg;
46 namemap_t m;
47 char *ok = NULL;
48 char *toolong = NULL;
49 namemap_init(&m);
51 ok = tor_malloc_zero(MAX_NAMEMAP_NAME_LEN+1);
52 memset(ok, 'x', MAX_NAMEMAP_NAME_LEN);
54 toolong = tor_malloc_zero(MAX_NAMEMAP_NAME_LEN+2);
55 memset(toolong, 'x', MAX_NAMEMAP_NAME_LEN+1);
57 tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m, ok));
58 tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m, toolong));
59 unsigned u1 = namemap_get_or_create_id(&m, toolong);
60 unsigned u2 = namemap_get_or_create_id(&m, ok);
61 tt_uint_op(u1, OP_EQ, NAMEMAP_ERR);
62 tt_uint_op(u2, OP_NE, NAMEMAP_ERR);
63 tt_uint_op(u2, OP_EQ, namemap_get_id(&m, ok));
64 tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m, toolong));
66 tt_str_op(ok, OP_EQ, namemap_get_name(&m, u2));
67 tt_ptr_op(NULL, OP_EQ, namemap_get_name(&m, u1));
69 done:
70 tor_free(ok);
71 tor_free(toolong);
72 namemap_clear(&m);
75 static void
76 test_namemap_blackbox(void *arg)
78 (void)arg;
80 namemap_t m1, m2;
81 namemap_init(&m1);
82 namemap_init(&m2);
84 unsigned u1 = namemap_get_or_create_id(&m1, "hello");
85 unsigned u2 = namemap_get_or_create_id(&m1, "world");
86 tt_uint_op(u1, OP_NE, NAMEMAP_ERR);
87 tt_uint_op(u2, OP_NE, NAMEMAP_ERR);
88 tt_uint_op(u1, OP_NE, u2);
90 tt_uint_op(u1, OP_EQ, namemap_get_id(&m1, "hello"));
91 tt_uint_op(u1, OP_EQ, namemap_get_or_create_id(&m1, "hello"));
92 tt_uint_op(u2, OP_EQ, namemap_get_id(&m1, "world"));
93 tt_uint_op(u2, OP_EQ, namemap_get_or_create_id(&m1, "world"));
95 tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m1, "HELLO"));
96 tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m2, "hello"));
98 unsigned u3 = namemap_get_or_create_id(&m2, "hola");
99 tt_uint_op(u3, OP_NE, NAMEMAP_ERR);
100 tt_uint_op(NAMEMAP_ERR, OP_EQ, namemap_get_id(&m1, "hola"));
101 tt_uint_op(u3, OP_EQ, namemap_get_or_create_id(&m2, "hola"));
102 tt_uint_op(u3, OP_EQ, namemap_get_id(&m2, "hola"));
104 unsigned int u4 = namemap_get_or_create_id(&m1, "hola");
105 tt_uint_op(u4, OP_NE, NAMEMAP_ERR);
106 tt_uint_op(u4, OP_EQ, namemap_get_id(&m1, "hola"));
107 tt_uint_op(u3, OP_EQ, namemap_get_id(&m2, "hola"));
109 tt_str_op("hello", OP_EQ, namemap_get_name(&m1, u1));
110 tt_str_op("world", OP_EQ, namemap_get_name(&m1, u2));
111 tt_str_op("hola", OP_EQ, namemap_get_name(&m2, u3));
112 tt_str_op("hola", OP_EQ, namemap_get_name(&m1, u4));
114 tt_ptr_op(NULL, OP_EQ, namemap_get_name(&m2, u3 + 10));
116 done:
117 namemap_clear(&m1);
118 namemap_clear(&m2);
121 static void
122 test_namemap_internals(void *arg)
124 (void)arg;
125 // This test actually assumes know something about the identity layout.
126 namemap_t m;
127 namemap_init(&m);
129 tt_uint_op(0, OP_EQ, namemap_get_or_create_id(&m, "that"));
130 tt_uint_op(0, OP_EQ, namemap_get_or_create_id(&m, "that"));
131 tt_uint_op(1, OP_EQ, namemap_get_or_create_id(&m, "is"));
132 tt_uint_op(1, OP_EQ, namemap_get_or_create_id(&m, "is"));
134 tt_uint_op(0, OP_EQ, namemap_get_id(&m, "that"));
135 tt_uint_op(0, OP_EQ, namemap_get_id(&m, "that"));
136 tt_uint_op(1, OP_EQ, namemap_get_id(&m, "is"));
137 tt_uint_op(2, OP_EQ, namemap_get_or_create_id(&m, "not"));
138 tt_uint_op(1, OP_EQ, namemap_get_or_create_id(&m, "is"));
139 tt_uint_op(2, OP_EQ, namemap_get_or_create_id(&m, "not"));
141 done:
142 namemap_clear(&m);
145 static void
146 test_namemap_fmt(void *arg)
148 (void)arg;
149 namemap_t m = NAMEMAP_INIT();
151 unsigned a = namemap_get_or_create_id(&m, "greetings");
152 unsigned b = namemap_get_or_create_id(&m, "earthlings");
154 tt_str_op(namemap_fmt_name(&m, a), OP_EQ, "greetings");
155 tt_str_op(namemap_fmt_name(&m, b), OP_EQ, "earthlings");
156 tt_int_op(a, OP_NE, 100);
157 tt_int_op(b, OP_NE, 100);
158 tt_str_op(namemap_fmt_name(&m, 100), OP_EQ, "{100}");
160 done:
161 namemap_clear(&m);
164 #define T(name) \
165 { #name, test_namemap_ ## name , 0, NULL, NULL }
167 struct testcase_t namemap_tests[] = {
168 T(empty),
169 T(toolong),
170 T(blackbox),
171 T(internals),
172 T(fmt),
173 END_OF_TESTCASES