source: trunk/src/cutest_sh_unix.c@ 530

Last change on this file since 530 was 481, checked in by katerina, 9 years ago

Enhancements and fixes for tickets #374, #375, #376, #377, #378, and #379.

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