summaryrefslogtreecommitdiffstats
path: root/src/cutest_sh_unix.c
blob: b4428cf8c397721e27580b40742765ff5b5fe074 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "config_xor.h"

#include <string.h>
#include "CuTest.h"
#include "samhain.h"
#include "sh_unix.h"

int malloc_count = 0;

void Test_dnmalloc (CuTest *tc) {

  const int nalloc = 64 /* original dnmalloc 1.0-beta5 fails for >= 45 */;
  int j, i;
  int sum;
#ifndef USE_SYSTEM_MALLOC
  int i_malloc =  malloc_count;
#endif

  char * buf;
  char * area[256];

#if !defined(USE_SYSTEM_MALLOC) && !defined(__clang__)
  /* test reuse of last freed chunk */
  buf = malloc(1024);
  CuAssertPtrNotNull(tc, buf);
  free(buf);
  area[0] = malloc(1024);
  CuAssertTrue(tc, buf == area[0]);
  free(area[0]);
#endif
  
  /* test realloc */
  buf = malloc(16);
  CuAssertPtrNotNull(tc, buf);
  strcpy(buf, "testing realloc");
  buf = realloc(buf, 32);
  strcat(buf, "testing realloc");
  CuAssertStrEquals(tc, "testing realloctesting realloc", buf);

#ifndef USE_SYSTEM_MALLOC
  i_malloc = malloc_count;
#endif

  for (j = 0; j < 64; ++j)
    {
      buf = malloc((j+1) * 1024);
      CuAssertPtrNotNull(tc, buf);
#ifndef USE_SYSTEM_MALLOC
#ifndef __clang__
      CuAssertIntEquals (tc, malloc_count, (i_malloc + 1));
#endif
#endif
      free(buf);
#ifndef USE_SYSTEM_MALLOC
      CuAssertIntEquals (tc, malloc_count, i_malloc);
#endif
    }

  /* test realloc */
  buf = malloc(16);
  CuAssertPtrNotNull(tc, buf);
  strcpy(buf, "testing realloc");
  buf = realloc(buf, 32);
  strcat(buf, "testing realloc");
  CuAssertStrEquals(tc, "testing realloctesting realloc", buf);

#ifndef USE_SYSTEM_MALLOC
  i_malloc = malloc_count;
#endif

  for (j = 0; j < 64; ++j)
    {
      buf = calloc(1, (j+1) * 1024);
      CuAssertPtrNotNull(tc, buf);
#ifndef USE_SYSTEM_MALLOC
      CuAssertIntEquals (tc, malloc_count, (i_malloc + 1));
#endif
      sum = 0;
      for (i = 0; i < ((j+1) * 1024); ++i)
	sum += buf[i];
      CuAssertIntEquals (tc, 0, sum);
      free(buf);
#ifndef USE_SYSTEM_MALLOC
      CuAssertIntEquals (tc, malloc_count, i_malloc);
#endif
    }

  /* test realloc */
  buf = malloc(16);
  CuAssertPtrNotNull(tc, buf);
  strcpy(buf, "testing realloc");
  buf = realloc(buf, 32);
  strcat(buf, "testing realloc");
  CuAssertStrEquals(tc, "testing realloctesting realloc", buf);

  for (j = 0; j < nalloc; ++j)
    {
      area[j] = malloc((j+1) * 1024);
      CuAssertPtrNotNull(tc, area[j]);
#ifndef USE_SYSTEM_MALLOC
      /* CuAssertIntEquals (tc, malloc_count, (i_malloc + (j+1))); */
#endif
      memset(area[j], (unsigned char) ('a'+1), (j+1) * 1024);
    }

#ifndef USE_SYSTEM_MALLOC
  i_malloc =  malloc_count;
#endif

  for (j = 0; j < nalloc; ++j)
    {
      sum = 0;
      for (i = 0; i < ((j+1) * 1024); ++i)
	sum +=  area[j][i];
      CuAssertIntEquals (tc, sum, ((j+1) * 1024 * ((unsigned char) ('a'+1))));
      free(area[j]);
#ifndef USE_SYSTEM_MALLOC
      CuAssertIntEquals (tc, malloc_count, i_malloc - (j+1));
#endif
    }

  /* test realloc */
  buf = malloc(16);
  CuAssertPtrNotNull(tc, buf);
  strcpy(buf, "testing realloc");
  buf = realloc(buf, 32);
  strcat(buf, "testing realloc");
  CuAssertStrEquals(tc, "testing realloctesting realloc", buf);


  for (j = 0; j < 32; ++j)
    {
#ifndef USE_SYSTEM_MALLOC
      i_malloc =  malloc_count;
#endif
      buf = malloc((j+1) * 1024 * 1024);
      CuAssertPtrNotNull(tc, buf);
      for (i = 0; i < 32; ++i)
	{
	  area[i] = malloc((i+1) * 1024);
	  CuAssertPtrNotNull(tc, area[i]);
	}
      free(buf);
      for (i = 0; i < 32; ++i)
	{
	  free(area[i]);
	}
#ifndef USE_SYSTEM_MALLOC
      CuAssertIntEquals (tc, malloc_count, i_malloc);
#endif
    }

  /* test realloc */
  buf = malloc(16);
  CuAssertPtrNotNull(tc, buf);
  strcpy(buf, "testing realloc");
  buf = realloc(buf, 32);
  strcat(buf, "testing realloc");
  CuAssertStrEquals(tc, "testing realloctesting realloc", buf);
}

  
void Test_sh_unix_lookup_page (CuTest *tc) {

  long pagesize = sh_unix_pagesize();
  
  unsigned long base;
  int          num_pages;

  CuAssert (tc, "pagesize > 0", (pagesize > 0));

  /* base = sh_unix_lookup_page(in_addr, len, *num_pages); */

  base = sh_unix_lookup_page(0, pagesize, &num_pages);
  CuAssert (tc, "base == 0", (base == 0));
  CuAssertIntEquals (tc, num_pages, 1);

  base = sh_unix_lookup_page(0, pagesize+1, &num_pages);
  CuAssert (tc, "base == 0", (base == 0));
  CuAssertIntEquals (tc, num_pages, 2);

  base = sh_unix_lookup_page((void*)pagesize, pagesize, &num_pages);
  CuAssert (tc, "base == 0", (base == (unsigned int)pagesize));
  CuAssertIntEquals (tc, num_pages, 1);

  base = sh_unix_lookup_page((void*)pagesize, pagesize+1, &num_pages);
  CuAssert (tc, "base == 0", (base == (unsigned int)pagesize));
  CuAssertIntEquals (tc, num_pages, 2);

  base = sh_unix_lookup_page((void*)(pagesize-1), pagesize+1, &num_pages);
  CuAssert (tc, "base == 0", (base == 0));
  CuAssertIntEquals (tc, num_pages, 2);

  base = sh_unix_lookup_page((void*)(pagesize-1), pagesize+2, &num_pages);
  CuAssert (tc, "base == 0", (base == 0));
  CuAssertIntEquals (tc, num_pages, 3);

}