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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/* tests of the GNIO utility functions */
#include "config.h"
#include <string.h>
#include <glib.h>
#include <dbus/dbus-glib.h>
#include <gio/gio.h>
#ifdef HAVE_GIO_UNIX
#include <gio/gunixsocketaddress.h>
#endif /* HAVE_GIO_UNIX */
#include <telepathy-glib/gnio-util.h>
#include <telepathy-glib/util.h>
#include <telepathy-glib/gtypes.h>
#define IPV4_ADDR "127.0.1.1"
#define IPV6_ADDR "::1"
#define UNIX_ADDR "/tmp/socket/test/123456"
#define ABST_ADDR "\000" "123456" "NOT_COPIED"
#define ABST_ADDR_LEN 7
#define PORT 41414
static void
test_variant_to_sockaddr_ipv4 (void)
{
GSocketAddress *sockaddr;
GInetSocketAddress *inetaddr;
GInetAddress *hostaddr;
char *host;
guint16 port;
GError *error = NULL;
sockaddr = tp_g_socket_address_from_g_variant (TP_SOCKET_ADDRESS_TYPE_IPV4,
g_variant_new_parsed ("(%s, %u)", IPV4_ADDR, (guint32) PORT), &error);
/* check the socket address */
g_assert_no_error (error);
g_assert (sockaddr != NULL);
g_assert (G_IS_INET_SOCKET_ADDRESS (sockaddr));
inetaddr = G_INET_SOCKET_ADDRESS (sockaddr);
hostaddr = g_inet_socket_address_get_address (inetaddr);
host = g_inet_address_to_string (hostaddr);
port = g_inet_socket_address_get_port (inetaddr);
g_assert_cmpstr (host, ==, IPV4_ADDR);
g_assert_cmpuint (port, ==, PORT);
g_free (host);
g_object_unref (sockaddr);
}
static void
test_variant_to_sockaddr_ipv6 (void)
{
GSocketAddress *sockaddr;
GInetSocketAddress *inetaddr;
GInetAddress *hostaddr;
char *host;
guint16 port;
GError *error = NULL;
sockaddr = tp_g_socket_address_from_g_variant (TP_SOCKET_ADDRESS_TYPE_IPV6,
g_variant_new_parsed ("(%s, %u)", IPV6_ADDR, (guint32) PORT), &error);
/* check the socket address */
g_assert_no_error (error);
g_assert (sockaddr != NULL);
g_assert (G_IS_INET_SOCKET_ADDRESS (sockaddr));
inetaddr = G_INET_SOCKET_ADDRESS (sockaddr);
hostaddr = g_inet_socket_address_get_address (inetaddr);
host = g_inet_address_to_string (hostaddr);
port = g_inet_socket_address_get_port (inetaddr);
g_assert_cmpstr (host, ==, IPV6_ADDR);
g_assert_cmpuint (port, ==, PORT);
g_free (host);
g_object_unref (sockaddr);
}
static void
test_sockaddr_to_variant_ipv4 (void)
{
GInetAddress *hostaddr = g_inet_address_new_from_string (IPV4_ADDR);
GSocketAddress *sockaddr = g_inet_socket_address_new (hostaddr, PORT);
GValue *variant, *value;
GVariant *gvariant, *other;
GValueArray *array;
TpSocketAddressType type;
GError *error = NULL;
g_object_unref (hostaddr);
variant = tp_address_variant_from_g_socket_address (sockaddr, &type, &error);
g_assert_no_error (error);
gvariant = tp_address_g_variant_from_g_socket_address (sockaddr, &type,
&error);
g_object_unref (sockaddr);
g_assert_no_error (error);
g_assert_cmpuint (type, ==, TP_SOCKET_ADDRESS_TYPE_IPV4);
g_assert (G_VALUE_HOLDS (variant, TP_STRUCT_TYPE_SOCKET_ADDRESS_IPV4));
array = g_value_get_boxed (variant);
value = g_value_array_get_nth (array, 0);
g_assert (G_VALUE_HOLDS_STRING (value));
g_assert_cmpstr (g_value_get_string (value), ==, IPV4_ADDR);
value = g_value_array_get_nth (array, 1);
g_assert (G_VALUE_HOLDS_UINT (value));
g_assert_cmpuint (g_value_get_uint (value), ==, PORT);
tp_g_value_slice_free (variant);
g_assert (g_variant_is_floating (gvariant));
other = g_variant_new_parsed ("(%s, %u)", IPV4_ADDR, (guint32) PORT);
g_assert (g_variant_equal (gvariant, other));
g_variant_unref (gvariant);
g_variant_unref (other);
}
static void
test_sockaddr_to_variant_ipv6 (void)
{
GInetAddress *hostaddr = g_inet_address_new_from_string (IPV6_ADDR);
GSocketAddress *sockaddr = g_inet_socket_address_new (hostaddr, PORT);
GValue *variant, *value;
GValueArray *array;
GVariant *gvariant, *other;
TpSocketAddressType type;
GError *error = NULL;
g_object_unref (hostaddr);
variant = tp_address_variant_from_g_socket_address (sockaddr, &type, &error);
g_assert_no_error (error);
gvariant = tp_address_g_variant_from_g_socket_address (sockaddr, &type,
&error);
g_object_unref (sockaddr);
g_assert_no_error (error);
g_assert_cmpuint (type, ==, TP_SOCKET_ADDRESS_TYPE_IPV6);
g_assert (G_VALUE_HOLDS (variant, TP_STRUCT_TYPE_SOCKET_ADDRESS_IPV6));
array = g_value_get_boxed (variant);
value = g_value_array_get_nth (array, 0);
g_assert (G_VALUE_HOLDS_STRING (value));
g_assert_cmpstr (g_value_get_string (value), ==, IPV6_ADDR);
value = g_value_array_get_nth (array, 1);
g_assert (G_VALUE_HOLDS_UINT (value));
g_assert_cmpuint (g_value_get_uint (value), ==, PORT);
tp_g_value_slice_free (variant);
g_assert (g_variant_is_floating (gvariant));
other = g_variant_new_parsed ("(%s, %u)", IPV6_ADDR, (guint32) PORT);
g_assert (g_variant_equal (gvariant, other));
g_variant_unref (gvariant);
g_variant_unref (other);
}
#ifdef HAVE_GIO_UNIX
static void
test_variant_to_sockaddr_unix (void)
{
GArray *array;
GValue value = { 0, };
GSocketAddress *sockaddr;
GUnixSocketAddress *unixaddr;
guint pathlen = strlen (UNIX_ADDR);
GError *error = NULL;
array = g_array_sized_new (TRUE, FALSE, sizeof (char), pathlen);
g_array_append_vals (array, UNIX_ADDR, pathlen);
g_value_init (&value, DBUS_TYPE_G_UCHAR_ARRAY);
g_value_take_boxed (&value, array);
sockaddr = tp_g_socket_address_from_variant (TP_SOCKET_ADDRESS_TYPE_UNIX,
&value, &error);
g_value_unset (&value);
g_assert_no_error (error);
g_assert (G_IS_UNIX_SOCKET_ADDRESS (sockaddr));
unixaddr = G_UNIX_SOCKET_ADDRESS (sockaddr);
g_assert (g_unix_socket_address_get_address_type (unixaddr) !=
G_UNIX_SOCKET_ADDRESS_ABSTRACT);
g_assert_cmpuint (g_unix_socket_address_get_path_len (unixaddr), ==, pathlen);
g_assert_cmpstr (g_unix_socket_address_get_path (unixaddr), ==, UNIX_ADDR);
g_object_unref (sockaddr);
}
static void
test_variant_to_sockaddr_abstract_unix (void)
{
GArray *array;
GValue value = { 0, };
GSocketAddress *sockaddr;
GUnixSocketAddress *unixaddr;
GError *error = NULL;
array = g_array_sized_new (TRUE, FALSE, sizeof (char), ABST_ADDR_LEN);
g_array_append_vals (array, ABST_ADDR, ABST_ADDR_LEN);
g_value_init (&value, DBUS_TYPE_G_UCHAR_ARRAY);
g_value_take_boxed (&value, array);
sockaddr = tp_g_socket_address_from_variant (
TP_SOCKET_ADDRESS_TYPE_ABSTRACT_UNIX,
&value, &error);
g_value_unset (&value);
g_assert_no_error (error);
g_assert (G_IS_UNIX_SOCKET_ADDRESS (sockaddr));
unixaddr = G_UNIX_SOCKET_ADDRESS (sockaddr);
g_assert (g_unix_socket_address_get_address_type (unixaddr) ==
G_UNIX_SOCKET_ADDRESS_ABSTRACT);
g_assert_cmpuint (g_unix_socket_address_get_path_len (unixaddr), ==,
ABST_ADDR_LEN);
g_assert (memcmp (g_unix_socket_address_get_path (unixaddr), ABST_ADDR,
ABST_ADDR_LEN) == 0);
g_object_unref (sockaddr);
}
static void
test_sockaddr_to_variant_unix (void)
{
GSocketAddress *sockaddr = g_unix_socket_address_new (UNIX_ADDR);
GValue *variant;
GArray *array;
TpSocketAddressType type;
GError *error = NULL;
variant = tp_address_variant_from_g_socket_address (sockaddr, &type, &error);
g_object_unref (sockaddr);
g_assert_no_error (error);
g_assert_cmpuint (type, ==, TP_SOCKET_ADDRESS_TYPE_UNIX);
g_assert (G_VALUE_HOLDS (variant, DBUS_TYPE_G_UCHAR_ARRAY));
array = g_value_get_boxed (variant);
g_assert_cmpuint (array->len, ==, strlen (UNIX_ADDR));
g_assert_cmpstr (array->data, ==, UNIX_ADDR);
tp_g_value_slice_free (variant);
}
static void
test_sockaddr_to_variant_abstract_unix (void)
{
GSocketAddress *sockaddr = g_unix_socket_address_new_with_type (
ABST_ADDR, ABST_ADDR_LEN, G_UNIX_SOCKET_ADDRESS_ABSTRACT);
GValue *variant;
GArray *array;
TpSocketAddressType type;
GError *error = NULL;
variant = tp_address_variant_from_g_socket_address (sockaddr, &type, &error);
g_object_unref (sockaddr);
g_assert_no_error (error);
g_assert_cmpuint (type, ==, TP_SOCKET_ADDRESS_TYPE_ABSTRACT_UNIX);
g_assert (G_VALUE_HOLDS (variant, DBUS_TYPE_G_UCHAR_ARRAY));
array = g_value_get_boxed (variant);
g_assert_cmpuint (array->len, ==, ABST_ADDR_LEN);
g_assert (memcmp (array->data, ABST_ADDR, ABST_ADDR_LEN) == 0);
tp_g_value_slice_free (variant);
}
#endif /* HAVE_GIO_UNIX */
int
main (int argc, char **argv)
{
g_type_init ();
dbus_g_type_specialized_init ();
test_variant_to_sockaddr_ipv4 ();
test_variant_to_sockaddr_ipv6 ();
test_sockaddr_to_variant_ipv4 ();
test_sockaddr_to_variant_ipv6 ();
#ifdef HAVE_GIO_UNIX
test_variant_to_sockaddr_unix ();
test_variant_to_sockaddr_abstract_unix ();
test_sockaddr_to_variant_unix ();
test_sockaddr_to_variant_abstract_unix ();
#endif /* HAVE_GIO_UNIX */
return 0;
}
|