test-stun.c 5.61 KB
Newer Older
1
2
3

#include <string.h>

Dafydd Harries's avatar
Dafydd Harries committed
4
5
6
#include "stun.h"
#include "udp-fake.h"
#include "agent.h"
7

8
9
10
static void
test_stun_no_password (
  NiceAgent *agent,
11
  NiceAddress from)
12
{
13
  NiceCandidate *candidate;
Dafydd Harries's avatar
Dafydd Harries committed
14
  NiceUDPSocket *sock;
15
  NiceAddress to = {0,};
16
17
  guint len;
  gchar buf[1024];
18
19
  guint packed_len;
  gchar *packed;
20
21
22

  memset (buf, '\0', 1024);

23
  candidate = agent->local_candidates->data;
24
25
26
27
28
29
  sock = &candidate->sock;

    {
      StunMessage *breq;

      /* send binding request without username */
30
      breq = stun_message_new (STUN_MESSAGE_BINDING_REQUEST,
31
          "0123456789abcdef", 0);
32
      packed_len = stun_message_pack (breq, &packed);
Dafydd Harries's avatar
Dafydd Harries committed
33
      nice_udp_fake_socket_push_recv (sock, &from, packed_len, packed);
34
35
36
      g_free (packed);
      stun_message_free (breq);
    }
37
38

  /* tell the agent there's a packet waiting */
39
  nice_agent_poll_read (agent, NULL, NULL, NULL);
40

41
  /* error response should have been sent */
Dafydd Harries's avatar
Dafydd Harries committed
42
43
  len = nice_udp_fake_socket_pop_send (sock, &to,
      sizeof (buf) / sizeof (gchar), buf);
44
45
46
47
48
49
50
51
52
  g_assert (len != 0);

    {
      StunMessage *bres;

      /* construct expected response */
      bres = stun_message_new (STUN_MESSAGE_BINDING_ERROR_RESPONSE,
          "0123456789abcdef", 0);
      packed_len = stun_message_pack (bres, &packed);
53
54

      stun_message_free (bres);
55
56
57
58
    }

  g_assert (len == packed_len);
  g_assert (0 == memcmp (buf, packed, len));
59
  g_free (packed);
60
}
61

62
63
64
static void
test_stun_invalid_password (
  NiceAgent *agent,
65
  NiceAddress from)
66
67
{
  NiceCandidate *candidate;
Dafydd Harries's avatar
Dafydd Harries committed
68
  NiceUDPSocket *sock;
69
  NiceAddress to = {0,};
70
71
  guint len;
  gchar buf[1024];
72
73
  guint packed_len;
  gchar *packed;
74
75
76
77
78
79
80
81
82
83

  memset (buf, '\0', 1024);

  candidate = agent->local_candidates->data;
  sock = &candidate->sock;

    {
      StunMessage *breq;

      /* send binding request with incorrect username */
84
      breq = stun_message_new (STUN_MESSAGE_BINDING_REQUEST,
85
          "0123456789abcdef", 1);
86
87
88
      breq->attributes[0] = stun_attribute_username_new ("lala");
      packed_len = stun_message_pack (breq, &packed);
      g_assert (packed_len != 0);
Dafydd Harries's avatar
Dafydd Harries committed
89
      nice_udp_fake_socket_push_recv (sock, &from, packed_len, packed);
90
91
92
      g_free (packed);
      stun_message_free (breq);
    }
93
94

  /* tell the agent there's a packet waiting */
95
  nice_agent_poll_read (agent, NULL, NULL, NULL);
96

97
  /* error should have been sent */
Dafydd Harries's avatar
Dafydd Harries committed
98
99
  len = nice_udp_fake_socket_pop_send (sock, &to,
      sizeof (buf) / sizeof (gchar), buf);
100
101
102
103
104
105
106
107
108
  g_assert (len != 0);

    {
      StunMessage *bres;

      /* construct expected response */
      bres = stun_message_new (STUN_MESSAGE_BINDING_ERROR_RESPONSE,
          "0123456789abcdef", 0);
      packed_len = stun_message_pack (bres, &packed);
109
      stun_message_free (bres);
110
111
112
113
    }

  g_assert (len == packed_len);
  g_assert (0 == memcmp (buf, packed, len));
114
  g_free (packed);
115
}
116

