From 4b19c2297e3f0e3cd671044d0981a587abfc257b Mon Sep 17 00:00:00 2001 From: Mazet Laurent Date: Thu, 5 Jun 2025 16:03:36 +0200 Subject: [PATCH] add tests --- mapec_test.c | 2 +- script-error.mapec | 25 ++++++++++++++++++ mapec_valid.c => udp_valid.c | 50 ++++++++++++++++++++++-------------- ulvpn.c | 3 ++- 4 files changed, 59 insertions(+), 21 deletions(-) create mode 100644 script-error.mapec rename mapec_valid.c => udp_valid.c (85%) diff --git a/mapec_test.c b/mapec_test.c index f59be33..e44bf47 100644 --- a/mapec_test.c +++ b/mapec_test.c @@ -84,7 +84,7 @@ void *client (UNUSED void *dummy) unsigned char rx_data[32768]; do { - rx_len = MAPEC_Receive (fid, rx_data, sizeof (rx_data)); + rx_len = MAPEC_Receive_timeout (fid, rx_data, sizeof (rx_data), 100); if (rx_len == 0) { VERBOSE (mapec, DEBUG, PRINTF ("client's sleeping...\n")); usleep (1e3); diff --git a/script-error.mapec b/script-error.mapec new file mode 100644 index 0000000..5e74209 --- /dev/null +++ b/script-error.mapec @@ -0,0 +1,25 @@ +# Test script + +TUDP PAYLOAD=@plaintext.txt text not ignored +SLEEP 100 + +TUDP PAYLOAD=@nofile.txt +SLEEP 100 + +TUDP PAYLOAD=@udp_valid.c +SLEEP 100 + +TUDP PAYLOAD="This is an unfinished text +SLEEP 100 + +TUDP PAYLOAD="This is a text" text not ignored +SLEEP 100 + +TUDP NOTPAYLOAD="This is a text" +SLEEP 100 + +TUDP PAYLOAD=01:02:03:04:05:06:07:08:09:0a:0b:0c:0d:0e:0f:10 text not ignored +SLEEP 100 + +TUDP PAYLOAD=01:02:fo:ob:ae:06:07:08:09:0a:0b:0c:0d:0e:0f:10 +SLEEP 100 diff --git a/mapec_valid.c b/udp_valid.c similarity index 85% rename from mapec_valid.c rename to udp_valid.c index 25837dd..10fea07 100644 --- a/mapec_valid.c +++ b/udp_valid.c @@ -1,13 +1,13 @@ /* - File name : mapec_valid.c + File name : udp_valid.c Projet : MERLIN Date of creation : 2025/05/23 Version : 1.0 Copyright : Thales SIX Author : Laurent Mazet - Description : Minimal API for Packet Exchange Commmunication validation - program + Description : Minimal API for Packet Exchange Commmunication (UDP) + validation program History : - initial version @@ -88,7 +88,7 @@ int parse_array (char *str, uint8_t *buffer, int maxlen) len = j; VERBOSE (mapec, TRACE, buffer[len] = '\0'; PRINTF ("string[%d]: '%s'\n", len, buffer)); } else { - VERBOSE (mapec, ERROR, PRINTF ("incomplet string '%s'\n", str)); + VERBOSE (mapec, DEBUG, PRINTF ("incomplet string '%s'\n", str)); } } @@ -112,7 +112,7 @@ int parse_array (char *str, uint8_t *buffer, int maxlen) } fclose (fid); } else { - VERBOSE (mapec, ERROR, PRINTF ("can't open file '%s'\n", str)); + VERBOSE (mapec, DEBUG, PRINTF ("can't open file '%s'\n", str)); } } @@ -135,7 +135,8 @@ int parse_array (char *str, uint8_t *buffer, int maxlen) digit[1] = str[3 * i + 1]; buffer[i] = strtol (digit, &ptr, 16); if ((*ptr != ':') && (*ptr != '\0') && (*ptr != ' ') && (*ptr != '\t')) { - VERBOSE (mapec, ERROR, PRINTF ("unrecognize hexa-string (%d) '%s'\n", 3 * i, str)); + VERBOSE (mapec, DEBUG, PRINTF ("unrecognize hexa-string (%d) '%s'\n", 3 * i, str)); + len = 0; break; } } @@ -143,7 +144,7 @@ int parse_array (char *str, uint8_t *buffer, int maxlen) /* unrecognize format */ else { - VERBOSE (mapec, WARNING, PRINTF ("can't parse buffer '%s'\n", str)); + VERBOSE (mapec, DEBUG, PRINTF ("can't parse buffer '%s'\n", str)); } } return len; @@ -488,17 +489,28 @@ int main (int argc, char **argv) return rc + GET_VERBOSE_ERRORS (mapec); } -/* test: mapec_valid.exe -h | grep usage */ -/* test: mapec_valid.exe -l 2>&1 | grep 'log file not specified' */ -/* test: mapec_valid.exe -r 2>&1 | grep 'receiver url not specified' */ -/* test: mapec_valid.exe -s 2>&1 | grep 'service name not specified' */ -/* test: mapec_valid.exe -t 2>&1 | grep 'transmitter url not specified' */ -/* test: mapec_valid.exe -v 2>&1 | grep 'verbose level not specified' */ - -/* test: mapec_valid.exe -s UDP o -r udp://localhost:1234 2>&1 | grep 'missing an url' */ -/* test: echo | mapec_valid.exe -s UDP -r udp://localhost:1234 -t udp://localhost:1235 */ -/* test: mapec_valid.exe -n -s UDP -r udp://localhost:1234 -t udp://localhost:1235 script-udp.mapec & pid=$!; sleep 1; mapec_valid.exe -s UDP -r udp://localhost:1235 -t udp://localhost:1234 script-udp.mapec | awk '{print "<2>", $0}' */ -/* test: mapec_valid.exe -s TUN0 -r tun://tun0 2>&1 | grep 'missing an url' */ -/* test: echo | mapec_valid.exe -s TUN0 -r tun://tun0 -t tun://1.2.3.4 */ +/* test: udp_valid.exe -h | grep usage */ +/* test: chmod a+w *.gcda */ +/* test: udp_valid.exe -l 2>&1 | grep 'log file not specified' */ +/* test: udp_valid.exe -r 2>&1 | grep 'receiver url not specified' */ +/* test: udp_valid.exe -s 2>&1 | grep 'service name not specified' */ +/* test: udp_valid.exe -t 2>&1 | grep 'transmitter url not specified' */ +/* test: udp_valid.exe -v 2>&1 | grep 'verbose level not specified' */ + +/* test: udp_valid.exe -s UDP o -r udp://localhost:1234; test $? -ne 0 */ +/* test: udp_valid.exe -s UDP -r udp://localhost -t udp://localhost:1235:1234; test $? -ne 0 */ +/* test: udp_valid.exe -s UDP -r udp://localhost:1234 -t udp://localhost; test $? -ne 0 */ +/* test: udp_valid.exe -s UDP -r udp://nowhere:1234 -t udp://localhost:1235; test $? -ne 0 */ +/* test: udp_valid.exe -s UDP -r udp://localhost:1234 -t udp://nowhere:1235; test $? -ne 0 */ +/* test: udp_valid.exe -s UDP -r udp://localhost:1234 -t tun://localhost:1235; test $? -ne 0 */ +/* test: sudo -u `awk -F: '/sh$/ && $3 != 0 {print $1; exit}' /etc/passwd` udp_valid.exe -s UDP -r udp://localhost:12 -t udp://localhost:35; test $? -ne 0 */ + +/* test: echo | udp_valid.exe -s UDP -r udp://localhost:1234 -t udp://localhost:1235 */ + +/* test: udp_valid.exe -n -s UDP -r udp://localhost:1234 -t udp://localhost:1235 script-udp.mapec & pid=$!; sleep 1; kill -INT $pid */ +/* test: udp_valid.exe -n -s UDP -r udp://localhost:1234 -t udp://localhost:1235 script-udp.mapec & pid=$!; sleep 1; kill -TERM $pid */ +/* test: udp_valid.exe -s UDP -r udp://localhost:1234 -t udp://localhost:1235 script-error.mapec; test $? -eq $(grep -c TUDP script-error.mapec) */ +/* test: udp_valid.exe -n -s UDP -r udp://localhost:1234 -t udp://localhost:1235 script-udp.mapec & pid=$!; sleep 1; udp_valid.exe -s UDP -r udp://localhost:1235 -t udp://localhost:1234 script-udp.mapec | awk '{print "<2>", $0}' */ +/* test: udp_valid.exe -l - -n -s UDP -r udp://localhost:1234 -t udp://localhost:1235 script-udp.mapec & pid=$!; sleep 1; udp_valid.exe -l script-udp.log -s UDP -r udp://localhost:1235 -t udp://localhost:1234 script-udp.mapec | awk '{print "<2>", $0}' */ /* vim: set ts=4 sw=4 si et: */ diff --git a/ulvpn.c b/ulvpn.c index d815d66..6138c73 100644 --- a/ulvpn.c +++ b/ulvpn.c @@ -244,6 +244,7 @@ int main (int argc, char **argv) } /* test: ulvpn.exe -h | grep usage */ +/* test: chmod a+w *.gcda */ /* test: ulvpn.exe help 2>&1 | grep usage */ /* test: ulvpn.exe -d 2>&1 | grep 'dev tun not specified' */ /* test: ulvpn.exe -l 2>&1 | grep 'local tun not specified' */ @@ -253,7 +254,7 @@ int main (int argc, char **argv) /* test: ulvpn.exe -v 2>&1 | grep 'verbose level not specified' */ /* test: ulvpn.exe -V 2>&1 | grep 'verbose level not specified' */ -/* test: sudo `awk -F: '/sh$/ && $3 != 0 {print $1; exit}' /etc/passwd` ulvpn.exe; test $? -ne 0 */ +/* test: sudo -u `awk -F: '/sh$/ && $3 != 0 {print $1; exit}' /etc/passwd` ulvpn.exe; test $? -ne 0 */ /* test: ulvpn.exe -d tun://tun0:-1; test $? -ne 0 */ /* test: ulvpn.exe -d tun://tun1; test $? -ne 0 */ -- 2.30.2