+/* Copyright 2011,2013, NORDUnet A/S. All rights reserved. */
+/* See LICENSE for licensing information. */
+
#include <stdlib.h>
-#include <cgreen/cgreen.h>
-#include <freeradius/libradius.h>
+#include <assert.h>
+#include <CUnit/Basic.h>
+#include "radius/client.h"
#include "radsec/radsec.h"
#include "radsec/request.h"
+#include "udp.h"
-#define true 1 /* FIXME: Bug report cgreen. */
-#define false 0
-
-#define FREERADIUS_DICT "/usr/share/freeradius/dictionary"
-
-void
+static void
authenticate (struct rs_connection *conn, const char *user, const char *pw)
{
struct rs_request *req;
struct rs_packet *msg, *resp;
- assert_true (rs_request_create (conn, &req) == 0);
- assert_true (rs_packet_create_auth_request (conn, &msg, user, pw) == 0);
- assert_true (rs_request_send (req, msg, &resp) == 0);
- assert_true (rs_packet_frpkt (resp)->code == PW_AUTHENTICATION_ACK);
+ CU_ASSERT (rs_request_create (conn, &req) == 0);
+ CU_ASSERT (!rs_packet_create_authn_request (conn, &msg, user, pw));
+ rs_request_add_reqpkt (req, msg);
+ CU_ASSERT (rs_request_send (req, &resp) == 0);
+ //printf ("%s\n", rs_err_msg (rs_err_conn_pop (conn), 1));
+ CU_ASSERT (rs_packet_code(resp) == PW_ACCESS_ACCEPT);
- rs_request_destroy(req);
+ rs_request_destroy (req);
}
-#if 0
-int
-send_more_than_one_msg_in_one_packet (const char *server)
+static void
+send_more_than_one_msg_in_one_packet (struct rs_connection *conn)
{
- struct rs_request *req;
- struct rs_packet *msg, *resp;
-
+ struct rs_packet *msg0, *msg1;
+ CU_ASSERT (rs_packet_create_authn_request (conn, &msg0, NULL, NULL) == 0);
+ CU_ASSERT (rs_packet_create_authn_request (conn, &msg1, NULL, NULL) == 0);
+ CU_ASSERT (rs_packet_send (msg0, NULL) == 0);
+ CU_ASSERT (rs_packet_send (msg1, NULL) == 0);
+}
+#if 0
+static void
+send_large_packet (struct rs_connection *conn)
+{
+ struct rs_packet *msg0;
+ struct radius_packet *frpkt = NULL;
+ char *buf;
+ int f;
+
+ buf = malloc (RS_MAX_PACKET_LEN);
+ CU_ASSERT (buf != NULL);
+ memset (buf, 0, RS_MAX_PACKET_LEN);
+
+ CU_ASSERT (rs_packet_create (conn, &msg0) == 0);
+ /* 16 chunks --> heap corruption in evbuffer_drain detected by free() */
+ for (f = 0; f < 15; f++)
+ {
+ memset (buf, 'a' + f, 252);
+ //vp = pairmake ("EAP-Message", buf, T_OP_EQ);
+ CU_ASSERT (rs_packet_append_avp (msg0, fixme...) == RSE_OK);
+ }
+ CU_ASSERT (rs_packet_send (msg0, NULL) == 0);
}
-#endif
+#endif /* 0 */
/* ************************************************************ */
static struct setup {
char *pw;
} setup;
-void
+static void
test_auth ()
{
struct rs_context *ctx;
struct rs_connection *conn;
- assert_true (rs_context_create (&ctx, FREERADIUS_DICT) == 0);
- assert_true (rs_context_read_config (ctx, setup.config_file) == 0);
- assert_true (rs_conn_create (ctx, &conn, setup.config_name) == 0);
+ setup.config_file = "test.conf";
+ setup.config_name = "test-udp-auth";
+ setup.username = "molgan@PROJECT-MOONSHOT.ORG";
+ setup.pw = "password";
+
+ CU_ASSERT (rs_context_create (&ctx) == 0);
+ CU_ASSERT (rs_context_read_config (ctx, setup.config_file) == 0);
+ CU_ASSERT (rs_conn_create (ctx, &conn, setup.config_name) == 0);
authenticate (conn, setup.username, setup.pw);
rs_context_destroy (ctx);
}
-int
-test_udp (int argc, char *argv[], TestSuite *ts)
+static ssize_t
+test_buffering_cb (const uint8_t *buf, ssize_t len)
{
- add_test (ts, test_auth);
+ /* "Exactly one RADIUS packet is encapsulated in the UDP Data field"
+ [RFC 2865]*/
+#if 0
+ hd (buf, len);
+#endif
+ CU_ASSERT (len >= 20);
+ CU_ASSERT (len <= RS_MAX_PACKET_LEN);
+ CU_ASSERT ((buf[2] << 8) + buf[3] == len);
+ return len;
+}
+
+static void
+test_buffering ()
+{
+ struct rs_context *ctx;
+ struct rs_connection *conn;
+ struct timeval timeout;
+ struct polldata *polldata;
+
+ CU_ASSERT (rs_context_create (&ctx) == 0);
+ CU_ASSERT (rs_context_read_config (ctx, "test.conf") == 0);
+ CU_ASSERT (rs_conn_create (ctx, &conn, "test-udp-buffering") == 0);
- if (argc > 1)
- return run_single_test (ts, argv[1], create_text_reporter ());
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 150000;
+ polldata = udp_server ("11820", &timeout, test_buffering_cb);
+ CU_ASSERT (polldata != NULL);
- return run_test_suite (ts, create_text_reporter ());
+ send_more_than_one_msg_in_one_packet (conn);
+ CU_ASSERT (udp_poll (polldata) > 0);
+ CU_ASSERT (udp_poll (polldata) > 0);
+
+
+ udp_free_polldata (polldata);
+ rs_conn_destroy (conn);
+ rs_context_destroy (ctx);
}
+/* ************************************************************ */
int
main (int argc, char *argv[])
{
- TestSuite *ts = create_test_suite ();
+ CU_pSuite s = NULL;
+ CU_pTest t = NULL;
- setup.config_file = "test.conf";
- setup.config_name = "test-udp";
- setup.username = "molgan";
- setup.pw = "password";
+ assert (CU_initialize_registry () == CUE_SUCCESS);
+ s = CU_add_suite ("auth", NULL, NULL); assert (s);
+ t = CU_ADD_TEST (s, test_auth); assert (t);
+ s = CU_add_suite ("buffering", NULL, NULL); assert (s);
+ t = CU_ADD_TEST (s, test_buffering); assert (t);
+
+ return !(CU_basic_run_tests () == CUE_SUCCESS);
- return test_udp (argc, argv, ts);
+ CU_cleanup_registry ();
+ return 0;
}