ede2b23447800462a46815cd2475d2a9e2d83962
[asterisk/asterisk.git] / tests / test_devicestate.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2010, Digium, Inc.
5  *
6  * David Vossel <dvossel@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 Device State Test Module
22  *
23  * \author David Vossel <dvossel@digium.com>
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 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36
37 #include "asterisk/utils.h"
38 #include "asterisk/module.h"
39 #include "asterisk/test.h"
40 #include "asterisk/devicestate.h"
41 #include "asterisk/pbx.h"
42
43
44 /* These arrays are the result of the 'core show device2extenstate' output. */
45 static int combined_results[] = {
46         AST_DEVICE_UNKNOWN,
47         AST_DEVICE_NOT_INUSE,
48         AST_DEVICE_INUSE,
49         AST_DEVICE_BUSY,
50         AST_DEVICE_UNKNOWN,
51         AST_DEVICE_UNAVAILABLE,
52         AST_DEVICE_RINGING,
53         AST_DEVICE_RINGINUSE,
54         AST_DEVICE_ONHOLD,
55         AST_DEVICE_NOT_INUSE,
56         AST_DEVICE_NOT_INUSE,
57         AST_DEVICE_INUSE,
58         AST_DEVICE_BUSY,
59         AST_DEVICE_NOT_INUSE,
60         AST_DEVICE_NOT_INUSE,
61         AST_DEVICE_RINGING,
62         AST_DEVICE_RINGINUSE,
63         AST_DEVICE_ONHOLD,
64         AST_DEVICE_INUSE,
65         AST_DEVICE_INUSE,
66         AST_DEVICE_INUSE,
67         AST_DEVICE_BUSY,
68         AST_DEVICE_INUSE,
69         AST_DEVICE_INUSE,
70         AST_DEVICE_RINGINUSE,
71         AST_DEVICE_RINGINUSE,
72         AST_DEVICE_INUSE,
73         AST_DEVICE_BUSY,
74         AST_DEVICE_BUSY,
75         AST_DEVICE_BUSY,
76         AST_DEVICE_BUSY,
77         AST_DEVICE_BUSY,
78         AST_DEVICE_BUSY,
79         AST_DEVICE_RINGINUSE,
80         AST_DEVICE_RINGINUSE,
81         AST_DEVICE_BUSY,
82         AST_DEVICE_UNKNOWN,
83         AST_DEVICE_NOT_INUSE,
84         AST_DEVICE_INUSE,
85         AST_DEVICE_BUSY,
86         AST_DEVICE_UNKNOWN,
87         AST_DEVICE_UNAVAILABLE,
88         AST_DEVICE_RINGING,
89         AST_DEVICE_RINGINUSE,
90         AST_DEVICE_ONHOLD,
91         AST_DEVICE_UNAVAILABLE,
92         AST_DEVICE_NOT_INUSE,
93         AST_DEVICE_INUSE,
94         AST_DEVICE_BUSY,
95         AST_DEVICE_UNAVAILABLE,
96         AST_DEVICE_UNAVAILABLE,
97         AST_DEVICE_RINGING,
98         AST_DEVICE_RINGINUSE,
99         AST_DEVICE_ONHOLD,
100         AST_DEVICE_RINGING,
101         AST_DEVICE_RINGING,
102         AST_DEVICE_RINGINUSE,
103         AST_DEVICE_RINGINUSE,
104         AST_DEVICE_RINGING,
105         AST_DEVICE_RINGING,
106         AST_DEVICE_RINGING,
107         AST_DEVICE_RINGINUSE,
108         AST_DEVICE_RINGINUSE,
109         AST_DEVICE_RINGINUSE,
110         AST_DEVICE_RINGINUSE,
111         AST_DEVICE_RINGINUSE,
112         AST_DEVICE_RINGINUSE,
113         AST_DEVICE_RINGINUSE,
114         AST_DEVICE_RINGINUSE,
115         AST_DEVICE_RINGINUSE,
116         AST_DEVICE_RINGINUSE,
117         AST_DEVICE_RINGINUSE,
118         AST_DEVICE_ONHOLD,
119         AST_DEVICE_ONHOLD,
120         AST_DEVICE_INUSE,
121         AST_DEVICE_BUSY,
122         AST_DEVICE_ONHOLD,
123         AST_DEVICE_ONHOLD,
124         AST_DEVICE_RINGINUSE,
125         AST_DEVICE_RINGINUSE,
126         AST_DEVICE_ONHOLD,
127 };
128
129 static int exten_results[] = {
130         AST_EXTENSION_NOT_INUSE,
131         AST_EXTENSION_NOT_INUSE,
132         AST_EXTENSION_INUSE,
133         AST_EXTENSION_BUSY,
134         AST_EXTENSION_NOT_INUSE,
135         AST_EXTENSION_UNAVAILABLE,
136         AST_EXTENSION_RINGING,
137         AST_EXTENSION_INUSE | AST_EXTENSION_RINGING,
138         AST_EXTENSION_ONHOLD,
139         AST_EXTENSION_NOT_INUSE,
140         AST_EXTENSION_NOT_INUSE,
141         AST_EXTENSION_INUSE,
142         AST_EXTENSION_BUSY,
143         AST_EXTENSION_NOT_INUSE,
144         AST_EXTENSION_NOT_INUSE,
145         AST_EXTENSION_RINGING,
146         AST_EXTENSION_INUSE | AST_EXTENSION_RINGING,
147         AST_EXTENSION_ONHOLD,
148         AST_EXTENSION_INUSE,
149         AST_EXTENSION_INUSE,
150         AST_EXTENSION_INUSE,
151         AST_EXTENSION_BUSY,
152         AST_EXTENSION_INUSE,
153         AST_EXTENSION_INUSE,
154         AST_EXTENSION_INUSE | AST_EXTENSION_RINGING,
155         AST_EXTENSION_INUSE | AST_EXTENSION_RINGING,
156         AST_EXTENSION_INUSE,
157         AST_EXTENSION_BUSY,
158         AST_EXTENSION_BUSY,
159         AST_EXTENSION_BUSY,
160         AST_EXTENSION_BUSY,
161         AST_EXTENSION_BUSY,
162         AST_EXTENSION_BUSY,
163         AST_EXTENSION_INUSE | AST_EXTENSION_RINGING,
164         AST_EXTENSION_INUSE | AST_EXTENSION_RINGING,
165         AST_EXTENSION_BUSY,
166         AST_EXTENSION_NOT_INUSE,
167         AST_EXTENSION_NOT_INUSE,
168         AST_EXTENSION_INUSE,
169         AST_EXTENSION_BUSY,
170         AST_EXTENSION_NOT_INUSE,
171         AST_EXTENSION_UNAVAILABLE,
172         AST_EXTENSION_RINGING,
173         AST_EXTENSION_INUSE | AST_EXTENSION_RINGING,
174         AST_EXTENSION_ONHOLD,
175         AST_EXTENSION_UNAVAILABLE,
176         AST_EXTENSION_NOT_INUSE,
177         AST_EXTENSION_INUSE,
178         AST_EXTENSION_BUSY,
179         AST_EXTENSION_UNAVAILABLE,
180         AST_EXTENSION_UNAVAILABLE,
181         AST_EXTENSION_RINGING,
182         AST_EXTENSION_INUSE | AST_EXTENSION_RINGING,
183         AST_EXTENSION_ONHOLD,
184         AST_EXTENSION_RINGING,
185         AST_EXTENSION_RINGING,
186         AST_EXTENSION_INUSE | AST_EXTENSION_RINGING,
187         AST_EXTENSION_INUSE | AST_EXTENSION_RINGING,
188         AST_EXTENSION_RINGING,
189         AST_EXTENSION_RINGING,
190         AST_EXTENSION_RINGING,
191         AST_EXTENSION_INUSE | AST_EXTENSION_RINGING,
192         AST_EXTENSION_INUSE | AST_EXTENSION_RINGING,
193         AST_EXTENSION_INUSE | AST_EXTENSION_RINGING,
194         AST_EXTENSION_INUSE | AST_EXTENSION_RINGING,
195         AST_EXTENSION_INUSE | AST_EXTENSION_RINGING,
196         AST_EXTENSION_INUSE | AST_EXTENSION_RINGING,
197         AST_EXTENSION_INUSE | AST_EXTENSION_RINGING,
198         AST_EXTENSION_INUSE | AST_EXTENSION_RINGING,
199         AST_EXTENSION_INUSE | AST_EXTENSION_RINGING,
200         AST_EXTENSION_INUSE | AST_EXTENSION_RINGING,
201         AST_EXTENSION_INUSE | AST_EXTENSION_RINGING,
202         AST_EXTENSION_ONHOLD,
203         AST_EXTENSION_ONHOLD,
204         AST_EXTENSION_INUSE,
205         AST_EXTENSION_BUSY,
206         AST_EXTENSION_ONHOLD,
207         AST_EXTENSION_ONHOLD,
208         AST_EXTENSION_INUSE | AST_EXTENSION_RINGING,
209         AST_EXTENSION_INUSE | AST_EXTENSION_RINGING,
210         AST_EXTENSION_ONHOLD,
211 };
212
213 AST_TEST_DEFINE(device2extenstate_test)
214 {
215         int res = AST_TEST_PASS;
216         struct ast_devstate_aggregate agg;
217         enum ast_device_state i, j, combined;
218         enum ast_extension_states exten;
219         int k = 0;
220
221         switch (cmd) {
222         case TEST_INIT:
223                 info->name = "device2extenstate_test";
224                 info->category = "/main/devicestate/";
225                 info->summary = "Tests combined devstate mapping and device to extension state mapping.";
226                 info->description =
227                         "Verifies device state aggregate results match the expected combined "
228                         "devstate.  Then verifies the combined devstate maps to the expected "
229                         "extension state.";
230                 return AST_TEST_NOT_RUN;
231         case TEST_EXECUTE:
232                 break;
233         }
234
235         if (ARRAY_LEN(exten_results) != (AST_DEVICE_TOTAL * AST_DEVICE_TOTAL)) {
236                 ast_test_status_update(test, "Result array is %d long when it should be %d. "
237                         "Something has changed, this test must be updated.\n",
238                         (int) ARRAY_LEN(exten_results), (AST_DEVICE_TOTAL * AST_DEVICE_TOTAL));
239                 return AST_TEST_FAIL;
240         }
241
242         if (ARRAY_LEN(combined_results) != ARRAY_LEN(exten_results)) {
243                 ast_test_status_update(test, "combined_results and exten_results arrays do not match in length.\n");
244                 return AST_TEST_FAIL;
245         }
246
247         for (i = 0; i < AST_DEVICE_TOTAL; i++) {
248                 for (j = 0; j < AST_DEVICE_TOTAL; j++) {
249                         ast_devstate_aggregate_init(&agg);
250                         ast_devstate_aggregate_add(&agg, i);
251                         ast_devstate_aggregate_add(&agg, j);
252                         combined = ast_devstate_aggregate_result(&agg);
253                         if (combined_results[k] != combined) {
254                                 ast_test_status_update(test, "Expected combined dev state %s "
255                                         "does not match %s at combined_result[%d].\n",
256                                         ast_devstate2str(combined_results[k]),
257                                         ast_devstate2str(combined), k);
258                                 res = AST_TEST_FAIL;
259                         }
260
261                         exten = ast_devstate_to_extenstate(combined);
262
263                         if (exten_results[k] != exten) {
264                                 ast_test_status_update(test, "Expected exten state %s "
265                                         "does not match %s at exten_result[%d]\n",
266                                         ast_extension_state2str(exten_results[k]),
267                                         ast_extension_state2str(exten), k);
268                                 res = AST_TEST_FAIL;
269                         }
270                         k++;
271                 }
272         }
273
274         return res;
275 }
276
277 static int unload_module(void)
278 {
279         AST_TEST_UNREGISTER(device2extenstate_test);
280         return 0;
281 }
282
283 static int load_module(void)
284 {
285         AST_TEST_REGISTER(device2extenstate_test);
286         return AST_MODULE_LOAD_SUCCESS;
287 }
288
289 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Device State Test");