Add tests for GNetworkAddress
[platform/upstream/glib.git] / gio / tests / network-address.c
1 #include <gio/gio.h>
2
3 static void
4 test_basic (void)
5 {
6   GNetworkAddress *address;
7   guint16 port;
8   gchar *hostname;
9
10   address = (GNetworkAddress*)g_network_address_new ("www.gnome.org", 8080);
11
12   g_assert_cmpstr (g_network_address_get_hostname (address), ==, "www.gnome.org");
13   g_assert_cmpint (g_network_address_get_port (address), ==, 8080);
14
15   g_object_get (address, "hostname", &hostname, "port", &port, NULL);
16   g_assert_cmpstr (hostname, ==, "www.gnome.org");
17   g_assert_cmpint (port, ==, 8080);
18   g_free (hostname);
19 }
20
21 typedef struct _ParseTest ParseTest;
22
23 struct _ParseTest
24 {
25   const gchar *input;
26   const gchar *hostname;
27   guint16 port;
28   gint error_code;
29 };
30
31 static ParseTest tests[] =
32 {
33   { "www.gnome.org", "www.gnome.org", 1234, -1 },
34   { "www.gnome.org:8080", "www.gnome.org", 8080, -1 },
35   { "[2001:db8::1]", "2001:db8::1", 1234, -1 },
36   { "[2001:db8::1]:888", "2001:db8::1", 888, -1 },
37   { "[hostname", NULL, 0, G_IO_ERROR_INVALID_ARGUMENT },
38   { "[hostnam]e", NULL, 0, G_IO_ERROR_INVALID_ARGUMENT },
39   { "hostname:", NULL, 0, G_IO_ERROR_INVALID_ARGUMENT },
40   { "hostname:-1", NULL, 0, G_IO_ERROR_INVALID_ARGUMENT },
41   { "hostname:9999999", NULL, 0, G_IO_ERROR_INVALID_ARGUMENT }
42 };
43
44 static void
45 test_parse (gconstpointer d)
46 {
47   ParseTest *test = d;
48   GNetworkAddress *address;
49   GError *error;
50
51   error = NULL;
52   address = (GNetworkAddress*)g_network_address_parse (test->input, 1234, &error);
53
54   if (address)
55     {
56       g_assert_cmpstr (g_network_address_get_hostname (address), ==, test->hostname);
57       g_assert_cmpint (g_network_address_get_port (address), ==, test->port);
58       g_assert_no_error (error);
59     }
60   else
61     {
62       g_assert_error (error, G_IO_ERROR, test->error_code);
63     }
64
65   if (address)
66     g_object_unref (address);
67   if (error)
68     g_error_free (error);
69 }
70
71 int
72 main (int argc, char *argv[])
73 {
74   gint i;
75   gchar *path;
76
77   g_type_init ();
78
79   g_test_init (&argc, &argv, NULL);
80
81   g_test_add_func ("/network-address/basic", test_basic);
82
83   for (i = 0; i < G_N_ELEMENTS (tests); i++)
84     {
85       path = g_strdup_printf ("/network-address/parse/%d", i);
86       g_test_add_data_func (path, &tests[i], test_parse);
87       g_free (path);
88     }
89
90   return g_test_run ();
91 }