suricata
util-memcmp.c
Go to the documentation of this file.
1/* Copyright (C) 2007-2010 Open Information Security Foundation
2 *
3 * You can copy, redistribute or modify this Program under the terms of
4 * the GNU General Public License version 2 as published by the Free
5 * Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * version 2 along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 */
17
18/**
19 * \file
20 *
21 * \author Victor Julien <victor@inliniac.net>
22 *
23 * Memcmp implementations.
24 */
25
26#include "suricata-common.h"
27#include "util-memcmp.h"
28#include "util-unittest.h"
29
30/* code is implemented in util-memcmp.h as it's all inlined */
31
32/* UNITTESTS */
33#ifdef UNITTESTS
34#include "util-debug.h"
35
36static int MemcmpTest01 (void)
37{
38 uint8_t a[] = "abcd";
39 uint8_t b[] = "abcd";
40
41 FAIL_IF(SCMemcmp(a, b, sizeof(a) - 1) != 0);
42 PASS;
43}
44
45static int MemcmpTest02 (void)
46{
47 uint8_t a[] = "abcdabcdabcdabcd";
48 uint8_t b[] = "abcdabcdabcdabcd";
49
50 FAIL_IF(SCMemcmp(a, b, sizeof(a) - 1) != 0);
51 PASS;
52}
53
54static int MemcmpTest03 (void)
55{
56 uint8_t a[] = "abcdabcd";
57 uint8_t b[] = "abcdabcd";
58
59 FAIL_IF(SCMemcmp(a, b, sizeof(a) - 1) != 0);
60 PASS;
61}
62
63static int MemcmpTest04 (void)
64{
65 uint8_t a[] = "abcd";
66 uint8_t b[] = "abcD";
67
68 int r = SCMemcmp(a, b, sizeof(a)-1);
69 FAIL_IF(r != 1);
70
71 PASS;
72}
73
74static int MemcmpTest05 (void)
75{
76 uint8_t a[] = "abcdabcdabcdabcd";
77 uint8_t b[] = "abcDabcdabcdabcd";
78
79 FAIL_IF(SCMemcmp(a, b, sizeof(a) - 1) != 1);
80 PASS;
81}
82
83static int MemcmpTest06 (void)
84{
85 uint8_t a[] = "abcdabcd";
86 uint8_t b[] = "abcDabcd";
87
88 FAIL_IF(SCMemcmp(a, b, sizeof(a) - 1) != 1);
89 PASS;
90}
91
92static int MemcmpTest07 (void)
93{
94 uint8_t a[] = "abcd";
95 uint8_t b[] = "abcde";
96
97 FAIL_IF(SCMemcmp(a, b, sizeof(a) - 1) != 0);
98 PASS;
99}
100
101static int MemcmpTest08 (void)
102{
103 uint8_t a[] = "abcdabcdabcdabcd";
104 uint8_t b[] = "abcdabcdabcdabcde";
105
106 FAIL_IF(SCMemcmp(a, b, sizeof(a) - 1) != 0);
107 PASS;
108}
109
110static int MemcmpTest09 (void)
111{
112 uint8_t a[] = "abcdabcd";
113 uint8_t b[] = "abcdabcde";
114
115 FAIL_IF(SCMemcmp(a, b, sizeof(a) - 1) != 0);
116 PASS;
117}
118
119static int MemcmpTest10 (void)
120{
121 uint8_t a[] = "abcd";
122 uint8_t b[] = "Zbcde";
123
124 FAIL_IF(SCMemcmp(a, b, sizeof(a) - 1) != 1);
125 PASS;
126}
127
128static int MemcmpTest11 (void)
129{
130 uint8_t a[] = "abcdabcdabcdabcd";
131 uint8_t b[] = "Zbcdabcdabcdabcde";
132
133 FAIL_IF(SCMemcmp(a, b, sizeof(a) - 1) != 1);
134 PASS;
135}
136
137static int MemcmpTest12 (void)
138{
139 uint8_t a[] = "abcdabcd";
140 uint8_t b[] = "Zbcdabcde";
141
142 FAIL_IF(SCMemcmp(a, b, sizeof(a) - 1) != 1);
143 PASS;
144}
145
146static int MemcmpTest13 (void)
147{
148 uint8_t a[] = "abcdefgh";
149 uint8_t b[] = "AbCdEfGhIjK";
150
151 FAIL_IF(SCMemcmpLowercase(a, b, sizeof(a) - 1) != 0);
152 PASS;
153}
154
155#include "util-cpu.h"
156
157static int MemcmpTest14 (void)
158{
159#ifdef PROFILING
160#define TEST_RUNS 1000000
161 uint64_t ticks_start = 0;
162 uint64_t ticks_end = 0;
163 const char *a[] = { "0123456789012345", "abc", "abcdefghij", "suricata", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
164 const char *b[] = { "1234567890123456", "abc", "abcdefghik", "suricatb", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
165
166 int t = 0;
167 int i, j;
168 int r1 = 0;
169
170 printf("\n");
171
172 ticks_start = UtilCpuGetTicks();
173 for (t = 0; t < TEST_RUNS; t++) {
174 for (i = 0; a[i] != NULL; i++) {
175 // printf("a[%d] = %s\n", i, a[i]);
176 size_t alen = strlen(a[i]) - 1;
177
178 for (j = 0; b[j] != NULL; j++) {
179 // printf("b[%d] = %s\n", j, b[j]);
180 size_t blen = strlen(b[j]) - 1;
181
182 r1 += (memcmp((uint8_t *)a[i], (uint8_t *)b[j], (alen < blen) ? alen : blen) ? 1 : 0);
183 }
184 }
185 }
186 ticks_end = UtilCpuGetTicks();
187 printf("memcmp(%d) \t\t\t%"PRIu64"\n", TEST_RUNS, ((uint64_t)(ticks_end - ticks_start))/TEST_RUNS);
188 SCLogInfo("ticks passed %"PRIu64, ticks_end - ticks_start);
189
190 printf("r1 %d\n", r1);
191 FAIL_IF(r1 != (51 * TEST_RUNS));
192#endif
193 PASS;
194}
195
196static int MemcmpTest15 (void)
197{
198#ifdef PROFILING
199 uint64_t ticks_start = 0;
200 uint64_t ticks_end = 0;
201 const char *a[] = { "0123456789012345", "abc", "abcdefghij", "suricata", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
202 const char *b[] = { "1234567890123456", "abc", "abcdefghik", "suricatb", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
203
204 int t = 0;
205 int i, j;
206 int r2 = 0;
207
208 printf("\n");
209
210 ticks_start = UtilCpuGetTicks();
211 for (t = 0; t < TEST_RUNS; t++) {
212 for (i = 0; a[i] != NULL; i++) {
213 // printf("a[%d] = %s\n", i, a[i]);
214 size_t alen = strlen(a[i]) - 1;
215
216 for (j = 0; b[j] != NULL; j++) {
217 // printf("b[%d] = %s\n", j, b[j]);
218 size_t blen = strlen(b[j]) - 1;
219
220 r2 += MemcmpLowercase((uint8_t *)a[i], (uint8_t *)b[j], (alen < blen) ? alen : blen);
221 }
222 }
223 }
224 ticks_end = UtilCpuGetTicks();
225 printf("MemcmpLowercase(%d) \t\t%"PRIu64"\n", TEST_RUNS, ((uint64_t)(ticks_end - ticks_start))/TEST_RUNS);
226 SCLogInfo("ticks passed %"PRIu64, ticks_end - ticks_start);
227
228 printf("r2 %d\n", r2);
229 FAIL_IF(r2 != (51 * TEST_RUNS));
230#endif
231 PASS;
232}
233
234static int MemcmpTest16 (void)
235{
236#ifdef PROFILING
237 uint64_t ticks_start = 0;
238 uint64_t ticks_end = 0;
239 const char *a[] = { "0123456789012345", "abc", "abcdefghij", "suricata", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
240 const char *b[] = { "1234567890123456", "abc", "abcdefghik", "suricatb", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
241
242 int t = 0;
243 int i, j;
244 int r3 = 0;
245
246 printf("\n");
247
248 ticks_start = UtilCpuGetTicks();
249 for (t = 0; t < TEST_RUNS; t++) {
250 for (i = 0; a[i] != NULL; i++) {
251 // printf("a[%d] = %s\n", i, a[i]);
252 size_t alen = strlen(a[i]) - 1;
253
254 for (j = 0; b[j] != NULL; j++) {
255 // printf("b[%d] = %s\n", j, b[j]);
256 size_t blen = strlen(b[j]) - 1;
257
258 r3 += SCMemcmp((uint8_t *)a[i], (uint8_t *)b[j], (alen < blen) ? alen : blen);
259 }
260 }
261 }
262 ticks_end = UtilCpuGetTicks();
263 printf("SCMemcmp(%d) \t\t\t%"PRIu64"\n", TEST_RUNS, ((uint64_t)(ticks_end - ticks_start))/TEST_RUNS);
264 SCLogInfo("ticks passed %"PRIu64, ticks_end - ticks_start);
265
266 printf("r3 %d\n", r3);
267 FAIL_IF(r3 != (51 * TEST_RUNS));
268#endif
269 PASS;
270}
271
272static int MemcmpTest17 (void)
273{
274#ifdef PROFILING
275 uint64_t ticks_start = 0;
276 uint64_t ticks_end = 0;
277 const char *a[] = { "0123456789012345", "abc", "abcdefghij", "suricata", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
278 const char *b[] = { "1234567890123456", "abc", "abcdefghik", "suricatb", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
279
280 int t = 0;
281 int i, j;
282 int r4 = 0;
283
284 printf("\n");
285
286 ticks_start = UtilCpuGetTicks();
287 for (t = 0; t < TEST_RUNS; t++) {
288 for (i = 0; a[i] != NULL; i++) {
289 // printf("a[%d] = %s\n", i, a[i]);
290 size_t alen = strlen(a[i]) - 1;
291
292 for (j = 0; b[j] != NULL; j++) {
293 // printf("b[%d] = %s\n", j, b[j]);
294 size_t blen = strlen(b[j]) - 1;
295
296 r4 += SCMemcmpLowercase((uint8_t *)a[i], (uint8_t *)b[j], (alen < blen) ? alen : blen);
297 }
298 }
299 }
300 ticks_end = UtilCpuGetTicks();
301 printf("SCMemcmpLowercase(%d) \t\t%"PRIu64"\n", TEST_RUNS, ((uint64_t)(ticks_end - ticks_start))/TEST_RUNS);
302 SCLogInfo("ticks passed %"PRIu64, ticks_end - ticks_start);
303
304 printf("r4 %d\n", r4);
305 FAIL_IF(r4 != (51 * TEST_RUNS));
306#endif
307 PASS;
308}
309
311 const char *a;
312 const char *b;
315 { "abcdefgh", "!bcdefgh", 1, },
316 { "?bcdefgh", "!bcdefgh", 1, },
317 { "!bcdefgh", "abcdefgh", 1, },
318 { "!bcdefgh", "?bcdefgh", 1, },
319 { "zbcdefgh", "bbcdefgh", 1, },
320
321 { "abcdefgh12345678", "!bcdefgh12345678", 1, },
322 { "?bcdefgh12345678", "!bcdefgh12345678", 1, },
323 { "!bcdefgh12345678", "abcdefgh12345678", 1, },
324 { "!bcdefgh12345678", "?bcdefgh12345678", 1, },
325 { "bbcdefgh12345678", "zbcdefgh12345678", 1, },
326
327 { "abcdefgh", "abcdefgh", 0, },
328 { "abcdefgh", "Abcdefgh", 0, },
329 { "abcdefgh12345678", "Abcdefgh12345678", 0, },
330
331 { NULL, NULL, 0 },
332
333 };
334
335static int MemcmpTest18 (void)
336{
338
339 while (t && t->a != NULL) {
340
341 FAIL_IF(SCMemcmpLowercase(t->a, t->b, strlen(t->a) - 1) != t->result);
342 t++;
343 }
344
345 PASS;
346}
347
348#endif /* UNITTESTS */
349
351{
352#ifdef UNITTESTS
353 UtRegisterTest("MemcmpTest01", MemcmpTest01);
354 UtRegisterTest("MemcmpTest02", MemcmpTest02);
355 UtRegisterTest("MemcmpTest03", MemcmpTest03);
356 UtRegisterTest("MemcmpTest04", MemcmpTest04);
357 UtRegisterTest("MemcmpTest05", MemcmpTest05);
358 UtRegisterTest("MemcmpTest06", MemcmpTest06);
359 UtRegisterTest("MemcmpTest07", MemcmpTest07);
360 UtRegisterTest("MemcmpTest08", MemcmpTest08);
361 UtRegisterTest("MemcmpTest09", MemcmpTest09);
362 UtRegisterTest("MemcmpTest10", MemcmpTest10);
363 UtRegisterTest("MemcmpTest11", MemcmpTest11);
364 UtRegisterTest("MemcmpTest12", MemcmpTest12);
365 UtRegisterTest("MemcmpTest13", MemcmpTest13);
366 UtRegisterTest("MemcmpTest14", MemcmpTest14);
367 UtRegisterTest("MemcmpTest15", MemcmpTest15);
368 UtRegisterTest("MemcmpTest16", MemcmpTest16);
369 UtRegisterTest("MemcmpTest17", MemcmpTest17);
370 UtRegisterTest("MemcmpTest18", MemcmpTest18);
371#endif /* UNITTESTS */
372}
373
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
#define PASS
Pass the test.
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
const char * a
const char * b
uint64_t UtilCpuGetTicks(void)
Definition util-cpu.c:161
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition util-debug.h:225
struct MemcmpTest18Tests memcmp_tests18_tests[]
void MemcmpRegisterTests(void)
#define TEST_RUNS
#define SCMemcmp(a, b, c)