1 /* $NetBSD: test_store.c,v 1.1.1.2 2014/04/24 12:45:51 pettai Exp $ */
4 * Copyright (c) 2006 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of KTH nor the names of its contributors may be
20 * used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
35 #include "krb5_locl.h"
36 #include <krb5/getarg.h>
39 test_int8(krb5_context context
, krb5_storage
*sp
)
47 krb5_storage_truncate(sp
, 0);
49 for (i
= 0; i
< sizeof(val
[0])/sizeof(val
); i
++) {
51 ret
= krb5_store_int8(sp
, val
[i
]);
53 krb5_err(context
, 1, ret
, "krb5_store_int8");
54 krb5_storage_seek(sp
, 0, SEEK_SET
);
55 ret
= krb5_ret_int8(sp
, &v
);
57 krb5_err(context
, 1, ret
, "krb5_ret_int8");
59 krb5_errx(context
, 1, "store and ret mismatch");
64 test_int16(krb5_context context
, krb5_storage
*sp
)
69 0, 1, -1, 32768, -32767
72 krb5_storage_truncate(sp
, 0);
74 for (i
= 0; i
< sizeof(val
[0])/sizeof(val
); i
++) {
76 ret
= krb5_store_int16(sp
, val
[i
]);
78 krb5_err(context
, 1, ret
, "krb5_store_int16");
79 krb5_storage_seek(sp
, 0, SEEK_SET
);
80 ret
= krb5_ret_int16(sp
, &v
);
82 krb5_err(context
, 1, ret
, "krb5_ret_int16");
84 krb5_errx(context
, 1, "store and ret mismatch");
89 test_int32(krb5_context context
, krb5_storage
*sp
)
94 0, 1, -1, 2147483647, -2147483646
97 krb5_storage_truncate(sp
, 0);
99 for (i
= 0; i
< sizeof(val
[0])/sizeof(val
); i
++) {
101 ret
= krb5_store_int32(sp
, val
[i
]);
103 krb5_err(context
, 1, ret
, "krb5_store_int32");
104 krb5_storage_seek(sp
, 0, SEEK_SET
);
105 ret
= krb5_ret_int32(sp
, &v
);
107 krb5_err(context
, 1, ret
, "krb5_ret_int32");
109 krb5_errx(context
, 1, "store and ret mismatch");
114 test_uint8(krb5_context context
, krb5_storage
*sp
)
122 krb5_storage_truncate(sp
, 0);
124 for (i
= 0; i
< sizeof(val
[0])/sizeof(val
); i
++) {
126 ret
= krb5_store_uint8(sp
, val
[i
]);
128 krb5_err(context
, 1, ret
, "krb5_store_uint8");
129 krb5_storage_seek(sp
, 0, SEEK_SET
);
130 ret
= krb5_ret_uint8(sp
, &v
);
132 krb5_err(context
, 1, ret
, "krb5_ret_uint8");
134 krb5_errx(context
, 1, "store and ret mismatch");
139 test_uint16(krb5_context context
, krb5_storage
*sp
)
147 krb5_storage_truncate(sp
, 0);
149 for (i
= 0; i
< sizeof(val
[0])/sizeof(val
); i
++) {
151 ret
= krb5_store_uint16(sp
, val
[i
]);
153 krb5_err(context
, 1, ret
, "krb5_store_uint16");
154 krb5_storage_seek(sp
, 0, SEEK_SET
);
155 ret
= krb5_ret_uint16(sp
, &v
);
157 krb5_err(context
, 1, ret
, "krb5_ret_uint16");
159 krb5_errx(context
, 1, "store and ret mismatch");
164 test_uint32(krb5_context context
, krb5_storage
*sp
)
172 krb5_storage_truncate(sp
, 0);
174 for (i
= 0; i
< sizeof(val
[0])/sizeof(val
); i
++) {
176 ret
= krb5_store_uint32(sp
, val
[i
]);
178 krb5_err(context
, 1, ret
, "krb5_store_uint32");
179 krb5_storage_seek(sp
, 0, SEEK_SET
);
180 ret
= krb5_ret_uint32(sp
, &v
);
182 krb5_err(context
, 1, ret
, "krb5_ret_uint32");
184 krb5_errx(context
, 1, "store and ret mismatch");
190 test_storage(krb5_context context
, krb5_storage
*sp
)
192 test_int8(context
, sp
);
193 test_int16(context
, sp
);
194 test_int32(context
, sp
);
195 test_uint8(context
, sp
);
196 test_uint16(context
, sp
);
197 test_uint32(context
, sp
);
202 test_truncate(krb5_context context
, krb5_storage
*sp
, int fd
)
206 krb5_store_string(sp
, "hej");
207 krb5_storage_truncate(sp
, 2);
209 if (fstat(fd
, &sb
) != 0)
210 krb5_err(context
, 1, errno
, "fstat");
212 krb5_errx(context
, 1, "length not 2");
214 krb5_storage_truncate(sp
, 1024);
216 if (fstat(fd
, &sb
) != 0)
217 krb5_err(context
, 1, errno
, "fstat");
218 if (sb
.st_size
!= 1024)
219 krb5_errx(context
, 1, "length not 2");
223 check_too_large(krb5_context context
, krb5_storage
*sp
)
225 uint32_t too_big_sizes
[] = { INT_MAX
, INT_MAX
/ 2, INT_MAX
/ 4, INT_MAX
/ 8 + 1};
230 for (n
= 0; n
< sizeof(too_big_sizes
) / sizeof(too_big_sizes
); n
++) {
231 krb5_storage_truncate(sp
, 0);
232 krb5_store_uint32(sp
, too_big_sizes
[n
]);
233 krb5_storage_seek(sp
, 0, SEEK_SET
);
234 ret
= krb5_ret_data(sp
, &data
);
235 if (ret
!= HEIM_ERR_TOO_BIG
)
236 errx(1, "not too big: %lu", (unsigned long)n
);
244 static int version_flag
= 0;
245 static int help_flag
= 0;
247 static struct getargs args
[] = {
248 {"version", 0, arg_flag
, &version_flag
,
249 "print version", NULL
},
250 {"help", 0, arg_flag
, &help_flag
,
257 arg_printusage (args
,
258 sizeof(args
)/sizeof(*args
),
265 main(int argc
, char **argv
)
267 krb5_context context
;
271 const char *fn
= "test-store-data";
273 setprogname(argv
[0]);
275 if(getarg(args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
, &optidx
))
289 ret
= krb5_init_context (&context
);
291 errx (1, "krb5_init_context failed: %d", ret
);
294 * Test encoding/decoding of primotive types on diffrent backends
297 sp
= krb5_storage_emem();
299 krb5_errx(context
, 1, "krb5_storage_emem: no mem");
301 test_storage(context
, sp
);
302 check_too_large(context
, sp
);
303 krb5_storage_free(sp
);
306 fd
= open(fn
, O_RDWR
|O_CREAT
|O_TRUNC
, 0600);
308 krb5_err(context
, 1, errno
, "open(%s)", fn
);
310 sp
= krb5_storage_from_fd(fd
);
313 krb5_errx(context
, 1, "krb5_storage_from_fd: %s no mem", fn
);
315 test_storage(context
, sp
);
316 krb5_storage_free(sp
);
320 * test truncate behavior
323 fd
= open(fn
, O_RDWR
|O_CREAT
|O_TRUNC
, 0600);
325 krb5_err(context
, 1, errno
, "open(%s)", fn
);
327 sp
= krb5_storage_from_fd(fd
);
329 krb5_errx(context
, 1, "krb5_storage_from_fd: %s no mem", fn
);
331 test_truncate(context
, sp
, fd
);
332 krb5_storage_free(sp
);
336 krb5_free_context(context
);