Merge "test_http_media_cache: Fix failing test."
[asterisk/asterisk.git] / tests / test_callerid.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2014, Kinsey Moore
5  *
6  * Kinsey Moore <kmoore@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 Callerid Tests
22  *
23  * \author\verbatim Kinsey Moore <kmoore@digium.com> \endverbatim
24  *
25  * This is an Asterisk test module for callerid functionality
26  * \ingroup tests
27  */
28
29 /*** MODULEINFO
30         <depend>TEST_FRAMEWORK</depend>
31         <support_level>core</support_level>
32  ***/
33
34 #include "asterisk.h"
35 #include "asterisk/callerid.h"
36
37 ASTERISK_REGISTER_FILE()
38
39 #include "asterisk/utils.h"
40 #include "asterisk/module.h"
41 #include "asterisk/test.h"
42
43 struct cid_set {
44         char *cid;
45         char *name;
46         char *number;
47 };
48
49 AST_TEST_DEFINE(parse_nominal)
50 {
51         static const struct cid_set cid_sets[] = {
52                 {"\"name\" <number>", "name", "number"},
53                 {"\"   name  \" <number>", "   name  ", "number"},
54                 {"name <number>", "name", "number"},
55                 {"         name     <number>", "name", "number"},
56                 {"\"\" <number>", NULL, "number"},
57                 {"<number>", NULL, "number"},
58                 {"name", "name", NULL},
59                 {" name", "name", NULL},
60                 {"\"name\"", "name", NULL},
61                 {"\"*10\"", "*10", NULL},
62                 {" \"*10\"", "*10", NULL},
63                 {"\"name\" <>", "name", NULL},
64                 {"name <>", "name", NULL},
65                 {"1234", NULL, "1234"},
66                 {" 1234", NULL, "1234"},
67                 {"\"na\\\"me\" <number>", "na\"me", "number"},
68         };
69         char *name;
70         char *number;
71         int i;
72
73         switch (cmd) {
74         case TEST_INIT:
75                 info->name = "parse_nominal";
76                 info->category = "/main/callerid/";
77                 info->summary = "Callerid nominal parse unit test";
78                 info->description =
79                         "This tests parsing of nominal callerid strings.";
80                 return AST_TEST_NOT_RUN;
81         case TEST_EXECUTE:
82                 break;
83         }
84
85         for (i = 0; i < ARRAY_LEN(cid_sets); i++) {
86                 RAII_VAR(char *, callerid, ast_strdup(cid_sets[i].cid), ast_free);
87
88                 ast_callerid_parse(callerid, &name, &number);
89                 if (!cid_sets[i].name == !ast_strlen_zero(name) || (cid_sets[i].name && strcmp(name, cid_sets[i].name))) {
90                         ast_test_status_update(test,
91                                 "Expected callerid name '%s' instead of '%s'\n",
92                                 cid_sets[i].name, name);
93                         return AST_TEST_FAIL;
94                 }
95
96                 if (!cid_sets[i].number == !ast_strlen_zero(number) || (cid_sets[i].number && strcmp(number, cid_sets[i].number))) {
97                         ast_test_status_update(test,
98                                 "Expected callerid number '%s' instead of '%s'\n",
99                                 cid_sets[i].number, number);
100                         return AST_TEST_FAIL;
101                 }
102         }
103
104         return AST_TEST_PASS;
105 }
106
107 AST_TEST_DEFINE(parse_off_nominal)
108 {
109         static const struct cid_set cid_sets[] = {
110                 {"na\\\"me <number>", "na\"me", "number"},
111                 {"\"na\"me\" <number>", "na\"me", "number"},
112                 {"na\"me <number>", "na\"me", "number"},
113                 {"\"name <number>", "\"name", "number"},
114                 {"name <number", "name", "number"},
115                 {"\"name <number>\"", "name", "number"},
116         };
117         char *name;
118         char *number;
119         int i;
120
121         switch (cmd) {
122         case TEST_INIT:
123                 info->name = "parse_off_nominal";
124                 info->category = "/main/callerid/";
125                 info->summary = "Callerid off-nominal parse unit test";
126                 info->description =
127                         "This tests parsing of off-nominal callerid strings.";
128                 return AST_TEST_NOT_RUN;
129         case TEST_EXECUTE:
130                 break;
131         }
132
133         for (i = 0; i < ARRAY_LEN(cid_sets); i++) {
134                 RAII_VAR(char *, callerid, ast_strdup(cid_sets[i].cid), ast_free);
135
136                 ast_callerid_parse(callerid, &name, &number);
137                 if (!cid_sets[i].name == !ast_strlen_zero(name) || (cid_sets[i].name && strcmp(name, cid_sets[i].name))) {
138                         ast_test_status_update(test,
139                                 "Expected callerid name '%s' instead of '%s'\n",
140                                 cid_sets[i].name, name);
141                         return AST_TEST_FAIL;
142                 }
143
144                 if (!cid_sets[i].number == !ast_strlen_zero(number) || (cid_sets[i].number && strcmp(number, cid_sets[i].number))) {
145                         ast_test_status_update(test,
146                                 "Expected callerid number '%s' instead of '%s'\n",
147                                 cid_sets[i].number, number);
148                         return AST_TEST_FAIL;
149                 }
150         }
151
152         return AST_TEST_PASS;
153 }
154
155 static int unload_module(void)
156 {
157         AST_TEST_UNREGISTER(parse_nominal);
158         AST_TEST_UNREGISTER(parse_off_nominal);
159         return 0;
160 }
161
162 static int load_module(void)
163 {
164         AST_TEST_REGISTER(parse_nominal);
165         AST_TEST_REGISTER(parse_off_nominal);
166         return AST_MODULE_LOAD_SUCCESS;
167 }
168
169 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Callerid Parse Tests");