doxygen: Fix doxygen errors
[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
21  * \brief Test Stasis Channel messages and objects
22  *
23  * \author\verbatim Matt Jordan <mjordan@digium.com> \endverbatim
24  *
25  * \ingroup tests
26  */
27
28 /*** MODULEINFO
29         <depend>TEST_FRAMEWORK</depend>
30         <support_level>core</support_level>
31  ***/
32
33 #include "asterisk.h"
34
35 ASTERISK_REGISTER_FILE()
36
37 #include "asterisk/astobj2.h"
38 #include "asterisk/module.h"
39 #include "asterisk/stasis.h"
40 #include "asterisk/stasis_message_router.h"
41 #include "asterisk/test.h"
42 #include "asterisk/stasis_channels.h"
43 #include "asterisk/channel.h"
44
45 static const char *test_category = "/stasis/channels/";
46
47 static void safe_channel_release(struct ast_channel *chan)
48 {
49         if (!chan) {
50                 return;
51         }
52         ast_channel_release(chan);
53 }
54
55 AST_TEST_DEFINE(channel_blob_create)
56 {
57         struct ast_channel_blob *blob;
58         RAII_VAR(struct stasis_message_type *, type, NULL, ao2_cleanup);
59         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
60         RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
61         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
62         RAII_VAR(struct ast_json *, bad_json, NULL, ast_json_unref);
63
64         switch (cmd) {
65         case TEST_INIT:
66                 info->name = __func__;
67                 info->category = test_category;
68                 info->summary = "Test creation of ast_channel_blob objects";
69                 info->description = "Test creation of ast_channel_blob objects";
70                 return AST_TEST_NOT_RUN;
71         case TEST_EXECUTE:
72                 break;
73         }
74
75         ast_test_validate(test, stasis_message_type_create("test-type", NULL, &type) == STASIS_MESSAGE_TYPE_SUCCESS);
76         chan = ast_channel_alloc(0, AST_STATE_DOWN, "100", "Alice", "100", "100", "default", NULL, NULL, 0, "TEST/Alice");
77         ast_channel_unlock(chan);
78         json = ast_json_pack("{s: s}",
79                      "foo", "bar");
80
81         /* Off nominal creation */
82         ast_channel_lock(chan);
83         ast_test_validate(test, NULL == ast_channel_blob_create(chan, NULL, json));
84
85         /* Test for single channel */
86         msg = ast_channel_blob_create(chan, type, json);
87         ast_channel_unlock(chan);
88         ast_test_validate(test, NULL != msg);
89         blob = stasis_message_data(msg);
90         ast_test_validate(test, NULL != blob);
91         ast_test_validate(test, NULL != blob->snapshot);
92         ast_test_validate(test, NULL != blob->blob);
93         ast_test_validate(test, type == stasis_message_type(msg));
94
95         ast_test_validate(test, 1 == ao2_ref(msg, 0));
96         ao2_cleanup(msg);
97
98         /* Test for global channels */
99         msg = ast_channel_blob_create(NULL, type, json);
100         ast_test_validate(test, NULL != msg);
101         blob = stasis_message_data(msg);
102         ast_test_validate(test, NULL != blob);
103         ast_test_validate(test, NULL == blob->snapshot);
104         ast_test_validate(test, NULL != blob->blob);
105         ast_test_validate(test, type == stasis_message_type(msg));
106
107         return AST_TEST_PASS;
108 }
109
110 AST_TEST_DEFINE(null_blob)
111 {
112         struct ast_channel_blob *blob;
113         RAII_VAR(struct stasis_message_type *, type, NULL, ao2_cleanup);
114         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
115         RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
116         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
117         RAII_VAR(struct ast_json *, bad_json, NULL, ast_json_unref);
118
119         switch (cmd) {
120         case TEST_INIT:
121                 info->name = __func__;
122                 info->category = test_category;
123                 info->summary = "Test creation of ast_channel_blob objects";
124                 info->description = "Test creation of ast_channel_blob objects";
125                 return AST_TEST_NOT_RUN;
126         case TEST_EXECUTE:
127                 break;
128         }
129
130         ast_test_validate(test, stasis_message_type_create("test-type", NULL, &type) == STASIS_MESSAGE_TYPE_SUCCESS);
131         chan = ast_channel_alloc(0, AST_STATE_DOWN, "100", "Alice", "100", "100", "default", NULL, NULL, 0, "TEST/Alice");
132         ast_channel_unlock(chan);
133         json = ast_json_pack("{s: s}",
134                      "foo", "bar");
135
136         /* Test for single channel */
137         ast_channel_lock(chan);
138         msg = ast_channel_blob_create(chan, type, NULL);
139         ast_channel_unlock(chan);
140         ast_test_validate(test, NULL != msg);
141         blob = stasis_message_data(msg);
142         ast_test_validate(test, NULL != blob);
143         ast_test_validate(test, NULL != blob->snapshot);
144         ast_test_validate(test, ast_json_null() == blob->blob);
145         ast_test_validate(test, type == stasis_message_type(msg));
146
147         return AST_TEST_PASS;
148 }
149
150 AST_TEST_DEFINE(multi_channel_blob_create)
151 {
152         RAII_VAR(struct ast_multi_channel_blob *, blob, NULL, ao2_cleanup);
153         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
154         RAII_VAR(struct ast_json *, bad_json, NULL, ast_json_unref);
155
156         switch (cmd) {
157         case TEST_INIT:
158                 info->name = __func__;
159                 info->category = test_category;
160                 info->summary = "Test creation of ast_multi_channel_blob objects";
161                 info->description = "Test creation of ast_multi_channel_blob objects";
162                 return AST_TEST_NOT_RUN;
163         case TEST_EXECUTE:
164                 break;
165         }
166
167         json = ast_json_pack("{s: s}",
168                      "foo", "bar");
169
170         /* Test for single channel */
171         blob = ast_multi_channel_blob_create(json);
172         ast_test_validate(test, NULL != blob);
173         ast_test_validate(test, NULL != ast_multi_channel_blob_get_json(blob));
174
175         return AST_TEST_PASS;
176 }
177
178 AST_TEST_DEFINE(multi_channel_blob_snapshots)
179 {
180         RAII_VAR(struct ast_multi_channel_blob *, blob, NULL, ao2_cleanup);
181         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
182         RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
183         RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
184         RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
185         struct ast_channel_snapshot *snapshot;
186         struct ao2_container *matches;
187
188         switch (cmd) {
189         case TEST_INIT:
190                 info->name = __func__;
191                 info->category = test_category;
192                 info->summary = "Test creation of ast_multi_channel_blob objects";
193                 info->description = "Test creation of ast_multi_channel_blob objects";
194                 return AST_TEST_NOT_RUN;
195         case TEST_EXECUTE:
196                 break;
197         }
198
199         json = ast_json_pack("{s: s}",
200                      "type", "test");
201         chan_alice = ast_channel_alloc(0, AST_STATE_DOWN, "100", "Alice", "100", "100", "default", NULL, NULL, 0, "TEST/Alice");
202         ast_channel_unlock(chan_alice);
203         chan_bob = ast_channel_alloc(0, AST_STATE_DOWN, "200", "Bob", "200", "200", "default", NULL, NULL, 0, "TEST/Bob");
204         ast_channel_unlock(chan_bob);
205         chan_charlie = ast_channel_alloc(0, AST_STATE_DOWN, "300", "Bob", "300", "300", "default", NULL, NULL, 0, "TEST/Charlie");
206         ast_channel_unlock(chan_charlie);
207
208         blob = ast_multi_channel_blob_create(json);
209         ast_channel_lock(chan_alice);
210         ast_multi_channel_blob_add_channel(blob, "Caller", ast_channel_snapshot_create(chan_alice));
211         ast_channel_unlock(chan_alice);
212         ast_channel_lock(chan_bob);
213         ast_multi_channel_blob_add_channel(blob, "Peer", ast_channel_snapshot_create(chan_bob));
214         ast_channel_unlock(chan_bob);
215         ast_channel_lock(chan_charlie);
216         ast_multi_channel_blob_add_channel(blob, "Peer", ast_channel_snapshot_create(chan_charlie));
217         ast_channel_unlock(chan_charlie);
218
219         /* Test for unknown role */
220         ast_test_validate(test, NULL == ast_multi_channel_blob_get_channel(blob, "Foobar"));
221
222         /* Test for single match */
223         snapshot = ast_multi_channel_blob_get_channel(blob, "Caller");
224         ast_test_validate(test, NULL != snapshot);
225         ast_test_validate(test, 0 == strcmp("TEST/Alice", snapshot->name));
226
227         /* Test for single match, multiple possibilities */
228         snapshot = ast_multi_channel_blob_get_channel(blob, "Peer");
229         ast_test_validate(test, NULL != snapshot);
230         ast_test_validate(test, 0 != strcmp("TEST/Alice", snapshot->name));
231
232         /* Multi-match */
233         matches = ast_multi_channel_blob_get_channels(blob, "Peer");
234         ast_test_validate(test, NULL != matches);
235         ast_test_validate(test, 2 == ao2_container_count(matches));
236         snapshot = ao2_find(matches, "TEST/Bob", OBJ_KEY);
237         ast_test_validate(test, NULL != snapshot);
238         ao2_cleanup(snapshot);
239         snapshot = ao2_find(matches, "TEST/Charlie", OBJ_KEY);
240         ast_test_validate(test, NULL != snapshot);
241         ao2_cleanup(snapshot);
242         ast_test_validate(test, 1 == ao2_ref(matches, 0));
243         ao2_cleanup(matches);
244
245         return AST_TEST_PASS;
246 }
247
248 AST_TEST_DEFINE(channel_snapshot_json)
249 {
250         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
251         RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
252         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
253         RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
254         RAII_VAR(struct ast_json *, actual, NULL, ast_json_unref);
255
256         switch (cmd) {
257         case TEST_INIT:
258                 info->name = __func__;
259                 info->category = test_category;
260                 info->summary = "Test creation of ast_channel_blob objects";
261                 info->description = "Test creation of ast_channel_blob objects";
262                 return AST_TEST_NOT_RUN;
263         case TEST_EXECUTE:
264                 break;
265         }
266
267         ast_test_validate(test, NULL == ast_channel_snapshot_to_json(NULL, NULL));
268
269         chan = ast_channel_alloc(0, AST_STATE_DOWN, "cid_num", "cid_name", "acctcode", "exten", "context", NULL, NULL, 0, "TEST/name");
270         ast_channel_unlock(chan);
271         ast_test_validate(test, NULL != chan);
272         ast_channel_lock(chan);
273         snapshot = ast_channel_snapshot_create(chan);
274         ast_channel_unlock(chan);
275         ast_test_validate(test, NULL != snapshot);
276
277         actual = ast_channel_snapshot_to_json(snapshot, NULL);
278         expected = ast_json_pack("{ s: s, s: s, s: s, s: s,"
279                                  "  s: { s: s, s: s, s: i },"
280                                  "  s: { s: s, s: s },"
281                                  "  s: { s: s, s: s },"
282                                  "  s: s"
283                                  "  s: o"
284                                  "}",
285                                  "name", "TEST/name",
286                                  "state", "Down",
287                                  "accountcode", "acctcode",
288                                  "id", ast_channel_uniqueid(chan),
289                                  "dialplan",
290                                  "context", "context",
291                                  "exten", "exten",
292                                  "priority", 1,
293                                  "caller",
294                                  "name", "cid_name",
295                                  "number", "cid_num",
296                                  "connected",
297                                  "name", "",
298                                  "number", "",
299                                  "language", "en",
300                                  "creationtime",
301                                  ast_json_timeval(
302                                          ast_channel_creationtime(chan), NULL));
303
304         ast_test_validate(test, ast_json_equal(expected, actual));
305
306         return AST_TEST_PASS;
307 }
308
309 static int unload_module(void)
310 {
311         AST_TEST_UNREGISTER(channel_blob_create);
312         AST_TEST_UNREGISTER(null_blob);
313         AST_TEST_UNREGISTER(multi_channel_blob_create);
314         AST_TEST_UNREGISTER(multi_channel_blob_snapshots);
315         AST_TEST_UNREGISTER(channel_snapshot_json);
316
317         return 0;
318 }
319
320 static int load_module(void)
321 {
322         AST_TEST_REGISTER(channel_blob_create);
323         AST_TEST_REGISTER(null_blob);
324         AST_TEST_REGISTER(multi_channel_blob_create);
325         AST_TEST_REGISTER(multi_channel_blob_snapshots);
326         AST_TEST_REGISTER(channel_snapshot_json);
327
328         return AST_MODULE_LOAD_SUCCESS;
329 }
330
331 AST_MODULE_INFO(ASTERISK_GPL_KEY, 0, "Stasis Channel Testing",
332         .load = load_module,
333         .unload = unload_module
334 );