summaryrefslogtreecommitdiff
path: root/lib/tests/test-udp.c
blob: f66eebd2686faef88bbdfde967f73e54b1c2964f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <stdlib.h>
#include <cgreen/cgreen.h>
#include "radsec/radsec.h"
#include "radsec/request.h"
#include "udp.h"

#define true 1			/* FIXME: Bug report cgreen.  */
#define false 0

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_authn_request (conn, &msg, user, pw) == 0);
  rs_request_add_reqpkt (req, msg);
  assert_true (rs_request_send (req, &resp) == 0);
  //printf ("%s\n", rs_err_msg (rs_err_conn_pop (conn), 1));
  assert_true (rs_packet_code(resp) == PW_ACCESS_ACCEPT);

  rs_request_destroy (req);
}

static void
send_more_than_one_msg_in_one_packet (struct rs_connection *conn)
{
  struct rs_packet *msg0, *msg1;

  assert_true (rs_packet_create_authn_request (conn, &msg0, NULL, NULL) == 0);
  assert_true (rs_packet_create_authn_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 (RS_MAX_PACKET_LEN);
  assert_true (buf != NULL);
  memset (buf, 0, RS_MAX_PACKET_LEN);

  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);
}

/* ************************************************************ */
static struct setup {
  char *config_file;
  char *config_name;
  char *username;
  char *pw;
} setup;

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, NULL) == 0);
  assert_true (rs_context_read_config (ctx, setup.config_file) == 0);
  assert_true (rs_conn_create (ctx, &conn, setup.config_name) == 0);

  authenticate (conn, setup.username, setup.pw);

  rs_conn_destroy (conn);
  rs_context_destroy (ctx);
}

static ssize_t
test_buffering_cb (const uint8_t *buf, ssize_t len)
{
  /* "Exactly one RADIUS packet is encapsulated in the UDP Data field"
     [RFC 2865]*/
#if 0
  hd (buf, len);
#endif
  assert_true (len >= 20);
  assert_true (len <= RS_MAX_PACKET_LEN);
  assert_true ((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;

  assert_true (rs_context_create (&ctx, NULL) == 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
main (int argc, char *argv[])
{
  TestSuite *ts = create_test_suite ();

  setup_auth (ts);
  setup_buffering (ts);

  if (argc > 1)
    return run_single_test (ts, argv[1], create_text_reporter ());
  else
    return run_test_suite (ts, create_text_reporter ());
}