Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2013, Digium, Inc.
*
* Matt Jordan <mjordan@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*!
* \file
* \brief CDR unit tests
*
* \author Matt Jordan <mjordan@digium.com>
*
*/
/*** MODULEINFO
<depend>TEST_FRAMEWORK</depend>
<support_level>core</support_level>
***/
#include "asterisk.h"
#include <math.h>
#include "asterisk/module.h"
#include "asterisk/test.h"
#include "asterisk/cdr.h"
#include "asterisk/linkedlists.h"
#include "asterisk/chanvars.h"
#include "asterisk/utils.h"
#include "asterisk/causes.h"
#include "asterisk/time.h"
#include "asterisk/stasis_channels.h"
#include "asterisk/stasis_bridges.h"
#include "asterisk/format_cache.h"
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#define EPSILON 0.001
#define TEST_CATEGORY "/main/cdr/"
#define MOCK_CDR_BACKEND "mock_cdr_backend"
#define CHANNEL_TECH_NAME "CDRTestChannel"
/*! \brief A placeholder for Asterisk's 'real' CDR configuration */
static struct ast_cdr_config *saved_config;
/*! \brief A configuration suitable for 'normal' CDRs */
static struct ast_cdr_config debug_cdr_config = {
.settings.flags = CDR_ENABLED | CDR_DEBUG,
};
/*! \brief A configuration suitable for CDRs with unanswered records */
static struct ast_cdr_config unanswered_cdr_config = {
.settings.flags = CDR_ENABLED | CDR_UNANSWERED | CDR_DEBUG,
};
/*! \brief A configuration suitable for CDRs with congestion enabled */
static struct ast_cdr_config congestion_cdr_config = {
.settings.flags = CDR_ENABLED | CDR_UNANSWERED | CDR_DEBUG | CDR_CONGESTION,
};
/*! \brief Macro to swap a configuration out from the CDR engine. This should be
* used at the beginning of each test to set the needed configuration for that
* test.
*/
#define SWAP_CONFIG(ao2_config, template) do { \
*(ao2_config) = (template); \
ast_cdr_set_config((ao2_config)); \
} while (0)
/*! \brief A linked list of received CDR entries from the engine */
static AST_LIST_HEAD(, test_cdr_entry) actual_cdr_entries = AST_LIST_HEAD_INIT_VALUE;
/*! \brief The Mock CDR backend condition wait */
static ast_cond_t mock_cdr_cond;
/*! \brief A channel technology used for the unit tests */
static struct ast_channel_tech test_cdr_chan_tech = {
.type = CHANNEL_TECH_NAME,
.description = "Mock channel technology for CDR tests",
};
struct test_cdr_entry {
struct ast_cdr *cdr;
AST_LIST_ENTRY(test_cdr_entry) list;
};
/*! \brief The number of CDRs the mock backend has received */
static int global_mock_cdr_count;
/*! \internal
* \brief Callback function for the mock CDR backend
*
* This function 'processes' a dispatched CDR record by adding it to the
* \ref actual_cdr_entries list. When a test completes, it can verify the
* expected records against this list of actual CDRs created by the engine.
*
* \param cdr The public CDR object created by the engine
*
* \retval -1 on error
* \retval 0 on success
*/
static int mock_cdr_backend_cb(struct ast_cdr *cdr)
{
struct ast_cdr *cdr_copy, *cdr_prev = NULL;
struct ast_cdr *mock_cdr = NULL;
struct test_cdr_entry *cdr_wrapper;
cdr_wrapper = ast_calloc(1, sizeof(*cdr_wrapper));
if (!cdr_wrapper) {
return -1;
}
for (; cdr; cdr = cdr->next) {
struct ast_var_t *var_entry, *var_copy;
cdr_copy = ast_calloc(1, sizeof(*cdr_copy));
if (!cdr_copy) {
return -1;
}
*cdr_copy = *cdr;
cdr_copy->varshead.first = NULL;
cdr_copy->varshead.last = NULL;
cdr_copy->next = NULL;
AST_LIST_TRAVERSE(&cdr->varshead, var_entry, entries) {
var_copy = ast_var_assign(var_entry->name, var_entry->value);
if (!var_copy) {
return -1;
}
AST_LIST_INSERT_TAIL(&cdr_copy->varshead, var_copy, entries);
}
if (!mock_cdr) {
mock_cdr = cdr_copy;
}
if (cdr_prev) {
cdr_prev->next = cdr_copy;
}
cdr_prev = cdr_copy;
}
cdr_wrapper->cdr = mock_cdr;
AST_LIST_LOCK(&actual_cdr_entries);
AST_LIST_INSERT_TAIL(&actual_cdr_entries, cdr_wrapper, list);
global_mock_cdr_count++;
ast_cond_signal(&mock_cdr_cond);
AST_LIST_UNLOCK(&actual_cdr_entries);
return 0;
}
/*! \internal
* \brief Remove all entries from \ref actual_cdr_entries
*/
static void clear_mock_cdr_backend(void)
{
struct test_cdr_entry *cdr_wrapper;
AST_LIST_LOCK(&actual_cdr_entries);
while ((cdr_wrapper = AST_LIST_REMOVE_HEAD(&actual_cdr_entries, list))) {
ast_cdr_free(cdr_wrapper->cdr);
ast_free(cdr_wrapper);
}
global_mock_cdr_count = 0;
AST_LIST_UNLOCK(&actual_cdr_entries);
}
/*! \brief Verify a string field. This will set the test status result to fail;
* as such, it assumes that (a) test is the test object variable, and (b) that
* a return variable res exists.
*/
#define VERIFY_STRING_FIELD(field, actual, expected) do { \
if (strcmp((actual)->field, (expected)->field)) { \
ast_test_status_update(test, "Field %s failed: actual %s, expected %s\n", #field, (actual)->field, (expected)->field); \
ast_test_set_result(test, AST_TEST_FAIL); \
res = AST_TEST_FAIL; \
} } while (0)
/*! \brief Verify a numeric field. This will set the test status result to fail;
* as such, it assumes that (a) test is the test object variable, and (b) that
* a return variable res exists.
*/
#define VERIFY_NUMERIC_FIELD(field, actual, expected) do { \
if ((actual)->field != (expected)->field) { \
ast_test_status_update(test, "Field %s failed: actual %ld, expected %ld\n", #field, (long)(actual)->field, (long)(expected)->field); \
ast_test_set_result(test, AST_TEST_FAIL); \
res = AST_TEST_FAIL; \
} } while (0)
/*! \brief Verify a time field. This will set the test status result to fail;
* as such, it assumes that (a) test is the test object variable, and (b) that
* a return variable res exists.
*/
#define VERIFY_TIME_VALUE(field, actual) do { \
if (ast_tvzero((actual)->field)) { \
ast_test_status_update(test, "Field %s failed: should not be 0\n", #field); \
ast_test_set_result(test, AST_TEST_FAIL); \
res = AST_TEST_FAIL; \
} } while (0)
/*! \brief Alice's Caller ID */
#define ALICE_CALLERID { .id.name.str = "Alice", .id.name.valid = 1, .id.number.str = "100", .id.number.valid = 1, }
/*! \brief Bob's Caller ID */
#define BOB_CALLERID { .id.name.str = "Bob", .id.name.valid = 1, .id.number.str = "200", .id.number.valid = 1, }
/*! \brief Charlie's Caller ID */
#define CHARLIE_CALLERID { .id.name.str = "Charlie", .id.name.valid = 1, .id.number.str = "300", .id.number.valid = 1, }
/*! \brief David's Caller ID */
#define DAVID_CALLERID { .id.name.str = "David", .id.name.valid = 1, .id.number.str = "400", .id.number.valid = 1, }
/*! \brief Copy the linkedid and uniqueid from a channel to an expected CDR */
#define COPY_IDS(channel_var, expected_record) do { \
ast_copy_string((expected_record)->uniqueid, ast_channel_uniqueid((channel_var)), sizeof((expected_record)->uniqueid)); \
ast_copy_string((expected_record)->linkedid, ast_channel_linkedid((channel_var)), sizeof((expected_record)->linkedid)); \
} while (0)
/*! \brief Set ulaw format on channel */
#define SET_FORMATS(chan) do {\
struct ast_format_cap *caps;\
caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);\
ast_format_cap_append(caps, ast_format_ulaw, 0);\
ast_channel_nativeformats_set((chan), caps);\
ast_channel_set_writeformat((chan), ast_format_ulaw);\
ast_channel_set_rawwriteformat((chan), ast_format_ulaw);\
ast_channel_set_readformat((chan), ast_format_ulaw);\
ast_channel_set_rawreadformat((chan), ast_format_ulaw);\
ao2_ref(caps, -1);\
} while (0)
/*! \brief Create a \ref test_cdr_chan_tech for Alice, and set the expected
* CDR records' linkedid and uniqueid. */
#define CREATE_ALICE_CHANNEL(channel_var, caller_id, expected_record) do { \
(channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, "100", "Alice", "100", "100", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/Alice"); \
ast_channel_set_caller((channel_var), (caller_id), NULL); \
ast_copy_string((expected_record)->uniqueid, ast_channel_uniqueid((channel_var)), sizeof((expected_record)->uniqueid)); \
ast_copy_string((expected_record)->linkedid, ast_channel_linkedid((channel_var)), sizeof((expected_record)->linkedid)); \
ast_channel_unlock((channel_var)); \
} while (0)
/*! \brief Create a \ref test_cdr_chan_tech for Bob, and set the expected
* CDR records' linkedid and uniqueid. */
#define CREATE_BOB_CHANNEL(channel_var, caller_id, expected_record) do { \
(channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, "200", "Bob", "200", "200", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/Bob"); \
ast_channel_set_caller((channel_var), (caller_id), NULL); \
ast_copy_string((expected_record)->uniqueid, ast_channel_uniqueid((channel_var)), sizeof((expected_record)->uniqueid)); \
ast_copy_string((expected_record)->linkedid, ast_channel_linkedid((channel_var)), sizeof((expected_record)->linkedid)); \
ast_channel_unlock((channel_var)); \
} while (0)
/*! \brief Create a \ref test_cdr_chan_tech for Charlie, and set the expected
* CDR records' linkedid and uniqueid. */
#define CREATE_CHARLIE_CHANNEL(channel_var, caller_id, expected_record) do { \
(channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, "300", "Charlie", "300", "300", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/Charlie"); \
ast_channel_set_caller((channel_var), (caller_id), NULL); \
ast_copy_string((expected_record)->uniqueid, ast_channel_uniqueid((channel_var)), sizeof((expected_record)->uniqueid)); \
ast_copy_string((expected_record)->linkedid, ast_channel_linkedid((channel_var)), sizeof((expected_record)->linkedid)); \
ast_channel_unlock((channel_var)); \
} while (0)
/*! \brief Create a \ref test_cdr_chan_tech for Charlie, and set the expected
* CDR records' linkedid and uniqueid. */
#define CREATE_DAVID_CHANNEL(channel_var, caller_id, expected_record) do { \
(channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, "400", "David", "400", "400", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/David"); \
ast_channel_set_caller((channel_var), (caller_id), NULL); \
ast_copy_string((expected_record)->uniqueid, ast_channel_uniqueid((channel_var)), sizeof((expected_record)->uniqueid)); \
ast_copy_string((expected_record)->linkedid, ast_channel_linkedid((channel_var)), sizeof((expected_record)->linkedid)); \
ast_channel_unlock((channel_var)); \
} while (0)
/*! \brief Emulate a channel entering into an application */
#define EMULATE_APP_DATA(channel, priority, application, data) do { \
if ((priority) > 0) { \
ast_channel_priority_set((channel), (priority)); \
} \
ast_channel_lock((channel)); \
ast_channel_appl_set((channel), (application)); \
ast_channel_data_set((channel), (data)); \
ast_channel_publish_snapshot((channel)); \
ast_channel_unlock((channel)); \
} while (0)
/*! \brief Hang up a test channel safely */
#define HANGUP_CHANNEL(channel, cause) \
do { \
ast_channel_hangupcause_set((channel), (cause)); \
ast_hangup(channel); \
channel = NULL; \
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
static enum ast_test_result_state verify_mock_cdr_record(struct ast_test *test, struct ast_cdr *expected, int record)
{
struct ast_cdr *actual = NULL;
struct test_cdr_entry *cdr_wrapper;
int count = 0;
struct timeval wait_now = ast_tvnow();
struct timespec wait_time = { .tv_sec = wait_now.tv_sec + 5, .tv_nsec = wait_now.tv_usec * 1000 };
enum ast_test_result_state res = AST_TEST_PASS;
while (count < record) {
AST_LIST_LOCK(&actual_cdr_entries);
if (global_mock_cdr_count < record) {
ast_cond_timedwait(&mock_cdr_cond, &actual_cdr_entries.lock, &wait_time);
}
cdr_wrapper = AST_LIST_REMOVE_HEAD(&actual_cdr_entries, list);
AST_LIST_UNLOCK(&actual_cdr_entries);
if (!cdr_wrapper) {
ast_test_status_update(test, "Unable to find actual CDR record at %d\n", count);
return AST_TEST_FAIL;
}
actual = cdr_wrapper->cdr;
if (!expected && actual) {
ast_test_status_update(test, "CDRs recorded where no record expected\n");
return AST_TEST_FAIL;
}
ast_test_debug(test, "Verifying expected record %s, %s\n",
expected->channel, S_OR(expected->dstchannel, "<none>"));
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
VERIFY_STRING_FIELD(accountcode, actual, expected);
VERIFY_NUMERIC_FIELD(amaflags, actual, expected);
VERIFY_STRING_FIELD(channel, actual, expected);
VERIFY_STRING_FIELD(clid, actual, expected);
VERIFY_STRING_FIELD(dcontext, actual, expected);
VERIFY_NUMERIC_FIELD(disposition, actual, expected);
VERIFY_STRING_FIELD(dst, actual, expected);
VERIFY_STRING_FIELD(dstchannel, actual, expected);
VERIFY_STRING_FIELD(lastapp, actual, expected);
VERIFY_STRING_FIELD(lastdata, actual, expected);
VERIFY_STRING_FIELD(linkedid, actual, expected);
VERIFY_STRING_FIELD(peeraccount, actual, expected);
VERIFY_STRING_FIELD(src, actual, expected);
VERIFY_STRING_FIELD(uniqueid, actual, expected);
VERIFY_STRING_FIELD(userfield, actual, expected);
VERIFY_TIME_VALUE(start, actual);
VERIFY_TIME_VALUE(end, actual);
/* Note: there's no way we can really calculate a duration or
* billsec - the unit tests are too short. However, if billsec is
* non-zero in the expected, then make sure we have an answer time
*/
if (expected->billsec) {
VERIFY_TIME_VALUE(answer, actual);
}
ast_test_debug(test, "Finished expected record %s, %s\n",
expected->channel, S_OR(expected->dstchannel, "<none>"));
expected = expected->next;
++count;
}
return res;
}
static void safe_channel_release(struct ast_channel *chan)
{
if (!chan) {
return;
}
ast_channel_release(chan);
}
static void safe_bridge_destroy(struct ast_bridge *bridge)
{
if (!bridge) {
return;
}
ast_bridge_destroy(bridge, 0);
}
static void do_sleep(struct timespec *to_sleep)
{
while ((nanosleep(to_sleep, to_sleep) == -1) && (errno == EINTR)) {
}
}
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
AST_TEST_DEFINE(test_cdr_channel_creation)
{
RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
ao2_cleanup);
struct ast_party_caller caller = ALICE_CALLERID;
struct ast_cdr expected = {
.clid = "\"Alice\" <100>",
.src = "100",
.dst = "100",
.dcontext = "default",
.channel = CHANNEL_TECH_NAME "/Alice",
.amaflags = AST_AMA_DOCUMENTATION,
.disposition = AST_CDR_NOANSWER,
.accountcode = "100",
};
enum ast_test_result_state result = AST_TEST_NOT_RUN;
switch (cmd) {
case TEST_INIT:
info->name = __func__;
info->category = TEST_CATEGORY;
info->summary = "Test that a CDR is created when a channel is created";
info->description =
"Test that a CDR is created when a channel is created";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
SWAP_CONFIG(config, unanswered_cdr_config);
CREATE_ALICE_CHANNEL(chan, (&caller), &expected);
HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL);
result = verify_mock_cdr_record(test, &expected, 1);
return result;
}
AST_TEST_DEFINE(test_cdr_unanswered_inbound_call)
{
RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
ao2_cleanup);
struct ast_party_caller caller = ALICE_CALLERID;
struct ast_cdr expected = {
.clid = "\"Alice\" <100>",
.src = "100",
.dst = "100",
.dcontext = "default",
.channel = CHANNEL_TECH_NAME "/Alice",
.lastapp = "Wait",
.lastdata = "1",
.amaflags = AST_AMA_DOCUMENTATION,
.disposition = AST_CDR_NOANSWER,
.accountcode = "100",
};
enum ast_test_result_state result = AST_TEST_NOT_RUN;
switch (cmd) {
case TEST_INIT:
info->name = __func__;
info->category = TEST_CATEGORY;
info->summary = "Test inbound unanswered calls";
info->description =
"Test the properties of a CDR for a call that is\n"
"inbound to Asterisk, executes some dialplan, but\n"
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
SWAP_CONFIG(config, unanswered_cdr_config);
CREATE_ALICE_CHANNEL(chan, &caller, &expected);
EMULATE_APP_DATA(chan, 1, "Wait", "1");
HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL);
result = verify_mock_cdr_record(test, &expected, 1);
return result;
}
AST_TEST_DEFINE(test_cdr_unanswered_outbound_call)
{
RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
ao2_cleanup);
struct ast_party_caller caller = {
.id.name.str = "",
.id.name.valid = 1,
.id.number.str = "",
.id.number.valid = 1, };
struct ast_cdr expected = {
.clid = "\"\" <>",
.dst = "s",
.dcontext = "default",
.channel = CHANNEL_TECH_NAME "/Alice",
.lastapp = "AppDial",
.lastdata = "(Outgoing Line)",
.amaflags = AST_AMA_DOCUMENTATION,
.disposition = AST_CDR_NOANSWER,
.accountcode = "100",
};
enum ast_test_result_state result = AST_TEST_NOT_RUN;
switch (cmd) {
case TEST_INIT:
info->name = __func__;
info->category = TEST_CATEGORY;
info->summary = "Test outbound unanswered calls";
info->description =
"Test the properties of a CDR for a call that is\n"
"outbound to Asterisk but is never answered.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
SWAP_CONFIG(config, unanswered_cdr_config);
CREATE_ALICE_CHANNEL(chan, &caller, &expected);
ast_channel_exten_set(chan, "s");
ast_channel_context_set(chan, "default");
ast_set_flag(ast_channel_flags(chan), AST_FLAG_ORIGINATED);
EMULATE_APP_DATA(chan, 0, "AppDial", "(Outgoing Line)");
HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL);
result = verify_mock_cdr_record(test, &expected, 1);
return result;
}
AST_TEST_DEFINE(test_cdr_outbound_bridged_call)
{
RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
ao2_cleanup);
struct timespec to_sleep = {1, 0};
enum ast_test_result_state result = AST_TEST_NOT_RUN;
struct ast_party_caller caller = ALICE_CALLERID;
struct ast_cdr alice_expected = {
.clid = "\"Alice\" <100>",
.src = "100",
.dst = "100",
.dcontext = "default",
.channel = CHANNEL_TECH_NAME "/Alice",
.dstchannel = CHANNEL_TECH_NAME "/Bob",
.lastapp = "",
.lastdata = "",
.amaflags = AST_AMA_DOCUMENTATION,
.billsec = 1,
.disposition = AST_CDR_ANSWERED,
.accountcode = "100",
.peeraccount = "200",
};
struct ast_cdr bob_expected = {
.clid = "\"\" <>",
.src = "",
.dst = "s",
.dcontext = "default",
.channel = CHANNEL_TECH_NAME "/Bob",
.dstchannel = "",
.lastapp = "AppDial",
.lastdata = "(Outgoing Line)",
.amaflags = AST_AMA_DOCUMENTATION,
.billsec = 1,
.disposition = AST_CDR_ANSWERED,
.accountcode = "200",
.peeraccount = "",
.next = &alice_expected,
};
switch (cmd) {
case TEST_INIT:
info->name = __func__;
info->category = TEST_CATEGORY;
info->summary = "Test dialing, answering, and going into a 2-party bridge";
info->description =
"The most 'basic' of scenarios";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
SWAP_CONFIG(config, debug_cdr_config);
CREATE_ALICE_CHANNEL(chan_alice, &caller, &alice_expected);
ast_channel_state_set(chan_alice, AST_STATE_UP);
bridge = ast_bridge_basic_new();
ast_test_validate(test, bridge != NULL);
do_sleep(&to_sleep);
ast_test_validate(test, !ast_bridge_impart(bridge, chan_alice, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
chan_bob = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, "200", NULL, NULL, NULL, chan_alice, 0, CHANNEL_TECH_NAME "/Bob");
ast_copy_string(bob_expected.linkedid, ast_channel_linkedid(chan_bob), sizeof(bob_expected.linkedid));
ast_copy_string(bob_expected.uniqueid, ast_channel_uniqueid(chan_bob), sizeof(bob_expected.uniqueid));
ast_set_flag(ast_channel_flags(chan_bob), AST_FLAG_OUTGOING);
ast_set_flag(ast_channel_flags(chan_bob), AST_FLAG_ORIGINATED);
EMULATE_APP_DATA(chan_bob, 0, "AppDial", "(Outgoing Line)");
ast_channel_publish_dial(NULL, chan_bob, "Bob", NULL);
ast_channel_state_set(chan_bob, AST_STATE_RINGING);
ast_channel_publish_dial(NULL, chan_bob, NULL, "ANSWER");
ast_channel_state_set(chan_bob, AST_STATE_UP);
do_sleep(&to_sleep);
ast_test_validate(test, !ast_bridge_impart(bridge, chan_bob, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
do_sleep(&to_sleep);
ast_bridge_depart(chan_bob);
ast_bridge_depart(chan_alice);
HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL);
HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL);
result = verify_mock_cdr_record(test, &bob_expected, 2);
return result;
}
AST_TEST_DEFINE(test_cdr_single_party)
{
RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
ao2_cleanup);
struct ast_party_caller caller = ALICE_CALLERID;
struct ast_cdr expected = {
.clid = "\"Alice\" <100>",
.src = "100",
.dst = "100",
.dcontext = "default",
.channel = CHANNEL_TECH_NAME "/Alice",
.dstchannel = "",
.lastapp = "VoiceMailMain",
.lastdata = "1",
.billsec = 1,
.amaflags = AST_AMA_DOCUMENTATION,
.disposition = AST_CDR_ANSWERED,
.accountcode = "100",
};
enum ast_test_result_state result = AST_TEST_NOT_RUN;
switch (cmd) {
case TEST_INIT:
info->name = __func__;
info->category = TEST_CATEGORY;
info->summary = "Test cdrs for a single party";
info->description =
"Test the properties of a CDR for a call that is\n"
"answered, but only involves a single channel";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
SWAP_CONFIG(config, debug_cdr_config);
CREATE_ALICE_CHANNEL(chan, &caller, &expected);
ast_channel_lock(chan);
EMULATE_APP_DATA(chan, 1, "Answer", "");
ast_setstate(chan, AST_STATE_UP);
EMULATE_APP_DATA(chan, 2, "VoiceMailMain", "1");
ast_channel_unlock(chan);
HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL);
result = verify_mock_cdr_record(test, &expected, 1);
return result;
}
AST_TEST_DEFINE(test_cdr_single_bridge)
{
RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
ao2_cleanup);
struct timespec to_sleep = {1, 0};
struct ast_party_caller caller = ALICE_CALLERID;
struct ast_cdr expected = {
.clid = "\"Alice\" <100>",
.src = "100",
.dst = "100",
.dcontext = "default",
.channel = CHANNEL_TECH_NAME "/Alice",
.lastapp = "Bridge",
.billsec = 1,
.amaflags = AST_AMA_DOCUMENTATION,
.disposition = AST_CDR_ANSWERED,
.accountcode = "100",
};
enum ast_test_result_state result = AST_TEST_NOT_RUN;
switch (cmd) {
case TEST_INIT:
info->name = __func__;
info->category = TEST_CATEGORY;
info->summary = "Test cdrs for a single party entering/leaving a bridge";
info->description =
"Test the properties of a CDR for a call that is\n"
"answered, enters a bridge, and leaves it.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
SWAP_CONFIG(config, debug_cdr_config);
CREATE_ALICE_CHANNEL(chan, &caller, &expected);
ast_channel_lock(chan);
EMULATE_APP_DATA(chan, 1, "Answer", "");
ast_setstate(chan, AST_STATE_UP);
EMULATE_APP_DATA(chan, 2, "Bridge", "");
ast_channel_unlock(chan);
bridge = ast_bridge_basic_new();
ast_test_validate(test, bridge != NULL);
do_sleep(&to_sleep);
ast_test_validate(test, !ast_bridge_impart(bridge, chan, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
do_sleep(&to_sleep);
ast_bridge_depart(chan);
HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL);
result = verify_mock_cdr_record(test, &expected, 1);
return result;
}
AST_TEST_DEFINE(test_cdr_single_bridge_continue)
{
RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
ao2_cleanup);
struct timespec to_sleep = {1, 0};
struct ast_party_caller caller = ALICE_CALLERID;
struct ast_cdr expected_two = {
.clid = "\"Alice\" <100>",
.src = "100",
.dst = "100",
.dcontext = "default",
.channel = CHANNEL_TECH_NAME "/Alice",
.lastapp = "Wait",
.billsec = 1,
.amaflags = AST_AMA_DOCUMENTATION,
.disposition = AST_CDR_ANSWERED,
.accountcode = "100",
};
struct ast_cdr expected_one = {
.clid = "\"Alice\" <100>",
.src = "100",
.dst = "100",
.dcontext = "default",
.channel = CHANNEL_TECH_NAME "/Alice",
.lastapp = "Bridge",
.billsec = 1,
.amaflags = AST_AMA_DOCUMENTATION,
.disposition = AST_CDR_ANSWERED,
.accountcode = "100",
.next = &expected_two,
};
enum ast_test_result_state result = AST_TEST_NOT_RUN;
switch (cmd) {
case TEST_INIT:
info->name = __func__;
info->category = TEST_CATEGORY;
info->summary = "Test cdrs for a single party entering/leaving a bridge";
info->description =
"Test the properties of a CDR for a call that is\n"
"answered, enters a bridge, and leaves it.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
SWAP_CONFIG(config, debug_cdr_config);
CREATE_ALICE_CHANNEL(chan, &caller, &expected_one);
COPY_IDS(chan, &expected_two);
ast_channel_lock(chan);
EMULATE_APP_DATA(chan, 1, "Answer", "");
ast_setstate(chan, AST_STATE_UP);
EMULATE_APP_DATA(chan, 2, "Bridge", "");
ast_channel_unlock(chan);
bridge = ast_bridge_basic_new();
ast_test_validate(test, bridge != NULL);
do_sleep(&to_sleep);
ast_test_validate(test, !ast_bridge_impart(bridge, chan, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
do_sleep(&to_sleep);
ast_bridge_depart(chan);
EMULATE_APP_DATA(chan, 3, "Wait", "");
/* And then it hangs up */
HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL);
result = verify_mock_cdr_record(test, &expected_one, 2);
return result;
}
AST_TEST_DEFINE(test_cdr_single_twoparty_bridge_a)
{
RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
ao2_cleanup);
struct timespec to_sleep = {1, 0};
struct ast_party_caller caller_alice = ALICE_CALLERID;
struct ast_party_caller caller_bob = BOB_CALLERID;
struct ast_cdr bob_expected = {
.clid = "\"Bob\" <200>",
.src = "200",
.dst = "200",
.dcontext = "default",
.channel = CHANNEL_TECH_NAME "/Bob",
.lastapp = "Bridge",
.billsec = 1,
.amaflags = AST_AMA_DOCUMENTATION,
.disposition = AST_CDR_ANSWERED,
.accountcode = "200",
};
struct ast_cdr alice_expected = {
.clid = "\"Alice\" <100>",
.src = "100",
.dst = "100",
.dcontext = "default",
.channel = CHANNEL_TECH_NAME "/Alice",
.dstchannel = CHANNEL_TECH_NAME "/Bob",
.lastapp = "Bridge",
.billsec = 1,
.amaflags = AST_AMA_DOCUMENTATION,
.disposition = AST_CDR_ANSWERED,
.accountcode = "100",
.peeraccount = "200",
.next = &bob_expected,
};
enum ast_test_result_state result = AST_TEST_NOT_RUN;
switch (cmd) {
case TEST_INIT:
info->name = __func__;
info->category = TEST_CATEGORY;
info->summary = "Test cdrs for a single party entering/leaving a bridge";
info->description =
"Test the properties of a CDR for a call that is\n"
"answered, enters a bridge, and leaves it. In this scenario, the\n"
"Party A should answer the bridge first.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
SWAP_CONFIG(config, debug_cdr_config);
CREATE_ALICE_CHANNEL(chan_alice, &caller_alice, &alice_expected);
CREATE_BOB_CHANNEL(chan_bob, &caller_bob, &bob_expected);
ast_copy_string(bob_expected.linkedid, ast_channel_linkedid(chan_alice), sizeof(bob_expected.linkedid));
ast_channel_lock(chan_alice);
EMULATE_APP_DATA(chan_alice, 1, "Answer", "");
ast_setstate(chan_alice, AST_STATE_UP);
EMULATE_APP_DATA(chan_alice, 2, "Bridge", "");
ast_channel_unlock(chan_alice);
bridge = ast_bridge_basic_new();
ast_test_validate(test, bridge != NULL);
ast_test_validate(test, !ast_bridge_impart(bridge, chan_alice, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
do_sleep(&to_sleep);
ast_channel_lock(chan_bob);
EMULATE_APP_DATA(chan_bob, 1, "Answer", "");
ast_setstate(chan_bob, AST_STATE_UP);
EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
ast_channel_unlock(chan_bob);
ast_test_validate(test, !ast_bridge_impart(bridge, chan_bob, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
do_sleep(&to_sleep);
ast_bridge_depart(chan_alice);
ast_bridge_depart(chan_bob);
HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL);
HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL);
result = verify_mock_cdr_record(test, &alice_expected, 2);
return result;
}
AST_TEST_DEFINE(test_cdr_single_twoparty_bridge_b)
{
RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
RAII_VAR(struct ast_cdr_config *, config, ao2_alloc(sizeof(*config), NULL),
ao2_cleanup);
struct timespec to_sleep = {1, 0};
struct ast_party_caller caller_alice = ALICE_CALLERID;
struct ast_party_caller caller_bob = BOB_CALLERID;
struct ast_cdr bob_expected = {
.clid = "\"Bob\" <200>",
.src = "200",
.dst = "200",
.dcontext = "default",
.channel = CHANNEL_TECH_NAME "/Bob",
.lastapp = "Bridge",
.billsec = 1,
.amaflags = AST_AMA_DOCUMENTATION,
.disposition = AST_CDR_ANSWERED,
.accountcode = "200",
};
struct ast_cdr alice_expected = {
.clid = "\"Alice\" <100>",
.src = "100",
.dst = "100",
.dcontext = "default",
.channel = CHANNEL_TECH_NAME "/Alice",
.dstchannel = CHANNEL_TECH_NAME "/Bob",
.lastapp = "Bridge",
.billsec = 1,
.amaflags = AST_AMA_DOCUMENTATION,
.disposition = AST_CDR_ANSWERED,
.accountcode = "100",
.peeraccount = "200",
.next = &bob_expected,
};
enum ast_test_result_state result = AST_TEST_NOT_RUN;
switch (cmd) {
case TEST_INIT:
info->name = __func__;
info->category = TEST_CATEGORY;
info->summary = "Test cdrs for a single party entering/leaving a bridge";
info->description =
"Test the properties of a CDR for a call that is\n"
"answered, enters a bridge, and leaves it. In this scenario, the\n"
"Party B should answer the bridge first.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
SWAP_CONFIG(config, debug_cdr_config);
CREATE_ALICE_CHANNEL(chan_alice, &caller_alice, &alice_expected);
CREATE_BOB_CHANNEL(chan_bob, &caller_bob, &bob_expected);
ast_copy_string(bob_expected.linkedid, ast_channel_linkedid(chan_alice), sizeof(bob_expected.linkedid));
ast_channel_unlock(chan_alice);
EMULATE_APP_DATA(chan_alice, 1, "Answer", "");
ast_setstate(chan_alice, AST_STATE_UP);
EMULATE_APP_DATA(chan_alice, 2, "Bridge", "");
ast_channel_unlock(chan_alice);
bridge = ast_bridge_basic_new();
ast_test_validate(test, bridge != NULL);
ast_channel_lock(chan_bob);
EMULATE_APP_DATA(chan_bob, 1, "Answer", "");
ast_setstate(chan_bob, AST_STATE_UP);
EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
ast_channel_unlock(chan_bob);
do_sleep(&to_sleep);
ast_test_validate(test, !ast_bridge_impart(bridge, chan_bob, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
do_sleep(&to_sleep);
ast_test_validate(test, !ast_bridge_impart(bridge, chan_alice, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
do_sleep(&to_sleep);
ast_bridge_depart(chan_alice);
ast_bridge_depart(chan_bob);