ensure that needed headers are included for chmod() and mkdir() (bug #3937)
[asterisk/asterisk.git] / apps / app_test.c
index dab7719..74ca7c5 100755 (executable)
@@ -1,9 +1,9 @@
 /*
  * Asterisk -- A telephony toolkit for Linux.
  *
- * Applictions connected with CDR engine
+ * Applications to test connection and produce report in text file
  * 
- * Copyright (C) 2004, Digium, Inc.
+ * Copyright (C) 2004 - 2005, Digium, Inc.
  *
  * Mark Spencer <markster@digium.com>
  * Russell Bryant <russelb@clemson.edu>
@@ -13,7 +13,9 @@
  */
 
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <asterisk/channel.h>
+#include <asterisk/options.h>
 #include <asterisk/module.h>
 #include <asterisk/logger.h>
 #include <asterisk/lock.h>
 #include "../astconf.h"
 
 
-static char *tdesc = "Make sure asterisk doesn't save CDR for a certain call";
+static char *tdesc = "Interface Test Application";
 
-static char *tests_descrip = "TestServer(): Perform test server function and write call report"
-                                                        "Results stored in /var/log/asterisk/testreports/<testid>.txt";
+static char *tests_descrip = 
+        "TestServer(): Perform test server function and write call report.\n"
+        "Results stored in /var/log/asterisk/testreports/<testid>-server.txt";
 static char *tests_app = "TestServer";
 static char *tests_synopsis = "Execute Interface Test Server";
 
-static char *testc_descrip = "TestClient(testid): Executes test client with given testid.\n"
-                                                        "Results stored in /var/log/asterisk/testreports/<testid>.txt";
+static char *testc_descrip = 
+        "TestClient(testid): Executes test client with given testid.\n"
+        "Results stored in /var/log/asterisk/testreports/<testid>-client.txt";
 
 static char *testc_app = "TestClient";
 static char *testc_synopsis = "Execute Interface Test Client";
@@ -88,6 +92,10 @@ static int measurenoise(struct ast_channel *chan, int ms, char *who)
        }
        if (res < 0)
                return res;
+       if (!samples) {
+               ast_log(LOG_NOTICE, "No samples were received from the other side!\n");
+               return -1;
+       }
        ast_log(LOG_DEBUG, "%s: Noise: %d, samples: %d, avg: %d\n", who, noise, samples, noise / samples);
        return (noise / samples);
 }
@@ -131,15 +139,18 @@ static int testclient_exec(struct ast_channel *chan, void *data)
        /* Transmit client version */
        if (!res)
                res = ast_dtmf_stream(chan, NULL, "8378*1#", 0);
-       ast_log(LOG_DEBUG, "Transmit client version\n");
+       if (option_debug)
+               ast_log(LOG_DEBUG, "Transmit client version\n");
        
        /* Read server version */
-       ast_log(LOG_DEBUG, "Read server version\n");
+       if (option_debug)
+               ast_log(LOG_DEBUG, "Read server version\n");
        if (!res) 
                res = ast_app_getdata(chan, NULL, serverver, sizeof(serverver) - 1, 0);
        if (res > 0)
                res = 0;
-       ast_log(LOG_DEBUG, "server version: %s\n", serverver);
+       if (option_debug)
+               ast_log(LOG_DEBUG, "server version: %s\n", serverver);
                
        if (res > 0)
                res = 0;
@@ -151,7 +162,8 @@ static int testclient_exec(struct ast_channel *chan, void *data)
                res = ast_dtmf_stream(chan, NULL, testid, 0);           
        if (!res) 
                res = ast_dtmf_stream(chan, NULL, "#", 0);              
