test_devicestate.c 7.4 KB

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