Commit 36ad7fdc authored by Dafydd Harries's avatar Dafydd Harries

split agent STUN test into several smaller ones

darcs-hash:20070131223218-c9803-dc5fba4bc334e7ebd9171a4c68768e2b96bfe984.gz
parent fcdef41f
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
#include "udp-fake.h" #include "udp-fake.h"
#include "agent.h" #include "agent.h"
void static void
handle_recv ( handle_recv (
NiceAgent *agent, NiceAgent *agent,
guint stream_id, guint stream_id,
...@@ -17,50 +17,35 @@ handle_recv ( ...@@ -17,50 +17,35 @@ handle_recv (
g_assert_not_reached (); g_assert_not_reached ();
} }
int static void
main (void) test_stun_no_password (
NiceAgent *agent,
struct sockaddr_in from)
{ {
NiceAgent *agent;
NiceAddress local_addr, remote_addr;
NiceCandidate *candidate; NiceCandidate *candidate;
UDPSocketManager mgr;
UDPSocket *sock; UDPSocket *sock;
StunMessage *breq, *bres;
struct sockaddr_in from = {0,}, to = {0,};
guint packed_len;
gchar *packed;
guint len; guint len;
struct sockaddr_in to = {0,};
gchar buf[1024]; gchar buf[1024];
gchar *username;
memset (buf, '\0', 1024); memset (buf, '\0', 1024);
udp_fake_socket_manager_init (&mgr);
nice_address_set_ipv4_from_string (&local_addr, "192.168.0.1");
nice_address_set_ipv4_from_string (&remote_addr, "192.168.0.5");
from.sin_family = AF_INET;
from.sin_addr.s_addr = htonl (remote_addr.addr_ipv4);
from.sin_port = htons (5678);
/* set up agent */
agent = nice_agent_new (&mgr);
nice_agent_add_local_address (agent, &local_addr);
nice_agent_add_stream (agent, handle_recv, NULL);
nice_agent_add_remote_candidate (agent, 1, 1, NICE_CANDIDATE_TYPE_HOST,
&remote_addr, 5678, "username", "password");
g_assert (agent->local_candidates != NULL);
candidate = agent->local_candidates->data; candidate = agent->local_candidates->data;
sock = &(candidate->sock); sock = &candidate->sock;
/* send binding request without username */ {
breq = stun_message_new (STUN_MESSAGE_BINDING_REQUEST); StunMessage *breq;
memcpy (breq->transaction_id, "0123456789abcdef", 16); guint packed_len;
packed_len = stun_message_pack (breq, &packed); gchar *packed;
udp_fake_socket_push_recv (sock, &from, packed_len, packed);
g_free (packed); /* send binding request without username */
stun_message_free (breq); breq = stun_message_new (STUN_MESSAGE_BINDING_REQUEST);
memcpy (breq->transaction_id, "0123456789abcdef", 16);
packed_len = stun_message_pack (breq, &packed);
udp_fake_socket_push_recv (sock, &from, packed_len, packed);
g_free (packed);
stun_message_free (breq);
}
/* tell the agent there's a packet waiting */ /* tell the agent there's a packet waiting */
nice_agent_recv (agent, candidate->id); nice_agent_recv (agent, candidate->id);
...@@ -69,17 +54,40 @@ main (void) ...@@ -69,17 +54,40 @@ main (void)
len = udp_fake_socket_pop_send (sock, &to, sizeof (buf) / sizeof (gchar), len = udp_fake_socket_pop_send (sock, &to, sizeof (buf) / sizeof (gchar),
buf); buf);
g_assert (len == 0); g_assert (len == 0);
}
/* send binding request with incorrect username */ static void
breq = stun_message_new (STUN_MESSAGE_BINDING_REQUEST); test_stun_invalid_password (
breq->attributes = g_malloc0 (2 * sizeof (StunAttribute *)); NiceAgent *agent,
breq->attributes[0] = stun_attribute_username_new ("lala"); struct sockaddr_in from)
memcpy (breq->transaction_id, "0123456789abcdef", 16); {
packed_len = stun_message_pack (breq, &packed); NiceCandidate *candidate;
g_assert (packed_len != 0); UDPSocket *sock;
udp_fake_socket_push_recv (sock, &from, packed_len, packed); guint len;
g_free (packed); struct sockaddr_in to = {0,};
stun_message_free (breq); gchar buf[1024];
memset (buf, '\0', 1024);
candidate = agent->local_candidates->data;
sock = &candidate->sock;
{
StunMessage *breq;
guint packed_len;
gchar *packed;
/* send binding request with incorrect username */
breq = stun_message_new (STUN_MESSAGE_BINDING_REQUEST);
breq->attributes = g_malloc0 (2 * sizeof (StunAttribute *));
breq->attributes[0] = stun_attribute_username_new ("lala");
memcpy (breq->transaction_id, "0123456789abcdef", 16);
packed_len = stun_message_pack (breq, &packed);
g_assert (packed_len != 0);
udp_fake_socket_push_recv (sock, &from, packed_len, packed);
g_free (packed);
stun_message_free (breq);
}
/* tell the agent there's a packet waiting */ /* tell the agent there's a packet waiting */
nice_agent_recv (agent, candidate->id); nice_agent_recv (agent, candidate->id);
...@@ -88,30 +96,61 @@ main (void) ...@@ -88,30 +96,61 @@ main (void)
len = udp_fake_socket_pop_send (sock, &to, sizeof (buf) / sizeof (gchar), len = udp_fake_socket_pop_send (sock, &to, sizeof (buf) / sizeof (gchar),
buf); buf);
g_assert (len == 0); g_assert (len == 0);
}
/* send binding request with correct username */ static void
breq = stun_message_new (STUN_MESSAGE_BINDING_REQUEST); test_stun_valid_password (
breq->attributes = g_malloc0 (2 * sizeof (StunAttribute *)); NiceAgent *agent,
username = g_strconcat ( struct sockaddr_in from)
"username", {
((NiceCandidate *) agent->local_candidates->data)->username, NiceCandidate *candidate;
NULL); UDPSocket *sock;
breq->attributes[0] = stun_attribute_username_new (username); guint len;
memcpy (breq->transaction_id, "0123456789abcdef", 16); guint packed_len;
packed_len = stun_message_pack (breq, &packed); struct sockaddr_in to = {0,};
g_assert (packed_len != 0); gchar buf[1024];
udp_fake_socket_push_recv (sock, &from, packed_len, packed); gchar *packed;
g_free (packed);
stun_message_free (breq);
/* construct expected response packet */ memset (buf, '\0', 1024);
bres = stun_message_new (STUN_MESSAGE_BINDING_RESPONSE);
memcpy (bres->transaction_id, "0123456789abcdef", 16); candidate = agent->local_candidates->data;
bres->attributes = g_malloc0 (2 * sizeof (StunAttribute *)); sock = &candidate->sock;
bres->attributes[0] = stun_attribute_mapped_address_new (
remote_addr.addr_ipv4, 5678); {
packed_len = stun_message_pack (bres, &packed); StunMessage *breq;
g_assert (packed_len == 32); guint packed_len;
gchar *packed;
gchar *username;
/* send binding request with correct username */
breq = stun_message_new (STUN_MESSAGE_BINDING_REQUEST);
breq->attributes = g_malloc0 (2 * sizeof (StunAttribute *));
username = g_strconcat (
"username",
((NiceCandidate *) agent->local_candidates->data)->username,
NULL);
breq->attributes[0] = stun_attribute_username_new (username);
memcpy (breq->transaction_id, "0123456789abcdef", 16);
packed_len = stun_message_pack (breq, &packed);
g_assert (packed_len != 0);
udp_fake_socket_push_recv (sock, &from, packed_len, packed);
g_free (packed);
stun_message_free (breq);
}
{
StunMessage *bres;
/* construct expected response packet */
bres = stun_message_new (STUN_MESSAGE_BINDING_RESPONSE);
memcpy (bres->transaction_id, "0123456789abcdef", 16);
bres->attributes = g_malloc0 (2 * sizeof (StunAttribute *));
bres->attributes[0] = stun_attribute_mapped_address_new (
ntohl (from.sin_addr.s_addr), 5678);
packed_len = stun_message_pack (bres, &packed);
g_assert (packed_len == 32);
stun_message_free (bres);
}
/* tell the agent there's a packet waiting */ /* tell the agent there's a packet waiting */
nice_agent_recv (agent, candidate->id); nice_agent_recv (agent, candidate->id);
...@@ -124,9 +163,42 @@ main (void) ...@@ -124,9 +163,42 @@ main (void)
g_assert (to.sin_family == from.sin_family); g_assert (to.sin_family == from.sin_family);
g_assert (to.sin_addr.s_addr == from.sin_addr.s_addr); g_assert (to.sin_addr.s_addr == from.sin_addr.s_addr);
g_assert (to.sin_port == from.sin_port); g_assert (to.sin_port == from.sin_port);
g_free (packed); g_free (packed);
stun_message_free (bres); }
int
main (void)
{
NiceAgent *agent;
NiceAddress local_addr, remote_addr;
NiceCandidate *candidate;
UDPSocketManager mgr;
UDPSocket *sock;
struct sockaddr_in from = {0,};
udp_fake_socket_manager_init (&mgr);
nice_address_set_ipv4_from_string (&local_addr, "192.168.0.1");
nice_address_set_ipv4_from_string (&remote_addr, "192.168.0.5");
from.sin_family = AF_INET;
from.sin_addr.s_addr = htonl (remote_addr.addr_ipv4);
from.sin_port = htons (5678);
/* set up agent */
agent = nice_agent_new (&mgr);
nice_agent_add_local_address (agent, &local_addr);
nice_agent_add_stream (agent, handle_recv, NULL);
nice_agent_add_remote_candidate (agent, 1, 1, NICE_CANDIDATE_TYPE_HOST,
&remote_addr, 5678, "username", "password");
g_assert (agent->local_candidates != NULL);
candidate = agent->local_candidates->data;
sock = &(candidate->sock);
/* run tests */
test_stun_no_password (agent, from);
test_stun_invalid_password (agent, from);
test_stun_valid_password (agent, from);
/* clean up */ /* clean up */
nice_agent_free (agent); nice_agent_free (agent);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment