1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krad/t_attrset.c - Attribute set test program */
3 /*
4  * Copyright 2013 Red Hat, Inc.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *    1. Redistributions of source code must retain the above copyright
10  *       notice, this list of conditions and the following disclaimer.
11  *
12  *    2. Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "t_test.h"
31 
32 const static unsigned char auth[] = {
33     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
34     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
35 };
36 
37 const static unsigned char encpass[] = {
38     0x58, 0x8d, 0xff, 0xda, 0x37, 0xf9, 0xe4, 0xca,
39     0x19, 0xae, 0x49, 0xb7, 0x16, 0x6d, 0x58, 0x27
40 };
41 
42 int
main()43 main()
44 {
45     unsigned char buffer[KRAD_PACKET_SIZE_MAX], encoded[MAX_ATTRSETSIZE];
46     const char *username = "testUser", *password = "accept";
47     const krb5_data *tmpp;
48     krad_attrset *set;
49     krb5_context ctx;
50     size_t len = 0, encode_len;
51     krb5_data tmp;
52 
53     noerror(krb5_init_context(&ctx));
54     noerror(krad_attrset_new(ctx, &set));
55 
56     /* Add username. */
57     tmp = string2data((char *)username);
58     noerror(krad_attrset_add(set, krad_attr_name2num("User-Name"), &tmp));
59 
60     /* Add password. */
61     tmp = string2data((char *)password);
62     noerror(krad_attrset_add(set, krad_attr_name2num("User-Password"), &tmp));
63 
64     /* Encode attrset. */
65     noerror(kr_attrset_encode(set, "foo", auth, buffer, &encode_len));
66     krad_attrset_free(set);
67 
68     /* Manually encode User-Name. */
69     encoded[len + 0] = krad_attr_name2num("User-Name");
70     encoded[len + 1] = strlen(username) + 2;
71     memcpy(encoded + len + 2, username, strlen(username));
72     len += encoded[len + 1];
73 
74     /* Manually encode User-Password. */
75     encoded[len + 0] = krad_attr_name2num("User-Password");
76     encoded[len + 1] = sizeof(encpass) + 2;
77     memcpy(encoded + len + 2, encpass, sizeof(encpass));
78     len += encoded[len + 1];
79 
80     /* Compare output. */
81     insist(len == encode_len);
82     insist(memcmp(encoded, buffer, len) == 0);
83 
84     /* Decode output. */
85     tmp = make_data(buffer, len);
86     noerror(kr_attrset_decode(ctx, &tmp, "foo", auth, &set));
87 
88     /* Test getting an attribute. */
89     tmp = string2data((char *)username);
90     tmpp = krad_attrset_get(set, krad_attr_name2num("User-Name"), 0);
91     insist(tmpp != NULL);
92     insist(tmpp->length == tmp.length);
93     insist(strncmp(tmpp->data, tmp.data, tmp.length) == 0);
94 
95     krad_attrset_free(set);
96     krb5_free_context(ctx);
97     return 0;
98 }
99