2 * Copyright (c) 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of KTH nor the names of its contributors may be
18 * used to endorse or promote products derived from this software without
19 * specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
33 #include "krb5_locl.h"
37 test_int8(krb5_context context
, krb5_storage
*sp
)
45 krb5_storage_truncate(sp
, 0);
47 for (i
= 0; i
< sizeof(val
)/sizeof(val
[0]); i
++) {
49 ret
= krb5_store_int8(sp
, val
[i
]);
51 krb5_err(context
, 1, ret
, "krb5_store_int8");
52 krb5_storage_seek(sp
, i
, SEEK_SET
);
53 ret
= krb5_ret_int8(sp
, &v
);
55 krb5_err(context
, 1, ret
, "krb5_ret_int8");
57 krb5_errx(context
, 1, "store and ret mismatch");
62 test_int16(krb5_context context
, krb5_storage
*sp
)
67 0, 1, -1, 32767, -32768
70 krb5_storage_truncate(sp
, 0);
72 for (i
= 0; i
< sizeof(val
)/sizeof(val
[0]); i
++) {
74 ret
= krb5_store_int16(sp
, val
[i
]);
76 krb5_err(context
, 1, ret
, "krb5_store_int16");
77 krb5_storage_seek(sp
, i
* sizeof (v
), SEEK_SET
);
78 ret
= krb5_ret_int16(sp
, &v
);
80 krb5_err(context
, 1, ret
, "krb5_ret_int16");
82 krb5_errx(context
, 1, "store and ret mismatch");
87 test_int32(krb5_context context
, krb5_storage
*sp
)
92 0, 1, -1, 2147483647, -2147483646
95 krb5_storage_truncate(sp
, 0);
97 for (i
= 0; i
< sizeof(val
)/sizeof(val
[0]); i
++) {
99 ret
= krb5_store_int32(sp
, val
[i
]);
101 krb5_err(context
, 1, ret
, "krb5_store_int32");
102 krb5_storage_seek(sp
, i
* sizeof (v
), SEEK_SET
);
103 ret
= krb5_ret_int32(sp
, &v
);
105 krb5_err(context
, 1, ret
, "krb5_ret_int32");
107 krb5_errx(context
, 1, "store and ret mismatch");
112 test_uint8(krb5_context context
, krb5_storage
*sp
)
120 krb5_storage_truncate(sp
, 0);
122 for (i
= 0; i
< sizeof(val
)/sizeof(val
[0]); i
++) {
124 ret
= krb5_store_uint8(sp
, val
[i
]);
126 krb5_err(context
, 1, ret
, "krb5_store_uint8");
127 krb5_storage_seek(sp
, i
* sizeof (v
), SEEK_SET
);
128 ret
= krb5_ret_uint8(sp
, &v
);
130 krb5_err(context
, 1, ret
, "krb5_ret_uint8");
132 krb5_errx(context
, 1, "store and ret mismatch");
137 test_uint16(krb5_context context
, krb5_storage
*sp
)
145 krb5_storage_truncate(sp
, 0);
147 for (i
= 0; i
< sizeof(val
)/sizeof(val
[0]); i
++) {
149 ret
= krb5_store_uint16(sp
, val
[i
]);
151 krb5_err(context
, 1, ret
, "krb5_store_uint16");
152 krb5_storage_seek(sp
, i
* sizeof (v
), SEEK_SET
);
153 ret
= krb5_ret_uint16(sp
, &v
);
155 krb5_err(context
, 1, ret
, "krb5_ret_uint16");
157 krb5_errx(context
, 1, "store and ret mismatch");
162 test_uint32(krb5_context context
, krb5_storage
*sp
)
170 krb5_storage_truncate(sp
, 0);
172 for (i
= 0; i
< sizeof(val
)/sizeof(val
[0]); i
++) {
174 ret
= krb5_store_uint32(sp
, val
[i
]);
176 krb5_err(context
, 1, ret
, "krb5_store_uint32");
177 krb5_storage_seek(sp
, i
* sizeof (v
), SEEK_SET
);
178 ret
= krb5_ret_uint32(sp
, &v
);
180 krb5_err(context
, 1, ret
, "krb5_ret_uint32");
182 krb5_errx(context
, 1, "store and ret mismatch");
188 test_storage(krb5_context context
, krb5_storage
*sp
)
190 test_int8(context
, sp
);
191 test_int16(context
, sp
);
192 test_int32(context
, sp
);
193 test_uint8(context
, sp
);
194 test_uint16(context
, sp
);
195 test_uint32(context
, sp
);
200 test_truncate(krb5_context context
, krb5_storage
*sp
, int fd
)
204 krb5_storage_truncate(sp
, 0);
205 krb5_store_string(sp
, "hej");
206 krb5_storage_truncate(sp
, 2);
208 if (fstat(fd
, &sb
) != 0)
209 krb5_err(context
, 1, errno
, "fstat");
211 krb5_errx(context
, 1, "length not 2");
213 krb5_storage_truncate(sp
, 1024);
215 if (fstat(fd
, &sb
) != 0)
216 krb5_err(context
, 1, errno
, "fstat");
217 if (sb
.st_size
!= 1024)
218 krb5_errx(context
, 1, "length not 1024");
222 test_buffer_issues(krb5_context context
, krb5_storage
*sp
)
228 krb5_storage_set_eof_code(sp
, -1);
229 krb5_storage_truncate(sp
, 0);
230 for (i
=0; i
< 4096; i
++) {
231 krb5_store_uint32(sp
, i
);
234 krb5_storage_truncate(sp
, 1024);
235 ret
= krb5_ret_uint32(sp
, &v
);
237 krb5_errx(context
, 1, "Should have received EOF");
239 krb5_storage_seek(sp
, 8, SEEK_SET
);
240 ret
= krb5_ret_uint32(sp
, &v
);
242 krb5_errx(context
, 1, "Should not have received EOF");
244 krb5_errx(context
, 1, "uint32 should have been 2");
248 check_too_large(krb5_context context
, krb5_storage
*sp
)
250 uint32_t too_big_sizes
[] = { UINT_MAX
, UINT_MAX
/ 2, UINT_MAX
/ 4, UINT_MAX
/ 8 + 1};
255 for (n
= 0; n
< sizeof(too_big_sizes
) / sizeof(too_big_sizes
[0]); n
++) {
256 krb5_storage_truncate(sp
, 0);
257 krb5_store_uint32(sp
, too_big_sizes
[n
]);
258 krb5_storage_seek(sp
, 0, SEEK_SET
);
259 ret
= krb5_ret_data(sp
, &data
);
260 if (ret
!= HEIM_ERR_TOO_BIG
)
261 errx(1, "not too big: %lu", (unsigned long)n
);
269 static int version_flag
= 0;
270 static int help_flag
= 0;
272 static struct getargs args
[] = {
273 {"version", 0, arg_flag
, &version_flag
,
274 "print version", NULL
},
275 {"help", 0, arg_flag
, &help_flag
,
282 arg_printusage (args
,
283 sizeof(args
)/sizeof(*args
),
290 main(int argc
, char **argv
)
292 krb5_context context
;
296 const char *fn
= "test-store-data";
298 setprogname(argv
[0]);
300 if(getarg(args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
, &optidx
))
314 ret
= krb5_init_context (&context
);
316 errx (1, "krb5_init_context failed: %d", ret
);
319 * Test encoding/decoding of primotive types on diffrent backends
322 sp
= krb5_storage_emem();
324 krb5_errx(context
, 1, "krb5_storage_emem: no mem");
326 test_storage(context
, sp
);
327 check_too_large(context
, sp
);
328 krb5_storage_free(sp
);
331 fd
= open(fn
, O_RDWR
|O_CREAT
|O_TRUNC
, 0600);
333 krb5_err(context
, 1, errno
, "open(%s)", fn
);
335 sp
= krb5_storage_from_fd(fd
);
337 krb5_errx(context
, 1, "krb5_storage_from_fd: %s no mem", fn
);
339 test_storage(context
, sp
);
340 test_truncate(context
, sp
, fd
);
341 test_buffer_issues(context
, sp
);
342 krb5_storage_free(sp
);
346 fd
= open(fn
, O_RDWR
|O_CREAT
|O_TRUNC
, 0600);
348 krb5_err(context
, 1, errno
, "open(%s)", fn
);
350 sp
= krb5_storage_stdio_from_fd(fd
, "r+");
352 krb5_errx(context
, 1, "krb5_storage_stdio_from_fd: %s no mem", fn
);
354 test_storage(context
, sp
);
355 test_truncate(context
, sp
, fd
);
356 test_buffer_issues(context
, sp
);
357 krb5_storage_free(sp
);
361 krb5_free_context(context
);