source: trunk/src/cutest_sh_unix.c@ 206

Last change on this file since 206 was 171, checked in by katerina, 16 years ago

Include dnmalloc (ticket #108) and fix bugs #106 (EINPROGRESS) and #107 (compressBound).

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