test for command line parameters

This commit is contained in:
yrutschle 2020-07-19 18:32:57 +02:00
parent d7cf82424b
commit 8638199f13
5 changed files with 84 additions and 17 deletions

View File

@ -23,6 +23,9 @@ v1.21: 11JUL2020
Use syslog_facility: "none" to disable syslog
output.
Changed exit code for illegal command line parameter
from 1 to 6 (for testing purposes)
v1.20: 20NOV2018
Added support for socks5 protocol (Eugene Protozanov)

View File

@ -1,5 +1,5 @@
/* Generated by conf2struct (https://www.rutschle.net/tech/conf2struct/README)
* on Sat Jul 18 17:26:18 2020.
* on Sun Jul 19 16:59:51 2020.
# conf2struct: generate libconf parsers that read to structs
# Copyright (C) 2018-2019 Yves Rutschle
@ -91,7 +91,9 @@ typedef union {
} any_val;
/* Copy an any_val to arbitrary memory location */
static void any_valcpy(config_type type, void* target, any_val val)
/* 0: success
* <0: error */
static int any_valcpy(config_type type, void* target, any_val val)
{
switch(type) {
case CFG_BOOL:
@ -116,14 +118,17 @@ static void any_valcpy(config_type type, void* target, any_val val)
default:
fprintf(stderr, "Unknown type specification %d\n", type);
exit(1);
return -1;
}
return 1;
}
/* Copy the value of a setting to an arbitrary memory that
* must be large enough */
static void settingcpy(config_type type, void* target, const config_setting_t* setting)
/* 0: success
* <0: error */
static int settingcpy(config_type type, void* target, const config_setting_t* setting)
{
any_val val;
char* str;
@ -157,13 +162,16 @@ static void settingcpy(config_type type, void* target, const config_setting_t* s
default:
fprintf(stderr, "Unknown type specification %d\n", type);
exit(1);
return -1;
}
return 0;
}
/* Copy the value of a command line arg to arbitrary memory
* that must be large enough for the type */
static void clcpy(config_type type, void* target, const void* cl_arg)
/* 0: success
* <0: error */
static int clcpy(config_type type, void* target, const void* cl_arg)
{
any_val val;
char* str;
@ -197,14 +205,17 @@ static void clcpy(config_type type, void* target, const void* cl_arg)
default:
fprintf(stderr, "Unknown type specification %d\n", type);
exit(1);
return -1;
}
return 0;
}
/* Copy the value of a string argument to arbitary memory
* location that must be large enough, converting on the way
* (i.e. CFG_INT gets atoi() and so on) */
static void stringcpy(config_type type, void* target, char* from)
/* 0: success
* <0: error */
static int stringcpy(config_type type, void* target, char* from)
{
any_val val;
@ -236,8 +247,9 @@ static void stringcpy(config_type type, void* target, char* from)
default:
fprintf(stderr, "Unknown type specification %d\n", type);
exit(1);
return -1;
}
return 0;
}
@ -1467,6 +1479,8 @@ static int c2s_parse_file(const char* filename, config_t* c, char**errmsg)
return 1;
}
/* 0: success
<0: error */
int sslhcfg_cl_parse(int argc, char* argv[], struct sslhcfg_item* cfg)
{
int nerrors, res;
@ -1506,7 +1520,7 @@ int sslhcfg_cl_parse(int argc, char* argv[], struct sslhcfg_item* cfg)
arg_print_errors(stdout, sslhcfg_end, "sslhcfg");
arg_print_syntax(stdout, argtable, "\n");
arg_print_glossary(stdout, argtable, " %-25s\t%s\n");
return 0;
return -1;
}
@ -1514,7 +1528,7 @@ int sslhcfg_cl_parse(int argc, char* argv[], struct sslhcfg_item* cfg)
if (sslhcfg_conffile->count) {
if (!c2s_parse_file(sslhcfg_conffile->filename[0], &c, &errmsg)) {
fprintf(stderr, "%s\n", errmsg);
exit(1);
return -1;
}
}
@ -1523,16 +1537,16 @@ int sslhcfg_cl_parse(int argc, char* argv[], struct sslhcfg_item* cfg)
res = read_block(s, cfg, table_sslhcfg, &errmsg);
if (!res) {
fprintf(stderr, "%s\n", errmsg);
return res;
return -1;
}
res = read_compounds(s, cfg, compound_cl_args, &errmsg);
if (!res) {
fprintf(stderr, "%s\n", errmsg);
return res;
return -1;
}
return res;
return 0;
}

