source: trunk/src/cutest_sh_unix.c@ 576

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

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

File size: 4.9 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
[237]13 const int nalloc = 64 /* original dnmalloc 1.0-beta5 fails for >= 45 */;
[171]14 int j, i;
15 int sum;
[481]16#ifndef USE_SYSTEM_MALLOC
[171]17 int i_malloc = malloc_count;
[481]18#endif
[171]19
20 char * buf;
21 char * area[256];
22
[539]23#if !defined(USE_SYSTEM_MALLOC) && !defined(__clang__)
[171]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]);
[539]31#endif
32
[171]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
[481]41#ifndef USE_SYSTEM_MALLOC
[171]42 i_malloc = malloc_count;
[481]43#endif
[171]44
45 for (j = 0; j < 64; ++j)
46 {
47 buf = malloc((j+1) * 1024);
48 CuAssertPtrNotNull(tc, buf);
49#ifndef USE_SYSTEM_MALLOC
[474]50#ifndef __clang__
[171]51 CuAssertIntEquals (tc, malloc_count, (i_malloc + 1));
52#endif
[474]53#endif
[171]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
[481]68#ifndef USE_SYSTEM_MALLOC
[171]69 i_malloc = malloc_count;
[481]70#endif
[171]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
[237]102 /* CuAssertIntEquals (tc, malloc_count, (i_malloc + (j+1))); */
[171]103#endif
104 memset(area[j], (unsigned char) ('a'+1), (j+1) * 1024);
105 }
106
[481]107#ifndef USE_SYSTEM_MALLOC
[171]108 i_malloc = malloc_count;
[481]109#endif
[171]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 {
[481]134#ifndef USE_SYSTEM_MALLOC
[171]135 i_malloc = malloc_count;
[481]136#endif
[171]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
[24]164void Test_sh_unix_lookup_page (CuTest *tc) {
165
[30]166 long pagesize = sh_unix_pagesize();
[24]167
[30]168 unsigned long base;
[24]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.