Merge "lpc10: Avoid compiler warning when DONT_OPTIMIZE/COMPILE_DOUBLE."
[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 #include "asterisk/utils.h"
38 #include "asterisk/module.h"
39 #include "asterisk/test.h"
40
41 struct cid_set {
42         char *cid;
43         char *name;
44         char *number;
45 };
46
47 AST_TEST_DEFINE(parse_nominal)
48 {
49         static const struct cid_set cid_sets[] = {
50                 {"\"name\" <number>", "name", "number"},
51                 {"\"   name  \" <number>", "   name  ", "number"},
52                 {"name <number>", "name", "number"},
53                 {"         name     <number>", "name", "number"},
54                 {"\"\" <number>", NULL, "number"},
55                 {"<number>", NULL, "number"},
56                 {"name", "name", NULL},
57                 {" name", "name", NULL},
58                 {"\"name\"", "name", NULL},
59                 {"\"*10\"", "*10", NULL},
60                 {" \"*10\"", "*10", NULL},
61                 {"\"name\" <>", "name", NULL},
62                 {"name <>", "name", NULL},
63                 {"1234", NULL, "1234"},
64                 {" 1234", NULL, "1234"},
65                 {"\"na\\\"me\" <number>", "na\"me", "number"},
66         };
67         char *name;
68         char *number;
69         int i;
70
71         switch (cmd) {
72         case TEST_INIT:
73                 info->name = "parse_nominal";
74                 info->category = "/main/callerid/";
75                 info->summary = "Callerid nominal parse unit test";
76                 info->description =
77                         "This tests parsing of nominal callerid strings.";
78                 return AST_TEST_NOT_RUN;
79         case TEST_EXECUTE:
80                 break;
81         }
82
83         for (i = 0; i < ARRAY_LEN(cid_sets); i++) {
84                 RAII_VAR(char *, callerid, ast_strdup(cid_sets[i].cid), ast_free);
85
86                 ast_callerid_parse(callerid, &name, &number);
87                 if (!cid_sets[i].name == !ast_strlen_zero(name) || (cid_sets[i].name && strcmp(name, cid_sets[i].name))) {
88                         ast_test_status_update(test,
89                                 "Expected callerid name '%s' instead of '%s'\n",
90                                 cid_sets[i].name, name);
91                         return AST_TEST_FAIL;
92                 }
93
94                 if (!cid_sets[i].number == !ast_strlen_zero(number) || (cid_sets[i].number && strcmp(number, cid_sets[i].number))) {
95                         ast_test_status_update(test,
96                                 "Expected callerid number '%s' instead of '%s'\n",
97                                 cid_sets[i].number, number);
98                         return AST_TEST_FAIL;
99                 }
100         }
101
102         return AST_TEST_PASS;
103 }
104
105 AST_TEST_DEFINE(parse_off_nominal)
106 {
107         static const struct cid_set cid_sets[] = {
108                 {"na\\\"me <number>", "na\"me", "number"},
109                 {"\"na\"me\" <number>", "na\"me", "number"},
110                 {"na\"me <number>", "na\"me", "number"},
111                 {"\"name <number>", "\"name", "number"},
112                 {"name <number", "name", "number"},
113                 {"\"name <number>\"", "name", "number"},
114         };
115         char *name;
116         char *number;
117         int i;
118
119         switch (cmd) {
120         case TEST_INIT:
121                 info->name = "parse_off_nominal";
122                 info->category = "/main/callerid/";
123                 info->summary = "Callerid off-nominal parse unit test";
124                 info->description =
125                         "This tests parsing of off-nominal callerid strings.";
126                 return AST_TEST_NOT_RUN;
127         case TEST_EXECUTE:
128                 break;
129         }
130
131         for (i = 0; i < ARRAY_LEN(cid_sets); i++) {
132                 RAII_VAR(char *, callerid, ast_strdup(cid_sets[i].cid), ast_free);
133
134                 ast_callerid_parse(callerid, &name, &number);
135                 if (!cid_sets[i].name == !ast_strlen_zero(name) || (cid_sets[i].name && strcmp(name, cid_sets[i].name))) {
136                         ast_test_status_update(test,
137                                 "Expected callerid name '%s' instead of '%s'\n",
138                                 cid_sets[i].name, name);
139                         return AST_TEST_FAIL;
140                 }
141
142                 if (!cid_sets[i].number == !ast_strlen_zero(number) || (cid_sets[i].number && strcmp(number, cid_sets[i].number))) {
143                         ast_test_status_update(test,
144                                 "Expected callerid number '%s' instead of '%s'\n",
145                                 cid_sets[i].number, number);
146                         return AST_TEST_FAIL;
147                 }
148         }
149
150         return AST_TEST_PASS;
151 }
152
153 static int unload_module(void)
154 {
155         AST_TEST_UNREGISTER(parse_nominal);
156         AST_TEST_UNREGISTER(parse_off_nominal);
157         return 0;
158 }
159
160 static int load_module(void)
161 {
162         AST_TEST_REGISTER(parse_nominal);
163         AST_TEST_REGISTER(parse_off_nominal);
164         return AST_MODULE_LOAD_SUCCESS;
165 }
166
167 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Callerid Parse Tests");