app_test.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. * Russell Bryant <russelb@clemson.edu>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*! \file
  20. *
  21. * \brief Applications to test connection and produce report in text file
  22. *
  23. * \author Mark Spencer <markster@digium.com>
  24. * \author Russell Bryant <russelb@clemson.edu>
  25. *
  26. * \ingroup applications
  27. */
  28. /*** MODULEINFO
  29. <support_level>extended</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include <sys/stat.h>
  34. #include "asterisk/paths.h" /* use ast_config_AST_LOG_DIR */
  35. #include "asterisk/channel.h"
  36. #include "asterisk/module.h"
  37. #include "asterisk/lock.h"
  38. #include "asterisk/app.h"
  39. #include "asterisk/pbx.h"
  40. #include "asterisk/utils.h"
  41. /*** DOCUMENTATION
  42. <application name="TestServer" language="en_US">
  43. <synopsis>
  44. Execute Interface Test Server.
  45. </synopsis>
  46. <syntax />
  47. <description>
  48. <para>Perform test server function and write call report. Results stored in
  49. <filename>/var/log/asterisk/testreports/&lt;testid&gt;-server.txt</filename></para>
  50. </description>
  51. <see-also>
  52. <ref type="application">TestClient</ref>
  53. </see-also>
  54. </application>
  55. <application name="TestClient" language="en_US">
  56. <synopsis>
  57. Execute Interface Test Client.
  58. </synopsis>
  59. <syntax>
  60. <parameter name="testid" required="true">
  61. <para>An ID to identify this test.</para>
  62. </parameter>
  63. </syntax>
  64. <description>
  65. <para>Executes test client with given <replaceable>testid</replaceable>. Results stored in
  66. <filename>/var/log/asterisk/testreports/&lt;testid&gt;-client.txt</filename></para>
  67. </description>
  68. <see-also>
  69. <ref type="application">TestServer</ref>
  70. </see-also>
  71. </application>
  72. ***/
  73. static char *tests_app = "TestServer";
  74. static char *testc_app = "TestClient";
  75. static int measurenoise(struct ast_channel *chan, int ms, char *who)
  76. {
  77. int res=0;
  78. int mssofar;
  79. int noise=0;
  80. int samples=0;
  81. int x;
  82. short *foo;
  83. struct timeval start;
  84. struct ast_frame *f;
  85. struct ast_format rformat;
  86. ast_format_copy(&rformat, ast_channel_readformat(chan));
  87. if (ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR)) {
  88. ast_log(LOG_NOTICE, "Unable to set to linear mode!\n");
  89. return -1;
  90. }
  91. start = ast_tvnow();
  92. for(;;) {
  93. mssofar = ast_tvdiff_ms(ast_tvnow(), start);
  94. if (mssofar > ms)
  95. break;
  96. res = ast_waitfor(chan, ms - mssofar);
  97. if (res < 1)
  98. break;
  99. f = ast_read(chan);
  100. if (!f) {
  101. res = -1;
  102. break;
  103. }
  104. if ((f->frametype == AST_FRAME_VOICE) && (f->subclass.format.id == AST_FORMAT_SLINEAR)) {
  105. foo = (short *)f->data.ptr;
  106. for (x=0;x<f->samples;x++) {
  107. noise += abs(foo[x]);
  108. samples++;
  109. }
  110. }
  111. ast_frfree(f);
  112. }
  113. if (rformat.id) {
  114. if (ast_set_read_format(chan, &rformat)) {
  115. ast_log(LOG_NOTICE, "Unable to restore original format!\n");
  116. return -1;
  117. }
  118. }
  119. if (res < 0)
  120. return res;
  121. if (!samples) {
  122. ast_log(LOG_NOTICE, "No samples were received from the other side!\n");
  123. return -1;
  124. }
  125. ast_debug(1, "%s: Noise: %d, samples: %d, avg: %d\n", who, noise, samples, noise / samples);
  126. return (noise / samples);
  127. }
  128. static int sendnoise(struct ast_channel *chan, int ms)
  129. {
  130. int res;
  131. res = ast_tonepair_start(chan, 1537, 2195, ms, 8192);
  132. if (!res) {
  133. res = ast_waitfordigit(chan, ms);
  134. ast_tonepair_stop(chan);
  135. }
  136. return res;
  137. }
  138. static int testclient_exec(struct ast_channel *chan, const char *data)
  139. {
  140. int res = 0;
  141. const char *testid=data;
  142. char fn[80];
  143. char serverver[80];
  144. FILE *f;
  145. /* Check for test id */
  146. if (ast_strlen_zero(testid)) {
  147. ast_log(LOG_WARNING, "TestClient requires an argument - the test id\n");
  148. return -1;
  149. }
  150. if (ast_channel_state(chan) != AST_STATE_UP)
  151. res = ast_answer(chan);
  152. /* Wait a few just to be sure things get started */
  153. res = ast_safe_sleep(chan, 3000);
  154. /* Transmit client version */
  155. if (!res)
  156. res = ast_dtmf_stream(chan, NULL, "8378*1#", 0, 0);
  157. ast_debug(1, "Transmit client version\n");
  158. /* Read server version */
  159. ast_debug(1, "Read server version\n");
  160. if (!res)
  161. res = ast_app_getdata(chan, NULL, serverver, sizeof(serverver) - 1, 0);
  162. if (res > 0)
  163. res = 0;
  164. ast_debug(1, "server version: %s\n", serverver);
  165. if (res > 0)
  166. res = 0;
  167. if (!res)
  168. res = ast_safe_sleep(chan, 1000);
  169. /* Send test id */
  170. if (!res)
  171. res = ast_dtmf_stream(chan, NULL, testid, 0, 0);
  172. if (!res)
  173. res = ast_dtmf_stream(chan, NULL, "#", 0, 0);
  174. ast_debug(1, "send test identifier: %s\n", testid);
  175. if ((res >=0) && (!ast_strlen_zero(testid))) {
  176. /* Make the directory to hold the test results in case it's not there */
  177. snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
  178. ast_mkdir(fn, 0777);
  179. snprintf(fn, sizeof(fn), "%s/testresults/%s-client.txt", ast_config_AST_LOG_DIR, testid);
  180. if ((f = fopen(fn, "w+"))) {
  181. setlinebuf(f);
  182. fprintf(f, "CLIENTCHAN: %s\n", ast_channel_name(chan));
  183. fprintf(f, "CLIENTTEST ID: %s\n", testid);
  184. fprintf(f, "ANSWER: PASS\n");
  185. res = 0;
  186. if (!res) {
  187. /* Step 1: Wait for "1" */
  188. ast_debug(1, "TestClient: 2. Wait DTMF 1\n");
  189. res = ast_waitfordigit(chan, 3000);
  190. fprintf(f, "WAIT DTMF 1: %s\n", (res != '1') ? "FAIL" : "PASS");
  191. if (res == '1')
  192. res = 0;
  193. else
  194. res = -1;
  195. }
  196. if (!res) {
  197. res = ast_safe_sleep(chan, 1000);
  198. }
  199. if (!res) {
  200. /* Step 2: Send "2" */
  201. ast_debug(1, "TestClient: 2. Send DTMF 2\n");
  202. res = ast_dtmf_stream(chan, NULL, "2", 0, 0);
  203. fprintf(f, "SEND DTMF 2: %s\n", (res < 0) ? "FAIL" : "PASS");
  204. if (res > 0)
  205. res = 0;
  206. }
  207. if (!res) {
  208. /* Step 3: Wait one second */
  209. ast_debug(1, "TestClient: 3. Wait one second\n");
  210. res = ast_safe_sleep(chan, 1000);
  211. fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
  212. if (res > 0)
  213. res = 0;
  214. }
  215. if (!res) {
  216. /* Step 4: Measure noise */
  217. ast_debug(1, "TestClient: 4. Measure noise\n");
  218. res = measurenoise(chan, 5000, "TestClient");
  219. fprintf(f, "MEASURENOISE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
  220. if (res > 0)
  221. res = 0;
  222. }
  223. if (!res) {
  224. /* Step 5: Wait for "4" */
  225. ast_debug(1, "TestClient: 5. Wait DTMF 4\n");
  226. res = ast_waitfordigit(chan, 3000);
  227. fprintf(f, "WAIT DTMF 4: %s\n", (res != '4') ? "FAIL" : "PASS");
  228. if (res == '4')
  229. res = 0;
  230. else
  231. res = -1;
  232. }
  233. if (!res) {
  234. /* Step 6: Transmit tone noise */
  235. ast_debug(1, "TestClient: 6. Transmit tone\n");
  236. res = sendnoise(chan, 6000);
  237. fprintf(f, "SENDTONE: %s\n", (res < 0) ? "FAIL" : "PASS");
  238. }
  239. if (!res || (res == '5')) {
  240. /* Step 7: Wait for "5" */
  241. ast_debug(1, "TestClient: 7. Wait DTMF 5\n");
  242. if (!res)
  243. res = ast_waitfordigit(chan, 3000);
  244. fprintf(f, "WAIT DTMF 5: %s\n", (res != '5') ? "FAIL" : "PASS");
  245. if (res == '5')
  246. res = 0;
  247. else
  248. res = -1;
  249. }
  250. if (!res) {
  251. /* Step 8: Wait one second */
  252. ast_debug(1, "TestClient: 8. Wait one second\n");
  253. res = ast_safe_sleep(chan, 1000);
  254. fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
  255. if (res > 0)
  256. res = 0;
  257. }
  258. if (!res) {
  259. /* Step 9: Measure noise */
  260. ast_debug(1, "TestClient: 9. Measure tone\n");
  261. res = measurenoise(chan, 4000, "TestClient");
  262. fprintf(f, "MEASURETONE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
  263. if (res > 0)
  264. res = 0;
  265. }
  266. if (!res) {
  267. /* Step 10: Send "7" */
  268. ast_debug(1, "TestClient: 10. Send DTMF 7\n");
  269. res = ast_dtmf_stream(chan, NULL, "7", 0, 0);
  270. fprintf(f, "SEND DTMF 7: %s\n", (res < 0) ? "FAIL" : "PASS");
  271. if (res > 0)
  272. res =0;
  273. }
  274. if (!res) {
  275. /* Step 11: Wait for "8" */
  276. ast_debug(1, "TestClient: 11. Wait DTMF 8\n");
  277. res = ast_waitfordigit(chan, 3000);
  278. fprintf(f, "WAIT DTMF 8: %s\n", (res != '8') ? "FAIL" : "PASS");
  279. if (res == '8')
  280. res = 0;
  281. else
  282. res = -1;
  283. }
  284. if (!res) {
  285. res = ast_safe_sleep(chan, 1000);
  286. }
  287. if (!res) {
  288. /* Step 12: Hangup! */
  289. ast_debug(1, "TestClient: 12. Hangup\n");
  290. }
  291. ast_debug(1, "-- TEST COMPLETE--\n");
  292. fprintf(f, "-- END TEST--\n");
  293. fclose(f);
  294. res = -1;
  295. } else
  296. res = -1;
  297. } else {
  298. ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", ast_channel_name(chan));
  299. res = -1;
  300. }
  301. return res;
  302. }
  303. static int testserver_exec(struct ast_channel *chan, const char *data)
  304. {
  305. int res = 0;
  306. char testid[80]="";
  307. char fn[80];
  308. FILE *f;
  309. if (ast_channel_state(chan) != AST_STATE_UP)
  310. res = ast_answer(chan);
  311. /* Read version */
  312. ast_debug(1, "Read client version\n");
  313. if (!res)
  314. res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
  315. if (res > 0)
  316. res = 0;
  317. ast_debug(1, "client version: %s\n", testid);
  318. ast_debug(1, "Transmit server version\n");
  319. res = ast_safe_sleep(chan, 1000);
  320. if (!res)
  321. res = ast_dtmf_stream(chan, NULL, "8378*1#", 0, 0);
  322. if (res > 0)
  323. res = 0;
  324. if (!res)
  325. res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
  326. ast_debug(1, "read test identifier: %s\n", testid);
  327. /* Check for sneakyness */
  328. if (strchr(testid, '/'))
  329. res = -1;
  330. if ((res >=0) && (!ast_strlen_zero(testid))) {
  331. /* Got a Test ID! Whoo hoo! */
  332. /* Make the directory to hold the test results in case it's not there */
  333. snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR);
  334. ast_mkdir(fn, 0777);
  335. snprintf(fn, sizeof(fn), "%s/testresults/%s-server.txt", ast_config_AST_LOG_DIR, testid);
  336. if ((f = fopen(fn, "w+"))) {
  337. setlinebuf(f);
  338. fprintf(f, "SERVERCHAN: %s\n", ast_channel_name(chan));
  339. fprintf(f, "SERVERTEST ID: %s\n", testid);
  340. fprintf(f, "ANSWER: PASS\n");
  341. ast_debug(1, "Processing Test ID '%s'\n", testid);
  342. res = ast_safe_sleep(chan, 1000);
  343. if (!res) {
  344. /* Step 1: Send "1" */
  345. ast_debug(1, "TestServer: 1. Send DTMF 1\n");
  346. res = ast_dtmf_stream(chan, NULL, "1", 0,0 );
  347. fprintf(f, "SEND DTMF 1: %s\n", (res < 0) ? "FAIL" : "PASS");
  348. if (res > 0)
  349. res = 0;
  350. }
  351. if (!res) {
  352. /* Step 2: Wait for "2" */
  353. ast_debug(1, "TestServer: 2. Wait DTMF 2\n");
  354. res = ast_waitfordigit(chan, 3000);
  355. fprintf(f, "WAIT DTMF 2: %s\n", (res != '2') ? "FAIL" : "PASS");
  356. if (res == '2')
  357. res = 0;
  358. else
  359. res = -1;
  360. }
  361. if (!res) {
  362. /* Step 3: Measure noise */
  363. ast_debug(1, "TestServer: 3. Measure noise\n");
  364. res = measurenoise(chan, 6000, "TestServer");
  365. fprintf(f, "MEASURENOISE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
  366. if (res > 0)
  367. res = 0;
  368. }
  369. if (!res) {
  370. /* Step 4: Send "4" */
  371. ast_debug(1, "TestServer: 4. Send DTMF 4\n");
  372. res = ast_dtmf_stream(chan, NULL, "4", 0, 0);
  373. fprintf(f, "SEND DTMF 4: %s\n", (res < 0) ? "FAIL" : "PASS");
  374. if (res > 0)
  375. res = 0;
  376. }
  377. if (!res) {
  378. /* Step 5: Wait one second */
  379. ast_debug(1, "TestServer: 5. Wait one second\n");
  380. res = ast_safe_sleep(chan, 1000);
  381. fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS");
  382. if (res > 0)
  383. res = 0;
  384. }
  385. if (!res) {
  386. /* Step 6: Measure noise */
  387. ast_debug(1, "TestServer: 6. Measure tone\n");
  388. res = measurenoise(chan, 4000, "TestServer");
  389. fprintf(f, "MEASURETONE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
  390. if (res > 0)
  391. res = 0;
  392. }
  393. if (!res) {
  394. /* Step 7: Send "5" */
  395. ast_debug(1, "TestServer: 7. Send DTMF 5\n");
  396. res = ast_dtmf_stream(chan, NULL, "5", 0, 0);
  397. fprintf(f, "SEND DTMF 5: %s\n", (res < 0) ? "FAIL" : "PASS");
  398. if (res > 0)
  399. res = 0;
  400. }
  401. if (!res) {
  402. /* Step 8: Transmit tone noise */
  403. ast_debug(1, "TestServer: 8. Transmit tone\n");
  404. res = sendnoise(chan, 6000);
  405. fprintf(f, "SENDTONE: %s\n", (res < 0) ? "FAIL" : "PASS");
  406. }
  407. if (!res || (res == '7')) {
  408. /* Step 9: Wait for "7" */
  409. ast_debug(1, "TestServer: 9. Wait DTMF 7\n");
  410. if (!res)
  411. res = ast_waitfordigit(chan, 3000);
  412. fprintf(f, "WAIT DTMF 7: %s\n", (res != '7') ? "FAIL" : "PASS");
  413. if (res == '7')
  414. res = 0;
  415. else
  416. res = -1;
  417. }
  418. if (!res) {
  419. res = ast_safe_sleep(chan, 1000);
  420. }
  421. if (!res) {
  422. /* Step 10: Send "8" */
  423. ast_debug(1, "TestServer: 10. Send DTMF 8\n");
  424. res = ast_dtmf_stream(chan, NULL, "8", 0, 0);
  425. fprintf(f, "SEND DTMF 8: %s\n", (res < 0) ? "FAIL" : "PASS");
  426. if (res > 0)
  427. res = 0;
  428. }
  429. if (!res) {
  430. /* Step 11: Wait for hangup to arrive! */
  431. ast_debug(1, "TestServer: 11. Waiting for hangup\n");
  432. res = ast_safe_sleep(chan, 10000);
  433. fprintf(f, "WAIT HANGUP: %s\n", (res < 0) ? "PASS" : "FAIL");
  434. }
  435. ast_log(LOG_NOTICE, "-- TEST COMPLETE--\n");
  436. fprintf(f, "-- END TEST--\n");
  437. fclose(f);
  438. res = -1;
  439. } else
  440. res = -1;
  441. } else {
  442. ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", ast_channel_name(chan));
  443. res = -1;
  444. }
  445. return res;
  446. }
  447. static int unload_module(void)
  448. {
  449. int res;
  450. res = ast_unregister_application(testc_app);
  451. res |= ast_unregister_application(tests_app);
  452. return res;
  453. }
  454. static int load_module(void)
  455. {
  456. int res;
  457. res = ast_register_application_xml(testc_app, testclient_exec);
  458. res |= ast_register_application_xml(tests_app, testserver_exec);
  459. return res;
  460. }
  461. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Interface Test Application");