source: branches/samhain_3_1/src/cutest_sh_unix.c@ 555

Last change on this file since 555 was 474, checked in by katerina, 9 years ago

Fix for ticket #372 (Replace obsolete smatch by clang in test suite).

File size: 4.6 KB
Line 
1
2#include "config_xor.h"
3
4#include <string.h>
5#include "CuTest.h"
6#include "samhain.h"
7#include "sh_unix.h"
8
9int malloc_count = 0;
10
11void Test_dnmalloc (CuTest *tc) {
12
13 const int nalloc = 64 /* original dnmalloc 1.0-beta5 fails for >= 45 */;
14 int j, i;
15 int sum;
16 int i_malloc = malloc_count;
17
18 char * buf;
19 char * area[256];
20
21 /* test reuse of last freed chunk */
22 buf = malloc(1024);
23 CuAssertPtrNotNull(tc, buf);
24 free(buf);
25 area[0] = malloc(1024);
26 CuAssertTrue(tc, buf == area[0]);
27 free(area[0]);
28
29 /* test realloc */
30 buf = malloc(16);
31 CuAssertPtrNotNull(tc, buf);
32 strcpy(buf, "testing realloc");
33 buf = realloc(buf, 32);
34 strcat(buf, "testing realloc");
35 CuAssertStrEquals(tc, "testing realloctesting realloc", buf);
36
37 i_malloc = malloc_count;
38
39 for (j = 0; j < 64; ++j)
40 {
41 buf = malloc((j+1) * 1024);
42 CuAssertPtrNotNull(tc, buf);
43#ifndef USE_SYSTEM_MALLOC
44#ifndef __clang__
45 CuAssertIntEquals (tc, malloc_count, (i_malloc + 1));
46#endif
47#endif
48 free(buf);
49#ifndef USE_SYSTEM_MALLOC
50 CuAssertIntEquals (tc, malloc_count, i_malloc);
51#endif
52 }
53
54 /* test realloc */
55 buf = malloc(16);
56 CuAssertPtrNotNull(tc, buf);
57 strcpy(buf, "testing realloc");
58 buf = realloc(buf, 32);
59 strcat(buf, "testing realloc");
60 CuAssertStrEquals(tc, "testing realloctesting realloc", buf);
61
62 i_malloc = malloc_count;
63
64 for (j = 0; j < 64; ++j)
65 {
66 buf = calloc(1, (j+1) * 1024);
67 CuAssertPtrNotNull(tc, buf);
68#ifndef USE_SYSTEM_MALLOC
69 CuAssertIntEquals (tc, malloc_count, (i_malloc + 1));
70#endif
71 sum = 0;
72 for (i = 0; i < ((j+1) * 1024); ++i)
73 sum += buf[i];
74 CuAssertIntEquals (tc, 0, sum);
75 free(buf);
76#ifndef USE_SYSTEM_MALLOC
77 CuAssertIntEquals (tc, malloc_count, i_malloc);
78#endif
79 }
80
81 /* test realloc */
82 buf = malloc(16);
83 CuAssertPtrNotNull(tc, buf);
84 strcpy(buf, "testing realloc");
85 buf = realloc(buf, 32);
86 strcat(buf, "testing realloc");
87 CuAssertStrEquals(tc, "testing realloctesting realloc", buf);
88
89 for (j = 0; j < nalloc; ++j)
90 {
91 area[j] = malloc((j+1) * 1024);
92 CuAssertPtrNotNull(tc, area[j]);
93#ifndef USE_SYSTEM_MALLOC
94 /* CuAssertIntEquals (tc, malloc_count, (i_malloc + (j+1))); */
95#endif
96 memset(area[j], (unsigned char) ('a'+1), (j+1) * 1024);
97 }
98
99 i_malloc = malloc_count;
100
101 for (j = 0; j < nalloc; ++j)
102 {
103 sum = 0;
104 for (i = 0; i < ((j+1) * 1024); ++i)
105 sum += area[j][i];
106 CuAssertIntEquals (tc, sum, ((j+1) * 1024 * ((unsigned char) ('a'+1))));
107 free(area[j]);
108#ifndef USE_SYSTEM_MALLOC
109 CuAssertIntEquals (tc, malloc_count, i_malloc - (j+1));
110#endif
111 }
112
113 /* test realloc */
114 buf = malloc(16);
115 CuAssertPtrNotNull(tc, buf);
116 strcpy(buf, "testing realloc");
117 buf = realloc(buf, 32);
118 strcat(buf, "testing realloc");
119 CuAssertStrEquals(tc, "testing realloctesting realloc", buf);
120
121
122 for (j = 0; j < 32; ++j)
123 {
124 i_malloc = malloc_count;
125 buf = malloc((j+1) * 1024 * 1024);
126 CuAssertPtrNotNull(tc, buf);
127 for (i = 0; i < 32; ++i)
128 {
129 area[i] = malloc((i+1) * 1024);
130 CuAssertPtrNotNull(tc, area[i]);
131 }
132 free(buf);
133 for (i = 0; i < 32; ++i)
134 {
135 free(area[i]);
136 }
137#ifndef USE_SYSTEM_MALLOC
138 CuAssertIntEquals (tc, malloc_count, i_malloc);
139#endif
140 }
141
142 /* test realloc */
143 buf = malloc(16);
144 CuAssertPtrNotNull(tc, buf);
145 strcpy(buf, "testing realloc");
146 buf = realloc(buf, 32);
147 strcat(buf, "testing realloc");
148 CuAssertStrEquals(tc, "testing realloctesting realloc", buf);
149}
150
151
152void Test_sh_unix_lookup_page (CuTest *tc) {
153
154 long pagesize = sh_unix_pagesize();
155
156 unsigned long base;
157 int num_pages;
158
159 CuAssert (tc, "pagesize > 0", (pagesize > 0));
160
161 /* base = sh_unix_lookup_page(in_addr, len, *num_pages); */
162
163 base = sh_unix_lookup_page(0, pagesize, &num_pages);
164 CuAssert (tc, "base == 0", (base == 0));
165 CuAssertIntEquals (tc, num_pages, 1);
166
167 base = sh_unix_lookup_page(0, pagesize+1, &num_pages);
168 CuAssert (tc, "base == 0", (base == 0));
169 CuAssertIntEquals (tc, num_pages, 2);
170
171 base = sh_unix_lookup_page((void*)pagesize, pagesize, &num_pages);
172 CuAssert (tc, "base == 0", (base == (unsigned int)pagesize));
173 CuAssertIntEquals (tc, num_pages, 1);
174
175 base = sh_unix_lookup_page((void*)pagesize, pagesize+1, &num_pages);
176 CuAssert (tc, "base == 0", (base == (unsigned int)pagesize));
177 CuAssertIntEquals (tc, num_pages, 2);
178
179 base = sh_unix_lookup_page((void*)(pagesize-1), pagesize+1, &num_pages);
180 CuAssert (tc, "base == 0", (base == 0));
181 CuAssertIntEquals (tc, num_pages, 2);
182
183 base = sh_unix_lookup_page((void*)(pagesize-1), pagesize+2, &num_pages);
184 CuAssert (tc, "base == 0", (base == 0));
185 CuAssertIntEquals (tc, num_pages, 3);
186
187}
188
189
Note: See TracBrowser for help on using the repository browser.