Fix UPGRADE.txt Due To Merging From Branch 11
[asterisk/asterisk.git] / tests / test_stasis_channels.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Matt Jordan <mjordan@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*!
20  * \file \brief Test Stasis Channel messages and objects
21  *
22  * \author\verbatim Matt Jordan <mjordan@digium.com> \endverbatim
23  *
24  * \ingroup tests
25  */
26
27 /*** MODULEINFO
28         <depend>TEST_FRAMEWORK</depend>
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include "asterisk/astobj2.h"
37 #include "asterisk/module.h"
38 #include "asterisk/stasis.h"
39 #include "asterisk/stasis_message_router.h"
40 #include "asterisk/test.h"
41 #include "asterisk/stasis_channels.h"
42 #include "asterisk/channel.h"
43
44 static const char *test_category = "/stasis/channels/";
45
46 static void safe_channel_release(struct ast_channel *chan)
47 {
48         if (!chan) {
49                 return;
50         }
51         ast_channel_release(chan);
52 }
53
54 AST_TEST_DEFINE(channel_blob_create)
55 {
56         struct ast_channel_blob *blob;
57         RAII_VAR(struct stasis_message_type *, type, NULL, ao2_cleanup);
58         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
59         RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
60         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
61         RAII_VAR(struct ast_json *, bad_json, NULL, ast_json_unref);
62
63         switch (cmd) {
64         case TEST_INIT:
65                 info->name = __func__;
66                 info->category = test_category;
67                 info->summary = "Test creation of ast_channel_blob objects";
68                 info->description = "Test creation of ast_channel_blob objects";
69                 return AST_TEST_NOT_RUN;
70         case TEST_EXECUTE:
71                 break;
72         }
73
74         type = stasis_message_type_create("test-type", NULL);
75         chan = ast_channel_alloc(0, AST_STATE_DOWN, "100", "Alice", "100", "100", "default", NULL, 0, "TEST/Alice");
76         json = ast_json_pack("{s: s}",
77                      "foo", "bar");
78
79         /* Off nominal creation */
80         ast_test_validate(test, NULL == ast_channel_blob_create(chan, NULL, json));
81
82         /* Test for single channel */
83         msg = ast_channel_blob_create(chan, type, json);
84         ast_test_validate(test, NULL != msg);
85         blob = stasis_message_data(msg);
86         ast_test_validate(test, NULL != blob);
87         ast_test_validate(test, NULL != blob->snapshot);
88         ast_test_validate(test, NULL != blob->blob);
89         ast_test_validate(test, type == stasis_message_type(msg));
90
91         ast_test_validate(test, 1 == ao2_ref(msg, 0));
92         ao2_cleanup(msg);
93
94         /* Test for global channels */
95         msg = ast_channel_blob_create(NULL, type, json);
96         ast_test_validate(test, NULL != msg);
97         blob = stasis_message_data(msg);
98         ast_test_validate(test, NULL != blob);
99         ast_test_validate(test, NULL == blob->snapshot);
100         ast_test_validate(test, NULL != blob->blob);
101         ast_test_validate(test, type == stasis_message_type(msg));
102
103         return AST_TEST_PASS;
104 }
105
106 AST_TEST_DEFINE(null_blob)
107 {
108         struct ast_channel_blob *blob;
109         RAII_VAR(struct stasis_message_type *, type, NULL, ao2_cleanup);
110         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
111         RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
112         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
113         RAII_VAR(struct ast_json *, bad_json, NULL, ast_json_unref);
114
115         switch (cmd) {
116         case TEST_INIT:
117                 info->name = __func__;
118                 info->category = test_category;
119                 info->summary = "Test creation of ast_channel_blob objects";
120                 info->description = "Test creation of ast_channel_blob objects";
121                 return AST_TEST_NOT_RUN;
122         case TEST_EXECUTE:
123                 break;
124         }
125
126         type = stasis_message_type_create("test-type", NULL);
127         chan = ast_channel_alloc(0, AST_STATE_DOWN, "100", "Alice", "100", "100", "default", NULL, 0, "TEST/Alice");
128         json = ast_json_pack("{s: s}",
129                      "foo", "bar");
130
131         /* Test for single channel */
132         msg = ast_channel_blob_create(chan, type, NULL);
133         ast_test_validate(test, NULL != msg);
134         blob = stasis_message_data(msg);
135         ast_test_validate(test, NULL != blob);
136         ast_test_validate(test, NULL != blob->snapshot);
137         ast_test_validate(test, ast_json_null() == blob->blob);
138         ast_test_validate(test, type == stasis_message_type(msg));
139
140         return AST_TEST_PASS;
141 }
142
143 AST_TEST_DEFINE(multi_channel_blob_create)
144 {
145         RAII_VAR(struct ast_multi_channel_blob *, blob, NULL, ao2_cleanup);
146         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
147         RAII_VAR(struct ast_json *, bad_json, NULL, ast_json_unref);
148
149         switch (cmd) {
150         case TEST_INIT:
151                 info->name = __func__;
152                 info->category = test_category;
153                 info->summary = "Test creation of ast_multi_channel_blob objects";
154                 info->description = "Test creation of ast_multi_channel_blob objects";
155                 return AST_TEST_NOT_RUN;
156         case TEST_EXECUTE:
157                 break;
158         }
159
160         json = ast_json_pack("{s: s}",
161                      "foo", "bar");
162
163         /* Test for single channel */
164         blob = ast_multi_channel_blob_create(json);
165         ast_test_validate(test, NULL != blob);
166         ast_test_validate(test, NULL != ast_multi_channel_blob_get_json(blob));
167
168         return AST_TEST_PASS;
169 }
170
171 AST_TEST_DEFINE(multi_channel_blob_snapshots)
172 {
173         RAII_VAR(struct ast_multi_channel_blob *, blob, NULL, ao2_cleanup);
174         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
175         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
176         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
177         RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
178         struct ast_channel_snapshot *snapshot;
179         struct ao2_container *matches;
180
181         switch (cmd) {
182         case TEST_INIT:
183                 info->name = __func__;
184                 info->category = test_category;
185                 info->summary = "Test creation of ast_multi_channel_blob objects";
186                 info->description = "Test creation of ast_multi_channel_blob objects";
187                 return AST_TEST_NOT_RUN;
188         case TEST_EXECUTE:
189                 break;
190         }
191
192         json = ast_json_pack("{s: s}",
193                      "type", "test");
194         chan_alice = ast_channel_alloc(0, AST_STATE_DOWN, "100", "Alice", "100", "100", "default", NULL, 0, "TEST/Alice");
195         chan_bob = ast_channel_alloc(0, AST_STATE_DOWN, "200", "Bob", "200", "200", "default", NULL, 0, "TEST/Bob");
196         chan_charlie = ast_channel_alloc(0, AST_STATE_DOWN, "300", "Bob", "300", "300", "default", NULL, 0, "TEST/Charlie");
197
198         blob = ast_multi_channel_blob_create(json);
199         ast_multi_channel_blob_add_channel(blob, "Caller", ast_channel_snapshot_create(chan_alice));
200         ast_multi_channel_blob_add_channel(blob, "Peer", ast_channel_snapshot_create(chan_bob));
201         ast_multi_channel_blob_add_channel(blob, "Peer", ast_channel_snapshot_create(chan_charlie));
202
203         /* Test for unknown role */
204         ast_test_validate(test, NULL == ast_multi_channel_blob_get_channel(blob, "Foobar"));
205
206         /* Test for single match */
207         snapshot = ast_multi_channel_blob_get_channel(blob, "Caller");
208         ast_test_validate(test, NULL != snapshot);
209         ast_test_validate(test, 0 == strcmp("TEST/Alice", snapshot->name));
210
211         /* Test for single match, multiple possibilities */
212         snapshot = ast_multi_channel_blob_get_channel(blob, "Peer");
213         ast_test_validate(test, NULL != snapshot);
214         ast_test_validate(test, 0 != strcmp("TEST/Alice", snapshot->name));
215
216         /* Multi-match */
217         matches = ast_multi_channel_blob_get_channels(blob, "Peer");
218         ast_test_validate(test, NULL != matches);
219         ast_test_validate(test, 2 == ao2_container_count(matches));
220         snapshot = ao2_find(matches, "TEST/Bob", OBJ_KEY);
221         ast_test_validate(test, NULL != snapshot);
222         ao2_cleanup(snapshot);
223         snapshot = ao2_find(matches, "TEST/Charlie", OBJ_KEY);
224         ast_test_validate(test, NULL != snapshot);
225         ao2_cleanup(snapshot);
226         ast_test_validate(test, 1 == ao2_ref(matches, 0));
227         ao2_cleanup(matches);
228
229         return AST_TEST_PASS;
230 }
231
232 AST_TEST_DEFINE(channel_snapshot_json)
233 {
234         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
235         RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
236         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
237         RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
238         RAII_VAR(struct ast_json *, actual, NULL, ast_json_unref);
239
240         switch (cmd) {
241         case TEST_INIT:
242                 info->name = __func__;
243                 info->category = test_category;
244                 info->summary = "Test creation of ast_channel_blob objects";
245                 info->description = "Test creation of ast_channel_blob objects";
246                 return AST_TEST_NOT_RUN;
247         case TEST_EXECUTE:
248                 break;
249         }
250
251         ast_test_validate(test, NULL == ast_channel_snapshot_to_json(NULL));
252
253         chan = ast_channel_alloc(0, AST_STATE_DOWN, "cid_num", "cid_name", "acctcode", "exten", "context", NULL, 0, "TEST/name");
254         ast_test_validate(test, NULL != chan);
255         snapshot = ast_channel_snapshot_create(chan);
256         ast_test_validate(test, NULL != snapshot);
257
258         actual = ast_channel_snapshot_to_json(snapshot);
259         expected = ast_json_pack("{ s: s, s: s, s: s, s: s,"
260                                  "  s: { s: s, s: s, s: i },"
261                                  "  s: { s: s, s: s },"
262                                  "  s: { s: s, s: s },"
263                                  "  s: o"
264                                  "}",
265                                  "name", "TEST/name",
266                                  "state", "Down",
267                                  "accountcode", "acctcode",
268                                  "id", ast_channel_uniqueid(chan),
269                                  "dialplan",
270                                  "context", "context",
271                                  "exten", "exten",
272                                  "priority", 1,
273                                  "caller",
274                                  "name", "cid_name",
275                                  "number", "cid_num",
276                                  "connected",
277                                  "name", "",
278                                  "number", "",
279                                  "creationtime",
280                                  ast_json_timeval(
281                                          ast_channel_creationtime(chan), NULL));
282
283         ast_test_validate(test, ast_json_equal(expected, actual));
284
285         return AST_TEST_PASS;
286 }
287
288 static int unload_module(void)
289 {
290         AST_TEST_UNREGISTER(channel_blob_create);
291         AST_TEST_UNREGISTER(null_blob);
292         AST_TEST_UNREGISTER(multi_channel_blob_create);
293         AST_TEST_UNREGISTER(multi_channel_blob_snapshots);
294         AST_TEST_UNREGISTER(channel_snapshot_json);
295
296         return 0;
297 }
298
299 static int load_module(void)
300 {
301         AST_TEST_REGISTER(channel_blob_create);
302         AST_TEST_REGISTER(null_blob);
303         AST_TEST_REGISTER(multi_channel_blob_create);
304         AST_TEST_REGISTER(multi_channel_blob_snapshots);
305         AST_TEST_REGISTER(channel_snapshot_json);
306
307         return AST_MODULE_LOAD_SUCCESS;
308 }
309
310 AST_MODULE_INFO(ASTERISK_GPL_KEY, 0, "Stasis Channel Testing",
311                 .load = load_module,
312                 .unload = unload_module
313         );