1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* lib/krad/t_attr.c - Attribute 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 encoded[] = {
33*7f2fe78bSCy Schubert 0xba, 0xfc, 0xed, 0x50, 0xe1, 0xeb, 0xa6, 0xc3,
34*7f2fe78bSCy Schubert 0xc1, 0x75, 0x20, 0xe9, 0x10, 0xce, 0xc2, 0xcb
35*7f2fe78bSCy Schubert };
36*7f2fe78bSCy Schubert
37*7f2fe78bSCy Schubert const static unsigned char auth[] = {
38*7f2fe78bSCy Schubert 0xac, 0x9d, 0xc1, 0x62, 0x08, 0xc4, 0xc7, 0x8b,
39*7f2fe78bSCy Schubert 0xa1, 0x2f, 0x25, 0x0a, 0xc4, 0x1d, 0x36, 0x41
40*7f2fe78bSCy Schubert };
41*7f2fe78bSCy Schubert
42*7f2fe78bSCy Schubert int
main()43*7f2fe78bSCy Schubert main()
44*7f2fe78bSCy Schubert {
45*7f2fe78bSCy Schubert unsigned char outbuf[MAX_ATTRSETSIZE];
46*7f2fe78bSCy Schubert const char *decoded = "accept";
47*7f2fe78bSCy Schubert const char *secret = "foo";
48*7f2fe78bSCy Schubert krb5_error_code retval;
49*7f2fe78bSCy Schubert krb5_context ctx;
50*7f2fe78bSCy Schubert const char *tmp;
51*7f2fe78bSCy Schubert krb5_data in;
52*7f2fe78bSCy Schubert size_t len;
53*7f2fe78bSCy Schubert
54*7f2fe78bSCy Schubert noerror(krb5_init_context(&ctx));
55*7f2fe78bSCy Schubert
56*7f2fe78bSCy Schubert /* Make sure User-Name is 1. */
57*7f2fe78bSCy Schubert insist(krad_attr_name2num("User-Name") == 1);
58*7f2fe78bSCy Schubert
59*7f2fe78bSCy Schubert /* Make sure 2 is User-Password. */
60*7f2fe78bSCy Schubert tmp = krad_attr_num2name(2);
61*7f2fe78bSCy Schubert insist(tmp != NULL);
62*7f2fe78bSCy Schubert insist(strcmp(tmp, "User-Password") == 0);
63*7f2fe78bSCy Schubert
64*7f2fe78bSCy Schubert /* Test decoding. */
65*7f2fe78bSCy Schubert in = make_data((void *)encoded, sizeof(encoded));
66*7f2fe78bSCy Schubert noerror(kr_attr_decode(ctx, secret, auth,
67*7f2fe78bSCy Schubert krad_attr_name2num("User-Password"),
68*7f2fe78bSCy Schubert &in, outbuf, &len));
69*7f2fe78bSCy Schubert insist(len == strlen(decoded));
70*7f2fe78bSCy Schubert insist(memcmp(outbuf, decoded, len) == 0);
71*7f2fe78bSCy Schubert
72*7f2fe78bSCy Schubert /* Test encoding. */
73*7f2fe78bSCy Schubert in = string2data((char *)decoded);
74*7f2fe78bSCy Schubert retval = kr_attr_encode(ctx, secret, auth,
75*7f2fe78bSCy Schubert krad_attr_name2num("User-Password"),
76*7f2fe78bSCy Schubert &in, outbuf, &len);
77*7f2fe78bSCy Schubert insist(retval == 0);
78*7f2fe78bSCy Schubert insist(len == sizeof(encoded));
79*7f2fe78bSCy Schubert insist(memcmp(outbuf, encoded, len) == 0);
80*7f2fe78bSCy Schubert
81*7f2fe78bSCy Schubert /* Test constraint. */
82*7f2fe78bSCy Schubert in.length = 100;
83*7f2fe78bSCy Schubert insist(kr_attr_valid(krad_attr_name2num("User-Password"), &in) == 0);
84*7f2fe78bSCy Schubert in.length = 200;
85*7f2fe78bSCy Schubert insist(kr_attr_valid(krad_attr_name2num("User-Password"), &in) != 0);
86*7f2fe78bSCy Schubert
87*7f2fe78bSCy Schubert krb5_free_context(ctx);
88*7f2fe78bSCy Schubert return 0;
89*7f2fe78bSCy Schubert }
90