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
|
#include <test.h>
#include <alloc.h>
#include <addr_lib.h>
/**
* @WARNING this test leaks all over the place. I'm strdup'ing because
* ParseHostPort() might modify the string, and a string literal
* will cause segfault since it is in read-only memory segment.
*/
static void test_ParseHostPort()
{
char *hostname, *port;
ParseHostPort(xstrdup("www.cfengine.com"), &hostname, &port);
assert_string_equal(hostname, "www.cfengine.com");
assert_int_equal(port, NULL);
hostname = NULL; port = NULL;
ParseHostPort(xstrdup("www.cfengine.com:80"), &hostname, &port);
assert_string_equal(hostname, "www.cfengine.com");
assert_string_equal(port, "80");
hostname = NULL; port = NULL;
ParseHostPort(xstrdup("www.cfengine.com:"), &hostname, &port);
assert_string_equal(hostname, "www.cfengine.com");
assert_int_equal(port, NULL);
hostname = NULL; port = NULL;
ParseHostPort(xstrdup("[www.cfengine.com]"), &hostname, &port);
assert_string_equal(hostname, "www.cfengine.com");
assert_int_equal(port, NULL);
hostname = NULL; port = NULL;
ParseHostPort(xstrdup("[www.cfengine.com]:80"), &hostname, &port);
assert_string_equal(hostname, "www.cfengine.com");
assert_string_equal(port, "80");
hostname = NULL; port = NULL;
ParseHostPort(xstrdup("[www.cfengine.com]:"), &hostname, &port);
assert_string_equal(hostname, "www.cfengine.com");
assert_int_equal(port, NULL);
hostname = NULL; port = NULL;
ParseHostPort(xstrdup("1.2.3.4"), &hostname, &port);
assert_string_equal(hostname, "1.2.3.4");
assert_int_equal(port, NULL);
hostname = NULL; port = NULL;
ParseHostPort(xstrdup("1.2.3.4:80"), &hostname, &port);
assert_string_equal(hostname, "1.2.3.4");
assert_string_equal(port, "80");
hostname = NULL; port = NULL;
ParseHostPort(xstrdup("1.2.3.4:"), &hostname, &port);
assert_string_equal(hostname, "1.2.3.4");
assert_int_equal(port, NULL);
hostname = NULL; port = NULL;
ParseHostPort(xstrdup("[ffff::dd:12:34]"), &hostname, &port);
assert_string_equal(hostname, "ffff::dd:12:34");
assert_int_equal(port, NULL);
hostname = NULL; port = NULL;
ParseHostPort(xstrdup("[ffff::dd:12:34]:80"), &hostname, &port);
assert_string_equal(hostname, "ffff::dd:12:34");
assert_string_equal(port, "80");
hostname = NULL; port = NULL;
ParseHostPort(xstrdup("[ffff::dd:12:34]:"), &hostname, &port);
assert_string_equal(hostname, "ffff::dd:12:34");
assert_int_equal(port, NULL);
hostname = NULL; port = NULL;
/***** CORNER CASES *****/
ParseHostPort(xstrdup(""), &hostname, &port);
assert_int_equal(hostname, NULL);
assert_int_equal(port, NULL);
hostname = NULL; port = NULL;
ParseHostPort(xstrdup("[]"), &hostname, &port);
assert_int_equal(hostname, NULL);
assert_int_equal(port, NULL);
hostname = NULL; port = NULL;
ParseHostPort(xstrdup("[]:"), &hostname, &port);
assert_int_equal(hostname, NULL);
assert_int_equal(port, NULL);
hostname = NULL; port = NULL;
ParseHostPort(xstrdup(":"), &hostname, &port);
assert_int_equal(hostname, NULL);
assert_int_equal(port, NULL);
hostname = NULL; port = NULL;
}
int main()
{
PRINT_TEST_BANNER();
const UnitTest tests[] =
{
unit_test(test_ParseHostPort)
};
return run_tests(tests);
}
|