View File

@ -1,5 +1,5 @@
/* Generated by conf2struct (https://www.rutschle.net/tech/conf2struct/README)
* on Sat Jul 18 17:26:18 2020.
* on Sun Jul 19 16:59:51 2020.
# conf2struct: generate libconf parsers that read to structs
# Copyright (C) 2018-2019 Yves Rutschle

View File

@ -266,7 +266,7 @@ int main(int argc, char *argv[], char* envp[])
memset(&cfg, 0, sizeof(cfg));
res = sslhcfg_cl_parse(argc, argv, &cfg);
if (!res) exit(1);
if (res) exit(6);
if (cfg.verbose > 3)
sslhcfg_fprint(stderr, &cfg, 0);
res = config_resolve_listen(&addr_listen);

52
t
View File

@ -36,6 +36,7 @@ my $RB_PARAM_NOHOST = 1;
my $RB_WRONG_USERNAME = 1;
my $RB_OPEN_PID_FILE = 1;
my $RB_RESOLVE_ADDRESS = 1;
my $RB_CL_PARAMS = 1;
`lcov --directory . --zerocounters`;
@ -343,7 +344,7 @@ if ($RB_PARAM_NOHOST) {
waitpid $sslh_pid, 0;
my $code = $? >> 8;
warn "exited with $code\n";
my_is($code, 1, "Exit status on illegal option");
my_is($code, 6, "Exit status on illegal option");
}
# Robustness: User does not exist
@ -390,6 +391,55 @@ if ($RB_RESOLVE_ADDRESS) {
my_is($code, 4, "Exit status if can't resolve address");
}
# Robustness: verify all command line options work
if ($RB_CL_PARAMS) {
print "***Test: Command line parameters\n";
my $sslh_pid;
if (!($sslh_pid = fork)) {
my $user = (getpwuid $<)[0]; # Run under current username
# This doesn't test --inetd
exec "./sslh-select -v 3 -f -u $user -P $pidfile".
" -n --transparent --timeout 10 -C /tmp".
" --syslog-facility auth --on-timeout ssh".
" --listen localhost:$no_listen --ssh $ssh_address --tls $ssl_address".
" --openvpn localhost:$no_listen".
" --tinc localhost:$no_listen".
" --xmpp localhost:$no_listen".
" --http localhost:$no_listen".
" --adb localhost:$no_listen".
" --socks5 localhost:$no_listen".
" --anyprot localhost:$no_listen";
exit 0;
}
warn "spawned $sslh_pid\n";
# It will die soon because $user cannot chroot (you
# don't test as root, do you?)
waitpid $sslh_pid, 0;
my $code = $? >> 8;
warn "exited with $code\n";
my_is($code, 1, "Command line arguments");
print "***Test: Bad command line parameters\n";
my $sslh_pid;
if (!($sslh_pid = fork)) {
my $user = (getpwuid $<)[0]; # Run under current username
# This doesn't test --inetd
exec "./sslh-select -v 3 -f -u $user -P $pidfile".
" -n --transparent --timeout 10 -C /tmp".
" --fakeoption".
" --anyprot localhost:$no_listen";
exit 0;
}
warn "spawned $sslh_pid\n";
waitpid $sslh_pid, 0;
my $code = $? >> 8;
warn "exited with $code\n";
my_is($code, 6, "Bad command line parameters");
}
`lcov --directory . --capture --output-file sslh_cov.info`;
`genhtml sslh_cov.info`;