e8cf513b65de0d8caac23cdc4b1cec79293bcffc
[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   guint 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   { "www.gnome.org:http", "www.gnome.org", 80, -1 },
36   { "[2001:db8::1]", "2001:db8::1", 1234, -1 },
37   { "[2001:db8::1]:888", "2001:db8::1", 888, -1 },
38   { "[hostname", NULL, 0, G_IO_ERROR_INVALID_ARGUMENT },
39   { "[hostnam]e", NULL, 0, G_IO_ERROR_INVALID_ARGUMENT },
40   { "hostname:", NULL, 0, G_IO_ERROR_INVALID_ARGUMENT },
41   { "hostname:-1", NULL, 0, G_IO_ERROR_INVALID_ARGUMENT },
42   { "hostname:9999999", NULL, 0, G_IO_ERROR_INVALID_ARGUMENT }
43 };
44
45 static void
46 test_parse (gconstpointer d)
47 {
48   const ParseTest *test = d;
49   GNetworkAddress *address;
50   GError *error;
51
52   error = NULL;
53   address = (GNetworkAddress*)g_network_address_parse (test->input, 1234, &error);
54
55   if (address)
56     {
57       g_assert_cmpstr (g_network_address_get_hostname (address), ==, test->hostname);
58       g_assert_cmpint (g_network_address_get_port (address), ==, test->port);
59       g_assert_no_error (error);
60     }
61   else
62     {
63       g_assert_error (error, G_IO_ERROR, test->error_code);
64     }
65
66   if (address)
67     g_object_unref (address);
68   if (error)
69     g_error_free (error);
70 }
71
72 int
73 main (int argc, char *argv[])
74 {
75   gint i;
76   gchar *path;
77
78   g_type_init ();
79
80   g_test_init (&argc, &argv, NULL);
81
82   g_test_add_func ("/network-address/basic", test_basic);
83
84   for (i = 0; i < G_N_ELEMENTS (tests); i++)
85     {
86       path = g_strdup_printf ("/network-address/parse/%d", i);
87       g_test_add_data_func (path, &tests[i], test_parse);
88       g_free (path);
89     }
90
91   return g_test_run ();
92 }