1
0
mirror of https://github.com/nmap/nmap.git synced 2026-01-06 14:39:03 +00:00

Add tests for nsock proxy parsing

This commit is contained in:
dmiller
2022-09-20 23:37:28 +00:00
parent 7eda670f4b
commit 0afb05b155
5 changed files with 84 additions and 19 deletions

View File

@@ -295,7 +295,8 @@ void nsock_set_loglevel(nsock_loglevel_t loglevel);
* accordingly. If the optional nsock_pool parameter is passed in, it gets
* associated to the chain object. The alternative is to pass nsp=NULL and call
* nsock_pool_set_proxychain() manually. Whatever is done, the chain object has
* to be deleted by the caller, using proxychain_delete(). */
* to be deleted by the caller, using proxychain_delete().
* Returns 1 on success, -1 on failure. */
int nsock_proxychain_new(const char *proxystr, nsock_proxychain *chain, nsock_pool nspool);
/* If nsock_proxychain_new() returned success, caller has to free the chain

View File

@@ -101,6 +101,10 @@ int nsock_proxychain_new(const char *proxystr, nsock_proxychain *chain, nsock_po
parser = proxy_parser_new(proxystr);
while (!parser->done) {
if (parser->value == NULL) {
nsock_proxychain_delete(pxc);
return -1;
}
gh_list_append(&pxc->nodes, &parser->value->nodeq);
proxy_parser_next(parser);
}
@@ -360,15 +364,17 @@ static struct proxy_node *proxy_node_new(char *proxystr) {
if (parse_uri(proxystr, &uri) < 0)
break;
if (pspec->ops->node_new(&proxy, &uri) < 0)
fatal("Cannot initialize proxy node %s", proxystr);
if (pspec->ops->node_new(&proxy, &uri) < 0) {
nsock_log_error("Cannot initialize proxy node %s", proxystr);
break;
}
uri_free(&uri);
return proxy;
}
}
fatal("Invalid protocol in proxy specification string: %s", proxystr);
nsock_log_error("Invalid protocol in proxy specification string: %s", proxystr);
return NULL;
}

View File

@@ -20,6 +20,7 @@ SRC = tests_main.c \
connect.c \
ghlists.c \
ghheaps.c \
proxychain.c \
cancel.c
OBJ = $(SRC:.c=.o)

View File

@@ -4,6 +4,13 @@
*/
#include "test-common.h"
#include "../src/nsock_log.h"
struct proxy_test_data {
int tn;
nsock_pool nsp;
};
static struct proxy_test_data *GlobalTD;
#define END_OF_TESTS -1
#define GOOD 0
@@ -11,7 +18,7 @@
struct proxy_test {
int ttype;
const char *input;
}
};
static const struct proxy_test Tests[] = {
/* single proxy */
@@ -36,14 +43,29 @@ static const struct proxy_test Tests[] = {
{BAD, "http://:8080/"},
/* socks4 */
{GOOD, "socks4://example.com"},
/* socks4 does not support IPv6 */
{BAD, "socks4://[::1]"},
/* Does SOCKS4 really support a path like this? */
{GOOD, "socks4://example.com/path?"},
/* multiple proxies */
{GOOD, "http://example.com:8080/,socks4://127.0.0.1/"},
{GOOD, "http://[::1]/,socks4://example.com:5000/"},
{GOOD, "socks4://[::1]/,socks4://example.com/,http://[::1]:9090"},
/* Should fail: socks4 cannot connect to IPv6 proxy */
{GOOD, "socks4://127.0.0.1/,socks4://example.com/,http://[::1]:9090"},
/* Dumb stuff */
#if 0
/* These tests should fail, but do not yet: #177 */
{BAD, ""},
{BAD, ","},
{BAD, ",,"},
{BAD, "http://example.com/,"},
{BAD, "http://example.com/,,"},
{BAD, ",http://example.com/"},
{BAD, ",,http://example.com/"},
{BAD, "socks4://127.0.0.1/,http://example.com/,"},
{BAD, ",socks4://127.0.0.1/,http://example.com/"},
{BAD, "socks4://127.0.0.1/,,http://example.com/"},
#endif
{BAD, "com"},
{BAD, "example.com"},
{BAD, "/example.com/"},
@@ -51,43 +73,75 @@ static const struct proxy_test Tests[] = {
{BAD, "http/example.com/"},
{BAD, "http//example.com/"},
{BAD, "sptth://example.com/"},
{BAD, ","},
{BAD, ", "},
{BAD, " ,"},
{BAD, ",,"},
{BAD, ", ,"},
{BAD, " , , "},
{BAD, "http://example.com/,asdf"},
{BAD, "http://example.com/,"},
{BAD, "http://example.com/,,"},
{BAD, ",http://example.com/"},
{BAD, ",,http://example.com/"},
{BAD, "socks4://127.0.0.1/,http://example.com/,"},
{BAD, "socks4://127.0.0.1/,,http://example.com/"},
{BAD, ",socks4://127.0.0.1/,http://example.com/"},
{END_OF_TESTS, NULL}
};
static int parser_test(void *testdata) {
int tn = 0;
struct proxy_test *pt = &Tests[tn];
struct proxy_test_data *ptd = (struct proxy_test_data *)testdata;
const struct proxy_test *pt = &Tests[ptd->tn];
while (pt->ttype != END_OF_TESTS) {
nsock_proxychain pxc = NULL;
if (pt->ttype == BAD)
nsock_log_info("Expected failure:");
int ret = nsock_proxychain_new(pt->input, &pxc, NULL);
nsock_log_debug("Test %d result: %d", ptd->tn, ret);
if (ret > 0 && pt->ttype == BAD) {
fprintf(stderr, "Proxy Test #%d: Failed to reject bad input: %s\n", ptd->tn, pt->input);
return -1;
}
else if (ret <= 0 && pt->ttype == GOOD) {
fprintf(stderr, "Proxy Test #%d: Failed to parse good input: %s\n", ptd->tn, pt->input);
return -2;
}
nsock_proxychain_delete(pxc);
ptd->tn++;
pt = &Tests[ptd->tn];
}
return 0;
}
static void log_handler(const struct nsock_log_rec *rec) {
/* Only print log messages if we expect the test to succeed. */
if (Tests[GlobalTD->tn].ttype == GOOD) {
fprintf(stderr, "Proxy Test #%d: %s(): %s\n", GlobalTD->tn, rec->func, rec->msg);
}
}
static int proxy_setup(void **tdata) {
struct proxy_test_data *ptd = calloc(1, sizeof(struct proxy_test_data));
if (ptd == NULL)
return -ENOMEM;
ptd->nsp = nsock_pool_new(ptd);
AssertNonNull(ptd->nsp);
nsock_set_log_function(log_handler);
*tdata = GlobalTD = ptd;
return 0;
}
static int proxy_teardown(void *tdata) {
struct proxy_test_data *ptd = (struct proxy_test_data *)tdata;
if (tdata) {
nsock_pool_delete(ptd->nsp);
free(tdata);
}
nsock_set_log_function(NULL);
GlobalTD = NULL;
return 0;
}
const struct test_case TestProxyParse = {
.t_name = "test nsock proxychain parsing",
.t_setup = NULL,
.t_setup = proxy_setup,
.t_run = parser_test,
.t_teardown = NULL
.t_teardown = proxy_teardown
};

View File

@@ -35,6 +35,7 @@ extern const struct test_case TestConnectFailure;
extern const struct test_case TestGHLists;
extern const struct test_case TestGHHeaps;
extern const struct test_case TestHeapOrdering;
extern const struct test_case TestProxyParse;
extern const struct test_case TestCancelTCP;
extern const struct test_case TestCancelUDP;
#ifdef HAVE_OPENSSL
@@ -58,6 +59,8 @@ static const struct test_case *TestCases[] = {
/* ---- ghheaps.c */
&TestGHHeaps,
&TestHeapOrdering,
/* ---- proxychain.c */
&TestProxyParse,
/* ---- cancel.c */
&TestCancelTCP,
&TestCancelUDP,