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