2 * Copyright (C) 2012 Wim Taymans <wim.taymans@gmail.com>
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.
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.
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.
20 #include <gst/check/gstcheck.h>
22 #include <rtsp-address-pool.h>
24 GST_START_TEST (test_pool)
26 GstRTSPAddressPool *pool;
27 GstRTSPAddress *addr, *addr2, *addr3;
28 GstRTSPAddressPoolResult res;
30 pool = gst_rtsp_address_pool_new ();
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));
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));
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);
57 addr = gst_rtsp_address_pool_acquire_address (pool,
58 GST_RTSP_ADDRESS_FLAG_MULTICAST, 2);
59 fail_unless (addr != NULL);
61 addr2 = gst_rtsp_address_copy (addr);
63 gst_rtsp_address_free (addr2);
64 gst_rtsp_address_free (addr);
66 addr = gst_rtsp_address_pool_acquire_address (pool,
67 GST_RTSP_ADDRESS_FLAG_MULTICAST, 4);
68 fail_unless (addr != NULL);
70 /* Will fail because pool is NULL */
71 ASSERT_CRITICAL (gst_rtsp_address_pool_clear (NULL));
73 /* will fail because an address is allocated */
74 ASSERT_CRITICAL (gst_rtsp_address_pool_clear (pool));
76 gst_rtsp_address_free (addr);
78 gst_rtsp_address_pool_clear (pool);
80 /* start with odd port to make sure we are allocated address
81 * starting with even port
83 fail_unless (gst_rtsp_address_pool_add_range (pool,
84 "FF11:DB8::1", "FF11:DB8::1", 5001, 5003, 1));
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"));
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);
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);
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);
108 gst_rtsp_address_free (addr);
110 gst_rtsp_address_pool_clear (pool);
112 fail_unless (gst_rtsp_address_pool_add_range (pool,
113 "233.252.0.0", "233.252.0.255", 5000, 5002, 1));
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"));
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"));
127 gst_rtsp_address_free (addr);
128 gst_rtsp_address_free (addr2);
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);
134 gst_rtsp_address_pool_clear (pool);
136 fail_unless (gst_rtsp_address_pool_add_range (pool,
137 "233.252.1.1", "233.252.1.1", 5000, 5001, 1));
139 res = gst_rtsp_address_pool_reserve_address (pool, "233.252.1.1", 5000, 3,
141 fail_unless (res == GST_RTSP_ADDRESS_POOL_ERANGE);
142 fail_unless (addr == NULL);
144 res = gst_rtsp_address_pool_reserve_address (pool, "233.252.1.2", 5000, 2,
146 fail_unless (res == GST_RTSP_ADDRESS_POOL_ERANGE);
147 fail_unless (addr == NULL);
149 res = gst_rtsp_address_pool_reserve_address (pool, "233.252.1.1", 500, 2, 1,
151 fail_unless (res == GST_RTSP_ADDRESS_POOL_ERANGE);
152 fail_unless (addr == NULL);
154 res = gst_rtsp_address_pool_reserve_address (pool, "233.252.1.1", 5000, 2,
156 fail_unless (res == GST_RTSP_ADDRESS_POOL_ERANGE);
157 fail_unless (addr == NULL);
159 res = gst_rtsp_address_pool_reserve_address (pool, "2000::1", 5000, 2, 2,
161 fail_unless (res == GST_RTSP_ADDRESS_POOL_EINVAL);
162 fail_unless (addr == NULL);
164 res = gst_rtsp_address_pool_reserve_address (pool, "ff02::1", 5000, 2, 2,
166 fail_unless (res == GST_RTSP_ADDRESS_POOL_ERANGE);
167 fail_unless (addr == NULL);
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);
173 res = gst_rtsp_address_pool_reserve_address (pool, "233.252.1.1", 5000, 2,
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"));
180 res = gst_rtsp_address_pool_reserve_address (pool, "233.252.1.1", 5000, 2,
182 fail_unless (res == GST_RTSP_ADDRESS_POOL_ERESERVED);
183 fail_unless (addr2 == NULL);
185 gst_rtsp_address_free (addr);
186 gst_rtsp_address_pool_clear (pool);
188 fail_unless (gst_rtsp_address_pool_add_range (pool,
189 "233.252.1.1", "233.252.1.3", 5000, 5001, 1));
191 res = gst_rtsp_address_pool_reserve_address (pool, "233.252.1.1", 5000, 2,
193 fail_unless (addr != NULL);
194 fail_unless (addr->port == 5000);
195 fail_unless (!strcmp (addr->address, "233.252.1.1"));
197 res = gst_rtsp_address_pool_reserve_address (pool, "233.252.1.3", 5000, 2,
199 fail_unless (addr2 != NULL);
200 fail_unless (addr2->port == 5000);
201 fail_unless (!strcmp (addr2->address, "233.252.1.3"));
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"));
209 fail_unless (gst_rtsp_address_pool_acquire_address (pool,
210 GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST, 2)
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);
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));
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);
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);
239 fail_unless (gst_rtsp_address_pool_add_range (pool,
240 GST_RTSP_ADDRESS_POOL_ANY_IPV4, GST_RTSP_ADDRESS_POOL_ANY_IPV4, 5000,
243 gst_rtsp_address_pool_reserve_address (pool, "192.168.0.1", 5000, 1, 0,
245 fail_unless (res == GST_RTSP_ADDRESS_POOL_ERANGE);
247 gst_rtsp_address_pool_reserve_address (pool, "0.0.0.0", 5000, 1, 0,
249 fail_unless (res == GST_RTSP_ADDRESS_POOL_OK);
250 gst_rtsp_address_free (addr);
251 gst_rtsp_address_pool_clear (pool);
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));
258 gst_rtsp_address_pool_reserve_address (pool, "192.168.0.1", 5000, 1, 0,
260 fail_unless (res == GST_RTSP_ADDRESS_POOL_OK);
262 gst_rtsp_address_pool_reserve_address (pool, "192.168.0.1", 5000, 1, 0,
264 fail_unless (res == GST_RTSP_ADDRESS_POOL_ERESERVED);
265 gst_rtsp_address_free (addr);
266 gst_rtsp_address_pool_clear (pool);
268 g_object_unref (pool);
274 rtspaddresspool_suite (void)
276 Suite *s = suite_create ("rtspaddresspool");
277 TCase *tc = tcase_create ("general");
279 suite_add_tcase (s, tc);
280 tcase_set_timeout (tc, 20);
281 tcase_add_test (tc, test_pool);
286 GST_CHECK_MAIN (rtspaddresspool);