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