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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
/* -*- mode: c; indent-tabs-mode: nil -*- */
/*
* t_k5buf.c
*
* Copyright 2008 Massachusetts Institute of Technology.
* All Rights Reserved.
*
* Export of this software from the United States of America may
* require a specific license from the United States Government.
* It is the responsibility of any person or organization contemplating
* export to obtain such a license before exporting.
*
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
* distribute this software and its documentation for any purpose and
* without fee is hereby granted, provided that the above copyright
* notice appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation, and that
* the name of M.I.T. not be used in advertising or publicity pertaining
* to distribution of the software without specific, written prior
* permission. Furthermore if you modify this software you must label
* your software as modified software and not distribute it in such a
* fashion that it might be confused with the original M.I.T. software.
* M.I.T. makes no representations about the suitability of
* this software for any purpose. It is provided "as is" without express
* or implied warranty.
*
* Test the k5buf string buffer module.
*/
#include "k5buf-int.h"
#include <stdio.h>
#include <stdlib.h>
static void fail_if(int condition, const char *name)
{
if (condition) {
fprintf(stderr, "%s failed\n", name);
exit(1);
}
}
/* Test the invariants of a buffer. */
static void check_buf(struct k5buf *buf, const char *name)
{
fail_if(buf->buftype != BUFTYPE_FIXED && buf->buftype != BUFTYPE_DYNAMIC
&& buf->buftype != BUFTYPE_ERROR, name);
if (buf->buftype == BUFTYPE_ERROR)
return;
fail_if(buf->space == 0, name);
fail_if(buf->space > SPACE_MAX, name);
fail_if(buf->len >= buf->space, name);
fail_if(buf->data[buf->len] != 0, name);
}
static void test_basic()
{
struct k5buf buf;
char storage[1024], *s;
ssize_t len;
krb5int_buf_init_fixed(&buf, storage, sizeof(storage));
krb5int_buf_add(&buf, "Hello ");
krb5int_buf_add_len(&buf, "world", 5);
check_buf(&buf, "basic fixed");
s = krb5int_buf_data(&buf);
len = krb5int_buf_len(&buf);
fail_if(!s || strcmp(s, "Hello world") != 0 || len != 11, "basic fixed");
krb5int_buf_init_dynamic(&buf);
krb5int_buf_add_len(&buf, "Hello", 5);
krb5int_buf_add(&buf, " world");
check_buf(&buf, "basic dynamic");
s = krb5int_buf_data(&buf);
len = krb5int_buf_len(&buf);
fail_if(!s || strcmp(s, "Hello world") != 0 || len != 11, "basic dynamic");
krb5int_free_buf(&buf);
}
static void test_realloc()
{
struct k5buf buf;
char data[1024], *s;
ssize_t len;
size_t i;
for (i = 0; i < sizeof(data); i++)
data[i] = 'a';
/* Cause the buffer size to double from 128 to 256 bytes. */
krb5int_buf_init_dynamic(&buf);
krb5int_buf_add_len(&buf, data, 10);
krb5int_buf_add_len(&buf, data, 128);
fail_if(buf.space != 256, "realloc 1");
check_buf(&buf, "realloc 1");
s = krb5int_buf_data(&buf);
len = krb5int_buf_len(&buf);
fail_if(!s || len != 138 || memcmp(s, data, len) != 0, "realloc 1");
/* Cause the same buffer to double in size to 512 bytes. */
krb5int_buf_add_len(&buf, data, 128);
fail_if(buf.space != 512, "realloc 2");
check_buf(&buf, "realloc 2");
s = krb5int_buf_data(&buf);
len = krb5int_buf_len(&buf);
fail_if(!s || len != 266 || memcmp(s, data, len) != 0, "realloc 2");
krb5int_free_buf(&buf);
/* Cause a buffer to increase from 128 to 512 bytes directly. */
krb5int_buf_init_dynamic(&buf);
krb5int_buf_add_len(&buf, data, 10);
krb5int_buf_add_len(&buf, data, 256);
fail_if(buf.space != 512, "realloc 3");
check_buf(&buf, "realloc 3");
s = krb5int_buf_data(&buf);
len = krb5int_buf_len(&buf);
fail_if(!s || len != 266 || memcmp(s, data, len) != 0, "realloc 3");
krb5int_free_buf(&buf);
/* Cause a buffer to increase from 128 to 1024 bytes directly. */
krb5int_buf_init_dynamic(&buf);
krb5int_buf_add_len(&buf, data, 10);
krb5int_buf_add_len(&buf, data, 512);
fail_if(buf.space != 1024, "realloc 4");
check_buf(&buf, "realloc 4");
s = krb5int_buf_data(&buf);
len = krb5int_buf_len(&buf);
fail_if(!s || len != 522 || memcmp(s, data, len) != 0, "realloc 4");
krb5int_free_buf(&buf);
/* Cause a reallocation to fail by exceeding SPACE_MAX. */
krb5int_buf_init_dynamic(&buf);
krb5int_buf_add_len(&buf, data, 10);
krb5int_buf_add_len(&buf, NULL, SPACE_MAX);
check_buf(&buf, "realloc 5");
s = krb5int_buf_data(&buf);
len = krb5int_buf_len(&buf);
fail_if(buf.buftype != BUFTYPE_ERROR || s != NULL || len != -1,
"realloc 5");
krb5int_free_buf(&buf);
/* Cause a reallocation to fail by integer overflow. */
krb5int_buf_init_dynamic(&buf);
krb5int_buf_add_len(&buf, data, 100);
krb5int_buf_add_len(&buf, NULL, SPACE_MAX * 2);
check_buf(&buf, "realloc 6");
s = krb5int_buf_data(&buf);
len = krb5int_buf_len(&buf);
fail_if(buf.buftype != BUFTYPE_ERROR || s != NULL || len != -1,
"realloc 6");
krb5int_free_buf(&buf);
}
static void test_overflow()
{
struct k5buf buf;
char storage[10], *s;
ssize_t len;
/* Cause a fixed-sized buffer overflow. */
krb5int_buf_init_fixed(&buf, storage, sizeof(storage));
krb5int_buf_add(&buf, "12345");
krb5int_buf_add(&buf, "12345");
check_buf(&buf, "overflow 1");
s = krb5int_buf_data(&buf);
len = krb5int_buf_len(&buf);
fail_if(buf.buftype != BUFTYPE_ERROR || s != NULL || len != -1,
"overflow 1");
/* Cause a fixed-sized buffer overflow with integer overflow. */
krb5int_buf_init_fixed(&buf, storage, sizeof(storage));
krb5int_buf_add(&buf, "12345");
krb5int_buf_add_len(&buf, NULL, SPACE_MAX * 2);
check_buf(&buf, "overflow 2");
s = krb5int_buf_data(&buf);
len = krb5int_buf_len(&buf);
fail_if(buf.buftype != BUFTYPE_ERROR || s != NULL || len != -1,
"overflow 2");
}
static void test_error()
{
struct k5buf buf;
char storage[1];
/* Cause an overflow and then perform actions afterwards. */
krb5int_buf_init_fixed(&buf, storage, sizeof(storage));
krb5int_buf_add(&buf, "1");
fail_if(buf.buftype != BUFTYPE_ERROR, "error");
check_buf(&buf, "error");
krb5int_buf_add(&buf, "test");
check_buf(&buf, "error");
krb5int_buf_add_len(&buf, "test", 4);
check_buf(&buf, "error");
krb5int_buf_truncate(&buf, 3);
check_buf(&buf, "error");
fail_if(buf.buftype != BUFTYPE_ERROR, "error");
}
static void test_truncate()
{
struct k5buf buf;
char *s;
ssize_t len;
krb5int_buf_init_dynamic(&buf);
krb5int_buf_add(&buf, "abcde");
krb5int_buf_add(&buf, "fghij");
krb5int_buf_truncate(&buf, 7);
check_buf(&buf, "truncate");
s = krb5int_buf_data(&buf);
len = krb5int_buf_len(&buf);
fail_if(!s || len != 7 || strcmp(s, "abcdefg") != 0, "truncate");
krb5int_free_buf(&buf);
}
static void test_binary()
{
struct k5buf buf;
char *s, data[] = { 'a', 0, 'b' };
ssize_t len;
krb5int_buf_init_dynamic(&buf);
krb5int_buf_add_len(&buf, data, 3);
krb5int_buf_add_len(&buf, data, 3);
check_buf(&buf, "binary");
s = krb5int_buf_data(&buf);
len = krb5int_buf_len(&buf);
fail_if(!s || len != 6, "binary");
fail_if(s[0] != 'a' || s[1] != 0 || s[2] != 'b', "binary");
fail_if(s[3] != 'a' || s[4] != 0 || s[5] != 'b', "binary");
krb5int_free_buf(&buf);
}
static void test_fmt()
{
struct k5buf buf;
char *s, storage[10], data[1024];
ssize_t len;
size_t i;
for (i = 0; i < sizeof(data) - 1; i++)
data[i] = 'a';
data[i] = '\0';
/* Format some text into a non-empty fixed buffer. */
krb5int_buf_init_fixed(&buf, storage, sizeof(storage));
krb5int_buf_add(&buf, "foo");
krb5int_buf_add_fmt(&buf, " %d ", 3);
check_buf(&buf, "fmt 1");
s = krb5int_buf_data(&buf);
len = krb5int_buf_len(&buf);
fail_if(!s || len != 6 || strcmp(s, "foo 3 ") != 0, "fmt 1");
/* Overflow the same buffer with formatted text. */
krb5int_buf_add_fmt(&buf, "%d%d%d%d", 1, 2, 3, 4);
check_buf(&buf, "fmt 2");
s = krb5int_buf_data(&buf);
len = krb5int_buf_len(&buf);
fail_if(buf.buftype != BUFTYPE_ERROR || s != NULL || len != -1, "fmt 2");
/* Format some text into a non-empty dynamic buffer. */
krb5int_buf_init_dynamic(&buf);
krb5int_buf_add(&buf, "foo");
krb5int_buf_add_fmt(&buf, " %d ", 3);
check_buf(&buf, "fmt 3");
s = krb5int_buf_data(&buf);
len = krb5int_buf_len(&buf);
fail_if(!s || len != 6 || strcmp(s, "foo 3 ") != 0, "fmt 3");
/* Format more text into the same buffer, causing a big resize. */
krb5int_buf_add_fmt(&buf, "%s", data);
check_buf(&buf, "fmt 4");
fail_if(buf.space != 2048, "fmt 4");
s = krb5int_buf_data(&buf);
len = krb5int_buf_len(&buf);
fail_if(!s || len != 1029 || strcmp(s + 6, data) != 0, "fmt 4");
krb5int_free_buf(&buf);
}
int main()
{
test_basic();
test_realloc();
test_overflow();
test_error();
test_truncate();
test_binary();
test_fmt();
return 0;
}
|