117
118
119
static void
test_stun_valid_password (
  NiceAgent *agent,
120
  NiceAddress from)
121
122
{
  NiceCandidate *candidate;
Dafydd Harries's avatar
Dafydd Harries committed
123
  NiceUDPSocket *sock;
124
  NiceAddress to = {0,};
125
126
127
128
  guint len;
  guint packed_len;
  gchar buf[1024];
  gchar *packed;
129
  gchar *username;
130

131
132
133
134
135
  memset (buf, '\0', 1024);

  candidate = agent->local_candidates->data;
  sock = &candidate->sock;

136
137
138
139
140
  username = g_strconcat (
      ((NiceCandidate *) agent->local_candidates->data)->username,
      "username",
      NULL);

141
142
143
144
145
146
    {
      StunMessage *breq;
      guint packed_len;
      gchar *packed;

      /* send binding request with correct username */
147
      breq = stun_message_new (STUN_MESSAGE_BINDING_REQUEST,
148
          "0123456789abcdef", 1);
149
150
151
      breq->attributes[0] = stun_attribute_username_new (username);
      packed_len = stun_message_pack (breq, &packed);
      g_assert (packed_len != 0);
Dafydd Harries's avatar
Dafydd Harries committed
152
      nice_udp_fake_socket_push_recv (sock, &from, packed_len, packed);
153
154
155
156
157
158
159
160
      g_free (packed);
      stun_message_free (breq);
    }

    {
      StunMessage *bres;

      /* construct expected response packet */
161
      bres = stun_message_new (STUN_MESSAGE_BINDING_RESPONSE,
162
          "0123456789abcdef", 2);
163
      bres->attributes[0] = stun_attribute_mapped_address_new (
164
          from.addr_ipv4, 5678);
165
      bres->attributes[1] = stun_attribute_username_new (username);
166
167
168
      packed_len = stun_message_pack (bres, &packed);
      stun_message_free (bres);
    }
169

170
171
  g_free (username);

172
  /* tell the agent there's a packet waiting */
173
  nice_agent_poll_read (agent, NULL, NULL, NULL);
174

175
  /* compare sent packet to expected */
176
177
  len = nice_udp_fake_socket_pop_send (sock, &to,
      sizeof (buf) / sizeof (gchar), buf);
178
179
  g_assert (len == packed_len);
  g_assert (0 == memcmp (buf, packed, len));
180
  g_assert (nice_address_equal (&to, &from));
181
  g_free (packed);
182
183
184
185
186
187
}

int
main (void)
{
  NiceAgent *agent;
Dafydd Harries's avatar
Dafydd Harries committed
188
189
  NiceAddress local_addr = {0,};
  NiceAddress remote_addr = {0,};
190
  NiceCandidate *candidate;
Dafydd Harries's avatar
Dafydd Harries committed
191
192
  NiceUDPSocketFactory factory;
  NiceUDPSocket *sock;
193

Dafydd Harries's avatar
Dafydd Harries committed
194
195
  g_type_init ();

Dafydd Harries's avatar
Dafydd Harries committed
196
  nice_udp_fake_socket_factory_init (&factory);
197

198
199
  g_assert (nice_address_set_ipv4_from_string (&local_addr, "192.168.0.1"));
  g_assert (nice_address_set_ipv4_from_string (&remote_addr, "192.168.0.5"));
200
  remote_addr.port = 5678;
201
202

  /* set up agent */
Dafydd Harries's avatar
Dafydd Harries committed
203
  agent = nice_agent_new (&factory);
204
  nice_agent_add_local_address (agent, &local_addr);
205
  nice_agent_add_stream (agent, 1);
206
  nice_agent_add_remote_candidate (agent, 1, 1, NICE_CANDIDATE_TYPE_HOST,
207
      &remote_addr, "username", "password");
208
209
210
211
212
  g_assert (agent->local_candidates != NULL);
  candidate = agent->local_candidates->data;
  sock = &(candidate->sock);

  /* run tests */
213
214
215
  test_stun_no_password (agent, remote_addr);
  test_stun_invalid_password (agent, remote_addr);
  test_stun_valid_password (agent, remote_addr);
216
217

  /* clean up */
218
  nice_agent_free (agent);
Dafydd Harries's avatar
Dafydd Harries committed
219
  nice_udp_socket_factory_close (&factory);
220
221
222
223

  return 0;
}