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  FAIL_IF(SCMemcmp(a, b, sizeof(a) - 1) != 0);
42  PASS;
43 }
44 
45 static 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 
54 static 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 
63 static 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 
74 static 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 
83 static 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 
92 static 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 
101 static 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 
110 static 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 
119 static 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 
128 static 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 
137 static 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 
146 static 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 
157 #define TEST_RUNS 1000000
158 
159 static int MemcmpTest14 (void)
160 {
161 #ifdef PROFILING
162  uint64_t ticks_start = 0;
163  uint64_t ticks_end = 0;
164  const char *a[] = { "0123456789012345", "abc", "abcdefghij", "suricata", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
165  const char *b[] = { "1234567890123456", "abc", "abcdefghik", "suricatb", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
166 
167  int t = 0;
168  int i, j;
169  int r1 = 0;
170 
171  printf("\n");
172 
173  ticks_start = UtilCpuGetTicks();
174  for (t = 0; t < TEST_RUNS; t++) {
175  for (i = 0; a[i] != NULL; i++) {
176  // printf("a[%d] = %s\n", i, a[i]);
177  size_t alen = strlen(a[i]) - 1;
178 
179  for (j = 0; b[j] != NULL; j++) {
180  // printf("b[%d] = %s\n", j, b[j]);
181  size_t blen = strlen(b[j]) - 1;
182 
183  r1 += (memcmp((uint8_t *)a[i], (uint8_t *)b[j], (alen < blen) ? alen : blen) ? 1 : 0);
184  }
185  }
186  }
187  ticks_end = UtilCpuGetTicks();
188  printf("memcmp(%d) \t\t\t%"PRIu64"\n", TEST_RUNS, ((uint64_t)(ticks_end - ticks_start))/TEST_RUNS);
189  SCLogInfo("ticks passed %"PRIu64, ticks_end - ticks_start);
190 
191  printf("r1 %d\n", r1);
192  FAIL_IF(r1 != (51 * TEST_RUNS));
193 #endif
194  PASS;
195 }
196 
197 static int MemcmpTest15 (void)
198 {
199 #ifdef PROFILING
200  uint64_t ticks_start = 0;
201  uint64_t ticks_end = 0;
202  const char *a[] = { "0123456789012345", "abc", "abcdefghij", "suricata", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
203  const char *b[] = { "1234567890123456", "abc", "abcdefghik", "suricatb", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
204 
205  int t = 0;
206  int i, j;
207  int r2 = 0;
208 
209  printf("\n");
210 
211  ticks_start = UtilCpuGetTicks();
212  for (t = 0; t < TEST_RUNS; t++) {
213  for (i = 0; a[i] != NULL; i++) {
214  // printf("a[%d] = %s\n", i, a[i]);
215  size_t alen = strlen(a[i]) - 1;
216 
217  for (j = 0; b[j] != NULL; j++) {
218  // printf("b[%d] = %s\n", j, b[j]);
219  size_t blen = strlen(b[j]) - 1;
220 
221  r2 += MemcmpLowercase((uint8_t *)a[i], (uint8_t *)b[j], (alen < blen) ? alen : blen);
222  }
223  }
224  }
225  ticks_end = UtilCpuGetTicks();
226  printf("MemcmpLowercase(%d) \t\t%"PRIu64"\n", TEST_RUNS, ((uint64_t)(ticks_end - ticks_start))/TEST_RUNS);
227  SCLogInfo("ticks passed %"PRIu64, ticks_end - ticks_start);
228 
229  printf("r2 %d\n", r2);
230  FAIL_IF(r2 != (51 * TEST_RUNS));
231 #endif
232  PASS;
233 }
234 
235 static int MemcmpTest16 (void)
236 {
237 #ifdef PROFILING
238  uint64_t ticks_start = 0;
239  uint64_t ticks_end = 0;
240  const char *a[] = { "0123456789012345", "abc", "abcdefghij", "suricata", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
241  const char *b[] = { "1234567890123456", "abc", "abcdefghik", "suricatb", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
242 
243  int t = 0;
244  int i, j;
245  int r3 = 0;
246 
247  printf("\n");
248 
249  ticks_start = UtilCpuGetTicks();
250  for (t = 0; t < TEST_RUNS; t++) {
251  for (i = 0; a[i] != NULL; i++) {
252  // printf("a[%d] = %s\n", i, a[i]);
253  size_t alen = strlen(a[i]) - 1;
254 
255  for (j = 0; b[j] != NULL; j++) {
256  // printf("b[%d] = %s\n", j, b[j]);
257  size_t blen = strlen(b[j]) - 1;
258 
259  r3 += SCMemcmp((uint8_t *)a[i], (uint8_t *)b[j], (alen < blen) ? alen : blen);
260  }
261  }
262  }
263  ticks_end = UtilCpuGetTicks();
264  printf("SCMemcmp(%d) \t\t\t%"PRIu64"\n", TEST_RUNS, ((uint64_t)(ticks_end - ticks_start))/TEST_RUNS);
265  SCLogInfo("ticks passed %"PRIu64, ticks_end - ticks_start);
266 
267  printf("r3 %d\n", r3);
268  FAIL_IF(r3 != (51 * TEST_RUNS));
269 #endif
270  PASS;
271 }
272 
273 static int MemcmpTest17 (void)
274 {
275 #ifdef PROFILING
276  uint64_t ticks_start = 0;
277  uint64_t ticks_end = 0;
278  const char *a[] = { "0123456789012345", "abc", "abcdefghij", "suricata", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
279  const char *b[] = { "1234567890123456", "abc", "abcdefghik", "suricatb", "test", "xyz", "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr", "abcdefghijklmnopqrstuvwxyz", NULL };
280 
281  int t = 0;
282  int i, j;
283  int r4 = 0;
284 
285  printf("\n");
286 
287  ticks_start = UtilCpuGetTicks();
288  for (t = 0; t < TEST_RUNS; t++) {
289  for (i = 0; a[i] != NULL; i++) {
290  // printf("a[%d] = %s\n", i, a[i]);
291  size_t alen = strlen(a[i]) - 1;
292 
293  for (j = 0; b[j] != NULL; j++) {
294  // printf("b[%d] = %s\n", j, b[j]);
295  size_t blen = strlen(b[j]) - 1;
296 
297  r4 += SCMemcmpLowercase((uint8_t *)a[i], (uint8_t *)b[j], (alen < blen) ? alen : blen);
298  }
299  }
300  }
301  ticks_end = UtilCpuGetTicks();
302  printf("SCMemcmpLowercase(%d) \t\t%"PRIu64"\n", TEST_RUNS, ((uint64_t)(ticks_end - ticks_start))/TEST_RUNS);
303  SCLogInfo("ticks passed %"PRIu64, ticks_end - ticks_start);
304 
305  printf("r4 %d\n", r4);
306  FAIL_IF(r4 != (51 * TEST_RUNS));
307 #endif
308  PASS;
309 }
310 
312  const char *a;
313  const char *b;
314  int result;
315 } memcmp_tests18_tests[] = {
316  { "abcdefgh", "!bcdefgh", 1, },
317  { "?bcdefgh", "!bcdefgh", 1, },
318  { "!bcdefgh", "abcdefgh", 1, },
319  { "!bcdefgh", "?bcdefgh", 1, },
320  { "zbcdefgh", "bbcdefgh", 1, },
321 
322  { "abcdefgh12345678", "!bcdefgh12345678", 1, },
323  { "?bcdefgh12345678", "!bcdefgh12345678", 1, },
324  { "!bcdefgh12345678", "abcdefgh12345678", 1, },
325  { "!bcdefgh12345678", "?bcdefgh12345678", 1, },
326  { "bbcdefgh12345678", "zbcdefgh12345678", 1, },
327 
328  { "abcdefgh", "abcdefgh", 0, },
329  { "abcdefgh", "Abcdefgh", 0, },
330  { "abcdefgh12345678", "Abcdefgh12345678", 0, },
331 
332  { NULL, NULL, 0 },
333 
334  };
335 
336 static int MemcmpTest18 (void)
337 {
339 
340  while (t && t->a != NULL) {
341 
342  FAIL_IF(SCMemcmpLowercase(t->a, t->b, strlen(t->a) - 1) != t->result);
343  t++;
344  }
345 
346  PASS;
347 }
348 
349 #endif /* UNITTESTS */
350 
352 {
353 #ifdef UNITTESTS
354  UtRegisterTest("MemcmpTest01", MemcmpTest01);
355  UtRegisterTest("MemcmpTest02", MemcmpTest02);
356  UtRegisterTest("MemcmpTest03", MemcmpTest03);
357  UtRegisterTest("MemcmpTest04", MemcmpTest04);
358  UtRegisterTest("MemcmpTest05", MemcmpTest05);
359  UtRegisterTest("MemcmpTest06", MemcmpTest06);
360  UtRegisterTest("MemcmpTest07", MemcmpTest07);
361  UtRegisterTest("MemcmpTest08", MemcmpTest08);
362  UtRegisterTest("MemcmpTest09", MemcmpTest09);
363  UtRegisterTest("MemcmpTest10", MemcmpTest10);
364  UtRegisterTest("MemcmpTest11", MemcmpTest11);
365  UtRegisterTest("MemcmpTest12", MemcmpTest12);
366  UtRegisterTest("MemcmpTest13", MemcmpTest13);
367  UtRegisterTest("MemcmpTest14", MemcmpTest14);
368  UtRegisterTest("MemcmpTest15", MemcmpTest15);
369  UtRegisterTest("MemcmpTest16", MemcmpTest16);
370  UtRegisterTest("MemcmpTest17", MemcmpTest17);
371  UtRegisterTest("MemcmpTest18", MemcmpTest18);
372 #endif /* UNITTESTS */
373 }
374 
TEST_RUNS
#define TEST_RUNS
Definition: util-memcmp.c:157
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:314
memcmp_tests18_tests
struct MemcmpTest18Tests memcmp_tests18_tests[]
util-unittest.h
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:312
MemcmpTest18Tests
Definition: util-memcmp.c:311
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:224
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:313
MemcmpRegisterTests
void MemcmpRegisterTests(void)
Definition: util-memcmp.c:351
SCMemcmp
#define SCMemcmp(a, b, c)
Definition: util-memcmp.h:290