1 /* Copyright 2011,2013, NORDUnet A/S. All rights reserved. */
2 /* See LICENSE for licensing information. */
6 #include <CUnit/Basic.h>
7 #include "radius/client.h"
8 #include "radsec/radsec.h"
9 #include "radsec/request.h"
13 authenticate (struct rs_connection *conn, const char *user, const char *pw)
15 struct rs_request *req;
16 struct rs_packet *msg, *resp;
18 CU_ASSERT (rs_request_create (conn, &req) == 0);
19 CU_ASSERT (!rs_packet_create_authn_request (conn, &msg, user, pw, "sikrit"));
20 rs_request_add_reqpkt (req, msg);
21 CU_ASSERT (rs_request_send (req, &resp) == 0);
22 //printf ("%s\n", rs_err_msg (rs_err_conn_pop (conn), 1));
23 CU_ASSERT (rs_packet_code(resp) == PW_ACCESS_ACCEPT);
25 rs_request_destroy (req);
29 send_more_than_one_msg_in_one_packet (struct rs_connection *conn)
31 struct rs_packet *msg0, *msg1;
33 CU_ASSERT (rs_packet_create_authn_request (conn, &msg0, NULL, NULL, "sikrit")
35 CU_ASSERT (rs_packet_create_authn_request (conn, &msg1, NULL, NULL, "sikrit")
37 CU_ASSERT (rs_packet_send (msg0, NULL) == 0);
38 CU_ASSERT (rs_packet_send (msg1, NULL) == 0);
43 send_large_packet (struct rs_connection *conn)
45 struct rs_packet *msg0;
46 struct radius_packet *frpkt = NULL;
50 buf = malloc (RS_MAX_PACKET_LEN);
51 CU_ASSERT (buf != NULL);
52 memset (buf, 0, RS_MAX_PACKET_LEN);
54 CU_ASSERT (rs_packet_create (conn, &msg0) == 0);
55 /* 16 chunks --> heap corruption in evbuffer_drain detected by free() */
56 for (f = 0; f < 15; f++)
58 memset (buf, 'a' + f, 252);
59 //vp = pairmake ("EAP-Message", buf, T_OP_EQ);
60 CU_ASSERT (rs_packet_append_avp (msg0, fixme...) == RSE_OK);
62 CU_ASSERT (rs_packet_send (msg0, NULL) == 0);
66 /* ************************************************************ */
77 struct rs_context *ctx;
78 struct rs_connection *conn;
80 setup.config_file = "test.conf";
81 setup.config_name = "test-udp-auth";
82 setup.username = "molgan@PROJECT-MOONSHOT.ORG";
83 setup.pw = "password";
85 CU_ASSERT (rs_context_create (&ctx) == 0);
86 CU_ASSERT (rs_context_read_config (ctx, setup.config_file) == 0);
87 CU_ASSERT (rs_conn_create (ctx, &conn, setup.config_name) == 0);
89 authenticate (conn, setup.username, setup.pw);
91 rs_conn_destroy (conn);
92 rs_context_destroy (ctx);
96 test_buffering_cb (const uint8_t *buf, ssize_t len)
98 /* "Exactly one RADIUS packet is encapsulated in the UDP Data field"
103 CU_ASSERT (len >= 20);
104 CU_ASSERT (len <= RS_MAX_PACKET_LEN);
105 CU_ASSERT ((buf[2] << 8) + buf[3] == len);
112 struct rs_context *ctx;
113 struct rs_connection *conn;
114 struct timeval timeout;
115 struct polldata *polldata;
117 CU_ASSERT (rs_context_create (&ctx) == 0);
118 CU_ASSERT (rs_context_read_config (ctx, "test.conf") == 0);
119 CU_ASSERT (rs_conn_create (ctx, &conn, "test-udp-buffering") == 0);
122 timeout.tv_usec = 150000;
123 polldata = udp_server ("11820", &timeout, test_buffering_cb);
124 CU_ASSERT (polldata != NULL);
126 send_more_than_one_msg_in_one_packet (conn);
127 CU_ASSERT (udp_poll (polldata) > 0);
128 CU_ASSERT (udp_poll (polldata) > 0);
131 udp_free_polldata (polldata);
132 rs_conn_destroy (conn);
133 rs_context_destroy (ctx);
136 /* ************************************************************ */
138 main (int argc, char *argv[])
143 assert (CU_initialize_registry () == CUE_SUCCESS);
144 s = CU_add_suite ("auth", NULL, NULL); assert (s);
145 t = CU_ADD_TEST (s, test_auth); assert (t);
146 s = CU_add_suite ("buffering", NULL, NULL); assert (s);
147 t = CU_ADD_TEST (s, test_buffering); assert (t);
149 return !(CU_basic_run_tests () == CUE_SUCCESS);
151 CU_cleanup_registry ();