de445458c95a74d1c08c8f6bcfc666060fabfa87
[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         ast_channel_unlock(chan);
77         json = ast_json_pack("{s: s}",
78                      "foo", "bar");
79
80         /* Off nominal creation */
81         ast_test_validate(test, NULL == ast_channel_blob_create(chan, NULL, json));
82
83         /* Test for single channel */
84         msg = ast_channel_blob_create(chan, type, json);
85         ast_test_validate(test, NULL != msg);
86         blob = stasis_message_data(msg);
87         ast_test_validate(test, NULL != blob);
88         ast_test_validate(test, NULL != blob->snapshot);
89         ast_test_validate(test, NULL != blob->blob);
90         ast_test_validate(test, type == stasis_message_type(msg));
91
92         ast_test_validate(test, 1 == ao2_ref(msg, 0));
93         ao2_cleanup(msg);
94
95         /* Test for global channels */
96         msg = ast_channel_blob_create(NULL, type, json);
97         ast_test_validate(test, NULL != msg);
98         blob = stasis_message_data(msg);
99         ast_test_validate(test, NULL != blob);
100         ast_test_validate(test, NULL == blob->snapshot);
101         ast_test_validate(test, NULL != blob->blob);
102         ast_test_validate(test, type == stasis_message_type(msg));
103
104         return AST_TEST_PASS;
105 }
106
107 AST_TEST_DEFINE(null_blob)
108 {
109         struct ast_channel_blob *blob;
110         RAII_VAR(struct stasis_message_type *, type, NULL, ao2_cleanup);
111         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
112         RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
113         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
114         RAII_VAR(struct ast_json *, bad_json, NULL, ast_json_unref);
115
116         switch (cmd) {
117         case TEST_INIT:
118                 info->name = __func__;
119                 info->category = test_category;
120                 info->summary = "Test creation of ast_channel_blob objects";
121                 info->description = "Test creation of ast_channel_blob objects";
122                 return AST_TEST_NOT_RUN;
123         case TEST_EXECUTE:
124                 break;
125         }
126
127         type = stasis_message_type_create("test-type", NULL);
128         chan = ast_channel_alloc(0, AST_STATE_DOWN, "100", "Alice", "100", "100", "default", NULL, 0, "TEST/Alice");
129         ast_channel_unlock(chan);
130         json = ast_json_pack("{s: s}",
131                      "foo", "bar");
132
133         /* Test for single channel */
134         msg = ast_channel_blob_create(chan, type, NULL);
135         ast_test_validate(test, NULL != msg);
136         blob = stasis_message_data(msg);
137         ast_test_validate(test, NULL != blob);
138         ast_test_validate(test, NULL != blob->snapshot);
139         ast_test_validate(test, ast_json_null() == blob->blob);
140         ast_test_validate(test, type == stasis_message_type(msg));
141
142         return AST_TEST_PASS;
143 }
144
145 AST_TEST_DEFINE(multi_channel_blob_create)
146 {
147         RAII_VAR(struct ast_multi_channel_blob *, blob, NULL, ao2_cleanup);
148         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
149         RAII_VAR(struct ast_json *, bad_json, NULL, ast_json_unref);
150
151         switch (cmd) {
152         case TEST_INIT:
153                 info->name = __func__;
154                 info->category = test_category;
155                 info->summary = "Test creation of ast_multi_channel_blob objects";
156                 info->description = "Test creation of ast_multi_channel_blob objects";
157                 return AST_TEST_NOT_RUN;
158         case TEST_EXECUTE:
159                 break;
160         }
161
162         json = ast_json_pack("{s: s}",
163                      "foo", "bar");
164
165         /* Test for single channel */
166         blob = ast_multi_channel_blob_create(json);
167         ast_test_validate(test, NULL != blob);
168         ast_test_validate(test, NULL != ast_multi_channel_blob_get_json(blob));
169
170         return AST_TEST_PASS;
171 }
172
173 AST_TEST_DEFINE(multi_channel_blob_snapshots)
174 {
175         RAII_VAR(struct ast_multi_channel_blob *, blob, NULL, ao2_cleanup);
176         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
177         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
178         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
179         RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
180         struct ast_channel_snapshot *snapshot;
181         struct ao2_container *matches;
182
183         switch (cmd) {
184         case TEST_INIT:
185                 info->name = __func__;
186                 info->category = test_category;
187                 info->summary = "Test creation of ast_multi_channel_blob objects";
188                 info->description = "Test creation of ast_multi_channel_blob objects";
189                 return AST_TEST_NOT_RUN;
190         case TEST_EXECUTE:
191                 break;
192         }
193
194         json = ast_json_pack("{s: s}",
195                      "type", "test");
196         chan_alice = ast_channel_alloc(0, AST_STATE_DOWN, "100", "Alice", "100", "100", "default", NULL, 0, "TEST/Alice");
197         ast_channel_unlock(chan_alice);
198         chan_bob = ast_channel_alloc(0, AST_STATE_DOWN, "200", "Bob", "200", "200", "default", NULL, 0, "TEST/Bob");
199         ast_channel_unlock(chan_bob);
200         chan_charlie = ast_channel_alloc(0, AST_STATE_DOWN, "300", "Bob", "300", "300", "default", NULL, 0, "TEST/Charlie");
201         ast_channel_unlock(chan_charlie);
202
203         blob = ast_multi_channel_blob_create(json);
204         ast_multi_channel_blob_add_channel(blob, "Caller", ast_channel_snapshot_create(chan_alice));
205         ast_multi_channel_blob_add_channel(blob, "Peer", ast_channel_snapshot_create(chan_bob));
206         ast_multi_channel_blob_add_channel(blob, "Peer", ast_channel_snapshot_create(chan_charlie));
207
208         /* Test for unknown role */
209         ast_test_validate(test, NULL == ast_multi_channel_blob_get_channel(blob, "Foobar"));
210
211         /* Test for single match */
212         snapshot = ast_multi_channel_blob_get_channel(blob, "Caller");
213         ast_test_validate(test, NULL != snapshot);
214         ast_test_validate(test, 0 == strcmp("TEST/Alice", snapshot->name));
215
216         /* Test for single match, multiple possibilities */
217         snapshot = ast_multi_channel_blob_get_channel(blob, "Peer");
218         ast_test_validate(test, NULL != snapshot);
219         ast_test_validate(test, 0 != strcmp("TEST/Alice", snapshot->name));
220
221         /* Multi-match */
222         matches = ast_multi_channel_blob_get_channels(blob, "Peer");
223         ast_test_validate(test, NULL != matches);
224         ast_test_validate(test, 2 == ao2_container_count(matches));
225         snapshot = ao2_find(matches, "TEST/Bob", OBJ_KEY);
226         ast_test_validate(test, NULL != snapshot);
227         ao2_cleanup(snapshot);
228         snapshot = ao2_find(matches, "TEST/Charlie", OBJ_KEY);
229         ast_test_validate(test, NULL != snapshot);
230         ao2_cleanup(snapshot);
231         ast_test_validate(test, 1 == ao2_ref(matches, 0));
232         ao2_cleanup(matches);
233
234         return AST_TEST_PASS;
235 }
236
237 AST_TEST_DEFINE(channel_snapshot_json)
238 {
239         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
240         RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
241         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
242         RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
243         RAII_VAR(struct ast_json *, actual, NULL, ast_json_unref);
244
245         switch (cmd) {
246         case TEST_INIT:
247                 info->name = __func__;
248                 info->category = test_category;
249                 info->summary = "Test creation of ast_channel_blob objects";
250                 info->description = "Test creation of ast_channel_blob objects";
251                 return AST_TEST_NOT_RUN;
252         case TEST_EXECUTE:
253                 break;
254         }
255
256         ast_test_validate(test, NULL == ast_channel_snapshot_to_json(NULL, NULL));
257
258         chan = ast_channel_alloc(0, AST_STATE_DOWN, "cid_num", "cid_name", "acctcode", "exten", "context", NULL, 0, "TEST/name");
259         ast_channel_unlock(chan);
260         ast_test_validate(test, NULL != chan);
261         snapshot = ast_channel_snapshot_create(chan);
262         ast_test_validate(test, NULL != snapshot);
263
264         actual = ast_channel_snapshot_to_json(snapshot, NULL);
265         expected = ast_json_pack("{ s: s, s: s, s: s, s: s,"
266                                  "  s: { s: s, s: s, s: i },"
267                                  "  s: { s: s, s: s },"
268                                  "  s: { s: s, s: s },"
269                                  "  s: o"
270                                  "}",
271                                  "name", "TEST/name",
272                                  "state", "Down",
273                                  "accountcode", "acctcode",
274                                  "id", ast_channel_uniqueid(chan),
275                                  "dialplan",
276                                  "context", "context",
277                                  "exten", "exten",
278                                  "priority", 1,
279                                  "caller",
280                                  "name", "cid_name",
281                                  "number", "cid_num",
282                                  "connected",
283                                  "name", "",
284                                  "number", "",
285                                  "creationtime",
286                                  ast_json_timeval(
287                                          ast_channel_creationtime(chan), NULL));
288
289         ast_test_validate(test, ast_json_equal(expected, actual));
290
291         return AST_TEST_PASS;
292 }
293
294 static int unload_module(void)
295 {
296         AST_TEST_UNREGISTER(channel_blob_create);
297         AST_TEST_UNREGISTER(null_blob);
298         AST_TEST_UNREGISTER(multi_channel_blob_create);
299         AST_TEST_UNREGISTER(multi_channel_blob_snapshots);
300         AST_TEST_UNREGISTER(channel_snapshot_json);
301
302         return 0;
303 }
304
305 static int load_module(void)
306 {
307         AST_TEST_REGISTER(channel_blob_create);
308         AST_TEST_REGISTER(null_blob);
309         AST_TEST_REGISTER(multi_channel_blob_create);
310         AST_TEST_REGISTER(multi_channel_blob_snapshots);
311         AST_TEST_REGISTER(channel_snapshot_json);
312
313         return AST_MODULE_LOAD_SUCCESS;
314 }
315
316 AST_MODULE_INFO(ASTERISK_GPL_KEY, 0, "Stasis Channel Testing",
317                 .load = load_module,
318                 .unload = unload_module
319         );