Debian Codegraph @

tests/unit_tests/slow_memmem.cpp

raw symbols SCIP
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <stdint.h>
36 #include "gtest/gtest.h"
37
38 // OS X, FreeBSD, and OpenBSD don't need malloc.h
39 #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && \
40 !defined(__DragonFly__)
41 #include <malloc.h>
42 #endif
43
44 //#define TEST_ORIGINAL
45 //#define VERBOSE
46
47 #ifdef TEST_ORIGINAL
48 size_t slow_memmem_original(void* start_buff, size_t buflen,void* pat,size_t patlen)
49 {
50 void* buf = start_buff;
51 void* end=(char*)buf+buflen-patlen;
52 while((buf=memchr(buf,((char*)pat)[0],buflen)))
53 {
54 if(buf>end)
55 return 0;
56 if(memcmp(buf,pat,patlen)==0)
57 return (char*)buf - (char*)start_buff;
58 buf=(char*)buf+1;
59 }
60 return 0;
61 }
62
63 #define slow_memmem slow_memmem_original
64 #else
65 namespace cryptonote {
66 size_t slow_memmem(const void* start_buff, size_t buflen,const void* pat,size_t patlen);
67 }
68 using namespace cryptonote;
69 #endif
70
71 static const struct {
72 size_t buflen;
73 const char *buf;
74 size_t patlen;
75 const char *pat;
76 size_t res;
77 } T[]={
78 {0,"",0,"",0},
79 {1,"",0,"",0},
80 {0,"",1,"",0},
81 {1,"x",1,"x",0},
82 {2,"x",1,"",1},
83 {1,"x",1,"",0},
84 {1,"x",2,"x",0},
85 {2,"ax",2,"x",0},
86 {1,"xx",2,"xx",0},
87 {4,"abcd",3,"abc",0},
88 {4,"abcd",3,"bcd",1},
89 {4,"abcd",4,"abcd",0},
90 {4,"abcd",1,"d",3},
91 {4,"abcd",1,"c",2},
92 {4,"abcd",1,"bc",1},
93 {4,"abcd",1,"",0},
94 {3,"abcd",1,"d",0},
95 {5,"aaaab",2,"ab",3},
96 {7,"aaaabab",2,"ab",3},
97 {7,"aaaabab",3,"abc",0},
98 {4,"abcd",2,"cd",2},
99 {3,"abcd",2,"cd",0},
100 {3,"a\0b",1,"",1},
101 {3,"a\0b",2,"\0b",1},
102 {8,"xxxxxxab",3,"xyz",0},
103 {8,"xxxxxxab",6,"abcdef",0},
104 {9,"\0xxxxxab",3,"ab",6},
105 {4,"\0\0a",3,"\0a",1}, //
106 };
107
108 TEST(slowmem,Success)
109 {
110 size_t n;
111 for (n=0;n<sizeof(T)/sizeof(T[0]);++n) {
112 #ifdef VERBOSE
113 printf("%3zu: ",n);
114 fflush(stdout);
115 #endif
116 void *buf=malloc(T[n].buflen);
117 memcpy(buf,T[n].buf,T[n].buflen);
118 void *pat=malloc(T[n].patlen);
119 memcpy(pat,T[n].pat,T[n].patlen);
120 size_t res=slow_memmem(buf,T[n].buflen,pat,T[n].patlen);
121 free(pat);
122 free(buf);
123 ASSERT_EQ(res,T[n].res);
124 #ifdef VERBOSE
125 if (res!=T[n].res) printf("failed (got %zu, expected %zu)",res,T[n].res); else printf("ok");
126 printf("\n");
127 #endif
128 }
129 }