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