test-send.c 3.87 KB
Newer Older
Dafydd Harries's avatar
Dafydd Harries committed
1
2
3
4
5
6

#include <string.h>

#include "agent.h"
#include "stun.h"
#include "udp-fake.h"
7
#include "random-glib.h"
Dafydd Harries's avatar
Dafydd Harries committed
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

void
send_connectivity_check (
  NiceAgent *agent,
  NiceUDPSocketFactory *factory,
  NiceAddress *remote_addr)
{
  NiceUDPSocket *sock;
  NiceCandidate *local;
  NiceCandidate *remote;
  gchar *username;

  g_assert (agent->local_candidates);
  g_assert (agent->local_candidates->data);
  local = agent->local_candidates->data;
  g_assert (local->id == 1);

  g_assert (agent->remote_candidates);
  g_assert (agent->remote_candidates->data);
  remote = agent->remote_candidates->data;

  sock = &local->sock;

  username = g_strconcat (local->username, remote->username, NULL);

  {
    StunMessage *msg;
    gchar *packed;
    guint len;

    msg = stun_message_new (STUN_MESSAGE_BINDING_REQUEST, NULL, 1);
    msg->attributes[0] = stun_attribute_username_new (username);
    len = stun_message_pack (msg, &packed);
41
    nice_udp_fake_socket_push_recv (sock, remote_addr, len, packed);
Dafydd Harries's avatar
Dafydd Harries committed
42
43
44
45
    g_free (packed);
    stun_message_free (msg);
  }

46
  nice_agent_poll_read (agent, NULL, NULL, NULL);
Dafydd Harries's avatar
Dafydd Harries committed
47
48
49

  {
    StunMessage *msg;
50
    NiceAddress addr = {0,};
Dafydd Harries's avatar
Dafydd Harries committed
51
52
53
54
55
    gchar packed[1024];
    gchar *dump;
    guint len;

    len = nice_udp_fake_socket_pop_send (sock, &addr, 1024, packed);
56
    g_assert (nice_address_equal (&addr, remote_addr));
Dafydd Harries's avatar
Dafydd Harries committed
57
58
59
60
61
62
63
64
65
66
    msg = stun_message_unpack (len, packed);
    dump = stun_message_dump (msg);
    g_assert (0 == strcmp (dump,
        "BINDING-RESPONSE 00000000:00000000:00000000:00000000\n"
        "  MAPPED-ADDRESS 192.168.0.2:2345\n"
        "  USERNAME \"S9PObXR5username\"\n"));
    g_free (dump);
    stun_message_free (msg);
  }

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  {
    StunMessage *msg;
    NiceAddress addr = {0,};
    gchar packed[1024];
    gchar *dump;
    guint len;

    len = nice_udp_fake_socket_pop_send (sock, &addr, 1024, packed);
    g_assert (nice_address_equal (&addr, remote_addr));
    msg = stun_message_unpack (len, packed);
    dump = stun_message_dump (msg);
    g_assert (0 == strcmp (dump,
        "BINDING-REQUEST ac2f75c0:43fbc367:09d315f2:245746d8\n"
        "  USERNAME \"usernameS9PObXR5\"\n"));
    g_free (dump);
    stun_message_free (msg);
  }

Dafydd Harries's avatar
Dafydd Harries committed
85
86
87
88
89
90
91
92
93
94
95
96
97
  g_free (username);
}

int
main (void)
{
  NiceUDPSocketFactory factory;
  NiceAgent *agent;
  NiceAddress local_addr = {0,};
  NiceAddress remote_addr = {0,};

  /* set up */

98
99
  nice_rng_set_new_func (nice_rng_glib_new_predictable);

Dafydd Harries's avatar
Dafydd Harries committed
100
101
102
103
104
105
106
  nice_udp_fake_socket_factory_init (&factory);
  agent = nice_agent_new (&factory);

  if (!nice_address_set_ipv4_from_string (&local_addr, "192.168.0.1"))
    g_assert_not_reached ();

  nice_agent_add_local_address (agent, &local_addr);
107
  nice_agent_add_stream (agent, 1);
Dafydd Harries's avatar
Dafydd Harries committed
108
109
110
111
112
113
114
115
116
117
118
119
120

  if (!nice_address_set_ipv4_from_string (&remote_addr, "192.168.0.2"))
    g_assert_not_reached ();

  remote_addr.port = 2345;
  nice_agent_add_remote_candidate (agent, 1, 1, NICE_CANDIDATE_TYPE_HOST,
      &remote_addr, "username", "password");

  /* test */

  {
    NiceUDPSocket *sock;
    NiceCandidate *candidate;
121
    NiceAddress addr;
Dafydd Harries's avatar
Dafydd Harries committed
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
    gchar buf[1024];
    guint len;

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

    /* If we send data before we've received a connectivity check, we won't
     * have an affinity for any of the remote candidates, so the packet will
     * get silently dropped.
     */

    nice_agent_send (agent, 1, 1, 5, "hello");
    g_assert (0 == nice_udp_fake_socket_pop_send (sock, &addr, 1024, buf));

    send_connectivity_check (agent, &factory, &remote_addr);

    /* Now that we've received a valid connectivity check, we have a local
     * socket to send from, and a remote address to send to.
     */

    nice_agent_send (agent, 1, 1, 5, "hello");
    len = nice_udp_fake_socket_pop_send (sock, &addr, 1024, buf);
    g_assert (len == 5);
    g_assert (0 == strncmp (buf, "hello", len));
  }

  /* clean up */

  nice_agent_free (agent);
  nice_udp_socket_factory_close (&factory);

  return 0;
}