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