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