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));
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) == 0);
34 CU_ASSERT (rs_packet_create_authn_request (conn, &msg1, NULL, NULL) == 0);
35 CU_ASSERT (rs_packet_send (msg0, NULL) == 0);
36 CU_ASSERT (rs_packet_send (msg1, NULL) == 0);
41 send_large_packet (struct rs_connection *conn)
43 struct rs_packet *msg0;
44 struct radius_packet *frpkt = NULL;
48 buf = malloc (RS_MAX_PACKET_LEN);
49 CU_ASSERT (buf != NULL);
50 memset (buf, 0, RS_MAX_PACKET_LEN);
52 CU_ASSERT (rs_packet_create (conn, &msg0) == 0);
53 /* 16 chunks --> heap corruption in evbuffer_drain detected by free() */
54 for (f = 0; f < 15; f++)
56 memset (buf, 'a' + f, 252);
57 //vp = pairmake ("EAP-Message", buf, T_OP_EQ);
58 CU_ASSERT (rs_packet_append_avp (msg0, fixme...) == RSE_OK);
60 CU_ASSERT (rs_packet_send (msg0, NULL) == 0);
64 /* ************************************************************ */
75 struct rs_context *ctx;
76 struct rs_connection *conn;
78 setup.config_file = "test.conf";
79 setup.config_name = "test-udp-auth";
80 setup.username = "molgan@PROJECT-MOONSHOT.ORG";
81 setup.pw = "password";
83 CU_ASSERT (rs_context_create (&ctx) == 0);
84 CU_ASSERT (rs_context_read_config (ctx, setup.config_file) == 0);
85 CU_ASSERT (rs_conn_create (ctx, &conn, setup.config_name) == 0);
87 authenticate (conn, setup.username, setup.pw);
89 rs_conn_destroy (conn);
90 rs_context_destroy (ctx);
94 test_buffering_cb (const uint8_t *buf, ssize_t len)
96 /* "Exactly one RADIUS packet is encapsulated in the UDP Data field"
101 CU_ASSERT (len >= 20);
102 CU_ASSERT (len <= RS_MAX_PACKET_LEN);
103 CU_ASSERT ((buf[2] << 8) + buf[3] == len);
110 struct rs_context *ctx;
111 struct rs_connection *conn;
112 struct timeval timeout;
113 struct polldata *polldata;
115 CU_ASSERT (rs_context_create (&ctx) == 0);
116 CU_ASSERT (rs_context_read_config (ctx, "test.conf") == 0);
117 CU_ASSERT (rs_conn_create (ctx, &conn, "test-udp-buffering") == 0);
120 timeout.tv_usec = 150000;
121 polldata = udp_server ("11820", &timeout, test_buffering_cb);
122 CU_ASSERT (polldata != NULL);
124 send_more_than_one_msg_in_one_packet (conn);
125 CU_ASSERT (udp_poll (polldata) > 0);
126 CU_ASSERT (udp_poll (polldata) > 0);
129 udp_free_polldata (polldata);
130 rs_conn_destroy (conn);
131 rs_context_destroy (ctx);
134 /* ************************************************************ */
136 main (int argc, char *argv[])
141 assert (CU_initialize_registry () == CUE_SUCCESS);
142 s = CU_add_suite ("auth", NULL, NULL); assert (s);
143 t = CU_ADD_TEST (s, test_auth); assert (t);
144 s = CU_add_suite ("buffering", NULL, NULL); assert (s);
145 t = CU_ADD_TEST (s, test_buffering); assert (t);
147 return !(CU_basic_run_tests () == CUE_SUCCESS);
149 CU_cleanup_registry ();