Newer
Older
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2010, Digium, Inc.
* Mark Michelson <mmichelson@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 Configuration unit tests
* \author Mark Michelson <mmichelson@digium.com>
*
*/
/*** MODULEINFO
<depend>TEST_FRAMEWORK</depend>
<support_level>core</support_level>
***/
#include "asterisk.h"
#include <math.h> /* HUGE_VAL */
#include <sys/stat.h>
#include "asterisk/module.h"
#include "asterisk/test.h"
#include "asterisk/paths.h"
#include "asterisk/config_options.h"
#include "asterisk/netsock2.h"
#include "asterisk/acl.h"
#include "asterisk/app.h"
#include "asterisk/pbx.h"
#include "asterisk/frame.h"
#include "asterisk/utils.h"
#include "asterisk/format_cap.h"
#define CONFIG_FILE "test_config.conf"
#define CONFIG_INCLUDE_FILE "test_config_include.conf"
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
/*
* This builds the folowing config:
* [Capitals]
* Germany = Berlin
* China = Beijing
* Canada = Ottawa
*
* [Protagonists]
* 1984 = Winston Smith
* Green Eggs And Ham = Sam I Am
* The Kalevala = Vainamoinen
*
* This config is used for all tests below.
*/
const char cat1[] = "Capitals";
const char cat1varname1[] = "Germany";
const char cat1varvalue1[] = "Berlin";
const char cat1varname2[] = "China";
const char cat1varvalue2[] = "Beijing";
const char cat1varname3[] = "Canada";
const char cat1varvalue3[] = "Ottawa";
const char cat2[] = "Protagonists";
const char cat2varname1[] = "1984";
const char cat2varvalue1[] = "Winston Smith";
const char cat2varname2[] = "Green Eggs And Ham";
const char cat2varvalue2[] = "Sam I Am";
const char cat2varname3[] = "The Kalevala";
const char cat2varvalue3[] = "Vainamoinen";
struct pair {
const char *name;
const char *val;
};
struct association {
const char *category;
struct pair vars[3];
} categories [] = {
{ cat1,
{
{ cat1varname1, cat1varvalue1 },
{ cat1varname2, cat1varvalue2 },
{ cat1varname3, cat1varvalue3 },
}
},
{ cat2,
{
{ cat2varname1, cat2varvalue1 },
{ cat2varname2, cat2varvalue2 },
{ cat2varname3, cat2varvalue3 },
}
},
};
/*!
* \brief Build ast_config struct from above definitions
*
* \retval NULL Failed to build the config
* \retval non-NULL An ast_config struct populated with data
*/
static struct ast_config *build_cfg(void)
{
struct ast_config *cfg;
struct association *cat_iter;
struct pair *var_iter;
size_t i;
size_t j;
cfg = ast_config_new();
if (!cfg) {
goto fail;
}
for (i = 0; i < ARRAY_LEN(categories); ++i) {
struct ast_category *cat;
cat_iter = &categories[i];
cat = ast_category_new(cat_iter->category, "", 999999);
if (!cat) {
goto fail;
}
ast_category_append(cfg, cat);
for (j = 0; j < ARRAY_LEN(cat_iter->vars); ++j) {
struct ast_variable *var;
var_iter = &cat_iter->vars[j];
var = ast_variable_new(var_iter->name, var_iter->val, "");
if (!var) {
goto fail;
}
ast_variable_append(cat, var);
}
}
return cfg;
fail:
ast_config_destroy(cfg);
return NULL;
}
/*!
* \brief Tests that the contents of an ast_config is what is expected
*
* \param cfg Config to test
* \retval -1 Failed to pass a test
* \retval 0 Config passes checks
*/
static int test_config_validity(struct ast_config *cfg)
{
int i;
const char *cat_iter = NULL;
/* Okay, let's see if the correct content is there */
for (i = 0; i < ARRAY_LEN(categories); ++i) {
struct ast_variable *var = NULL;
size_t j;
cat_iter = ast_category_browse(cfg, cat_iter);
if (strcmp(cat_iter, categories[i].category)) {
ast_log(LOG_ERROR, "Category name mismatch, %s does not match %s\n", cat_iter, categories[i].category);
return -1;
}
for (j = 0; j < ARRAY_LEN(categories[i].vars); ++j) {
var = var ? var->next : ast_variable_browse(cfg, cat_iter);
if (strcmp(var->name, categories[i].vars[j].name)) {
ast_log(LOG_ERROR, "Variable name mismatch, %s does not match %s\n", var->name, categories[i].vars[j].name);
return -1;
}
if (strcmp(var->value, categories[i].vars[j].val)) {
ast_log(LOG_ERROR, "Variable value mismatch, %s does not match %s\n", var->value, categories[i].vars[j].val);
return -1;
}
}
}
return 0;
}
AST_TEST_DEFINE(copy_config)
{
enum ast_test_result_state res = AST_TEST_FAIL;
struct ast_config *cfg = NULL;
struct ast_config *copy = NULL;
switch (cmd) {
case TEST_INIT:
info->name = "copy_config";
info->category = "/main/config/";
info->summary = "Test copying configuration";
info->description =
"Ensure that variables and categories are copied correctly";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
cfg = build_cfg();
if (!cfg) {
goto out;
}
copy = ast_config_copy(cfg);
if (!copy) {
goto out;
}
if (test_config_validity(copy) != 0) {
goto out;
}
res = AST_TEST_PASS;
out:
ast_config_destroy(cfg);
ast_config_destroy(copy);
return res;
}
AST_TEST_DEFINE(config_basic_ops)
{
enum ast_test_result_state res = AST_TEST_FAIL;
struct ast_config *cfg = NULL;
struct ast_category *cat = NULL;
struct ast_variable *var;
struct ast_variable *varlist;
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
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
337
338
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
char temp[32];
const char *cat_name;
const char *var_value;
int i;
switch (cmd) {
case TEST_INIT:
info->name = "config_basic_ops";
info->category = "/main/config/";
info->summary = "Test basic config ops";
info->description = "Test basic config ops";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
cfg = ast_config_new();
if (!cfg) {
return res;
}
/* load the config */
for(i = 0; i < 5; i++) {
snprintf(temp, sizeof(temp), "test%d", i);
ast_category_append(cfg, ast_category_new(temp, "dummy", -1));
}
/* test0 test1 test2 test3 test4 */
/* check the config has 5 elements */
i = 0;
cat = NULL;
while ((cat = ast_category_browse_filtered(cfg, NULL, cat, NULL))) {
snprintf(temp, sizeof(temp), "test%d", i);
if (strcmp(ast_category_get_name(cat), temp)) {
ast_test_status_update(test, "%s != %s\n", ast_category_get_name(cat), temp);
goto out;
}
i++;
}
if (i != 5) {
ast_test_status_update(test, "There were %d matches instead of 5.\n", i);
goto out;
}
/* search for test2 */
cat = ast_category_get(cfg, "test2", NULL);
if (!cat || strcmp(ast_category_get_name(cat), "test2")) {
ast_test_status_update(test, "Get failed %s != %s\n", ast_category_get_name(cat), "test2");
goto out;
}
/* delete test2 */
cat = ast_category_delete(cfg, cat);
/* Now: test0 test1 test3 test4 */
/* make sure the curr category is test1 */
if (!cat || strcmp(ast_category_get_name(cat), "test1")) {
ast_test_status_update(test, "Delete failed %s != %s\n", ast_category_get_name(cat), "test1");
goto out;
}
/* Now: test0 test1 test3 test4 */
/* make sure the test2 is not found */
cat = ast_category_get(cfg, "test2", NULL);
if (cat) {
ast_test_status_update(test, "Should not have found test2\n");
goto out;
}
/* Now: test0 test1 test3 test4 */
/* make sure the sequence is correctly missing test2 */
i = 0;
cat = NULL;
while ((cat = ast_category_browse_filtered(cfg, NULL, cat, NULL))) {
snprintf(temp, sizeof(temp), "test%d", i);
if (strcmp(ast_category_get_name(cat), temp)) {
ast_test_status_update(test, "%s != %s\n", ast_category_get_name(cat), temp);
goto out;
}
i++;
if (i == 2) {
i++;
}
}
if (i != 5) {
ast_test_status_update(test, "There were %d matches instead of 5.\n", i);
goto out;
}
/* insert test2 back in before test3 */
ast_category_insert(cfg, ast_category_new("test2", "dummy", -1), "test3");
/* Now: test0 test1 test2 test3 test4 */
/* make sure the sequence is correct again */
i = 0;
cat = NULL;
while ((cat = ast_category_browse_filtered(cfg, NULL, cat, NULL))) {
snprintf(temp, sizeof(temp), "test%d", i);
if (strcmp(ast_category_get_name(cat), temp)) {
ast_test_status_update(test, "%s != %s\n", ast_category_get_name(cat), temp);
goto out;
}
i++;
}
if (i != 5) {
ast_test_status_update(test, "There were %d matches instead of 5.\n", i);
goto out;
}
/* Now: test0 test1 test2 test3 test4 */
/* make sure non filtered browse still works */
i = 0;
cat_name = NULL;
while ((cat_name = ast_category_browse(cfg, cat_name))) {
snprintf(temp, sizeof(temp), "test%d", i);
if (strcmp(cat_name, temp)) {
ast_test_status_update(test, "%s != %s\n", cat_name, temp);
goto out;
}
i++;
}
if (i != 5) {
ast_test_status_update(test, "There were %d matches instead of 5.\n", i);
goto out;
}
/* append another test2 */
ast_category_append(cfg, ast_category_new("test2", "dummy", -1));
/* Now: test0 test1 test2 test3 test4 test2*/
/* make sure only test2's are returned */
i = 0;
cat = NULL;
while ((cat = ast_category_browse_filtered(cfg, "test2", cat, NULL))) {
if (strcmp(ast_category_get_name(cat), "test2")) {
ast_test_status_update(test, "Should have returned test2 instead of %s\n", ast_category_get_name(cat));
goto out;
}
i++;
}
/* make sure 2 test2's were found */
if (i != 2) {
ast_test_status_update(test, "Should have found 2 test2's %d\n", i);
goto out;
}
/* Test in-flight deletion using ast_category_browse_filtered */
/* Now: test0 test1 test2 test3 test4 test2 */
/* Delete the middle test2 and continue */
cat = NULL;
for(i = 0; i < 5; i++) {
snprintf(temp, sizeof(temp), "test%d", i);
cat = ast_category_browse_filtered(cfg, NULL, cat, NULL);
cat_name = ast_category_get_name(cat);
if (strcmp(cat_name, temp)) {
ast_test_status_update(test, "Should have returned %s instead of %s: %d\n", temp, cat_name, i);
goto out;
}
if (i == 2) {
cat = ast_category_delete(cfg, cat);
}
}
/* Now: test0 test3 test4 test2 */
/* delete the head item */
cat = ast_category_browse_filtered(cfg, NULL, NULL, NULL);
cat_name = ast_category_get_name(cat);
if (strcmp(cat_name, "test0")) {
ast_test_status_update(test, "Should have returned test0 instead of %s\n", cat_name);
goto out;
}
ast_category_delete(cfg, cat);
/* Now: test3 test4 test2 */
/* make sure head got updated to the new first element */
cat = ast_category_browse_filtered(cfg, NULL, NULL, NULL);
cat_name = ast_category_get_name(cat);
if (strcmp(cat_name, "test1")) {
ast_test_status_update(test, "Should have returned test3 instead of %s\n", cat_name);
goto out;
}
/* delete the tail item */
cat = ast_category_get(cfg, "test2", NULL);
cat_name = ast_category_get_name(cat);
if (strcmp(cat_name, "test2")) {
ast_test_status_update(test, "Should have returned test2 instead of %s\n", cat_name);
goto out;
}
ast_category_delete(cfg, cat);
/* Now: test3 test4 */
/* There should now only be 2 elements in the list */
cat = NULL;
cat = ast_category_browse_filtered(cfg, NULL, cat, NULL);
cat_name = ast_category_get_name(cat);
if (strcmp(cat_name, "test1")) {
ast_test_status_update(test, "Should have returned test1 instead of %s\n", cat_name);
goto out;
}
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
464
465
cat = ast_category_browse_filtered(cfg, NULL, cat, NULL);
cat_name = ast_category_get_name(cat);
if (strcmp(cat_name, "test3")) {
ast_test_status_update(test, "Should have returned test3 instead of %s\n", cat_name);
goto out;
}
cat = ast_category_browse_filtered(cfg, NULL, cat, NULL);
cat_name = ast_category_get_name(cat);
if (strcmp(cat_name, "test4")) {
ast_test_status_update(test, "Should have returned test4 instead of %s\n", cat_name);
goto out;
}
/* There should be nothing more */
cat = ast_category_browse_filtered(cfg, NULL, cat, NULL);
if (cat) {
ast_test_status_update(test, "Should not have returned anything\n");
goto out;
}
/* Test ast_variable retrieve.
* Get the second category.
*/
cat = ast_category_browse_filtered(cfg, NULL, NULL, NULL);
cat = ast_category_browse_filtered(cfg, NULL, cat, NULL);
cat_name = ast_category_get_name(cat);
var = ast_variable_new("aaa", "bbb0", "dummy");
if (!var) {
ast_test_status_update(test, "Couldn't allocate variable.\n");
goto out;
}
ast_variable_append(cat, var);
/* Make sure we can retrieve with specific category name */
var_value = ast_variable_retrieve(cfg, cat_name, "aaa");
if (!var_value || strcmp(var_value, "bbb0")) {
ast_test_status_update(test, "Variable not found or wrong value.\n");
goto out;
}
/* Make sure we can retrieve with NULL category name */
var_value = ast_variable_retrieve(cfg, NULL, "aaa");
if (!var_value || strcmp(var_value, "bbb0")) {
ast_test_status_update(test, "Variable not found or wrong value.\n");
goto out;
}
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/* Now test variable retrieve inside a browse loop
* with multiple categories of the same name
*/
cat = ast_category_new("test3", "dummy", -1);
if (!cat) {
ast_test_status_update(test, "Couldn't allocate category.\n");
goto out;
}
var = ast_variable_new("aaa", "bbb1", "dummy");
if (!var) {
ast_test_status_update(test, "Couldn't allocate variable.\n");
goto out;
}
ast_variable_append(cat, var);
ast_category_append(cfg, cat);
cat = ast_category_new("test3", "dummy", -1);
if (!cat) {
ast_test_status_update(test, "Couldn't allocate category.\n");
goto out;
}
var = ast_variable_new("aaa", "bbb2", "dummy");
if (!var) {
ast_test_status_update(test, "Couldn't allocate variable.\n");
goto out;
}
ast_variable_append(cat, var);
ast_category_append(cfg, cat);
cat_name = NULL;
i = 0;
while ((cat_name = ast_category_browse(cfg, cat_name))) {
if (!strcmp(cat_name, "test3")) {
snprintf(temp, sizeof(temp), "bbb%d", i);
var_value = ast_variable_retrieve(cfg, cat_name, "aaa");
if (!var_value || strcmp(var_value, temp)) {
ast_test_status_update(test, "Variable not found or wrong value %s.\n", var_value);
goto out;
}
var = ast_variable_browse(cfg, cat_name);
if (!var->value || strcmp(var->value, temp)) {
ast_test_status_update(test, "Variable not found or wrong value %s.\n", var->value);
goto out;
}
i++;
}
}
if (i != 3) {
ast_test_status_update(test, "There should have been 3 matches instead of %d.\n", i);
goto out;
}
varlist = ast_variable_new("name1", "value1", "");
ast_variable_list_append_hint(&varlist, NULL, ast_variable_new("name1", "value2", ""));
ast_variable_list_append_hint(&varlist, NULL, ast_variable_new("name1", "value3", ""));
var_value = ast_variable_find_in_list(varlist, "name1");
if (strcmp(var_value, "value1") != 0) {
ast_test_status_update(test, "Wrong variable retrieved %s.\n", var_value);
goto out;
}
var_value = ast_variable_find_last_in_list(varlist, "name1");
if (strcmp(var_value, "value3") != 0) {
ast_test_status_update(test, "Wrong variable retrieved %s.\n", var_value);
goto out;
}
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
res = AST_TEST_PASS;
out:
ast_config_destroy(cfg);
return res;
}
AST_TEST_DEFINE(config_filtered_ops)
{
enum ast_test_result_state res = AST_TEST_FAIL;
struct ast_config *cfg = NULL;
struct ast_category *cat = NULL;
char temp[32];
const char *value;
int i;
switch (cmd) {
case TEST_INIT:
info->name = "config_filtered_ops";
info->category = "/main/config/";
info->summary = "Test filtered config ops";
info->description = "Test filtered config ops";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
cfg = ast_config_new();
if (!cfg) {
return res;
}
/* load the config */
for(i = 0; i < 5; i++) {
snprintf(temp, sizeof(temp), "test%d", i);
cat = ast_category_new(temp, "dummy", -1);
ast_variable_insert(cat, ast_variable_new("type", "a", "dummy"), "0");
ast_category_append(cfg, cat);
}
for(i = 0; i < 5; i++) {
snprintf(temp, sizeof(temp), "test%d", i);
cat = ast_category_new(temp, "dummy", -1);
ast_variable_insert(cat, ast_variable_new("type", "b", "dummy"), "0");
ast_category_append(cfg, cat);
}
/* check the config has 5 elements for each type*/
i = 0;
cat = NULL;
while ((cat = ast_category_browse_filtered(cfg, NULL, cat, "type=a"))) {
snprintf(temp, sizeof(temp), "test%d", i);
if (strcmp(ast_category_get_name(cat), temp)) {
ast_test_status_update(test, "%s != %s\n", ast_category_get_name(cat), temp);
goto out;
}
value = ast_variable_find(cat, "type");
if (!value || strcmp(value, "a")) {
ast_test_status_update(test, "Type %s != %s\n", "a", value);
goto out;
}
i++;
}
if (i != 5) {
ast_test_status_update(test, "There were %d matches instead of 5.\n", i);
goto out;
}
i = 0;
cat = NULL;
while ((cat = ast_category_browse_filtered(cfg, NULL, cat, "type=b"))) {
snprintf(temp, sizeof(temp), "test%d", i);
if (!cat || strcmp(ast_category_get_name(cat), temp)) {
ast_test_status_update(test, "%s != %s\n", ast_category_get_name(cat), temp);
goto out;
}
value = ast_variable_find(cat, "type");
if (!value || strcmp(value, "b")) {
ast_test_status_update(test, "Type %s != %s\n", "b", value);
goto out;
}
i++;
}
if (i != 5) {
ast_test_status_update(test, "There were %d matches instead of 5.\n", i);
goto out;
}
/* Delete b3 and make sure it's gone and a3 is still there.
* Really this is a test of get since delete takes a specific category structure.
*/
cat = ast_category_get(cfg, "test3", "type=b");
value = ast_variable_find(cat, "type");
if (strcmp(value, "b")) {
ast_test_status_update(test, "Type %s != %s\n", "b", value);
goto out;
}
ast_category_delete(cfg, cat);
cat = ast_category_get(cfg, "test3", "type=b");
if (cat) {
ast_test_status_update(test, "Category b was not deleted.\n");
goto out;
}
cat = ast_category_get(cfg, "test3", "type=a");
if (!cat) {
ast_test_status_update(test, "Category a was deleted.\n");
goto out;
}
value = ast_variable_find(cat, "type");
if (strcmp(value, "a")) {
ast_test_status_update(test, "Type %s != %s\n", value, "a");
goto out;
}
/* Basic regex stuff is handled by regcomp/regexec so not testing here.
* Still need to test multiple name/value pairs though.
*/
ast_category_empty(cat);
ast_variable_insert(cat, ast_variable_new("type", "bx", "dummy"), "0");
ast_variable_insert(cat, ast_variable_new("e", "z", "dummy"), "0");
cat = ast_category_get(cfg, "test3", "type=.,e=z");
if (!cat) {
ast_test_status_update(test, "Category not found.\n");
goto out;
}
cat = ast_category_get(cfg, "test3", "type=.,e=zX");
if (cat) {
ast_test_status_update(test, "Category found.\n");
goto out;
}
cat = ast_category_get(cfg, "test3", "TEMPLATE=restrict,type=.,e=z");
if (cat) {
ast_test_status_update(test, "Category found.\n");
goto out;
}
res = AST_TEST_PASS;
out:
ast_config_destroy(cfg);
return res;
}
AST_TEST_DEFINE(config_template_ops)
{
enum ast_test_result_state res = AST_TEST_FAIL;
struct ast_config *cfg = NULL;
struct ast_category *cat = NULL;
char temp[32];
const char *value;
int i;
switch (cmd) {
case TEST_INIT:
info->name = "config_template_ops";
info->category = "/main/config/";
info->summary = "Test template config ops";
info->description = "Test template config ops";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
cfg = ast_config_new();
if (!cfg) {
return res;
}
/* load the config with 5 templates and 5 regular */
for(i = 0; i < 5; i++) {
snprintf(temp, sizeof(temp), "test%d", i);
cat = ast_category_new_template(temp, "dummy", -1);
ast_variable_insert(cat, ast_variable_new("type", "a", "dummy"), "0");
ast_category_append(cfg, cat);
}
for(i = 0; i < 5; i++) {
snprintf(temp, sizeof(temp), "test%d", i);
cat = ast_category_new(temp, "dummy", -1);
ast_variable_insert(cat, ast_variable_new("type", "b", "dummy"), "0");
ast_category_append(cfg, cat);
}
/* check the config has 5 template elements of type a */
i = 0;
cat = NULL;
while ((cat = ast_category_browse_filtered(cfg, NULL, cat, "TEMPLATES=restrict,type=a"))) {
snprintf(temp, sizeof(temp), "test%d", i);
if (strcmp(ast_category_get_name(cat), temp)) {
ast_test_status_update(test, "%s != %s\n", ast_category_get_name(cat), temp);
goto out;
}
value = ast_variable_find(cat, "type");
if (!value || strcmp(value, "a")) {
ast_test_status_update(test, "Type %s != %s\n", value, "a");
goto out;
}
i++;
}
if (i != 5) {
ast_test_status_update(test, "There were %d matches instead of 5.\n", i);
goto out;
}
/* Test again with 'include'. There should still only be 5 (type a) */
i = 0;
cat = NULL;
while ((cat = ast_category_browse_filtered(cfg, NULL, cat, "TEMPLATES=include,type=a"))) {
snprintf(temp, sizeof(temp), "test%d", i);
if (strcmp(ast_category_get_name(cat), temp)) {
ast_test_status_update(test, "%s != %s\n", ast_category_get_name(cat), temp);
goto out;
}
value = ast_variable_find(cat, "type");
if (!value || strcmp(value, "a")) {
ast_test_status_update(test, "Type %s != %s\n", value, "a");
goto out;
}
i++;
}
if (i != 5) {
ast_test_status_update(test, "There were %d matches instead of 5.\n", i);
goto out;
}
/* Test again with 'include' but no type. There should now be 10 (type a and type b) */
i = 0;
cat = NULL;
while ((cat = ast_category_browse_filtered(cfg, NULL, cat, "TEMPLATES=include"))) {
i++;
}
if (i != 10) {
ast_test_status_update(test, "There were %d matches instead of 10.\n", i);
goto out;
}
/* Test again with 'restrict' and type b. There should 0 */
i = 0;
cat = NULL;
while ((cat = ast_category_browse_filtered(cfg, NULL, cat, "TEMPLATES=restrict,type=b"))) {
i++;
}
if (i != 0) {
ast_test_status_update(test, "There were %d matches instead of 0.\n", i);
goto out;
}
res = AST_TEST_PASS;
out:
ast_config_destroy(cfg);
return res;
}
818
819
820
821
822
823
824
825
826
827
828
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
873
874
875
876
877
878
879
880
881
882
883
884
/*!
* \brief Write the config file to disk
*
* This is necessary for testing config hooks since
* they are only triggered when a config is read from
* its intended storage medium
*/
static int write_config_file(void)
{
int i;
FILE *config_file;
char filename[PATH_MAX];
snprintf(filename, sizeof(filename), "%s/%s",
ast_config_AST_CONFIG_DIR, CONFIG_FILE);
config_file = fopen(filename, "w");
if (!config_file) {
return -1;
}
for (i = 0; i < ARRAY_LEN(categories); ++i) {
int j;
fprintf(config_file, "[%s]\n", categories[i].category);
for (j = 0; j < ARRAY_LEN(categories[i].vars); ++j) {
fprintf(config_file, "%s = %s\n",
categories[i].vars[j].name,
categories[i].vars[j].val);
}
}
fclose(config_file);
return 0;
}
/*!
* \brief Delete config file created by write_config_file
*/
static void delete_config_file(void)
{
char filename[PATH_MAX];
snprintf(filename, sizeof(filename), "%s/%s",
ast_config_AST_CONFIG_DIR, CONFIG_FILE);
unlink(filename);
}
/*
* Boolean to indicate if the config hook has run
*/
static int hook_run;
/*
* Boolean to indicate if, when the hook runs, the
* data passed to it is what is expected
*/
static int hook_config_sane;
static int hook_cb(struct ast_config *cfg)
{
hook_run = 1;
if (test_config_validity(cfg) == 0) {
hook_config_sane = 1;
}
ast_config_destroy(cfg);
return 0;
}
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
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
AST_TEST_DEFINE(config_save)
{
enum ast_test_result_state res = AST_TEST_FAIL;
struct ast_flags config_flags = { 0 };
struct ast_config *cfg;
char config_filename[PATH_MAX];
char include_filename[PATH_MAX];
struct stat config_stat;
off_t before_save;
switch (cmd) {
case TEST_INIT:
info->name = "config_save";
info->category = "/main/config/";
info->summary = "Test config save";
info->description =
"Test configuration save.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
if (write_config_file()) {
ast_test_status_update(test, "Could not write initial config files\n");
return res;
}
snprintf(config_filename, PATH_MAX, "%s/%s", ast_config_AST_CONFIG_DIR, CONFIG_FILE);
snprintf(include_filename, PATH_MAX, "%s/%s", ast_config_AST_CONFIG_DIR, CONFIG_INCLUDE_FILE);
cfg = ast_config_load(CONFIG_FILE, config_flags);
if (!cfg) {
ast_test_status_update(test, "Could not load config\n");
goto out;
}
/* We need to re-save to get the generator header */
if (ast_config_text_file_save(CONFIG_FILE, cfg, "TEST")) {
ast_test_status_update(test, "Unable to write files\n");
goto out;
}
stat(config_filename, &config_stat);
before_save = config_stat.st_size;
if (!ast_include_new(cfg, CONFIG_FILE, CONFIG_INCLUDE_FILE, 0, NULL, 4, include_filename, PATH_MAX)) {
ast_test_status_update(test, "Could not create include\n");
goto out;
}
if (ast_config_text_file_save(CONFIG_FILE, cfg, "TEST")) {
ast_test_status_update(test, "Unable to write files\n");
goto out;
}
stat(config_filename, &config_stat);
if (config_stat.st_size <= before_save) {
ast_test_status_update(test, "Did not save config file with #include\n");
goto out;
}
res = AST_TEST_PASS;
out:
ast_config_destroy(cfg);
unlink(config_filename);
unlink(include_filename);
return res;
}
AST_TEST_DEFINE(config_hook)
{
enum ast_test_result_state res = AST_TEST_FAIL;
enum config_hook_flags hook_flags = { 0, };
struct ast_flags config_flags = { 0 };
struct ast_flags reload_flags = { CONFIG_FLAG_FILEUNCHANGED };
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
struct ast_config *cfg;
switch (cmd) {
case TEST_INIT:
info->name = "config_hook";
info->category = "/main/config/";
info->summary = "Test config hooks";
info->description =
"Ensure that config hooks are called at approriate times,"
"not called at inappropriate times, and that all information"
"that should be present is present.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
write_config_file();
/*
* Register a config hook to run when CONFIG_FILE is loaded by this module
*/
ast_config_hook_register("test_hook",
CONFIG_FILE,
AST_MODULE,
hook_flags,
hook_cb);
/*
* Try loading the config file. This should result in the hook
* being called
*/
cfg = ast_config_load(CONFIG_FILE, config_flags);
ast_config_destroy(cfg);
if (!hook_run || !hook_config_sane) {
ast_test_status_update(test, "Config hook either did not run or was given bad data!\n");
goto out;
}
/*