Follow API change in tests.
[libradsec.git] / lib / tests / test-udp.c
1 /* Copyright 2011,2013, NORDUnet A/S. All rights reserved. */
2 /* See LICENSE for licensing information. */
3
4 #include <stdlib.h>
5 #include <assert.h>
6 #include <CUnit/Basic.h>
7 #include "radius/client.h"
8 #include "radsec/radsec.h"
9 #include "radsec/request.h"
10 #include "udp.h"
11
12 static void
13 authenticate (struct rs_connection *conn, const char *user, const char *pw)
14 {
15   struct rs_request *req;
16   struct rs_packet *msg, *resp;
17
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);
24
25   rs_request_destroy (req);
26 }
27
28 static void
29 send_more_than_one_msg_in_one_packet (struct rs_connection *conn)
30 {
31   struct rs_packet *msg0, *msg1;
32
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);
37 }
38
39 #if 0
40 static void
41 send_large_packet (struct rs_connection *conn)
42 {
43   struct rs_packet *msg0;
44   struct radius_packet *frpkt = NULL;
45   char *buf;
46   int f;
47
48   buf = malloc (RS_MAX_PACKET_LEN);
49   CU_ASSERT (buf != NULL);
50   memset (buf, 0, RS_MAX_PACKET_LEN);
51
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++)
55     {
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);
59     }
60   CU_ASSERT (rs_packet_send (msg0, NULL) == 0);
61 }
62 #endif  /* 0 */
63
64 /* ************************************************************ */
65 static struct setup {
66   char *config_file;
67   char *config_name;
68   char *username;
69   char *pw;
70 } setup;
71
72 static void
73 test_auth ()
74 {
75   struct rs_context *ctx;
76   struct rs_connection *conn;
77
78   setup.config_file = "test.conf";
79   setup.config_name = "test-udp-auth";
80   setup.username = "molgan@PROJECT-MOONSHOT.ORG";
81   setup.pw = "password";
82
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);
86
87   authenticate (conn, setup.username, setup.pw);
88
89   rs_conn_destroy (conn);
90   rs_context_destroy (ctx);
91 }
92
93 static ssize_t
94 test_buffering_cb (const uint8_t *buf, ssize_t len)
95 {
96   /* "Exactly one RADIUS packet is encapsulated in the UDP Data field"
97      [RFC 2865]*/
98 #if 0
99   hd (buf, len);
100 #endif
101   CU_ASSERT (len >= 20);
102   CU_ASSERT (len <= RS_MAX_PACKET_LEN);
103   CU_ASSERT ((buf[2] << 8) +  buf[3] == len);
104   return len;
105 }
106
107 static void
108 test_buffering ()
109 {
110   struct rs_context *ctx;
111   struct rs_connection *conn;
112   struct timeval timeout;
113   struct polldata *polldata;
114
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);
118
119   timeout.tv_sec = 0;
120   timeout.tv_usec = 150000;
121   polldata = udp_server ("11820", &timeout, test_buffering_cb);
122   CU_ASSERT (polldata != NULL);
123
124   send_more_than_one_msg_in_one_packet (conn);
125   CU_ASSERT (udp_poll (polldata) > 0);
126   CU_ASSERT (udp_poll (polldata) > 0);
127
128
129   udp_free_polldata (polldata);
130   rs_conn_destroy (conn);
131   rs_context_destroy (ctx);
132 }
133
134 /* ************************************************************ */
135 int
136 main (int argc, char *argv[])
137 {
138   CU_pSuite s = NULL;
139   CU_pTest t = NULL;
140
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);
146
147   return !(CU_basic_run_tests () == CUE_SUCCESS);
148
149   CU_cleanup_registry ();
150   return 0;
151 }