Asterisk - The Open Source Telephony Project  18.5.0
test_stasis_channels.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Matt Jordan <[email protected]>
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 <[email protected]> \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 #include "asterisk/astobj2.h"
36 #include "asterisk/module.h"
37 #include "asterisk/stasis.h"
39 #include "asterisk/test.h"
41 #include "asterisk/channel.h"
42 
43 static const char *test_category = "/stasis/channels/";
44 
45 static void safe_channel_release(struct ast_channel *chan)
46 {
47  if (!chan) {
48  return;
49  }
50  ast_channel_release(chan);
51 }
52 
53 AST_TEST_DEFINE(channel_blob_create)
54 {
55  struct ast_channel_blob *blob;
57  RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
59  RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
60  RAII_VAR(struct ast_json *, bad_json, NULL, ast_json_unref);
61 
62  switch (cmd) {
63  case TEST_INIT:
64  info->name = __func__;
65  info->category = test_category;
66  info->summary = "Test creation of ast_channel_blob objects";
67  info->description = "Test creation of ast_channel_blob objects";
68  return AST_TEST_NOT_RUN;
69  case TEST_EXECUTE:
70  break;
71  }
72 
73  ast_test_validate(test, stasis_message_type_create("test-type", NULL, &type) == STASIS_MESSAGE_TYPE_SUCCESS);
74  chan = ast_channel_alloc(0, AST_STATE_DOWN, "100", "Alice", "100", "100", "default", NULL, NULL, 0, "TEST/Alice");
75  ast_channel_unlock(chan);
76  json = ast_json_pack("{s: s}",
77  "foo", "bar");
78 
79  /* Off nominal creation */
80  ast_channel_lock(chan);
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_channel_unlock(chan);
86  ast_test_validate(test, NULL != msg);
87  blob = stasis_message_data(msg);
88  ast_test_validate(test, NULL != blob);
89  ast_test_validate(test, NULL != blob->snapshot);
90  ast_test_validate(test, NULL != blob->blob);
91  ast_test_validate(test, type == stasis_message_type(msg));
92 
93  ast_test_validate(test, 1 == ao2_ref(msg, 0));
94  ao2_cleanup(msg);
95 
96  /* Test for global channels */
97  msg = ast_channel_blob_create(NULL, type, json);
98  ast_test_validate(test, NULL != msg);
99  blob = stasis_message_data(msg);
100  ast_test_validate(test, NULL != blob);
101  ast_test_validate(test, NULL == blob->snapshot);
102  ast_test_validate(test, NULL != blob->blob);
103  ast_test_validate(test, type == stasis_message_type(msg));
104 
105  return AST_TEST_PASS;
106 }
107 
108 AST_TEST_DEFINE(null_blob)
109 {
110  struct ast_channel_blob *blob;
112  RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
113  RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
114  RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
115  RAII_VAR(struct ast_json *, bad_json, NULL, ast_json_unref);
116 
117  switch (cmd) {
118  case TEST_INIT:
119  info->name = __func__;
120  info->category = test_category;
121  info->summary = "Test creation of ast_channel_blob objects";
122  info->description = "Test creation of ast_channel_blob objects";
123  return AST_TEST_NOT_RUN;
124  case TEST_EXECUTE:
125  break;
126  }
127 
128  ast_test_validate(test, stasis_message_type_create("test-type", NULL, &type) == STASIS_MESSAGE_TYPE_SUCCESS);
129  chan = ast_channel_alloc(0, AST_STATE_DOWN, "100", "Alice", "100", "100", "default", NULL, NULL, 0, "TEST/Alice");
130  ast_channel_unlock(chan);
131  json = ast_json_pack("{s: s}",
132  "foo", "bar");
133 
134  /* Test for single channel */
135  ast_channel_lock(chan);
136  msg = ast_channel_blob_create(chan, type, NULL);
137  ast_channel_unlock(chan);
138  ast_test_validate(test, NULL != msg);
139  blob = stasis_message_data(msg);
140  ast_test_validate(test, NULL != blob);
141  ast_test_validate(test, NULL != blob->snapshot);
142  ast_test_validate(test, ast_json_null() == blob->blob);
143  ast_test_validate(test, type == stasis_message_type(msg));
144 
145  return AST_TEST_PASS;
146 }
147 
148 AST_TEST_DEFINE(multi_channel_blob_create)
149 {
151  RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
152  RAII_VAR(struct ast_json *, bad_json, NULL, ast_json_unref);
153 
154  switch (cmd) {
155  case TEST_INIT:
156  info->name = __func__;
157  info->category = test_category;
158  info->summary = "Test creation of ast_multi_channel_blob objects";
159  info->description = "Test creation of ast_multi_channel_blob objects";
160  return AST_TEST_NOT_RUN;
161  case TEST_EXECUTE:
162  break;
163  }
164 
165  json = ast_json_pack("{s: s}",
166  "foo", "bar");
167 
168  /* Test for single channel */
170  ast_test_validate(test, NULL != blob);
171  ast_test_validate(test, NULL != ast_multi_channel_blob_get_json(blob));
172 
173  return AST_TEST_PASS;
174 }
175 
176 AST_TEST_DEFINE(multi_channel_blob_snapshots)
177 {
179  RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
180  RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
181  RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
182  RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
183  struct ast_channel_snapshot *snapshot;
184  struct ao2_container *matches;
185 
186  switch (cmd) {
187  case TEST_INIT:
188  info->name = __func__;
189  info->category = test_category;
190  info->summary = "Test creation of ast_multi_channel_blob objects";
191  info->description = "Test creation of ast_multi_channel_blob objects";
192  return AST_TEST_NOT_RUN;
193  case TEST_EXECUTE:
194  break;
195  }
196 
197  json = ast_json_pack("{s: s}",
198  "type", "test");
199  chan_alice = ast_channel_alloc(0, AST_STATE_DOWN, "100", "Alice", "100", "100", "default", NULL, NULL, 0, "TEST/Alice");
200  ast_channel_unlock(chan_alice);
201  chan_bob = ast_channel_alloc(0, AST_STATE_DOWN, "200", "Bob", "200", "200", "default", NULL, NULL, 0, "TEST/Bob");
202  ast_channel_unlock(chan_bob);
203  chan_charlie = ast_channel_alloc(0, AST_STATE_DOWN, "300", "Bob", "300", "300", "default", NULL, NULL, 0, "TEST/Charlie");
204  ast_channel_unlock(chan_charlie);
205 
206  blob = ast_multi_channel_blob_create(json);
207  ast_channel_lock(chan_alice);
209  ast_channel_unlock(chan_alice);
210  ast_channel_lock(chan_bob);
212  ast_channel_unlock(chan_bob);
213  ast_channel_lock(chan_charlie);
215  ast_channel_unlock(chan_charlie);
216 
217  /* Test for unknown role */
218  ast_test_validate(test, NULL == ast_multi_channel_blob_get_channel(blob, "Foobar"));
219 
220  /* Test for single match */
221  snapshot = ast_multi_channel_blob_get_channel(blob, "Caller");
222  ast_test_validate(test, NULL != snapshot);
223  ast_test_validate(test, 0 == strcmp("TEST/Alice", snapshot->base->name));
224 
225  /* Test for single match, multiple possibilities */
226  snapshot = ast_multi_channel_blob_get_channel(blob, "Peer");
227  ast_test_validate(test, NULL != snapshot);
228  ast_test_validate(test, 0 != strcmp("TEST/Alice", snapshot->base->name));
229 
230  /* Multi-match */
231  matches = ast_multi_channel_blob_get_channels(blob, "Peer");
232  ast_test_validate(test, NULL != matches);
233  ast_test_validate(test, 2 == ao2_container_count(matches));
234  snapshot = ao2_find(matches, "TEST/Bob", OBJ_KEY);
235  ast_test_validate(test, NULL != snapshot);
236  ao2_cleanup(snapshot);
237  snapshot = ao2_find(matches, "TEST/Charlie", OBJ_KEY);
238  ast_test_validate(test, NULL != snapshot);
239  ao2_cleanup(snapshot);
240  ast_test_validate(test, 1 == ao2_ref(matches, 0));
241  ao2_cleanup(matches);
242 
243  return AST_TEST_PASS;
244 }
245 
246 AST_TEST_DEFINE(channel_snapshot_json)
247 {
248  RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
249  RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
250  RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
251  RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
252  RAII_VAR(struct ast_json *, actual, NULL, ast_json_unref);
253 
254  switch (cmd) {
255  case TEST_INIT:
256  info->name = __func__;
257  info->category = test_category;
258  info->summary = "Test creation of ast_channel_blob objects";
259  info->description = "Test creation of ast_channel_blob objects";
260  return AST_TEST_NOT_RUN;
261  case TEST_EXECUTE:
262  break;
263  }
264 
265  ast_test_validate(test, NULL == ast_channel_snapshot_to_json(NULL, NULL));
266 
267  chan = ast_channel_alloc(0, AST_STATE_DOWN, "cid_num", "cid_name", "acctcode", "exten", "context", NULL, NULL, 0, "TEST/name");
268  ast_channel_unlock(chan);
269  ast_test_validate(test, NULL != chan);
270  ast_channel_lock(chan);
271  snapshot = ast_channel_snapshot_create(chan);
272  ast_channel_unlock(chan);
273  ast_test_validate(test, NULL != snapshot);
274 
275  actual = ast_channel_snapshot_to_json(snapshot, NULL);
276  expected = ast_json_pack("{ s: s, s: s, s: s, s: s,"
277  " s: { s: s, s: s, s: i, s: s, s: s },"
278  " s: { s: s, s: s },"
279  " s: { s: s, s: s },"
280  " s: s"
281  " s: o"
282  "}",
283  "name", "TEST/name",
284  "state", "Down",
285  "accountcode", "acctcode",
286  "id", ast_channel_uniqueid(chan),
287  "dialplan",
288  "context", "context",
289  "exten", "exten",
290  "priority", 1,
291  "app_name", "",
292  "app_data", "",
293  "caller",
294  "name", "cid_name",
295  "number", "cid_num",
296  "connected",
297  "name", "",
298  "number", "",
299  "language", "en",
300  "creationtime",
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 
329 }
330 
331 AST_MODULE_INFO(ASTERISK_GPL_KEY, 0, "Stasis Channel Testing",
332  .support_level = AST_MODULE_SUPPORT_CORE,
333  .load = load_module,
334  .unload = unload_module
335 );
static const char type[]
Definition: chan_ooh323.c:109
#define ast_channel_lock(chan)
Definition: channel.h:2945
Main Channel structure associated with a channel.
struct ast_channel_snapshot_base * base
Asterisk main include file. File version handling, generic pbx functions.
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
#define OBJ_KEY
Definition: astobj2.h:1155
struct ast_json * blob
struct ast_channel_snapshot * snapshot
struct ast_channel * ast_channel_release(struct ast_channel *chan)
Unlink and release reference to a channel.
Definition: channel.c:1584
Stasis Message Bus API. See Stasis Message Bus API for detailed documentation.
Structure representing a snapshot of channel state.
Test Framework API.
static void safe_channel_release(struct ast_channel *chan)
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
struct stasis_message_type * stasis_message_type(const struct stasis_message *msg)
Get the message type for a stasis_message.
enum stasis_message_type_result stasis_message_type_create(const char *name, struct stasis_message_vtable *vtable, struct stasis_message_type **result)
Create a new message type.
#define NULL
Definition: resample.c:96
struct ast_channel_snapshot * ast_channel_snapshot_create(struct ast_channel *chan)
Generate a snapshot of the channel state. This is an ao2 object, so ao2_cleanup() to deallocate...
struct ast_json * ast_multi_channel_blob_get_json(struct ast_multi_channel_blob *obj)
Retrieve the JSON blob from a ast_multi_channel_blob. Returned ast_json is still owned by obj...
Blob of data associated with a channel.
struct ast_json * ast_json_null(void)
Get the JSON null value.
Definition: json.c:248
static const char * test_category
General Asterisk PBX channel definitions.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
void ast_multi_channel_blob_add_channel(struct ast_multi_channel_blob *obj, const char *role, struct ast_channel_snapshot *snapshot)
Add a ast_channel_snapshot to a ast_multi_channel_blob object.
AST_TEST_DEFINE(channel_blob_create)
#define ao2_ref(o, delta)
Definition: astobj2.h:464
struct ast_json * ast_json_timeval(const struct timeval tv, const char *zone)
Construct a timeval as JSON.
Definition: json.c:649
struct ao2_container * ast_multi_channel_blob_get_channels(struct ast_multi_channel_blob *obj, const char *role)
Retrieve all channel snapshots associated with a specific role from a ast_multi_channel_blob.
const char * ast_channel_uniqueid(const struct ast_channel *chan)
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128
def info(msg)
struct timeval ast_channel_creationtime(struct ast_channel *chan)
struct stasis_message * ast_channel_blob_create(struct ast_channel *chan, struct stasis_message_type *type, struct ast_json *blob)
Creates a ast_channel_blob message.
#define ast_channel_unlock(chan)
Definition: channel.h:2946
struct ast_json * ast_channel_snapshot_to_json(const struct ast_channel_snapshot *snapshot, const struct stasis_message_sanitizer *sanitize)
Build a JSON object from a ast_channel_snapshot.
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
int ast_json_equal(const struct ast_json *lhs, const struct ast_json *rhs)
Compare two JSON objects.
Definition: json.c:347
struct ast_channel_snapshot * ast_multi_channel_blob_get_channel(struct ast_multi_channel_blob *obj, const char *role)
Retrieve a channel snapshot associated with a specific role from a ast_multi_channel_blob.
A multi channel blob data structure for multi_channel_blob stasis messages.
Abstract JSON element (object, array, string, int, ...).
Generic container type.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
#define ast_channel_alloc(needqueue, state, cid_num, cid_name, acctcode, exten, context, assignedids, requestor, amaflag,...)
Create a channel structure.
Definition: channel.h:1259
Asterisk module definitions.
static int unload_module(void)
struct ast_multi_channel_blob * ast_multi_channel_blob_create(struct ast_json *blob)
Create a ast_multi_channel_blob suitable for a stasis_message.
static int load_module(void)
const ast_string_field name