test_store.c revision 178825
1178825Sdfr/* 2178825Sdfr * Copyright (c) 2006 Kungliga Tekniska H�gskolan 3178825Sdfr * (Royal Institute of Technology, Stockholm, Sweden). 4178825Sdfr * All rights reserved. 5178825Sdfr * 6178825Sdfr * Redistribution and use in source and binary forms, with or without 7178825Sdfr * modification, are permitted provided that the following conditions 8178825Sdfr * are met: 9178825Sdfr * 10178825Sdfr * 1. Redistributions of source code must retain the above copyright 11178825Sdfr * notice, this list of conditions and the following disclaimer. 12178825Sdfr * 13178825Sdfr * 2. Redistributions in binary form must reproduce the above copyright 14178825Sdfr * notice, this list of conditions and the following disclaimer in the 15178825Sdfr * documentation and/or other materials provided with the distribution. 16178825Sdfr * 17178825Sdfr * 3. Neither the name of KTH nor the names of its contributors may be 18178825Sdfr * used to endorse or promote products derived from this software without 19178825Sdfr * specific prior written permission. 20178825Sdfr * 21178825Sdfr * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY 22178825Sdfr * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23178825Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24178825Sdfr * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE 25178825Sdfr * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26178825Sdfr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27178825Sdfr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 28178825Sdfr * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29178825Sdfr * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 30178825Sdfr * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 31178825Sdfr * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 32178825Sdfr 33178825Sdfr#include "krb5_locl.h" 34178825Sdfr#include <getarg.h> 35178825Sdfr 36178825SdfrRCSID("$Id: test_store.c 20192 2007-02-05 23:21:03Z lha $"); 37178825Sdfr 38178825Sdfrstatic void 39178825Sdfrtest_int8(krb5_context context, krb5_storage *sp) 40178825Sdfr{ 41178825Sdfr krb5_error_code ret; 42178825Sdfr int i; 43178825Sdfr int8_t val[] = { 44178825Sdfr 0, 1, -1, 128, -127 45178825Sdfr }, v; 46178825Sdfr 47178825Sdfr for (i = 0; i < sizeof(val[0])/sizeof(val); i++) { 48178825Sdfr 49178825Sdfr ret = krb5_store_int8(sp, val[i]); 50178825Sdfr if (ret) 51178825Sdfr krb5_err(context, 1, ret, "krb5_store_int8"); 52178825Sdfr krb5_storage_seek(sp, 0, SEEK_SET); 53178825Sdfr ret = krb5_ret_int8(sp, &v); 54178825Sdfr if (ret) 55178825Sdfr krb5_err(context, 1, ret, "krb5_ret_int8"); 56178825Sdfr if (v != val[i]) 57178825Sdfr krb5_errx(context, 1, "store and ret mismatch"); 58178825Sdfr } 59178825Sdfr} 60178825Sdfr 61178825Sdfrstatic void 62178825Sdfrtest_int16(krb5_context context, krb5_storage *sp) 63178825Sdfr{ 64178825Sdfr krb5_error_code ret; 65178825Sdfr int i; 66178825Sdfr int16_t val[] = { 67178825Sdfr 0, 1, -1, 32768, -32767 68178825Sdfr }, v; 69178825Sdfr 70178825Sdfr for (i = 0; i < sizeof(val[0])/sizeof(val); i++) { 71178825Sdfr 72178825Sdfr ret = krb5_store_int16(sp, val[i]); 73178825Sdfr if (ret) 74178825Sdfr krb5_err(context, 1, ret, "krb5_store_int16"); 75178825Sdfr krb5_storage_seek(sp, 0, SEEK_SET); 76178825Sdfr ret = krb5_ret_int16(sp, &v); 77178825Sdfr if (ret) 78178825Sdfr krb5_err(context, 1, ret, "krb5_ret_int16"); 79178825Sdfr if (v != val[i]) 80178825Sdfr krb5_errx(context, 1, "store and ret mismatch"); 81178825Sdfr } 82178825Sdfr} 83178825Sdfr 84178825Sdfrstatic void 85178825Sdfrtest_int32(krb5_context context, krb5_storage *sp) 86178825Sdfr{ 87178825Sdfr krb5_error_code ret; 88178825Sdfr int i; 89178825Sdfr int32_t val[] = { 90178825Sdfr 0, 1, -1, 2147483647, -2147483646 91178825Sdfr }, v; 92178825Sdfr 93178825Sdfr for (i = 0; i < sizeof(val[0])/sizeof(val); i++) { 94178825Sdfr 95178825Sdfr ret = krb5_store_int32(sp, val[i]); 96178825Sdfr if (ret) 97178825Sdfr krb5_err(context, 1, ret, "krb5_store_int32"); 98178825Sdfr krb5_storage_seek(sp, 0, SEEK_SET); 99178825Sdfr ret = krb5_ret_int32(sp, &v); 100178825Sdfr if (ret) 101178825Sdfr krb5_err(context, 1, ret, "krb5_ret_int32"); 102178825Sdfr if (v != val[i]) 103178825Sdfr krb5_errx(context, 1, "store and ret mismatch"); 104178825Sdfr } 105178825Sdfr} 106178825Sdfr 107178825Sdfrstatic void 108178825Sdfrtest_uint8(krb5_context context, krb5_storage *sp) 109178825Sdfr{ 110178825Sdfr krb5_error_code ret; 111178825Sdfr int i; 112178825Sdfr uint8_t val[] = { 113178825Sdfr 0, 1, 255 114178825Sdfr }, v; 115178825Sdfr 116178825Sdfr for (i = 0; i < sizeof(val[0])/sizeof(val); i++) { 117178825Sdfr 118178825Sdfr ret = krb5_store_uint8(sp, val[i]); 119178825Sdfr if (ret) 120178825Sdfr krb5_err(context, 1, ret, "krb5_store_uint8"); 121178825Sdfr krb5_storage_seek(sp, 0, SEEK_SET); 122178825Sdfr ret = krb5_ret_uint8(sp, &v); 123178825Sdfr if (ret) 124178825Sdfr krb5_err(context, 1, ret, "krb5_ret_uint8"); 125178825Sdfr if (v != val[i]) 126178825Sdfr krb5_errx(context, 1, "store and ret mismatch"); 127178825Sdfr } 128178825Sdfr} 129178825Sdfr 130178825Sdfrstatic void 131178825Sdfrtest_uint16(krb5_context context, krb5_storage *sp) 132178825Sdfr{ 133178825Sdfr krb5_error_code ret; 134178825Sdfr int i; 135178825Sdfr uint16_t val[] = { 136178825Sdfr 0, 1, 65535 137178825Sdfr }, v; 138178825Sdfr 139178825Sdfr for (i = 0; i < sizeof(val[0])/sizeof(val); i++) { 140178825Sdfr 141178825Sdfr ret = krb5_store_uint16(sp, val[i]); 142178825Sdfr if (ret) 143178825Sdfr krb5_err(context, 1, ret, "krb5_store_uint16"); 144178825Sdfr krb5_storage_seek(sp, 0, SEEK_SET); 145178825Sdfr ret = krb5_ret_uint16(sp, &v); 146178825Sdfr if (ret) 147178825Sdfr krb5_err(context, 1, ret, "krb5_ret_uint16"); 148178825Sdfr if (v != val[i]) 149178825Sdfr krb5_errx(context, 1, "store and ret mismatch"); 150178825Sdfr } 151178825Sdfr} 152178825Sdfr 153178825Sdfrstatic void 154178825Sdfrtest_uint32(krb5_context context, krb5_storage *sp) 155178825Sdfr{ 156178825Sdfr krb5_error_code ret; 157178825Sdfr int i; 158178825Sdfr uint32_t val[] = { 159178825Sdfr 0, 1, 4294967295UL 160178825Sdfr }, v; 161178825Sdfr 162178825Sdfr for (i = 0; i < sizeof(val[0])/sizeof(val); i++) { 163178825Sdfr 164178825Sdfr ret = krb5_store_uint32(sp, val[i]); 165178825Sdfr if (ret) 166178825Sdfr krb5_err(context, 1, ret, "krb5_store_uint32"); 167178825Sdfr krb5_storage_seek(sp, 0, SEEK_SET); 168178825Sdfr ret = krb5_ret_uint32(sp, &v); 169178825Sdfr if (ret) 170178825Sdfr krb5_err(context, 1, ret, "krb5_ret_uint32"); 171178825Sdfr if (v != val[i]) 172178825Sdfr krb5_errx(context, 1, "store and ret mismatch"); 173178825Sdfr } 174178825Sdfr} 175178825Sdfr 176178825Sdfr 177178825Sdfrstatic void 178178825Sdfrtest_storage(krb5_context context) 179178825Sdfr{ 180178825Sdfr krb5_storage *sp; 181178825Sdfr 182178825Sdfr sp = krb5_storage_emem(); 183178825Sdfr if (sp == NULL) 184178825Sdfr krb5_errx(context, 1, "krb5_storage_emem: no mem"); 185178825Sdfr 186178825Sdfr test_int8(context, sp); 187178825Sdfr test_int16(context, sp); 188178825Sdfr test_int32(context, sp); 189178825Sdfr test_uint8(context, sp); 190178825Sdfr test_uint16(context, sp); 191178825Sdfr test_uint32(context, sp); 192178825Sdfr 193178825Sdfr krb5_storage_free(sp); 194178825Sdfr} 195178825Sdfr 196178825Sdfr/* 197178825Sdfr * 198178825Sdfr */ 199178825Sdfr 200178825Sdfrstatic int version_flag = 0; 201178825Sdfrstatic int help_flag = 0; 202178825Sdfr 203178825Sdfrstatic struct getargs args[] = { 204178825Sdfr {"version", 0, arg_flag, &version_flag, 205178825Sdfr "print version", NULL }, 206178825Sdfr {"help", 0, arg_flag, &help_flag, 207178825Sdfr NULL, NULL } 208178825Sdfr}; 209178825Sdfr 210178825Sdfrstatic void 211178825Sdfrusage (int ret) 212178825Sdfr{ 213178825Sdfr arg_printusage (args, 214178825Sdfr sizeof(args)/sizeof(*args), 215178825Sdfr NULL, 216178825Sdfr ""); 217178825Sdfr exit (ret); 218178825Sdfr} 219178825Sdfr 220178825Sdfrint 221178825Sdfrmain(int argc, char **argv) 222178825Sdfr{ 223178825Sdfr krb5_context context; 224178825Sdfr krb5_error_code ret; 225178825Sdfr int optidx = 0; 226178825Sdfr 227178825Sdfr setprogname(argv[0]); 228178825Sdfr 229178825Sdfr if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx)) 230178825Sdfr usage(1); 231178825Sdfr 232178825Sdfr if (help_flag) 233178825Sdfr usage (0); 234178825Sdfr 235178825Sdfr if(version_flag){ 236178825Sdfr print_version(NULL); 237178825Sdfr exit(0); 238178825Sdfr } 239178825Sdfr 240178825Sdfr argc -= optidx; 241178825Sdfr argv += optidx; 242178825Sdfr 243178825Sdfr ret = krb5_init_context (&context); 244178825Sdfr if (ret) 245178825Sdfr errx (1, "krb5_init_context failed: %d", ret); 246178825Sdfr 247178825Sdfr test_storage(context); 248178825Sdfr 249178825Sdfr krb5_free_context(context); 250178825Sdfr 251178825Sdfr return 0; 252178825Sdfr} 253