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