static void handshake_io_slowly(void **state) { selene_error_t *err; sln_parser_baton_t *baton; selene_conf_t *conf = NULL; selene_t *s = NULL; sln_bucket_t *e1; size_t maxlen = sizeof(openssl_client_hello_basic); size_t i; selene_conf_create(&conf); SLN_ERR(selene_conf_use_reasonable_defaults(conf)); SLN_ERR(selene_server_create(conf, &s)); SLN_ASSERT_CONTEXT(s); baton = (sln_parser_baton_t *)s->backend_baton; for (i = maxlen; i <= maxlen; i++) { SLN_ERR(sln_bucket_create_copy_bytes(sln_test_alloc, &e1, openssl_client_hello_basic, i)); SLN_BRIGADE_INSERT_TAIL(baton->in_handshake, e1); err = sln_io_handshake_read(s, baton); if (err) { SLN_ASSERT(err->err == SELENE_EINVAL); } else { /* TODO: more asserts */ } sln_brigade_clear(baton->in_handshake); } selene_destroy(s); selene_conf_destroy(conf); }
static void tls_http_accident(void **state) { selene_error_t *err; sln_parser_baton_t *baton; selene_conf_t *conf = NULL; selene_t *s = NULL; http_cb_t cbb; sln_bucket_t *e1; selene_conf_create(&conf); SLN_ERR(selene_conf_use_reasonable_defaults(conf)); SLN_ERR(selene_server_create(conf, &s)); SLN_ASSERT_CONTEXT(s); baton = (sln_parser_baton_t *)s->backend_baton; cbb.gotit = 0; selene_handler_set(s, SELENE_EVENT_TLS_GOT_HTTP, http_cb, &cbb); SLN_ERR(sln_bucket_create_copy_bytes(sln_test_alloc, &e1, http_message, strlen(http_message))); SLN_BRIGADE_INSERT_TAIL(s->bb.in_enc, e1); err = sln_io_tls_read(s, baton); SLN_ASSERT(err != NULL); SLN_ASSERT(err->err == SELENE_EINVAL); selene_error_clear(err); assert_int_equal(1, cbb.gotit); selene_destroy(s); selene_conf_destroy(conf); }
static void init_server(void **state) { selene_conf_t *conf = NULL; selene_t *ctxt = NULL; selene_conf_create(&conf); SLN_ERR(selene_conf_use_reasonable_defaults(conf)); SLN_ERR(selene_server_create(conf, &ctxt)); SLN_ASSERT_CONTEXT(ctxt); selene_destroy(ctxt); selene_conf_destroy(conf); }
static void logging_levels(void **state) { selene_conf_t *conf = NULL; selene_t *ctxt = NULL; selene_conf_create(&conf); SLN_ERR(selene_conf_use_reasonable_defaults(conf)); SLN_ERR(selene_server_create(conf, &ctxt)); SLN_ASSERT_CONTEXT(ctxt); sln_log_level_set(ctxt, SLN_LOG_NOTHING); assert_int_equal(SLN_LOG_NOTHING, sln_log_level_get(ctxt)); sln_log_level_set(ctxt, SLN_LOG_EVERYTHING); assert_int_equal(SLN_LOG_EVERYTHING, sln_log_level_get(ctxt)); selene_destroy(ctxt); selene_conf_destroy(conf); }
static void logging_types(void **state) { selene_conf_t *conf = NULL; selene_t *s = NULL; log_cb_t b; selene_conf_create(&conf); SLN_ERR(selene_conf_use_reasonable_defaults(conf)); SLN_ERR(selene_server_create(conf, &s)); SLN_ASSERT_CONTEXT(s); sln_log_level_set(s, SLN_LOG_EVERYTHING); selene_handler_set(s, SELENE_EVENT_LOG_MSG, log_cb, &b); b.cmp = "CRT: test:0\n"; slnCrit(s, "test:%u", 0); b.cmp = "ERR: test:1\n"; slnErr(s, "test:%u", 1); b.cmp = "INF: test:2\n"; slnInfo(s, "test:%u", 2); b.cmp = "WRN: test:3\n"; slnWarn(s, "test:%u", 3); b.cmp = "DBG: test:4\n"; slnDbg(s, "test:%u", 4); b.cmp = "TRC: test:5\n"; slnTrace(s, "test:%u", 5); b.cmp = "UNK: test:6\n"; sln_log_fmt(s, 44, "test:%u", 6); sln_log_level_set(s, SLN_LOG_NOTHING); slnDbg(s, "test:%u", 4); selene_destroy(s); selene_conf_destroy(conf); }
static void prf_vector_from_book(void **state) { selene_conf_t *conf = NULL; selene_t *s = NULL; char buf[20]; selene_conf_create(&conf); SLN_ERR(selene_conf_use_reasonable_defaults(conf)); SLN_ERR(selene_server_create(conf, &s)); SLN_ASSERT_CONTEXT(s); /** * Test vector from Implementing SSL/TLS.... * inputs: secret label seed 20 * output: b5baf4722b91851a8816d22ebd8c1d8cc2e94d55 */ SLN_ERR(sln_prf(s, "label", strlen("label"), "secret", strlen("secret"), "seed", strlen("seed"), buf, sizeof(buf))); assert_memory_equal(buf, ssl_test_vector, 20); selene_destroy(s); selene_conf_destroy(conf); }
static int listen_to(selene_conf_t *conf, const char *host, int port, FILE *fp) { fd_set readers; int rv = 0; server_t server; char buf[8096]; char port_str[16]; char *p = NULL; struct addrinfo hints, *res, *res0; selene_error_t *err = NULL; char ip_buf[INET6_ADDRSTRLEN]; char *ip_str = NULL; memset(&server, 0, sizeof(server)); snprintf(port_str, sizeof(port_str), "%i", port); memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV; rv = getaddrinfo(host, port_str, &hints, &res0); if (rv != 0) { fprintf(stderr, "TCP getaddrinfo(%s:%d) failed: (%d) %s\n", host, port, rv, gai_strerror(rv)); exit(EXIT_FAILURE); } server.sock = -1; for (res = res0; res; res = res->ai_next) { server.listen_sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if (server.listen_sock < 0) { continue; } ip_str = addr2str(res->ai_addr, &ip_buf[0], sizeof(ip_buf)); fprintf(stderr, "TCP bind(%s:%d)\n", ip_str, port); rv = bind(server.listen_sock, res->ai_addr, res->ai_addrlen); if (rv != 0) { close(server.listen_sock); server.listen_sock = -1; continue; } rv = listen(server.listen_sock, 10); if (rv != 0) { close(server.listen_sock); server.listen_sock = -1; continue; } break; } freeaddrinfo(res0); if (server.listen_sock == -1) { fprintf(stderr, "TCP bind(%s:%d) failed\n", host, port); exit(EXIT_FAILURE); } server.sock = -1; while (server.write_err == 0) { FD_ZERO(&readers); FD_SET(server.listen_sock, &readers); if (server.sock != -1) { FD_SET(server.sock, &readers); } FD_SET(fileno(fp), &readers); rv = select(FD_SETSIZE, &readers, NULL, NULL, NULL); if (rv > 0) { if (FD_ISSET(fileno(fp), &readers)) { p = fgets(buf, sizeof(buf), fp); if (p == NULL) { break; } if (server.sock != -1) { SERR(selene_io_in_clear_bytes(server.s, p, strlen(p))); } } else if (FD_ISSET(server.listen_sock, &readers)) { /* TODO: multiple client support */ if (server.sock == -1) { server.sock = accept(server.listen_sock, NULL, 0); err = selene_server_create(conf, &server.s); if (err != SELENE_SUCCESS) { fprintf(stderr, "Failed to create client instance: (%d) %s [%s:%d]\n", err->err, err->msg, err->file, err->line); exit(EXIT_FAILURE); } selene_subscribe(server.s, SELENE_EVENT_LOG_MSG, have_logline, NULL); SERR(selene_subscribe(server.s, SELENE_EVENT_IO_OUT_ENC, want_pull, &server)); SERR(selene_subscribe(server.s, SELENE_EVENT_IO_OUT_CLEAR, have_cleartext, &server)); SERR(selene_start(server.s)); } } else if (server.sock != -1 && FD_ISSET(server.sock, &readers)) { read_from_sock(&server); } } } if (server.write_err != 0) { /* TODO: client ip */ fprintf(stderr, "TCP write from %s:%d failed: (%d) %s\n", host, port, server.write_err, strerror(server.write_err)); exit(EXIT_FAILURE); } if (server.read_err != 0) { /* TODO: just disconnect client, keep listening */ fprintf(stderr, "TCP read on %s:%d failed: (%d) %s\n", host, port, server.read_err, strerror(server.read_err)); exit(EXIT_FAILURE); } if (server.s) { selene_destroy(server.s); server.s = NULL; } return 0; }
static void destroy_ctxt(void **state, selene_t *s, selene_conf_t *conf) { selene_destroy(s); selene_conf_destroy(conf); }