Skip to content

Commit a4fadaf

Browse files
committed
Added small test for avpair reading
1 parent ffbdacd commit a4fadaf

2 files changed

Lines changed: 200 additions & 3 deletions

File tree

tests/Makefile.am

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ EXTRA_DIST = radiusclient-ipv6.conf servers-ipv6 \
1010
dtls/radsecproxy.conf
1111

1212
AM_CPPFLAGS = -I$(srcdir) -I$(top_srcdir)/include -I$(top_srcdir)/src -I$(top_builddir)
13-
LDADD = ../lib/libfreeradius-client.la
13+
LDADD = ../lib/libradcli.la
1414

1515
nodist_check_SCRIPTS = basic-tests.sh ipv6-tests.sh tls-tests.sh
1616
TESTS = basic-tests.sh ipv6-tests.sh
1717
check_PROGRAMS =
1818

1919
if ENABLE_GNUTLS
20-
TESTS += tls-tests.sh
20+
TESTS += tls-tests.sh avpair
2121

22-
check_PROGRAMS += tls-restart
22+
check_PROGRAMS += tls-restart avpair
2323

2424
tls_restart_SOURCES = tls-restart.c
2525
tls_restart_LDADD = ../src/libtools.a ../lib/libradcli.la

tests/avpair.c

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
/*
2+
* Copyright (c) 2015, Nikos Mavrogiannopoulos. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
* 1. Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* 2. Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
*
13+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16+
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18+
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22+
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23+
*/
24+
25+
#include <ctype.h>
26+
#include <stdio.h>
27+
#include <stdlib.h>
28+
#include <string.h>
29+
#include <unistd.h>
30+
#include <netinet/in.h>
31+
32+
#include <radcli/radcli.h>
33+
34+
int main(int argc, char **argv)
35+
{
36+
VALUE_PAIR **vp = NULL;
37+
VALUE_PAIR *vp2, *send = NULL;
38+
rc_handle *rh;
39+
int checks;
40+
int ret, prev;
41+
42+
if ((rh = rc_read_config("radiusclient.conf")) == NULL) {
43+
fprintf(stderr, "%s: error opening radius configuration file\n", argv[0]);
44+
exit(1);
45+
}
46+
47+
vp = &send;
48+
/* insert values */
49+
ret = rc_avpair_parse(rh, "User-Name=test", vp);
50+
if (ret < 0) {
51+
fprintf(stderr, "error in %d\n", __LINE__);
52+
exit(1);
53+
}
54+
55+
ret = rc_avpair_parse(rh, "Idle-Timeout=1821", vp);
56+
if (ret < 0) {
57+
fprintf(stderr, "error in %d\n", __LINE__);
58+
exit(1);
59+
}
60+
61+
ret = rc_avpair_parse(rh, "Framed-IP-Address=192.168.1.1", vp);
62+
if (ret < 0) {
63+
fprintf(stderr, "error in %d\n", __LINE__);
64+
exit(1);
65+
}
66+
67+
ret = rc_avpair_parse(rh, "Framed-IPv6-Address=::1", vp);
68+
if (ret < 0) {
69+
fprintf(stderr, "error in %d\n", __LINE__);
70+
exit(1);
71+
}
72+
73+
ret = rc_avpair_parse(rh, "Route-IPv6-Information=fc64:5f83:803:e88a:4d74:5f71:16fd:0/112", vp);
74+
if (ret < 0) {
75+
fprintf(stderr, "error in %d\n", __LINE__);
76+
exit(1);
77+
}
78+
79+
/* check if values match using the structs directly */
80+
vp2 = send;
81+
checks = 0;
82+
prev = -1;
83+
while(vp2 != NULL) {
84+
if (vp2->attribute == PW_USER_NAME && (memcmp(vp2->strvalue, "test", 4) != 0 || vp2->type != PW_TYPE_STRING)) {
85+
fprintf(stderr, "%d: error checking username: %s/%d\n", __LINE__, vp2->strvalue, vp2->lvalue);
86+
exit(1);
87+
} else if (vp2->attribute == PW_IDLE_TIMEOUT && (vp2->lvalue != 1821 || vp2->type != PW_TYPE_INTEGER)) {
88+
fprintf(stderr, "%d: error checking Idle-Timeout: %d\n", __LINE__, vp2->lvalue);
89+
exit(1);
90+
} else if (vp2->attribute == PW_FRAMED_IP_ADDRESS && (vp2->lvalue != 3232235777 || vp2->type != PW_TYPE_IPADDR)) {
91+
fprintf(stderr, "%d: error checking Framed-IP-Address: %u\n", __LINE__, vp2->lvalue);
92+
exit(1);
93+
} else if (vp2->attribute == PW_FRAMED_IPV6_ADDRESS &&
94+
(memcmp(vp2->strvalue, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", 16) != 0 ||
95+
vp2->type != PW_TYPE_IPV6ADDR)) {
96+
fprintf(stderr, "%d: error checking Framed-IPv6-Address\n", __LINE__);
97+
exit(1);
98+
} else if (vp2->attribute == PW_ROUTE_IPV6_INFORMATION &&
99+
(memcmp(vp2->strvalue+2, "\xfc\x64\x5f\x83\x08\x03\xe8\x8a\x4d\x74\x5f\x71\x16\xfd\x00\x00", 16) != 0 ||
100+
vp2->strvalue[1] != 112 || vp2->type != PW_TYPE_IPV6PREFIX)) {
101+
fprintf(stderr, "%d: error checking Route-IPv6-Information: %d\n", __LINE__, vp2->strvalue[1]);
102+
exit(1);
103+
}
104+
105+
if (prev != vp2->attribute) {
106+
checks++;
107+
prev = vp2->attribute;
108+
}
109+
vp2 = vp2->next;
110+
};
111+
112+
if (checks != 5) {
113+
fprintf(stderr, "%s: error: not all attributes were found\n", __LINE__);
114+
exit(1);
115+
}
116+
117+
/* check if values match using the new API */
118+
vp2 = send;
119+
checks = 0;
120+
prev = -1;
121+
while(vp2 != NULL) {
122+
unsigned type, id, len;
123+
uint32_t uint;
124+
char *p;
125+
struct in6_addr ip6;
126+
127+
rc_avpair_get_attr(vp2, &type, &id);
128+
129+
if (id == PW_USER_NAME) {
130+
if (rc_avpair_get_raw(vp2, &p, &len) != 0) {
131+
fprintf(stderr, "error in %d\n", __LINE__);
132+
exit(1);
133+
}
134+
135+
if (len != 4 || strcmp(p, "test") != 0 || type != PW_TYPE_STRING) {
136+
fprintf(stderr, "%d: error checking username: %s/%d\n", __LINE__, vp2->strvalue, vp2->lvalue);
137+
exit(1);
138+
}
139+
} else if (id == PW_IDLE_TIMEOUT) {
140+
if (rc_avpair_get_uint32(vp2, &uint) != 0) {
141+
fprintf(stderr, "error in %d\n", __LINE__);
142+
exit(1);
143+
}
144+
if (uint != 1821 || type != PW_TYPE_INTEGER) {
145+
fprintf(stderr, "%d: error checking Idle-Timeout: %d\n", __LINE__, vp2->lvalue);
146+
exit(1);
147+
}
148+
} else if (id == PW_FRAMED_IP_ADDRESS) {
149+
if (rc_avpair_get_uint32(vp2, &uint) != 0) {
150+
fprintf(stderr, "error in %d\n", __LINE__);
151+
exit(1);
152+
}
153+
154+
if (uint != 3232235777 || type != PW_TYPE_IPADDR) {
155+
fprintf(stderr, "%d: error checking Framed-IP-Address: %u\n", __LINE__, vp2->lvalue);
156+
exit(1);
157+
}
158+
} else if (id == PW_FRAMED_IPV6_ADDRESS) {
159+
if (rc_avpair_get_in6(vp2, &ip6, NULL) != 0) {
160+
fprintf(stderr, "error in %d\n", __LINE__);
161+
exit(1);
162+
}
163+
164+
if (memcmp(&ip6, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", 16) != 0 ||
165+
type != PW_TYPE_IPV6ADDR) {
166+
fprintf(stderr, "%d: error checking Framed-IPv6-Address\n", __LINE__);
167+
exit(1);
168+
}
169+
} else if (id == PW_ROUTE_IPV6_INFORMATION) {
170+
if (rc_avpair_get_in6(vp2, &ip6, &len) != 0) {
171+
fprintf(stderr, "error in %d\n", __LINE__);
172+
exit(1);
173+
}
174+
175+
if (memcmp(&ip6, "\xfc\x64\x5f\x83\x08\x03\xe8\x8a\x4d\x74\x5f\x71\x16\xfd\x00\x00", 16) != 0 ||
176+
len != 112 || type != PW_TYPE_IPV6PREFIX) {
177+
fprintf(stderr, "%d: error checking Route-IPv6-Information: %d\n", __LINE__, vp2->strvalue[1]);
178+
exit(1);
179+
}
180+
}
181+
182+
if (prev != id) {
183+
checks++;
184+
prev = id;
185+
}
186+
vp2 = rc_avpair_next(vp2);
187+
};
188+
189+
if (checks != 5) {
190+
fprintf(stderr, "%s: error: not all attributes were found\n", __LINE__);
191+
exit(1);
192+
}
193+
rc_avpair_free(send);
194+
195+
return 0;
196+
197+
}

0 commit comments

Comments
 (0)