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