Commit a4b89d60 authored by Dafydd Harries's avatar Dafydd Harries

split STUN test into smaller test programs

darcs-hash:20070130094435-c9803-5c0a4340ba829ae5c83be5aeef2c55ceaf116d69.gz
parent 1be4f2f0
AM_CFLAGS = -Wall -Werror $(GLIB_CFLAGS)
COMMON_LDADD = libstun.la $(GLIB_LIBS)
LIBS = $(GLIB_LIBS)
noinst_LTLIBRARIES = libstun.la
......@@ -9,13 +9,31 @@ libstun_la_SOURCES = stun.h stun.c
noinst_PROGRAMS = stun-client stun-server
stun_client_LDADD = $(COMMON_LDADD)
stun_server_LDADD = $(COMMON_LDADD)
check_PROGRAMS = test
stun_client_LDADD = libstun.la
stun_server_LDADD = libstun.la
check_PROGRAMS = \
test-attribute-pack \
test-attribute-pack-unknown \
test-attribute-dump \
test-attribute-dump-unknown \
test-attribute-unpack \
test-attribute-unpack-unknown \
test-attribute-unpack-wrong-length \
test-message-pack \
test-message-dump \
test-message-unpack
test_attribute_pack_LDADD = libstun.la
test_attribute_pack_unknown_LDADD = libstun.la
test_attribute_dump_LDADD = libstun.la
test_attribute_dump_unknown_LDADD = libstun.la
test_attribute_unpack_LDADD = libstun.la
test_attribute_unpack_unknown_LDADD = libstun.la
test_attribute_unpack_wrong_length_LDADD = libstun.la
test_message_pack_LDADD = libstun.la
test_message_dump_LDADD = libstun.la
test_message_unpack_LDADD = libstun.la
TESTS = $(check_PROGRAMS)
test_LDADD = $(CHECK_LIBS) $(COMMON_LDADD)
#include <string.h>
#include "stun.h"
int
main (void)
{
gchar *dump;
StunAttribute *attr = stun_attribute_unpack (4,
"\x00\xff" // type
"\x00\x00" // length
);
dump = stun_attribute_dump (attr);
g_assert (0 == strcmp (dump, "UNKNOWN (255)"));
g_free (dump);
stun_attribute_free (attr);
return 0;
}
#include <string.h>
#include "stun.h"
int
main (void)
{
StunAttribute *attr = stun_attribute_mapped_address_new (0x02030405, 2345);
gchar *dump = stun_attribute_dump (attr);
g_assert (NULL != dump);
g_assert (0 == strcmp (dump, "MAPPED-ADDRESS 2.3.4.5:2345"));
g_free (dump);
stun_attribute_free (attr);
return 0;
}
#include "stun.h"
int
main (void)
{
/* can't create an unknown attribute directly, so create a MAPPED-ADDRESS
* and change its type
*/
StunAttribute *attr = stun_attribute_mapped_address_new (0x02030405, 2345);
gchar *packed = NULL;
guint length;
attr->type = 0xff;
length = stun_attribute_pack (attr, &packed);
g_assert (0 == length);
g_assert (NULL == packed);
stun_attribute_free (attr);
return 0;
}
#include <string.h>
#include "stun.h"
int
main (void)
{
StunAttribute *attr = stun_attribute_mapped_address_new (0x02030405, 2345);
gchar *packed;
guint length;
length = stun_attribute_pack (attr, &packed);
g_assert (12 == length);
g_assert (NULL != packed);
g_assert (0 == memcmp (packed,
"\x00\x01" // type
"\x00\x08" // length
"\x00\x01" // padding, address family
"\x09\x29" // port
"\x02\x03\x04\x05", // IP address
length));
g_free (packed);
stun_attribute_free (attr);
return 0;
}
#include "stun.h"
int
main (void)
{
StunAttribute *attr = stun_attribute_unpack (8,
"\x00\xff" // type
"\x00\x04" // length
"\xff\xff" // some data
"\xff\xff"
);
g_assert (NULL != attr);
g_assert (attr->type == 0xff);
stun_attribute_free (attr);
return 0;
}
#include "stun.h"
int
main (void)
{
StunAttribute *attr;
// attributes must be at least 4 bytes long
attr = stun_attribute_unpack (0, NULL);
g_assert (NULL == attr);
attr = stun_attribute_unpack (8,
"\x00\x01" // type = MAPPED-ADDRESS
"\x00\x04" // length = 4 (invalid!)
"\x00\x01" // padding, address family
"\x09\x29" // port
);
g_assert (NULL == attr);
return 0;
}
#include <string.h>
#include "stun.h"
int
main (void)
{
StunAttribute *attr;
attr = stun_attribute_unpack (12,
"\x00\x01" // type
"\x00\x08" // length
"\x00\x01" // padding, address family
"\x09\x29" // port
"\x02\x03\x04\x05" // IP address
);
g_assert (NULL != attr);
g_assert (attr->type == STUN_ATTRIBUTE_MAPPED_ADDRESS);
// length is not used
g_assert (attr->length == 0);
g_assert (attr->address.af == 1);
g_assert (attr->address.port == 2345);
g_assert (attr->address.ip == 0x02030405);
stun_attribute_free (attr);
attr = stun_attribute_unpack (9,
"\x00\x06" // type
"\x00\x05" // length
"abcde" // value
);
g_assert (NULL != attr);
g_assert (attr->length == 5);
g_assert (attr->type == STUN_ATTRIBUTE_USERNAME);
g_assert (0 == memcmp (attr->username, "abcde", 5));
stun_attribute_free (attr);
attr = stun_attribute_unpack (10,
"\x00\x07" // type
"\x00\x06" // length
"fghijk" // value
);
g_assert (NULL != attr);
g_assert (attr->length == 6);
g_assert (attr->type == STUN_ATTRIBUTE_PASSWORD);
g_assert (0 == memcmp (attr->password, "fghijk", 6));
stun_attribute_free (attr);
return 0;
}
#include <string.h>
#include "stun.h"
int
main (void)
{
StunMessage *msg = stun_message_binding_request_new ();
gchar *dump;
msg->attributes = g_malloc0 (2 * sizeof (StunAttribute *));
msg->attributes[0] = stun_attribute_mapped_address_new (0x02030405, 2345);
dump = stun_message_dump (msg);
g_assert (NULL != dump);
g_assert (0 == strcmp (dump,
"BINDING-REQUEST 00000000:00000000:00000000:00000000\n"
" MAPPED-ADDRESS 2.3.4.5:2345"));
g_free (dump);
stun_message_free (msg);
return 0;
}
#include <string.h>
#include "stun.h"
int
main (void)
{
StunMessage *msg = stun_message_binding_request_new ();
gchar *packed;
guint length;
memcpy (msg->transaction_id,
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f", 16);
msg->attributes = g_malloc0 (2 * sizeof (StunAttribute *));
msg->attributes[0] = stun_attribute_mapped_address_new (0x02030405, 2345);
length = stun_message_pack (msg, &packed);
g_assert (packed != NULL);
g_assert (length == 32);
g_assert (0 == memcmp (packed + 0, "\x00\x01", 2));
g_assert (0 == memcmp (packed + 2, "\x00\x0c", 2));
g_assert (0 == memcmp (packed + 4,
"\x00\x01\x02\x03"
"\x04\x05\x06\x07"
"\x08\x09\x0a\x0b"
"\x0c\x0d\x0e\x0f", 16));
g_assert (0 == memcmp (packed + 20,
"\x00\x01" // type
"\x00\x08" // length
"\x00\x01" // padding, address family
"\x09\x29" // port
"\x02\x03\x04\x05", // IP address
12));
g_free (packed);
stun_message_free (msg);
return 0;
}
#include "stun.h"
int
main (void)
{
StunMessage *msg = stun_message_unpack (32,
"\x00\x01" // type
"\x00\x0c" // length
"\x00\x01\x02\x03" // transaction ID
"\x04\x05\x06\x07"
"\x08\x09\x0a\x0b"
"\x0c\x0d\x0e\x0f"
"\x00\x01" // attr1 type
"\x00\x08" // attr1 length
"\x00\x01" // padding, address family
"\x09\x29" // port
"\x02\x03\x04\x05" // IP address
);
g_assert (msg->type == STUN_MESSAGE_BINDING_REQUEST);
g_assert (msg->attributes[0] != NULL);
g_assert (msg->attributes[0]->type == STUN_ATTRIBUTE_MAPPED_ADDRESS);
g_assert (msg->attributes[0]->address.port == 2345);
g_assert (msg->attributes[0]->address.ip == 0x02030405);
g_assert (msg->attributes[1] == NULL);
stun_message_free (msg);
return 0;
}
#include <string.h>
#include <check.h>
#include <stun.h>
START_TEST (test_attribute_pack)
{
StunAttribute *attr = stun_attribute_mapped_address_new (0x02030405, 2345);
gchar *packed;
guint length;
length = stun_attribute_pack (attr, &packed);
fail_unless (12 == length);
fail_unless (NULL != packed);
fail_unless (0 == memcmp (packed,
"\x00\x01" // type
"\x00\x08" // length
"\x00\x01" // padding, address family
"\x09\x29" // port
"\x02\x03\x04\x05", // IP address
length));
g_free (packed);
stun_attribute_free (attr);
}
END_TEST
START_TEST (test_attribute_pack_unknown)
{
/* can't create an unknown attribute directly, so create a MAPPED-ADDRESS
* and change its type
*/
StunAttribute *attr = stun_attribute_mapped_address_new (0x02030405, 2345);
gchar *packed = NULL;
guint length;
attr->type = 0xff;
length = stun_attribute_pack (attr, &packed);
fail_unless (0 == length);
fail_unless (NULL == packed);
stun_attribute_free (attr);
}
END_TEST
START_TEST (test_attribute_dump)
{
StunAttribute *attr = stun_attribute_mapped_address_new (0x02030405, 2345);
gchar *dump = stun_attribute_dump (attr);
fail_unless (NULL != dump);
fail_unless (0 == strcmp (dump, "MAPPED-ADDRESS 2.3.4.5:2345"));
g_free (dump);
stun_attribute_free (attr);
}
END_TEST
START_TEST (test_attribute_dump_unknown)
{
gchar *dump;
StunAttribute *attr = stun_attribute_unpack (4,
"\x00\xff" // type
"\x00\x00" // length
);
dump = stun_attribute_dump (attr);
fail_unless (0 == strcmp (dump, "UNKNOWN (255)"));
g_free (dump);
stun_attribute_free (attr);
}
END_TEST
START_TEST (test_attribute_unpack)
{
StunAttribute *attr;
attr = stun_attribute_unpack (12,
"\x00\x01" // type
"\x00\x08" // length
"\x00\x01" // padding, address family
"\x09\x29" // port
"\x02\x03\x04\x05" // IP address
);
fail_unless (NULL != attr);
fail_unless (attr->type == STUN_ATTRIBUTE_MAPPED_ADDRESS);
// length is not used
fail_unless (attr->length == 0);
fail_unless (attr->address.af == 1);
fail_unless (attr->address.port == 2345);
fail_unless (attr->address.ip == 0x02030405);
stun_attribute_free (attr);
attr = stun_attribute_unpack (9,
"\x00\x06" // type
"\x00\x05" // length
"abcde" // value
);
fail_unless (NULL != attr);
fail_unless (attr->length == 5);
fail_unless (attr->type == STUN_ATTRIBUTE_USERNAME);
fail_unless (0 == memcmp (attr->username, "abcde", 5));
stun_attribute_free (attr);
attr = stun_attribute_unpack (10,
"\x00\x07" // type
"\x00\x06" // length
"fghijk" // value
);
fail_unless (NULL != attr);
fail_unless (attr->length == 6);
fail_unless (attr->type == STUN_ATTRIBUTE_PASSWORD);
fail_unless (0 == memcmp (attr->password, "fghijk", 6));
stun_attribute_free (attr);
}
END_TEST
START_TEST (test_attribute_unpack_unknown)
{
StunAttribute *attr = stun_attribute_unpack (8,
"\x00\xff" // type
"\x00\x04" // length
"\xff\xff" // some data
"\xff\xff"
);
fail_unless (NULL != attr);
fail_unless (attr->type == 0xff);
stun_attribute_free (attr);
}
END_TEST
START_TEST (test_attribute_unpack_wrong_length)
{
StunAttribute *attr;
// attributes must be at least 4 bytes long
attr = stun_attribute_unpack (0, NULL);
fail_unless (NULL == attr);
attr = stun_attribute_unpack (8,
"\x00\x01" // type = MAPPED-ADDRESS
"\x00\x04" // length = 4 (invalid!)
"\x00\x01" // padding, address family
"\x09\x29" // port
);
fail_unless (NULL == attr);
}
END_TEST
START_TEST (test_message_pack)
{
StunMessage *msg = stun_message_binding_request_new ();
gchar *packed;
guint length;
memcpy (msg->transaction_id,
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f", 16);
msg->attributes = g_malloc0 (2 * sizeof (StunAttribute *));
msg->attributes[0] = stun_attribute_mapped_address_new (0x02030405, 2345);
length = stun_message_pack (msg, &packed);
fail_unless (packed != NULL);
fail_unless (length == 32);
fail_unless (0 == memcmp (packed + 0, "\x00\x01", 2));
fail_unless (0 == memcmp (packed + 2, "\x00\x0c", 2));
fail_unless (0 == memcmp (packed + 4,
"\x00\x01\x02\x03"
"\x04\x05\x06\x07"
"\x08\x09\x0a\x0b"
"\x0c\x0d\x0e\x0f", 16));
fail_unless (0 == memcmp (packed + 20,
"\x00\x01" // type
"\x00\x08" // length
"\x00\x01" // padding, address family
"\x09\x29" // port
"\x02\x03\x04\x05", // IP address
12));
g_free (packed);
stun_message_free (msg);
}
END_TEST
START_TEST (test_message_dump)
{
StunMessage *msg = stun_message_binding_request_new ();
gchar *dump;
msg->attributes = g_malloc0 (2 * sizeof (StunAttribute *));
msg->attributes[0] = stun_attribute_mapped_address_new (0x02030405, 2345);
dump = stun_message_dump (msg);
fail_unless (NULL != dump);
fail_unless (0 == strcmp (dump,
"BINDING-REQUEST 00000000:00000000:00000000:00000000\n"
" MAPPED-ADDRESS 2.3.4.5:2345"));
g_free (dump);
stun_message_free (msg);
}
END_TEST
START_TEST (test_message_unpack)
{
StunMessage *msg = stun_message_unpack (32,
"\x00\x01" // type
"\x00\x0c" // length
"\x00\x01\x02\x03" // transaction ID
"\x04\x05\x06\x07"
"\x08\x09\x0a\x0b"
"\x0c\x0d\x0e\x0f"
"\x00\x01" // attr1 type
"\x00\x08" // attr1 length
"\x00\x01" // padding, address family
"\x09\x29" // port
"\x02\x03\x04\x05" // IP address
);
fail_unless (msg->type == STUN_MESSAGE_BINDING_REQUEST);
fail_unless (msg->attributes[0] != NULL);
fail_unless (msg->attributes[0]->type == STUN_ATTRIBUTE_MAPPED_ADDRESS);
fail_unless (msg->attributes[0]->address.port == 2345);
fail_unless (msg->attributes[0]->address.ip == 0x02030405);
fail_unless (msg->attributes[1] == NULL);
stun_message_free (msg);
}
END_TEST
Suite *
stun_suite (void)
{
Suite *suite;
TCase *tcase;
suite = suite_create ("STUN");
tcase = tcase_create ("attribute pack");
tcase_add_test (tcase, test_attribute_pack);
suite_add_tcase (suite, tcase);
tcase = tcase_create ("attribute pack unknown");
tcase_add_test (tcase, test_attribute_pack_unknown);
suite_add_tcase (suite, tcase);
tcase = tcase_create ("attribute dump");
tcase_add_test (tcase, test_attribute_dump);
suite_add_tcase (suite, tcase);
tcase = tcase_create ("attribute dump unknown");
tcase_add_test (tcase, test_attribute_dump_unknown);
suite_add_tcase (suite, tcase);
tcase = tcase_create ("attribute unpack");
tcase_add_test (tcase, test_attribute_unpack);
suite_add_tcase (suite, tcase);
tcase = tcase_create ("attribute unpack unknown");
tcase_add_test (tcase, test_attribute_unpack_unknown);
suite_add_tcase (suite, tcase);
tcase = tcase_create ("attribute unpack unknown wrong length");
tcase_add_test (tcase, test_attribute_unpack_wrong_length);
suite_add_tcase (suite, tcase);
tcase = tcase_create ("message pack");
tcase_add_test (tcase, test_message_pack);
suite_add_tcase (suite, tcase);
tcase = tcase_create ("message dump");
tcase_add_test (tcase, test_message_dump);
suite_add_tcase (suite, tcase);
tcase = tcase_create ("message unpack");
tcase_add_test (tcase, test_message_unpack);
suite_add_tcase (suite, tcase);
return suite;
}
int
main (void)
{
Suite *suite;
SRunner *runner;
int failures;
suite = stun_suite ();
runner = srunner_create (suite);
srunner_run_all (runner, CK_NORMAL | CK_NOFORK);
failures = srunner_ntests_failed (runner);
srunner_free (runner);
return (failures == 0) ? 0 : 1;
}
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