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 
36 static int MemcmpTest01 (void)
37 {
38  uint8_t a[] = "abcd";
39  uint8_t b[] = "abcd";
40 
41  if (SCMemcmp(a, b, sizeof(a)-1) != 0)
42  return 0;
43 
44  return 1;
45 }
46 
47 static int MemcmpTest02 (void)
48 {
49  uint8_t a[] = "abcdabcdabcdabcd";
50  uint8_t b[] = "abcdabcdabcdabcd";
51 
52  if (SCMemcmp(a, b, sizeof(a)-1) != 0)
53  return 0;
54 
55  return 1;
56 }
57 
58 static int MemcmpTest03 (void)
59 {
60  uint8_t a[] = "abcdabcd";
61  uint8_t b[] = "abcdabcd";
62 
63  if (SCMemcmp(a, b, sizeof(a)-1) != 0)
64  return 0;
65 
66  return 1;
67 }
68 
69 static int MemcmpTest04 (void)
70 {
71  uint8_t a[] = "abcd";
72  uint8_t b[] = "abcD";
73 
74  int r = SCMemcmp(a, b, sizeof(a)-1);
75  if (r != 1) {
76  printf("%s != %s, but memcmp returned %d: ", a, b, r);
77  return 0;
78  }
79 
80  return 1;
81 }
82 
83 static int MemcmpTest05 (void)
84 {
85  uint8_t a[] = "abcdabcdabcdabcd";
86  uint8_t b[] = "abcDabcdabcdabcd";
87 
88  if (SCMemcmp(a, b, sizeof(a)-1) != 1)
89  return 0;
90 
91  return 1;
92 }
93 
94 static int MemcmpTest06 (void)
95 {
96  uint8_t a[] = "abcdabcd";
97  uint8_t b[] = "abcDabcd";
98 
99  if (SCMemcmp(a, b, sizeof(a)-1) != 1)
100  return 0;
101 
102  return 1;
103 }
104 
105 static int MemcmpTest07 (void)
106 {
107  uint8_t a[] = "abcd";
108  uint8_t b[] = "abcde";
109 
110  if (SCMemcmp(a, b, sizeof(a)-1) != 0)
111  return 0;
112 
113  return 1;
114 }
115 
116 static int MemcmpTest08 (void)
117 {
118  uint8_t a[] = "abcdabcdabcdabcd";
119  uint8_t b[] = "abcdabcdabcdabcde";
120 
121  if (SCMemcmp(a, b, sizeof(a)-1) != 0)
122  return 0;
123 
124  return 1;
125 }
126 
127 static int MemcmpTest09 (void)
128 {
129  uint8_t a[] = "abcdabcd";
130  uint8_t b[] = "abcdabcde";
131 
132  if (SCMemcmp(a, b, sizeof(a)-1) != 0)
133  return 0;
134 
135  return 1;
136 }
137 
138 static int MemcmpTest10 (void)
139 {
140  uint8_t a[] = "abcd";
141  uint8_t b[] = "Zbcde";
142 
143  if (SCMemcmp(a, b, sizeof(a)-1) != 1)
144  return 0;
145 
146  return 1;
147 }
148 
149 static int MemcmpTest11 (void)
150 {
151  uint8_t a[] = "abcdabcdabcdabcd";
152  uint8_t b[] = "Zbcdabcdabcdabcde";
153 
154  if (SCMemcmp(a, b, sizeof(a)-1) != 1)
155  return 0;
156 
157  return 1;
158 }
159 
160 static int MemcmpTest12 (void)
161 {
162  uint8_t a[] = "abcdabcd";
163  uint8_t b[] = "Zbcdabcde";
164 
165  if (SCMemcmp(a, b, sizeof(a)-1) != 1)
166  return 0;
167 
168  return 1;
169 }
170 
171 static int MemcmpTest13 (void)
172 {
173  uint8_t a[] = "abcdefgh";
174  uint8_t b[] = "AbCdEfGhIjK";
175 
176  if (SCMemcmpLowercase(a, b, sizeof(a)-1) != 0)
177  return 0;
178 
179  return 1;
180 }
181 
182 #include "util-cpu.h"
183 
184 #define TEST_RUNS 1000000
185 
186 static int MemcmpTest14 (void)
187 {
188 #ifdef PROFILING
189  uint64_t ticks_start = 0;
190  uint64_t ticks_end = 0;
191  const char *a[] = { "0123456789012345", "abc", "abcdefghij", "suricata", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
192  const char *b[] = { "1234567890123456", "abc", "abcdefghik", "suricatb", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
193 
194  int t = 0;
195  int i, j;
196  int r1 = 0;
197 
198  printf("\n");
199 
200  ticks_start = UtilCpuGetTicks();
201  for (t = 0; t < TEST_RUNS; t++) {
202  for (i = 0; a[i] != NULL; i++) {
203  // printf("a[%d] = %s\n", i, a[i]);
204  size_t alen = strlen(a[i]) - 1;
205 
206  for (j = 0; b[j] != NULL; j++) {
207  // printf("b[%d] = %s\n", j, b[j]);
208  size_t blen = strlen(b[j]) - 1;
209 
210  r1 += (memcmp((uint8_t *)a[i], (uint8_t *)b[j], (alen < blen) ? alen : blen) ? 1 : 0);
211  }
212  }
213  }
214  ticks_end = UtilCpuGetTicks();
215  printf("memcmp(%d) \t\t\t%"PRIu64"\n", TEST_RUNS, ((uint64_t)(ticks_end - ticks_start))/TEST_RUNS);
216  SCLogInfo("ticks passed %"PRIu64, ticks_end - ticks_start);
217 
218  printf("r1 %d\n", r1);
219  if (r1 != (51 * TEST_RUNS))
220  return 0;
221 #endif
222  return 1;
223 }
224 
225 static int MemcmpTest15 (void)
226 {
227 #ifdef PROFILING
228  uint64_t ticks_start = 0;
229  uint64_t ticks_end = 0;
230  const char *a[] = { "0123456789012345", "abc", "abcdefghij", "suricata", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
231  const char *b[] = { "1234567890123456", "abc", "abcdefghik", "suricatb", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
232 
233  int t = 0;
234  int i, j;
235  int r2 = 0;
236 
237  printf("\n");
238 
239  ticks_start = UtilCpuGetTicks();
240  for (t = 0; t < TEST_RUNS; t++) {
241  for (i = 0; a[i] != NULL; i++) {
242  // printf("a[%d] = %s\n", i, a[i]);
243  size_t alen = strlen(a[i]) - 1;
244 
245  for (j = 0; b[j] != NULL; j++) {
246  // printf("b[%d] = %s\n", j, b[j]);
247  size_t blen = strlen(b[j]) - 1;
248 
249  r2 += MemcmpLowercase((uint8_t *)a[i], (uint8_t *)b[j], (alen < blen) ? alen : blen);
250  }
251  }
252  }
253  ticks_end = UtilCpuGetTicks();
254  printf("MemcmpLowercase(%d) \t\t%"PRIu64"\n", TEST_RUNS, ((uint64_t)(ticks_end - ticks_start))/TEST_RUNS);
255  SCLogInfo("ticks passed %"PRIu64, ticks_end - ticks_start);
256 
257  printf("r2 %d\n", r2);
258  if (r2 != (51 * TEST_RUNS))
259  return 0;
260 #endif
261  return 1;
262 }
263 
264 static int MemcmpTest16 (void)
265 {
266 #ifdef PROFILING
267  uint64_t ticks_start = 0;
268  uint64_t ticks_end = 0;
269  const char *a[] = { "0123456789012345", "abc", "abcdefghij", "suricata", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
270  const char *b[] = { "1234567890123456", "abc", "abcdefghik", "suricatb", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
271 
272  int t = 0;
273  int i, j;
274  int r3 = 0;
275 
276  printf("\n");
277 
278  ticks_start = UtilCpuGetTicks();
279  for (t = 0; t < TEST_RUNS; t++) {
280  for (i = 0; a[i] != NULL; i++) {
281  // printf("a[%d] = %s\n", i, a[i]);
282  size_t alen = strlen(a[i]) - 1;
283 
284  for (j = 0; b[j] != NULL; j++) {
285  // printf("b[%d] = %s\n", j, b[j]);
286  size_t blen = strlen(b[j]) - 1;
287 
288  r3 += SCMemcmp((uint8_t *)a[i], (uint8_t *)b[j], (alen < blen) ? alen : blen);
289  }
290  }
291  }
292  ticks_end = UtilCpuGetTicks();
293  printf("SCMemcmp(%d) \t\t\t%"PRIu64"\n", TEST_RUNS, ((uint64_t)(ticks_end - ticks_start))/TEST_RUNS);
294  SCLogInfo("ticks passed %"PRIu64, ticks_end - ticks_start);
295 
296  printf("r3 %d\n", r3);
297  if (r3 != (51 * TEST_RUNS))
298  return 0;
299 #endif
300  return 1;
301 }
302 
303 static int MemcmpTest17 (void)
304 {
305 #ifdef PROFILING
306  uint64_t ticks_start = 0;
307  uint64_t ticks_end = 0;
308  const char *a[] = { "0123456789012345", "abc", "abcdefghij", "suricata", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
309  const char *b[] = { "1234567890123456", "abc", "abcdefghik", "suricatb", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
310 
311  int t = 0;
312  int i, j;
313  int r4 = 0;
314 
315  printf("\n");
316 
317  ticks_start = UtilCpuGetTicks();
318  for (t = 0; t < TEST_RUNS; t++) {
319  for (i = 0; a[i] != NULL; i++) {
320  // printf("a[%d] = %s\n", i, a[i]);
321  size_t alen = strlen(a[i]) - 1;
322 
323  for (j = 0; b[j] != NULL; j++) {
324  // printf("b[%d] = %s\n", j, b[j]);
325  size_t blen = strlen(b[j]) - 1;
326 
327  r4 += SCMemcmpLowercase((uint8_t *)a[i], (uint8_t *)b[j], (alen < blen) ? alen : blen);
328  }
329  }
330  }
331  ticks_end = UtilCpuGetTicks();
332  printf("SCMemcmpLowercase(%d) \t\t%"PRIu64"\n", TEST_RUNS, ((uint64_t)(ticks_end - ticks_start))/TEST_RUNS);
333  SCLogInfo("ticks passed %"PRIu64, ticks_end - ticks_start);
334 
335  printf("r4 %d\n", r4);
336  if (r4 != (51 * TEST_RUNS))
337  return 0;
338 #endif
339  return 1;
340 }
341 
343  const char *a;
344  const char *b;
345  int result;
346 } memcmp_tests18_tests[] = {
347  { "abcdefgh", "!bcdefgh", 1, },
348  { "?bcdefgh", "!bcdefgh", 1, },
349  { "!bcdefgh", "abcdefgh", 1, },
350  { "!bcdefgh", "?bcdefgh", 1, },
351  { "zbcdefgh", "bbcdefgh", 1, },
352 
353  { "abcdefgh12345678", "!bcdefgh12345678", 1, },
354  { "?bcdefgh12345678", "!bcdefgh12345678", 1, },
355  { "!bcdefgh12345678", "abcdefgh12345678", 1, },
356  { "!bcdefgh12345678", "?bcdefgh12345678", 1, },
357  { "bbcdefgh12345678", "zbcdefgh12345678", 1, },
358 
359  { "abcdefgh", "abcdefgh", 0, },
360  { "abcdefgh", "Abcdefgh", 0, },
361  { "abcdefgh12345678", "Abcdefgh12345678", 0, },
362 
363  { NULL, NULL, 0 },
364 
365  };
366 
367 static int MemcmpTest18 (void)
368 {
370 
371  while (t && t->a != NULL) {
372 
373  if (SCMemcmpLowercase(t->a, t->b, strlen(t->a)-1) != t->result)
374  return 0;
375  t++;
376  }
377 
378  return 1;
379 }
380 
381 #endif /* UNITTESTS */
382 
384 {
385 #ifdef UNITTESTS
386  UtRegisterTest("MemcmpTest01", MemcmpTest01);
387  UtRegisterTest("MemcmpTest02", MemcmpTest02);
388  UtRegisterTest("MemcmpTest03", MemcmpTest03);
389  UtRegisterTest("MemcmpTest04", MemcmpTest04);
390  UtRegisterTest("MemcmpTest05", MemcmpTest05);
391  UtRegisterTest("MemcmpTest06", MemcmpTest06);
392  UtRegisterTest("MemcmpTest07", MemcmpTest07);
393  UtRegisterTest("MemcmpTest08", MemcmpTest08);
394  UtRegisterTest("MemcmpTest09", MemcmpTest09);
395  UtRegisterTest("MemcmpTest10", MemcmpTest10);
396  UtRegisterTest("MemcmpTest11", MemcmpTest11);
397  UtRegisterTest("MemcmpTest12", MemcmpTest12);
398  UtRegisterTest("MemcmpTest13", MemcmpTest13);
399  UtRegisterTest("MemcmpTest14", MemcmpTest14);
400  UtRegisterTest("MemcmpTest15", MemcmpTest15);
401  UtRegisterTest("MemcmpTest16", MemcmpTest16);
402  UtRegisterTest("MemcmpTest17", MemcmpTest17);
403  UtRegisterTest("MemcmpTest18", MemcmpTest18);
404 #endif /* UNITTESTS */
405 }
406 
TEST_RUNS
#define TEST_RUNS
Definition: util-memcmp.c:184
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
MemcmpTest18Tests::result
int result
Definition: util-memcmp.c:345
memcmp_tests18_tests
struct MemcmpTest18Tests memcmp_tests18_tests[]
util-unittest.h
UtilCpuGetTicks
uint64_t UtilCpuGetTicks(void)
Definition: util-cpu.c:194
util-memcmp.h
util-debug.h
util-cpu.h
MemcmpTest18Tests::a
const char * a
Definition: util-memcmp.c:343
MemcmpTest18Tests
Definition: util-memcmp.c:342
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:224
suricata-common.h
MemcmpTest18Tests::b
const char * b
Definition: util-memcmp.c:344
MemcmpRegisterTests
void MemcmpRegisterTests(void)
Definition: util-memcmp.c:383
SCMemcmp
#define SCMemcmp(a, b, c)
Definition: util-memcmp.h:290