source: trunk/src/cutest_sh_unix.c@ 551

Last change on this file since 551 was 539, checked in by katerina, 6 years ago

Fixes for tickets #431 (OpenBSD compatibility) and #432 (compiler warnings).

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