From f58ce3d98a13e9c8d0930b54c5f593cbfa196d65 Mon Sep 17 00:00:00 2001 From: linus Date: Fri, 21 Jan 2011 16:32:28 +0100 Subject: Some more UDP tests. --- lib/tests/Makefile.am | 2 +- lib/tests/test-blocking.c | 64 -------------------------- lib/tests/test-udp.c | 115 ++++++++++++++++++++++++++++++++++++++-------- lib/tests/test.conf | 24 +++++++++- lib/tests/udp-server.c | 19 +------- lib/tests/udp.c | 29 ++++++++++++ lib/tests/udp.h | 4 ++ 7 files changed, 154 insertions(+), 103 deletions(-) delete mode 100644 lib/tests/test-blocking.c diff --git a/lib/tests/Makefile.am b/lib/tests/Makefile.am index 243719f..526dcdc 100644 --- a/lib/tests/Makefile.am +++ b/lib/tests/Makefile.am @@ -4,7 +4,7 @@ AM_CFLAGS = -Wall -g bin_PROGRAMS = test-udp udp-server -test_udp_SOURCES = test-udp.c +test_udp_SOURCES = test-udp.c udp.c test_udp_LDADD = ../libradsec.la -lcgreen -lm udp_server_SOURCES = udp-server.c udp.c diff --git a/lib/tests/test-blocking.c b/lib/tests/test-blocking.c deleted file mode 100644 index e1f657b..0000000 --- a/lib/tests/test-blocking.c +++ /dev/null @@ -1,64 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "blocking.h" - -int -f (const struct sockaddr *addr, - socklen_t addrlen, - int out_fd) -{ - int fd = -1; - //struct rs_alloc_scheme as = { calloc, malloc, free, realloc }; - struct rs_handle ctx = { RS_CONN_TYPE_TCP, - { RS_CRED_NONE, NULL, NULL }, - { NULL, NULL, NULL, NULL } }; - struct rs_packet *p = NULL; - - fd = rs_connect (&ctx, addr, addrlen); - if (fd < 0) - { - perror ("rs_connect"); - return -1; - } - - p = next_packet (&ctx, fd); - if (p == NULL) - { - perror ("next_packet"); - rs_disconnect (&ctx, fd); - return -1; - } - rs_disconnect (&ctx, fd); - - if (send_packet (&ctx, out_fd, p)) - { - rs_packet_free (&ctx, &p); - perror ("send_packet"); - return -1; - } - - return 0; -} - -int -main (int argc, char *argv[]) -{ - struct addrinfo *ai; - int rc; - - rc = getaddrinfo (argv[1], argv[2], NULL, &ai); - if (rc) - { - if (rc == EAI_SYSTEM) - perror ("getaddrinfo"); - else - fprintf (stderr, "getaddrinfo: %s\n", gai_strerror (rc)); - return -1; - } - - return f (ai->ai_addr, ai->ai_addrlen, 1); -} diff --git a/lib/tests/test-udp.c b/lib/tests/test-udp.c index 3ebbc96..7a6d996 100644 --- a/lib/tests/test-udp.c +++ b/lib/tests/test-udp.c @@ -3,13 +3,14 @@ #include #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; @@ -18,22 +19,45 @@ authenticate (struct rs_connection *conn, const char *user, const char *pw) 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); + //printf ("%s\n", rs_err_msg (rs_err_conn_pop (conn), 1)); assert_true (rs_packet_frpkt (resp)->code == PW_AUTHENTICATION_ACK); 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; + assert_true (rs_packet_create_auth_request (conn, &msg0, NULL, NULL) == 0); + assert_true (rs_packet_create_auth_request (conn, &msg1, NULL, NULL) == 0); + assert_true (rs_packet_send (msg0, NULL) == 0); + assert_true (rs_packet_send (msg1, NULL) == 0); +} +static void +send_large_packet (struct rs_connection *conn) +{ + struct rs_packet *msg0; + struct rs_attr *attr_x; + char *buf; + int f; + + buf = malloc (4096); + assert_true (buf != NULL); + memset (buf, 0, 4096); + + assert_true (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); + rs_attr_create (conn, &attr_x, "EAP-Message", buf); + rs_packet_add_attr (msg0, attr_x); + } + assert_true (rs_packet_send (msg0, NULL) == 0); } -#endif /* ************************************************************ */ static struct setup { @@ -43,12 +67,17 @@ static struct setup { char *pw; } setup; -void +static void test_auth () { struct rs_context *ctx; struct rs_connection *conn; + setup.config_file = "test.conf"; + setup.config_name = "test-udp-auth"; + setup.username = "molgan"; + setup.pw = "password"; + 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); @@ -59,15 +88,60 @@ test_auth () 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 1 + hd (buf, len); +#endif + assert_true (len >= 20); + assert_true (len <= 4096); + assert_true ((buf[2] << 8) + buf[3] == len); + return len; +} - if (argc > 1) - return run_single_test (ts, argv[1], create_text_reporter ()); +static void +test_buffering () +{ + struct rs_context *ctx; + struct rs_connection *conn; + struct timeval timeout; + struct polldata *polldata; - return run_test_suite (ts, create_text_reporter ()); + assert_true (rs_context_create (&ctx, FREERADIUS_DICT) == 0); + assert_true (rs_context_read_config (ctx, "test.conf") == 0); + assert_true (rs_conn_create (ctx, &conn, "test-udp-buffering") == 0); + + timeout.tv_sec = 0; + timeout.tv_usec = 150000; + polldata = udp_server ("11820", &timeout, test_buffering_cb); + assert_true (polldata != NULL); + + send_more_than_one_msg_in_one_packet (conn); + assert_true (udp_poll (polldata) > 0); + assert_true (udp_poll (polldata) > 0); + + send_large_packet (conn); + assert_true (udp_poll (polldata) > 0); + + udp_free_polldata (polldata); + rs_conn_destroy (conn); + rs_context_destroy (ctx); +} + +/* ************************************************************ */ +static void +setup_auth (TestSuite *ts) +{ + add_test (ts, test_auth); +} + +static void +setup_buffering (TestSuite *ts) +{ + add_test (ts, test_buffering); } int @@ -75,10 +149,11 @@ main (int argc, char *argv[]) { TestSuite *ts = create_test_suite (); - setup.config_file = "test.conf"; - setup.config_name = "test-udp"; - setup.username = "molgan"; - setup.pw = "password"; + setup_auth (ts); + setup_buffering (ts); - return test_udp (argc, argv, ts); + if (argc > 1) + return run_single_test (ts, argv[1], create_text_reporter ()); + else + return run_test_suite (ts, create_text_reporter ()); } diff --git a/lib/tests/test.conf b/lib/tests/test.conf index 2f65a80..9be968d 100644 --- a/lib/tests/test.conf +++ b/lib/tests/test.conf @@ -1,4 +1,4 @@ -config test-udp { +config test-udp-auth { type = "UDP" server { hostname = "localhost" @@ -6,3 +6,25 @@ config test-udp { secret = "sikrit" } } + +config test-udp-buffering { + type = "UDP" + server { + hostname = "localhost" + service = "11820" + secret = "sikrit" + } +} + +config test-tls-test { + type = "TLS" + cacertfile = "/home/linus/nordberg-ca.crt" + certfile = "/home/linus/p/radsecproxy/src/maatuska.nordberg.se.crt" + certkeyfile = "/home/linus/p/radsecproxy/src/maatuska.nordberg.se.key" + + server { + hostname = "localhost" + service = "1820" + secret = "sikrit" + } +} diff --git a/lib/tests/udp-server.c b/lib/tests/udp-server.c index 768fba0..ce0136a 100644 --- a/lib/tests/udp-server.c +++ b/lib/tests/udp-server.c @@ -5,17 +5,7 @@ ssize_t handle_data (const uint8_t *buf, ssize_t len) { - int i; - - printf ("# len: %ld\n", len); - for (i = 0; i < len; i++) - { - printf ("%02x%s", buf[i], (i+1) % 8 ? " " : " "); - if ((i + 1) % 16 == 0) - printf ("\n"); - } - printf ("\n"); - return len; + return hd (buf, len); } int @@ -37,11 +27,6 @@ main (int argc, char *argv[]) tv.tv_sec, tv.tv_sec > 1 ? "s" : ""); } - if (data) - { - if (data->timeout) - free (data->timeout); - free (data); - } + udp_free_polldata (data); return (n <= 0); } diff --git a/lib/tests/udp.c b/lib/tests/udp.c index 184325e..a29880a 100644 --- a/lib/tests/udp.c +++ b/lib/tests/udp.c @@ -40,6 +40,19 @@ _resolve (const char *str) return rp; } +void +udp_free_polldata (struct polldata *data) +{ + if (data) + { + if (data->timeout) + free (data->timeout); + free (data); + } +} + +/* @return if select() returns error or timeout, return select() + else return value from invoked callback function */ ssize_t udp_poll (struct polldata *data) { @@ -106,3 +119,19 @@ udp_server (const char *bindto, struct timeval *timeout, data_cb cb) return NULL; } + +ssize_t +hd (const uint8_t *buf, ssize_t len) +{ + int i; + + printf ("# len: %ld\n", len); + for (i = 0; i < len; i++) + { + printf ("%02x%s", buf[i], (i+1) % 8 ? " " : " "); + if ((i + 1) % 16 == 0) + printf ("\n"); + } + printf ("\n"); + return len; +} diff --git a/lib/tests/udp.h b/lib/tests/udp.h index 02e1b50..004b7ca 100644 --- a/lib/tests/udp.h +++ b/lib/tests/udp.h @@ -3,6 +3,7 @@ #include typedef ssize_t (*data_cb) (const uint8_t *buf, ssize_t len); + struct polldata { int s; data_cb cb; @@ -11,3 +12,6 @@ struct polldata { struct polldata *udp_server (const char *bindto, struct timeval *timeout, data_cb cb); ssize_t udp_poll (struct polldata *data); +void udp_free_polldata (struct polldata *data); + +ssize_t hd (const uint8_t *buf, ssize_t len); -- cgit v1.1