diff options
Diffstat (limited to 'testcode')
-rwxr-xr-x | testcode/do-tests.sh | 3 | ||||
-rw-r--r-- | testcode/dohclient.c | 136 | ||||
-rw-r--r-- | testcode/fake_event.c | 27 | ||||
-rwxr-xr-x | testcode/run_vm.sh | 2 | ||||
-rw-r--r-- | testcode/testbound.c | 4 | ||||
-rw-r--r-- | testcode/unitmain.c | 47 |
6 files changed, 170 insertions, 49 deletions
diff --git a/testcode/do-tests.sh b/testcode/do-tests.sh index 5439f0f285e6..effb7c16a5bb 100755 --- a/testcode/do-tests.sh +++ b/testcode/do-tests.sh @@ -29,6 +29,9 @@ else HAVE_MINGW=no fi +# stop tests from notifying systemd, if that is compiled in. +export -n NOTIFY_SOCKET + cd testdata; sh ../testcode/mini_tdir.sh clean rm -f .perfstats.txt diff --git a/testcode/dohclient.c b/testcode/dohclient.c index adcc7d831554..263418049beb 100644 --- a/testcode/dohclient.c +++ b/testcode/dohclient.c @@ -90,6 +90,7 @@ static void usage(char* argv[]) printf("-e HTTP endpoint, default: /dns-query\n"); printf("-c Content-type in request, default: " "application/dns-message\n"); + printf("-n no-tls, TLS is disabled\n"); printf("-h This help text\n"); exit(1); } @@ -185,7 +186,10 @@ submit_query(struct http2_session* h2_session, struct sldns_buffer* buf) headers[1].name = (uint8_t*)":path"; headers[1].value = (uint8_t*)h2_stream->path; headers[2].name = (uint8_t*)":scheme"; - headers[2].value = (uint8_t*)"https"; + if(h2_session->ssl) + headers[2].value = (uint8_t*)"https"; + else + headers[2].value = (uint8_t*)"http"; headers[3].name = (uint8_t*)":authority"; headers[3].value = (uint8_t*)h2_session->authority; headers[4].name = (uint8_t*)"content-type"; @@ -246,6 +250,7 @@ static ssize_t http2_recv_cb(nghttp2_session* ATTR_UNUSED(session), { struct http2_session* h2_session = (struct http2_session*)cb_arg; int r; + ssize_t ret; struct timeval tv, *waittv; fd_set rfd; ERR_clear_error(); @@ -267,35 +272,58 @@ static ssize_t http2_recv_cb(nghttp2_session* ATTR_UNUSED(session), return NGHTTP2_ERR_WOULDBLOCK; } - r = SSL_read(h2_session->ssl, buf, len); - if(r <= 0) { - int want = SSL_get_error(h2_session->ssl, r); - if(want == SSL_ERROR_ZERO_RETURN) { + if(h2_session->ssl) { + r = SSL_read(h2_session->ssl, buf, len); + if(r <= 0) { + int want = SSL_get_error(h2_session->ssl, r); + if(want == SSL_ERROR_ZERO_RETURN) { + return NGHTTP2_ERR_EOF; + } + log_crypto_err("could not SSL_read"); return NGHTTP2_ERR_EOF; } - log_crypto_err("could not SSL_read"); + return r; + } + + ret = read(h2_session->fd, buf, len); + if(ret == 0) { + return NGHTTP2_ERR_EOF; + } else if(ret < 0) { + log_err("could not http2 read: %s", strerror(errno)); return NGHTTP2_ERR_EOF; } - return r; + return ret; } static ssize_t http2_send_cb(nghttp2_session* ATTR_UNUSED(session), const uint8_t* buf, size_t len, int ATTR_UNUSED(flags), void* cb_arg) { struct http2_session* h2_session = (struct http2_session*)cb_arg; + ssize_t ret; - int r; - ERR_clear_error(); - r = SSL_write(h2_session->ssl, buf, len); - if(r <= 0) { - int want = SSL_get_error(h2_session->ssl, r); - if(want == SSL_ERROR_ZERO_RETURN) { + if(h2_session->ssl) { + int r; + ERR_clear_error(); + r = SSL_write(h2_session->ssl, buf, len); + if(r <= 0) { + int want = SSL_get_error(h2_session->ssl, r); + if(want == SSL_ERROR_ZERO_RETURN) { + return NGHTTP2_ERR_CALLBACK_FAILURE; + } + log_crypto_err("could not SSL_write"); return NGHTTP2_ERR_CALLBACK_FAILURE; } - log_crypto_err("could not SSL_write"); + return r; + } + + ret = write(h2_session->fd, buf, len); + if(ret == 0) { + return NGHTTP2_ERR_CALLBACK_FAILURE; + } else if(ret < 0) { + log_err("could not http2 write: %s", strerror(errno)); return NGHTTP2_ERR_CALLBACK_FAILURE; } - return r; + return ret; } static int http2_stream_close_cb(nghttp2_session* ATTR_UNUSED(session), @@ -459,7 +487,7 @@ http2_read(struct http2_session* h2_session) } static void -run(struct http2_session* h2_session, int port, int count, char** q) +run(struct http2_session* h2_session, int port, int no_tls, int count, char** q) { int i; SSL_CTX* ctx = NULL; @@ -470,26 +498,28 @@ run(struct http2_session* h2_session, int port, int count, char** q) fd = open_svr(h2_session->authority, port); h2_session->fd = fd; - ctx = connect_sslctx_create(NULL, NULL, NULL, 0); - if(!ctx) fatal_exit("cannot create ssl ctx"); - SSL_CTX_set_alpn_protos(ctx, (const unsigned char *)"\x02h2", 3); - ssl = outgoing_ssl_fd(ctx, fd); - if(!ssl) { - printf("cannot create ssl\n"); - exit(1); - } - h2_session->ssl = ssl; - while(1) { - int r; - ERR_clear_error(); - if( (r=SSL_do_handshake(ssl)) == 1) - break; - r = SSL_get_error(ssl, r); - if(r != SSL_ERROR_WANT_READ && - r != SSL_ERROR_WANT_WRITE) { - log_crypto_err("could not ssl_handshake"); + if(!no_tls) { + ctx = connect_sslctx_create(NULL, NULL, NULL, 0); + if(!ctx) fatal_exit("cannot create ssl ctx"); + SSL_CTX_set_alpn_protos(ctx, (const unsigned char *)"\x02h2", 3); + ssl = outgoing_ssl_fd(ctx, fd); + if(!ssl) { + printf("cannot create ssl\n"); exit(1); } + h2_session->ssl = ssl; + while(1) { + int r; + ERR_clear_error(); + if( (r=SSL_do_handshake(ssl)) == 1) + break; + r = SSL_get_error(ssl, r); + if(r != SSL_ERROR_WANT_READ && + r != SSL_ERROR_WANT_WRITE) { + log_crypto_err("could not ssl_handshake"); + exit(1); + } + } } http2_submit_setting(h2_session); @@ -511,9 +541,13 @@ run(struct http2_session* h2_session, int port, int count, char** q) /* shutdown */ http2_session_delete(h2_session); - SSL_shutdown(ssl); - SSL_free(ssl); - SSL_CTX_free(ctx); + if(ssl) { + SSL_shutdown(ssl); + SSL_free(ssl); + } + if(ctx) { + SSL_CTX_free(ctx); + } close(fd); } @@ -524,10 +558,21 @@ extern char* optarg; int main(int argc, char** argv) { int c; - int port = UNBOUND_DNS_OVER_HTTPS_PORT; - struct http2_session* h2_session = http2_session_create(); - if(!h2_session) fatal_exit("out of memory"); + int port = UNBOUND_DNS_OVER_HTTPS_PORT, no_tls = 0; + struct http2_session* h2_session; +#ifdef USE_WINSOCK + WSADATA wsa_data; + if(WSAStartup(MAKEWORD(2,2), &wsa_data) != 0) { + printf("WSAStartup failed\n"); + return 1; + } +#endif + log_init(0, 0, 0); + checklock_start(); + + h2_session = http2_session_create(); + if(!h2_session) fatal_exit("out of memory"); if(argc == 1) { usage(argv); } @@ -537,7 +582,7 @@ int main(int argc, char** argv) h2_session->endpoint = "/dns-query"; h2_session->content_type = "application/dns-message"; - while((c=getopt(argc, argv, "c:e:hs:p:P")) != -1) { + while((c=getopt(argc, argv, "c:e:hns:p:P")) != -1) { switch(c) { case 'c': h2_session->content_type = optarg; @@ -545,6 +590,9 @@ int main(int argc, char** argv) case 'e': h2_session->endpoint = optarg; break; + case 'n': + no_tls = 1; + break; case 'p': if(atoi(optarg)==0 && strcmp(optarg,"0")!=0) { printf("error parsing port, " @@ -573,8 +621,12 @@ int main(int argc, char** argv) } - run(h2_session, port, argc, argv); + run(h2_session, port, no_tls, argc, argv); + checklock_stop(); +#ifdef USE_WINSOCK + WSACleanup(); +#endif return 0; } #else diff --git a/testcode/fake_event.c b/testcode/fake_event.c index d8df76492540..591557c35f12 100644 --- a/testcode/fake_event.c +++ b/testcode/fake_event.c @@ -872,6 +872,7 @@ listen_create(struct comm_base* base, struct listen_port* ATTR_UNUSED(ports), int ATTR_UNUSED(harden_large_queries), uint32_t ATTR_UNUSED(http_max_streams), char* ATTR_UNUSED(http_endpoint), + int ATTR_UNUSED(http_notls), struct tcl_list* ATTR_UNUSED(tcp_conn_limit), void* ATTR_UNUSED(sslctx), struct dt_env* ATTR_UNUSED(dtenv), comm_point_callback_type* cb, void *cb_arg) @@ -1044,7 +1045,7 @@ outside_network_create(struct comm_base* base, size_t bufsize, void (*unwanted_action)(void*), void* ATTR_UNUSED(unwanted_param), int ATTR_UNUSED(do_udp), void* ATTR_UNUSED(sslctx), int ATTR_UNUSED(delayclose), int ATTR_UNUSED(tls_use_sni), - struct dt_env* ATTR_UNUSED(dtenv)) + struct dt_env* ATTR_UNUSED(dtenv), int ATTR_UNUSED(udp_connect)) { struct replay_runtime* runtime = (struct replay_runtime*)base; struct outside_network* outnet = calloc(1, @@ -1213,7 +1214,7 @@ struct serviced_query* outnet_serviced_query(struct outside_network* outnet, sldns_buffer_flip(pend->buffer); if(1) { struct edns_data edns; - struct edns_tag_addr* client_tag_addr; + struct edns_string_addr* client_string_addr; if(!inplace_cb_query_call(env, qinfo, flags, addr, addrlen, zone, zonelen, qstate, qstate->region)) { free(pend); @@ -1227,13 +1228,13 @@ struct serviced_query* outnet_serviced_query(struct outside_network* outnet, edns.bits = 0; if(dnssec) edns.bits = EDNS_DO; - if((client_tag_addr = edns_tag_addr_lookup( - &env->edns_tags->client_tags, + if((client_string_addr = edns_string_addr_lookup( + &env->edns_strings->client_strings, addr, addrlen))) { - uint16_t client_tag = htons(client_tag_addr->tag_data); edns_opt_list_append(&qstate->edns_opts_back_out, - env->edns_tags->client_tag_opcode, 2, - (uint8_t*)&client_tag, qstate->region); + env->edns_strings->client_string_opcode, + client_string_addr->string_len, + client_string_addr->string, qstate->region); } edns.opt_list = qstate->edns_opts_back_out; attach_edns_record(pend->buffer, &edns); @@ -1510,6 +1511,18 @@ int serviced_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b)) return 0; } +int reuse_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b)) +{ + log_assert(0); + return 0; +} + +int reuse_id_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b)) +{ + log_assert(0); + return 0; +} + /* timers in testbound for autotrust. statistics tested in tdir. */ struct comm_timer* comm_timer_create(struct comm_base* base, void (*cb)(void*), void* cb_arg) diff --git a/testcode/run_vm.sh b/testcode/run_vm.sh index 5f599e144a5b..363a32b52cb5 100755 --- a/testcode/run_vm.sh +++ b/testcode/run_vm.sh @@ -40,6 +40,8 @@ cleanup() { exit 0 } trap cleanup INT +# stop tests from notifying systemd, if that is compiled in. +export -n NOTIFY_SOCKET for t in $RUNLIST do diff --git a/testcode/testbound.c b/testcode/testbound.c index 602dffaff14a..3f3e106b039c 100644 --- a/testcode/testbound.c +++ b/testcode/testbound.c @@ -362,6 +362,10 @@ main(int argc, char* argv[]) /* we do not want the test to depend on the timezone */ (void)putenv("TZ=UTC"); memset(pass_argv, 0, sizeof(pass_argv)); +#ifdef HAVE_SYSTEMD + /* we do not want the test to use systemd daemon startup notification*/ + (void)unsetenv("NOTIFY_SOCKET"); +#endif /* HAVE_SYSTEMD */ log_init(NULL, 0, NULL); /* determine commandline options for the daemon */ diff --git a/testcode/unitmain.c b/testcode/unitmain.c index a42be424e9e9..c61026f26808 100644 --- a/testcode/unitmain.c +++ b/testcode/unitmain.c @@ -839,6 +839,52 @@ static void respip_test(void) respip_conf_actions_test(); } +#include "services/outside_network.h" +/** add number of new IDs to the reuse tree, randomly chosen */ +static void tcpid_addmore(struct reuse_tcp* reuse, + struct outside_network* outnet, unsigned int addnum) +{ + unsigned int i; + struct waiting_tcp* w; + for(i=0; i<addnum; i++) { + uint16_t id = reuse_tcp_select_id(reuse, outnet); + unit_assert(!reuse_tcp_by_id_find(reuse, id)); + w = calloc(1, sizeof(*w)); + unit_assert(w); + w->id = id; + w->outnet = outnet; + w->next_waiting = (void*)reuse->pending; + reuse_tree_by_id_insert(reuse, w); + } +} + +/** fill up the reuse ID tree and test assertions */ +static void tcpid_fillup(struct reuse_tcp* reuse, + struct outside_network* outnet) +{ + int t, numtest=3; + for(t=0; t<numtest; t++) { + rbtree_init(&reuse->tree_by_id, reuse_id_cmp); + tcpid_addmore(reuse, outnet, 65535); + reuse_del_readwait(&reuse->tree_by_id); + } +} + +/** test TCP ID selection */ +static void tcpid_test(void) +{ + struct pending_tcp pend; + struct outside_network outnet; + unit_show_func("services/outside_network.c", "reuse_tcp_select_id"); + memset(&pend, 0, sizeof(pend)); + pend.reuse.pending = &pend; + memset(&outnet, 0, sizeof(outnet)); + outnet.rnd = ub_initstate(NULL); + rbtree_init(&pend.reuse.tree_by_id, reuse_id_cmp); + tcpid_fillup(&pend.reuse, &outnet); + ub_randfree(outnet.rnd); +} + void unit_show_func(const char* file, const char* func) { printf("test %s:%s\n", file, func); @@ -907,6 +953,7 @@ main(int argc, char* argv[]) infra_test(); ldns_test(); msgparse_test(); + tcpid_test(); #ifdef CLIENT_SUBNET ecs_test(); #endif /* CLIENT_SUBNET */ |