Initial release including wifi display based on gst-rtsp-server-1.4.1
[platform/upstream/gstreamer.git] / tests / check / gst / addresspool.c
1 /* GStreamer
2  * Copyright (C) 2012 Wim Taymans <wim.taymans@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include <gst/check/gstcheck.h>
21
22 #include <rtsp-address-pool.h>
23
24 GST_START_TEST (test_pool)
25 {
26   GstRTSPAddressPool *pool;
27   GstRTSPAddress *addr, *addr2, *addr3;
28   GstRTSPAddressPoolResult res;
29
30   pool = gst_rtsp_address_pool_new ();
31
32   fail_if (gst_rtsp_address_pool_add_range (pool,
33           "233.252.0.1", "233.252.0.0", 5000, 5010, 1));
34   fail_if (gst_rtsp_address_pool_add_range (pool,
35           "233.252.0.1", "::1", 5000, 5010, 1));
36   fail_if (gst_rtsp_address_pool_add_range (pool,
37           "233.252.0.1", "ff02::1", 5000, 5010, 1));
38   fail_if (gst_rtsp_address_pool_add_range (pool,
39           "233.252.0.1.1", "233.252.0.1", 5000, 5010, 1));
40   fail_if (gst_rtsp_address_pool_add_range (pool,
41           "233.252.0.1", "233.252.0.1.1", 5000, 5010, 1));
42   ASSERT_CRITICAL (gst_rtsp_address_pool_add_range (pool,
43           "233.252.0.0", "233.252.0.1", 5010, 5000, 1));
44
45   fail_unless (gst_rtsp_address_pool_add_range (pool,
46           "233.252.0.0", "233.252.0.255", 5000, 5010, 1));
47   fail_unless (gst_rtsp_address_pool_add_range (pool,
48           "233.255.0.0", "233.255.0.0", 5000, 5010, 1));
49   fail_unless (gst_rtsp_address_pool_add_range (pool,
50           "233.255.0.0", "233.255.0.0", 5020, 5020, 1));
51
52   /* should fail, we can't allocate a block of 256 ports */
53   addr = gst_rtsp_address_pool_acquire_address (pool,
54       GST_RTSP_ADDRESS_FLAG_MULTICAST, 256);
55   fail_unless (addr == NULL);
56
57   addr = gst_rtsp_address_pool_acquire_address (pool,
58       GST_RTSP_ADDRESS_FLAG_MULTICAST, 2);
59   fail_unless (addr != NULL);
60
61   addr2 = gst_rtsp_address_copy (addr);
62
63   gst_rtsp_address_free (addr2);
64   gst_rtsp_address_free (addr);
65
66   addr = gst_rtsp_address_pool_acquire_address (pool,
67       GST_RTSP_ADDRESS_FLAG_MULTICAST, 4);
68   fail_unless (addr != NULL);
69
70   /* Will fail because pool is NULL */
71   ASSERT_CRITICAL (gst_rtsp_address_pool_clear (NULL));
72
73   /* will fail because an address is allocated */
74   ASSERT_CRITICAL (gst_rtsp_address_pool_clear (pool));
75
76   gst_rtsp_address_free (addr);
77
78   gst_rtsp_address_pool_clear (pool);
79
80   /* start with odd port to make sure we are allocated address
81    * starting with even port
82    */
83   fail_unless (gst_rtsp_address_pool_add_range (pool,
84           "FF11:DB8::1", "FF11:DB8::1", 5001, 5003, 1));
85
86   addr = gst_rtsp_address_pool_acquire_address (pool,
87       GST_RTSP_ADDRESS_FLAG_IPV6 | GST_RTSP_ADDRESS_FLAG_EVEN_PORT |
88       GST_RTSP_ADDRESS_FLAG_MULTICAST, 2);
89   fail_unless (addr != NULL);
90   fail_unless (addr->port == 5002);
91   fail_unless (!g_ascii_strcasecmp (addr->address, "FF11:DB8::1"));
92
93   /* Will fail becuse there is only one IPv6 address left */
94   addr2 = gst_rtsp_address_pool_acquire_address (pool,
95       GST_RTSP_ADDRESS_FLAG_IPV6 | GST_RTSP_ADDRESS_FLAG_MULTICAST, 2);
96   fail_unless (addr2 == NULL);
97
98   /* Will fail because the only IPv6 address left has an odd port */
99   addr2 = gst_rtsp_address_pool_acquire_address (pool,
100       GST_RTSP_ADDRESS_FLAG_IPV6 | GST_RTSP_ADDRESS_FLAG_EVEN_PORT |
101       GST_RTSP_ADDRESS_FLAG_MULTICAST, 1);
102   fail_unless (addr2 == NULL);
103
104   addr2 = gst_rtsp_address_pool_acquire_address (pool,
105       GST_RTSP_ADDRESS_FLAG_IPV4 | GST_RTSP_ADDRESS_FLAG_MULTICAST, 1);
106   fail_unless (addr2 == NULL);
107
108   gst_rtsp_address_free (addr);
109
110   gst_rtsp_address_pool_clear (pool);
111
112   fail_unless (gst_rtsp_address_pool_add_range (pool,
113           "233.252.0.0", "233.252.0.255", 5000, 5002, 1));
114
115   addr = gst_rtsp_address_pool_acquire_address (pool,
116       GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST, 2);
117   fail_unless (addr != NULL);
118   fail_unless (addr->port == 5000);
119   fail_unless (!strcmp (addr->address, "233.252.0.0"));
120
121   addr2 = gst_rtsp_address_pool_acquire_address (pool,
122       GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST, 2);
123   fail_unless (addr2 != NULL);
124   fail_unless (addr2->port == 5000);
125   fail_unless (!strcmp (addr2->address, "233.252.0.1"));
126
127   gst_rtsp_address_free (addr);
128   gst_rtsp_address_free (addr2);
129
130   addr = gst_rtsp_address_pool_acquire_address (pool,
131       GST_RTSP_ADDRESS_FLAG_IPV6 | GST_RTSP_ADDRESS_FLAG_MULTICAST, 1);
132   fail_unless (addr == NULL);
133
134   gst_rtsp_address_pool_clear (pool);
135
136   fail_unless (gst_rtsp_address_pool_add_range (pool,
137           "233.252.1.1", "233.252.1.1", 5000, 5001, 1));
138
139   res = gst_rtsp_address_pool_reserve_address (pool, "233.252.1.1", 5000, 3,
140       1, &addr);
141   fail_unless (res == GST_RTSP_ADDRESS_POOL_ERANGE);
142   fail_unless (addr == NULL);
143
144   res = gst_rtsp_address_pool_reserve_address (pool, "233.252.1.2", 5000, 2,
145       1, &addr);
146   fail_unless (res == GST_RTSP_ADDRESS_POOL_ERANGE);
147   fail_unless (addr == NULL);
148
149   res = gst_rtsp_address_pool_reserve_address (pool, "233.252.1.1", 500, 2, 1,
150       &addr);
151   fail_unless (res == GST_RTSP_ADDRESS_POOL_ERANGE);
152   fail_unless (addr == NULL);
153
154   res = gst_rtsp_address_pool_reserve_address (pool, "233.252.1.1", 5000, 2,
155       2, &addr);
156   fail_unless (res == GST_RTSP_ADDRESS_POOL_ERANGE);
157   fail_unless (addr == NULL);
158
159   res = gst_rtsp_address_pool_reserve_address (pool, "2000::1", 5000, 2, 2,
160       &addr);
161   fail_unless (res == GST_RTSP_ADDRESS_POOL_EINVAL);
162   fail_unless (addr == NULL);
163
164   res = gst_rtsp_address_pool_reserve_address (pool, "ff02::1", 5000, 2, 2,
165       &addr);
166   fail_unless (res == GST_RTSP_ADDRESS_POOL_ERANGE);
167   fail_unless (addr == NULL);
168
169   res = gst_rtsp_address_pool_reserve_address (pool, "1.1", 5000, 2, 2, &addr);
170   fail_unless (res == GST_RTSP_ADDRESS_POOL_EINVAL);
171   fail_unless (addr == NULL);
172
173   res = gst_rtsp_address_pool_reserve_address (pool, "233.252.1.1", 5000, 2,
174       1, &addr);
175   fail_unless (res == GST_RTSP_ADDRESS_POOL_OK);
176   fail_unless (addr != NULL);
177   fail_unless (addr->port == 5000);
178   fail_unless (!strcmp (addr->address, "233.252.1.1"));
179
180   res = gst_rtsp_address_pool_reserve_address (pool, "233.252.1.1", 5000, 2,
181       1, &addr2);
182   fail_unless (res == GST_RTSP_ADDRESS_POOL_ERESERVED);
183   fail_unless (addr2 == NULL);
184
185   gst_rtsp_address_free (addr);
186   gst_rtsp_address_pool_clear (pool);
187
188   fail_unless (gst_rtsp_address_pool_add_range (pool,
189           "233.252.1.1", "233.252.1.3", 5000, 5001, 1));
190
191   res = gst_rtsp_address_pool_reserve_address (pool, "233.252.1.1", 5000, 2,
192       1, &addr);
193   fail_unless (addr != NULL);
194   fail_unless (addr->port == 5000);
195   fail_unless (!strcmp (addr->address, "233.252.1.1"));
196
197   res = gst_rtsp_address_pool_reserve_address (pool, "233.252.1.3", 5000, 2,
198       1, &addr2);
199   fail_unless (addr2 != NULL);
200   fail_unless (addr2->port == 5000);
201   fail_unless (!strcmp (addr2->address, "233.252.1.3"));
202
203   addr3 = gst_rtsp_address_pool_acquire_address (pool,
204       GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST, 2);
205   fail_unless (addr3 != NULL);
206   fail_unless (addr3->port == 5000);
207   fail_unless (!strcmp (addr3->address, "233.252.1.2"));
208
209   fail_unless (gst_rtsp_address_pool_acquire_address (pool,
210           GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST, 2)
211       == NULL);
212
213   gst_rtsp_address_free (addr);
214   gst_rtsp_address_free (addr2);
215   gst_rtsp_address_free (addr3);
216   gst_rtsp_address_pool_clear (pool);
217
218   fail_unless (gst_rtsp_address_pool_add_range (pool,
219           "233.252.1.1", "233.252.1.1", 5000, 5001, 1));
220   fail_if (gst_rtsp_address_pool_has_unicast_addresses (pool));
221   fail_unless (gst_rtsp_address_pool_add_range (pool,
222           "192.168.1.1", "192.168.1.1", 6000, 6001, 0));
223   fail_unless (gst_rtsp_address_pool_has_unicast_addresses (pool));
224
225   addr = gst_rtsp_address_pool_acquire_address (pool,
226       GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST, 2);
227   fail_unless (addr != NULL);
228   fail_unless (addr->port == 5000);
229   fail_unless (!strcmp (addr->address, "233.252.1.1"));
230   gst_rtsp_address_free (addr);
231
232   addr = gst_rtsp_address_pool_acquire_address (pool,
233       GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_UNICAST, 2);
234   fail_unless (addr != NULL);
235   fail_unless (addr->port == 6000);
236   fail_unless (!strcmp (addr->address, "192.168.1.1"));
237   gst_rtsp_address_free (addr);
238
239   fail_unless (gst_rtsp_address_pool_add_range (pool,
240           GST_RTSP_ADDRESS_POOL_ANY_IPV4, GST_RTSP_ADDRESS_POOL_ANY_IPV4, 5000,
241           5001, 0));
242   res =
243       gst_rtsp_address_pool_reserve_address (pool, "192.168.0.1", 5000, 1, 0,
244       &addr);
245   fail_unless (res == GST_RTSP_ADDRESS_POOL_ERANGE);
246   res =
247       gst_rtsp_address_pool_reserve_address (pool, "0.0.0.0", 5000, 1, 0,
248       &addr);
249   fail_unless (res == GST_RTSP_ADDRESS_POOL_OK);
250   gst_rtsp_address_free (addr);
251   gst_rtsp_address_pool_clear (pool);
252
253   /* Error case 2. Using ANY as min address makes it possible to allocate the
254    * same address twice */
255   fail_unless (gst_rtsp_address_pool_add_range (pool,
256           GST_RTSP_ADDRESS_POOL_ANY_IPV4, "255.255.255.255", 5000, 5001, 0));
257   res =
258       gst_rtsp_address_pool_reserve_address (pool, "192.168.0.1", 5000, 1, 0,
259       &addr);
260   fail_unless (res == GST_RTSP_ADDRESS_POOL_OK);
261   res =
262       gst_rtsp_address_pool_reserve_address (pool, "192.168.0.1", 5000, 1, 0,
263       &addr2);
264   fail_unless (res == GST_RTSP_ADDRESS_POOL_ERESERVED);
265   gst_rtsp_address_free (addr);
266   gst_rtsp_address_pool_clear (pool);
267
268   g_object_unref (pool);
269 }
270
271 GST_END_TEST;
272
273 static Suite *
274 rtspaddresspool_suite (void)
275 {
276   Suite *s = suite_create ("rtspaddresspool");
277   TCase *tc = tcase_create ("general");
278
279   suite_add_tcase (s, tc);
280   tcase_set_timeout (tc, 20);
281   tcase_add_test (tc, test_pool);
282
283   return s;
284 }
285
286 GST_CHECK_MAIN (rtspaddresspool);