tests: Check the passing around of a RTSPAddressPool
[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;
28
29   pool = gst_rtsp_address_pool_new ();
30
31   fail_if (gst_rtsp_address_pool_add_range (pool,
32           "233.252.0.1", "233.252.0.0", 5000, 5010, 1));
33   fail_if (gst_rtsp_address_pool_add_range (pool,
34           "233.252.0.1", "::1", 5000, 5010, 1));
35   fail_if (gst_rtsp_address_pool_add_range (pool,
36           "233.252.0.1.1", "233.252.0.1", 5000, 5010, 1));
37   fail_if (gst_rtsp_address_pool_add_range (pool,
38           "233.252.0.1", "233.252.0.1.1", 5000, 5010, 1));
39   ASSERT_CRITICAL (gst_rtsp_address_pool_add_range (pool,
40           "233.252.0.0", "233.252.0.1", 5010, 5000, 1));
41
42   fail_unless (gst_rtsp_address_pool_add_range (pool,
43           "233.252.0.0", "233.252.0.255", 5000, 5010, 1));
44   fail_unless (gst_rtsp_address_pool_add_range (pool,
45           "233.255.0.0", "233.255.0.0", 5000, 5010, 1));
46   fail_unless (gst_rtsp_address_pool_add_range (pool,
47           "233.255.0.0", "233.255.0.0", 5020, 5020, 1));
48
49   /* should fail, we can't allocate a block of 256 ports */
50   addr = gst_rtsp_address_pool_acquire_address (pool, 0, 256);
51   fail_unless (addr == NULL);
52
53   addr = gst_rtsp_address_pool_acquire_address (pool, 0, 2);
54   fail_unless (addr != NULL);
55
56   addr2 = gst_rtsp_address_copy (addr);
57
58   gst_rtsp_address_free (addr2);
59   gst_rtsp_address_free (addr);
60
61   addr = gst_rtsp_address_pool_acquire_address (pool, 0, 4);
62   fail_unless (addr != NULL);
63
64   /* Will fail because pool is NULL */
65   ASSERT_CRITICAL (gst_rtsp_address_pool_clear (NULL));
66
67   /* will fail because an address is allocated */
68   ASSERT_CRITICAL (gst_rtsp_address_pool_clear (pool));
69
70   gst_rtsp_address_free (addr);
71
72   gst_rtsp_address_pool_clear (pool);
73
74   /* start with odd port to make sure we are allocated address
75    * starting with even port
76    */
77   fail_unless (gst_rtsp_address_pool_add_range (pool,
78           "2001:DB8::1", "2001:DB8::1", 5001, 5003, 1));
79
80   addr = gst_rtsp_address_pool_acquire_address (pool,
81       GST_RTSP_ADDRESS_FLAG_IPV6 | GST_RTSP_ADDRESS_FLAG_EVEN_PORT, 2);
82   fail_unless (addr != NULL);
83   fail_unless (addr->port == 5002);
84
85   /* Will fail becuse there is only one IPv6 address left */
86   addr2 = gst_rtsp_address_pool_acquire_address (pool,
87       GST_RTSP_ADDRESS_FLAG_IPV6, 2);
88   fail_unless (addr2 == NULL);
89
90   /* Will fail because the only IPv6 address left has an odd port */
91   addr2 = gst_rtsp_address_pool_acquire_address (pool,
92       GST_RTSP_ADDRESS_FLAG_IPV6 | GST_RTSP_ADDRESS_FLAG_EVEN_PORT, 1);
93   fail_unless (addr2 == NULL);
94
95   gst_rtsp_address_free (addr);
96
97   gst_rtsp_address_pool_clear (pool);
98
99   fail_unless (gst_rtsp_address_pool_add_range (pool,
100           "233.252.0.0", "233.252.0.255", 5000, 5002, 1));
101
102   addr = gst_rtsp_address_pool_acquire_address (pool,
103       GST_RTSP_ADDRESS_FLAG_EVEN_PORT, 2);
104   fail_unless (addr != NULL);
105   fail_unless (addr->port == 5000);
106
107   addr2 = gst_rtsp_address_pool_acquire_address (pool,
108       GST_RTSP_ADDRESS_FLAG_EVEN_PORT, 2);
109   fail_unless (addr2 != NULL);
110   fail_unless (addr2->port == 5000);
111
112   gst_rtsp_address_free (addr);
113   gst_rtsp_address_free (addr2);
114
115   g_object_unref (pool);
116 }
117
118 GST_END_TEST;
119
120 static Suite *
121 rtspaddresspool_suite (void)
122 {
123   Suite *s = suite_create ("rtspaddresspool");
124   TCase *tc = tcase_create ("general");
125
126   suite_add_tcase (s, tc);
127   tcase_set_timeout (tc, 20);
128   tcase_add_test (tc, test_pool);
129
130   return s;
131 }
132
133 GST_CHECK_MAIN (rtspaddresspool);