xref: /minix3/crypto/external/bsd/heimdal/dist/lib/krb5/store-test.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: store-test.c,v 1.1.1.2 2014/04/24 12:45:51 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 2001 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of KTH nor the names of its contributors may be
20ebfedea0SLionel Sambuc  *    used to endorse or promote products derived from this software without
21ebfedea0SLionel Sambuc  *    specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24ebfedea0SLionel Sambuc  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27ebfedea0SLionel Sambuc  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28ebfedea0SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29ebfedea0SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30ebfedea0SLionel Sambuc  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31ebfedea0SLionel Sambuc  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32ebfedea0SLionel Sambuc  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33ebfedea0SLionel Sambuc  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
34ebfedea0SLionel Sambuc 
35ebfedea0SLionel Sambuc #include "krb5_locl.h"
36ebfedea0SLionel Sambuc 
37ebfedea0SLionel Sambuc static void
print_data(unsigned char * data,size_t len)38ebfedea0SLionel Sambuc print_data(unsigned char *data, size_t len)
39ebfedea0SLionel Sambuc {
40ebfedea0SLionel Sambuc     int i;
41ebfedea0SLionel Sambuc     for(i = 0; i < len; i++) {
42ebfedea0SLionel Sambuc 	if(i > 0 && (i % 16) == 0)
43ebfedea0SLionel Sambuc 	    printf("\n            ");
44ebfedea0SLionel Sambuc 	printf("%02x ", data[i]);
45ebfedea0SLionel Sambuc     }
46ebfedea0SLionel Sambuc     printf("\n");
47ebfedea0SLionel Sambuc }
48ebfedea0SLionel Sambuc 
49ebfedea0SLionel Sambuc static int
compare(const char * name,krb5_storage * sp,void * expected,size_t len)50ebfedea0SLionel Sambuc compare(const char *name, krb5_storage *sp, void *expected, size_t len)
51ebfedea0SLionel Sambuc {
52ebfedea0SLionel Sambuc     int ret = 0;
53ebfedea0SLionel Sambuc     krb5_data data;
54ebfedea0SLionel Sambuc     if (krb5_storage_to_data(sp, &data))
55ebfedea0SLionel Sambuc 	errx(1, "krb5_storage_to_data failed");
56ebfedea0SLionel Sambuc     krb5_storage_free(sp);
57ebfedea0SLionel Sambuc     if(data.length != len || memcmp(data.data, expected, len) != 0) {
58ebfedea0SLionel Sambuc 	printf("%s mismatch\n", name);
59ebfedea0SLionel Sambuc 	printf("  Expected: ");
60ebfedea0SLionel Sambuc 	print_data(expected, len);
61ebfedea0SLionel Sambuc 	printf("  Actual:   ");
62ebfedea0SLionel Sambuc 	print_data(data.data, data.length);
63ebfedea0SLionel Sambuc 	ret++;
64ebfedea0SLionel Sambuc     }
65ebfedea0SLionel Sambuc     krb5_data_free(&data);
66ebfedea0SLionel Sambuc     return ret;
67ebfedea0SLionel Sambuc }
68ebfedea0SLionel Sambuc 
69ebfedea0SLionel Sambuc int
main(int argc,char ** argv)70ebfedea0SLionel Sambuc main(int argc, char **argv)
71ebfedea0SLionel Sambuc {
72ebfedea0SLionel Sambuc     int nerr = 0;
73ebfedea0SLionel Sambuc     krb5_storage *sp;
74ebfedea0SLionel Sambuc     krb5_context context;
75ebfedea0SLionel Sambuc     krb5_principal principal;
76ebfedea0SLionel Sambuc 
77ebfedea0SLionel Sambuc 
78ebfedea0SLionel Sambuc     krb5_init_context(&context);
79ebfedea0SLionel Sambuc 
80ebfedea0SLionel Sambuc     sp = krb5_storage_emem();
81ebfedea0SLionel Sambuc     krb5_store_int32(sp, 0x01020304);
82ebfedea0SLionel Sambuc     nerr += compare("Integer", sp, "\x1\x2\x3\x4", 4);
83ebfedea0SLionel Sambuc 
84ebfedea0SLionel Sambuc     sp = krb5_storage_emem();
85ebfedea0SLionel Sambuc     krb5_storage_set_byteorder(sp, KRB5_STORAGE_BYTEORDER_LE);
86ebfedea0SLionel Sambuc     krb5_store_int32(sp, 0x01020304);
87ebfedea0SLionel Sambuc     nerr += compare("Integer (LE)", sp, "\x4\x3\x2\x1", 4);
88ebfedea0SLionel Sambuc 
89ebfedea0SLionel Sambuc     sp = krb5_storage_emem();
90ebfedea0SLionel Sambuc     krb5_storage_set_byteorder(sp, KRB5_STORAGE_BYTEORDER_BE);
91ebfedea0SLionel Sambuc     krb5_store_int32(sp, 0x01020304);
92ebfedea0SLionel Sambuc     nerr += compare("Integer (BE)", sp, "\x1\x2\x3\x4", 4);
93ebfedea0SLionel Sambuc 
94ebfedea0SLionel Sambuc     sp = krb5_storage_emem();
95ebfedea0SLionel Sambuc     krb5_storage_set_byteorder(sp, KRB5_STORAGE_BYTEORDER_HOST);
96ebfedea0SLionel Sambuc     krb5_store_int32(sp, 0x01020304);
97ebfedea0SLionel Sambuc     {
98ebfedea0SLionel Sambuc 	int test = 1;
99ebfedea0SLionel Sambuc 	void *data;
100ebfedea0SLionel Sambuc 	if(*(char*)&test)
101ebfedea0SLionel Sambuc 	    data = "\x4\x3\x2\x1";
102ebfedea0SLionel Sambuc 	else
103ebfedea0SLionel Sambuc 	    data = "\x1\x2\x3\x4";
104ebfedea0SLionel Sambuc 	nerr += compare("Integer (host)", sp, data, 4);
105ebfedea0SLionel Sambuc     }
106ebfedea0SLionel Sambuc 
107ebfedea0SLionel Sambuc     sp = krb5_storage_emem();
108ebfedea0SLionel Sambuc     krb5_make_principal(context, &principal, "TEST", "foobar", NULL);
109ebfedea0SLionel Sambuc     krb5_store_principal(sp, principal);
110ebfedea0SLionel Sambuc     krb5_free_principal(context, principal);
111ebfedea0SLionel Sambuc     nerr += compare("Principal", sp, "\x0\x0\x0\x1"
112ebfedea0SLionel Sambuc 		    "\x0\x0\x0\x1"
113ebfedea0SLionel Sambuc 		    "\x0\x0\x0\x4TEST"
114ebfedea0SLionel Sambuc 		    "\x0\x0\x0\x6""foobar", 26);
115ebfedea0SLionel Sambuc 
116ebfedea0SLionel Sambuc     krb5_free_context(context);
117ebfedea0SLionel Sambuc 
118ebfedea0SLionel Sambuc     return nerr ? 1 : 0;
119ebfedea0SLionel Sambuc }
120