-       ast_log(LOG_DEBUG, "send test identifier: %s\n", testid);
+       if (option_debug)
+               ast_log(LOG_DEBUG, "send test identifier: %s\n", testid);
 
        if ((res >=0) && (!ast_strlen_zero(testid))) {
                /* Make the directory to hold the test results in case it's not there */
@@ -167,7 +179,8 @@ static int testclient_exec(struct ast_channel *chan, void *data)
                        
                        if (!res) {
                                /* Step 1: Wait for "1" */
-                               ast_log(LOG_DEBUG, "TestClient: 2.  Wait DTMF 1\n");
+                               if (option_debug)
+                                       ast_log(LOG_DEBUG, "TestClient: 2.  Wait DTMF 1\n");
                                res = ast_waitfordigit(chan, 3000);
                                fprintf(f, "WAIT DTMF 1:   %s\n", (res != '1') ? "FAIL" : "PASS");
                                if (res == '1')
@@ -179,7 +192,8 @@ static int testclient_exec(struct ast_channel *chan, void *data)
                                res = ast_safe_sleep(chan, 1000);
                        if (!res) {
                                /* Step 2: Send "2" */
-                               ast_log(LOG_DEBUG, "TestClient: 2.  Send DTMF 2\n");
+                               if (option_debug)
+                                       ast_log(LOG_DEBUG, "TestClient: 2.  Send DTMF 2\n");
                                res = ast_dtmf_stream(chan, NULL, "2", 0);
                                fprintf(f, "SEND DTMF 2:   %s\n", (res < 0) ? "FAIL" : "PASS");
                                if (res > 0)
@@ -187,7 +201,8 @@ static int testclient_exec(struct ast_channel *chan, void *data)
                        }
                        if (!res) {
                                /* Step 3: Wait one second */
-                               ast_log(LOG_DEBUG, "TestClient: 3.  Wait one second\n");
+                               if (option_debug)
+                                       ast_log(LOG_DEBUG, "TestClient: 3.  Wait one second\n");
                                res = ast_safe_sleep(chan, 1000);
                                fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
                                if (res > 0)
@@ -195,7 +210,8 @@ static int testclient_exec(struct ast_channel *chan, void *data)
                        }                       
                        if (!res) {
                                /* Step 4: Measure noise */
-                               ast_log(LOG_DEBUG, "TestClient: 4.  Measure noise\n");
+                               if (option_debug)
+                                       ast_log(LOG_DEBUG, "TestClient: 4.  Measure noise\n");
                                res = measurenoise(chan, 5000, "TestClient");
                                fprintf(f, "MEASURENOISE:  %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
                                if (res > 0)
@@ -203,7 +219,8 @@ static int testclient_exec(struct ast_channel *chan, void *data)
                        }
                        if (!res) {
                                /* Step 5: Wait for "4" */
-                               ast_log(LOG_DEBUG, "TestClient: 5.  Wait DTMF 4\n");
+                               if (option_debug)
+                                       ast_log(LOG_DEBUG, "TestClient: 5.  Wait DTMF 4\n");
                                res = ast_waitfordigit(chan, 3000);
                                fprintf(f, "WAIT DTMF 4:   %s\n", (res != '4') ? "FAIL" : "PASS");
                                if (res == '4')
@@ -213,13 +230,15 @@ static int testclient_exec(struct ast_channel *chan, void *data)
                        }
                        if (!res) {
                                /* Step 6: Transmit tone noise */
-                               ast_log(LOG_DEBUG, "TestClient: 6.  Transmit tone\n");
+                               if (option_debug)
+                                       ast_log(LOG_DEBUG, "TestClient: 6.  Transmit tone\n");
                                res = sendnoise(chan, 6000);
-                               fprintf(f, "SENDTONE:      %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
+                               fprintf(f, "SENDTONE:      %s\n", (res < 0) ? "FAIL" : "PASS");
                        }
                        if (!res || (res == '5')) {
                                /* Step 7: Wait for "5" */
-                               ast_log(LOG_DEBUG, "TestClient: 7.  Wait DTMF 5\n");
+                               if (option_debug)
+                                       ast_log(LOG_DEBUG, "TestClient: 7.  Wait DTMF 5\n");
                                if (!res)
                                        res = ast_waitfordigit(chan, 3000);
                                fprintf(f, "WAIT DTMF 5:   %s\n", (res != '5') ? "FAIL" : "PASS");
@@ -230,7 +249,8 @@ static int testclient_exec(struct ast_channel *chan, void *data)
                        }
                        if (!res) {
                                /* Step 8: Wait one second */
-                               ast_log(LOG_DEBUG, "TestClient: 8.  Wait one second\n");
+                               if (option_debug)
+                                       ast_log(LOG_DEBUG, "TestClient: 8.  Wait one second\n");
                                res = ast_safe_sleep(chan, 1000);
                                fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
                                if (res > 0)
@@ -238,7 +258,8 @@ static int testclient_exec(struct ast_channel *chan, void *data)
                        }
                        if (!res) {
                                /* Step 9: Measure noise */
-                               ast_log(LOG_DEBUG, "TestClient: 6.  Measure tone\n");
+                               if (option_debug)
+                                       ast_log(LOG_DEBUG, "TestClient: 6.  Measure tone\n");
                                res = measurenoise(chan, 4000, "TestClient");
                                fprintf(f, "MEASURETONE:   %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
                                if (res > 0)
@@ -246,7 +267,8 @@ static int testclient_exec(struct ast_channel *chan, void *data)
                        }
                        if (!res) {
                                /* Step 10: Send "7" */
-                               ast_log(LOG_DEBUG, "TestClient: 7.  Send DTMF 7\n");
+                               if (option_debug)
+                                       ast_log(LOG_DEBUG, "TestClient: 7.  Send DTMF 7\n");
                                res = ast_dtmf_stream(chan, NULL, "7", 0);
                                fprintf(f, "SEND DTMF 7:   %s\n", (res < 0) ? "FAIL" : "PASS");
                                if (res > 0)
@@ -254,7 +276,8 @@ static int testclient_exec(struct ast_channel *chan, void *data)
                        }
                        if (!res) {
                                /* Step 11: Wait for "8" */
-                               ast_log(LOG_DEBUG, "TestClient: 11.  Wait DTMF 8\n");
+                               if (option_debug)
+                                       ast_log(LOG_DEBUG, "TestClient: 11.  Wait DTMF 8\n");
                                res = ast_waitfordigit(chan, 3000);
                                fprintf(f, "WAIT DTMF 8:   %s\n", (res != '8') ? "FAIL" : "PASS");
                                if (res == '8')
@@ -262,12 +285,13 @@ static int testclient_exec(struct ast_channel *chan, void *data)
                                else
                                        res = -1;
                        }
-                       if (!res) {
+                       if (option_debug && !res ) {
                                /* Step 12: Hangup! */
                                ast_log(LOG_DEBUG, "TestClient: 12.  Hangup\n");
                        }
 
-                       ast_log(LOG_DEBUG, "-- TEST COMPLETE--\n");
+                       if (option_debug)
+                               ast_log(LOG_DEBUG, "-- TEST COMPLETE--\n");
                        fprintf(f, "-- END TEST--\n");
                        fclose(f);
                        res = -1;
@@ -292,13 +316,16 @@ static int testserver_exec(struct ast_channel *chan, void *data)
        if (chan->_state != AST_STATE_UP)
                res = ast_answer(chan);
        /* Read version */
-       ast_log(LOG_DEBUG, "Read client version\n");
+       if (option_debug)
+               ast_log(LOG_DEBUG, "Read client version\n");
        if (!res) 
                res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
        if (res > 0)
                res = 0;
-       ast_log(LOG_DEBUG, "client version: %s\n", testid);
-       ast_log(LOG_DEBUG, "Transmit server version\n");
+       if (option_debug) {
+               ast_log(LOG_DEBUG, "client version: %s\n", testid);
+               ast_log(LOG_DEBUG, "Transmit server version\n");
+       }
        res = ast_safe_sleep(chan, 1000);
        if (!res)
                res = ast_dtmf_stream(chan, NULL, "8378*1#", 0);
@@ -307,7 +334,8 @@ static int testserver_exec(struct ast_channel *chan, void *data)
 
        if (!res) 
                res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);               
-       ast_log(LOG_DEBUG, "read test identifier: %s\n", testid);
+       if (option_debug) 
+               ast_log(LOG_DEBUG, "read test identifier: %s\n", testid);
        /* Check for sneakyness */
        if (strchr(testid, '/'))
                res = -1;
@@ -326,7 +354,8 @@ static int testserver_exec(struct ast_channel *chan, void *data)
                        res = ast_safe_sleep(chan, 1000);
                        if (!res) {
                                /* Step 1: Send "1" */
-                               ast_log(LOG_DEBUG, "TestServer: 1.  Send DTMF 1\n");
+                               if (option_debug) 
+                                       ast_log(LOG_DEBUG, "TestServer: 1.  Send DTMF 1\n");
                                res = ast_dtmf_stream(chan, NULL, "1", 0);
                                fprintf(f, "SEND DTMF 1:   %s\n", (res < 0) ? "FAIL" : "PASS");
                                if (res > 0)
@@ -334,7 +363,8 @@ static int testserver_exec(struct ast_channel *chan, void *data)
                        }
                        if (!res) {
                                /* Step 2: Wait for "2" */
-                               ast_log(LOG_DEBUG, "TestServer: 2.  Wait DTMF 2\n");
+                               if (option_debug) 
+                                       ast_log(LOG_DEBUG, "TestServer: 2.  Wait DTMF 2\n");
                                res = ast_waitfordigit(chan, 3000);
                                fprintf(f, "WAIT DTMF 2:   %s\n", (res != '2') ? "FAIL" : "PASS");
                                if (res == '2')
@@ -344,7 +374,8 @@ static int testserver_exec(struct ast_channel *chan, void *data)
                        }
                        if (!res) {
                                /* Step 3: Measure noise */
-                               ast_log(LOG_DEBUG, "TestServer: 3.  Measure noise\n");
+                               if (option_debug) 
+                                       ast_log(LOG_DEBUG, "TestServer: 3.  Measure noise\n");
                                res = measurenoise(chan, 6000, "TestServer");
                                fprintf(f, "MEASURENOISE:  %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
                                if (res > 0)
@@ -352,7 +383,8 @@ static int testserver_exec(struct ast_channel *chan, void *data)
                        }
                        if (!res) {
                                /* Step 4: Send "4" */
-                               ast_log(LOG_DEBUG, "TestServer: 4.  Send DTMF 4\n");
+                               if (option_debug) 
+                                       ast_log(LOG_DEBUG, "TestServer: 4.  Send DTMF 4\n");
                                res = ast_dtmf_stream(chan, NULL, "4", 0);
                                fprintf(f, "SEND DTMF 4:   %s\n", (res < 0) ? "FAIL" : "PASS");
                                if (res > 0)
@@ -361,7 +393,8 @@ static int testserver_exec(struct ast_channel *chan, void *data)
                
                        if (!res) {
                                /* Step 5: Wait one second */
-                               ast_log(LOG_DEBUG, "TestServer: 5.  Wait one second\n");
+                               if (option_debug) 
+                                       ast_log(LOG_DEBUG, "TestServer: 5.  Wait one second\n");
                                res = ast_safe_sleep(chan, 1000);
                                fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
                                if (res > 0)
@@ -370,7 +403,8 @@ static int testserver_exec(struct ast_channel *chan, void *data)
                
                        if (!res) {
                                /* Step 6: Measure noise */
-                               ast_log(LOG_DEBUG, "TestServer: 6.  Measure tone\n");
+                               if (option_debug) 
+                                       ast_log(LOG_DEBUG, "TestServer: 6.  Measure tone\n");
                                res = measurenoise(chan, 4000, "TestServer");
                                fprintf(f, "MEASURETONE:   %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
                                if (res > 0)
@@ -379,7 +413,8 @@ static int testserver_exec(struct ast_channel *chan, void *data)
 
                        if (!res) {
                                /* Step 7: Send "5" */
-                               ast_log(LOG_DEBUG, "TestServer: 7.  Send DTMF 5\n");
+                               if (option_debug) 
+                                       ast_log(LOG_DEBUG, "TestServer: 7.  Send DTMF 5\n");
                                res = ast_dtmf_stream(chan, NULL, "5", 0);
                                fprintf(f, "SEND DTMF 5:   %s\n", (res < 0) ? "FAIL" : "PASS");
                                if (res > 0)
@@ -388,14 +423,16 @@ static int testserver_exec(struct ast_channel *chan, void *data)
 
                        if (!res) {
                                /* Step 8: Transmit tone noise */
-                               ast_log(LOG_DEBUG, "TestServer: 8.  Transmit tone\n");
+                               if (option_debug) 
+                                       ast_log(LOG_DEBUG, "TestServer: 8.  Transmit tone\n");
                                res = sendnoise(chan, 6000);
                                fprintf(f, "SENDTONE:      %s\n", (res < 0) ? "FAIL" : "PASS");
                        }
                
                        if (!res || (res == '7')) {
                                /* Step 9: Wait for "7" */
-                               ast_log(LOG_DEBUG, "TestServer: 9.  Wait DTMF 7\n");
+                               if (option_debug) 
+                                       ast_log(LOG_DEBUG, "TestServer: 9.  Wait DTMF 7\n");
                                if (!res)
                                        res = ast_waitfordigit(chan, 3000);
                                fprintf(f, "WAIT DTMF 7:   %s\n", (res != '7') ? "FAIL" : "PASS");
@@ -408,7 +445,8 @@ static int testserver_exec(struct ast_channel *chan, void *data)
                                res = ast_safe_sleep(chan, 1000);
                        if (!res) {
                                /* Step 10: Send "8" */
-                               ast_log(LOG_DEBUG, "TestServer: 10.  Send DTMF 8\n");
+                               if (option_debug) 
+                                       ast_log(LOG_DEBUG, "TestServer: 10.  Send DTMF 8\n");
                                res = ast_dtmf_stream(chan, NULL, "8", 0);
                                fprintf(f, "SEND DTMF 8:   %s\n", (res < 0) ? "FAIL" : "PASS");
                                if (res > 0)
@@ -416,12 +454,13 @@ static int testserver_exec(struct ast_channel *chan, void *data)
                        }
                        if (!res) {
                                /* Step 11: Wait for hangup to arrive! */
-                               ast_log(LOG_DEBUG, "TestServer: 11.  Waiting for hangup\n");
+                               if (option_debug) 
+                                       ast_log(LOG_DEBUG, "TestServer: 11.  Waiting for hangup\n");
                                res = ast_safe_sleep(chan, 10000);
                                fprintf(f, "WAIT HANGUP:   %s\n", (res < 0) ? "PASS" : "FAIL");
                        }
 
-                       ast_log(LOG_DEBUG, "-- TEST COMPLETE--\n");
+                       ast_log(LOG_NOTICE, "-- TEST COMPLETE--\n");
                        fprintf(f, "-- END TEST--\n");
                        fclose(f);
                        res = -1;