TEST_END TEST_START(parses_ssl_options_require_cert_and_key) { opt options; clax_options_init(&options); char *argv[] = {"clax", "-r", "."}; int ret = clax_parse_options(&options, sizeof_array(argv), argv); ASSERT_EQ(ret, -1) clax_options_free(&options); clax_options_init(&options); char *argv2[] = {"clax", "-r", ".", "-t", "ssl/server.crt"}; int ret2 = clax_parse_options(&options, sizeof_array(argv2), argv2); ASSERT_EQ(ret2, -1) clax_options_free(&options); clax_options_init(&options); char *argv3[] = {"clax", "-r", ".", "-p", "ssl/server.key"}; int ret3 = clax_parse_options(&options, sizeof_array(argv3), argv3); ASSERT_EQ(ret3, -1) clax_options_free(&options); }
TEST_END TEST_START(parses_basic_auth_invalid) { opt options; clax_options_init(&options); char *argv[] = {"clax", "-n", "-r", ".", "-a", "foobar"}; int ret = clax_parse_options(&options, sizeof_array(argv), argv); ASSERT_EQ(ret, -1); clax_options_free(&options); }
TEST_END TEST_START(returns_error_when_config_not_found) { opt options; clax_options_init(&options); char *argv[] = {"clax", "-c", "unknown.ini"}; int ret = clax_parse_options(&options, sizeof_array(argv), argv); ASSERT_EQ(ret, -1) clax_options_free(&options); }
TEST_END TEST_START(parses_no_ssl_options) { opt options; clax_options_init(&options); char *argv[] = {"clax", "-n", "-r", "."}; int ret = clax_parse_options(&options, sizeof_array(argv), argv); ASSERT_EQ(ret, 0); ASSERT_EQ(options.no_ssl, 1); clax_options_free(&options); }
TEST_END TEST_START(parses_log_file) { opt options; clax_options_init(&options); char *argv[] = {"clax", "-r", ".", "-n", "-l", "/dev/null"}; int ret = clax_parse_options(&options, sizeof_array(argv), argv); ASSERT_EQ(ret, 0) ASSERT_STR_EQ(options.log_file, "/dev/null") clax_options_free(&options); }
TEST_END TEST_START(parses_ssl_options_entropy_file) { opt options; clax_options_init(&options); char *argv[] = {"clax", "-r", ".", "-t", "ssl/server.crt", "-p", "ssl/server.key", "-e", "ssl/entropy"}; int ret = clax_parse_options(&options, sizeof_array(argv), argv); ASSERT_EQ(ret, 0) ASSERT_STR_EQ(options.entropy_file, "ssl/entropy") clax_options_free(&options); }
TEST_END TEST_START(parses_ssl_options_no_verify) { opt options; clax_options_init(&options); char *argv[] = {"clax", "-r", ".", "-t", "ssl/server.crt", "-p", "ssl/server.key", "-k"}; int ret = clax_parse_options(&options, sizeof_array(argv), argv); ASSERT_EQ(ret, 0) ASSERT_EQ(options.no_ssl_verify, 1) clax_options_free(&options); }
TEST_END TEST_START(parses_basic_auth) { opt options; clax_options_init(&options); char *argv[] = {"clax", "-n", "-r", ".", "-a", "foo:bar"}; int ret = clax_parse_options(&options, sizeof_array(argv), argv); ASSERT_EQ(ret, 0); ASSERT_STR_EQ(options.basic_auth_username, "foo"); ASSERT_STR_EQ(options.basic_auth_password, "bar"); clax_options_free(&options); }
TEST_END TEST_START(root_is_required) { opt options; clax_options_init(&options); char *argv[] = {"clax", "-n"}; int ret = clax_parse_options(&options, sizeof_array(argv), argv); ASSERT_EQ(ret, -1); char *argv2[] = {"clax", "-n", "-r", "unknown-root"}; int ret2 = clax_parse_options(&options, sizeof_array(argv2), argv2); ASSERT_EQ(ret2, -1); clax_options_free(&options); }
TEST_END TEST_START(parses_config) { opt options; clax_options_init(&options); char *argv[] = {"clax", "-c", "config.ini"}; int ret = clax_parse_options(&options, sizeof_array(argv), argv); ASSERT_EQ(ret, 0) ASSERT_STR_EQ(options.config_file, "config.ini") ASSERT_EQ(options.no_ssl, 0); ASSERT_EQ(options.no_ssl_verify, 1) ASSERT_STR_EQ(options.cert_file, "ssl/server.crt"); ASSERT_STR_EQ(options.key_file, "ssl/server.key"); ASSERT_STR_EQ(options.entropy_file, "ssl/entropy"); ASSERT_STR_EQ(options.basic_auth_username, "clax"); ASSERT_STR_EQ(options.basic_auth_password, "password"); clax_options_free(&options); }
int main(int argc, char **argv) { clax_ctx_t clax_ctx; #ifdef _WIN32 _setmode(_fileno(stdin), _O_BINARY); _setmode(_fileno(stdout), _O_BINARY); _setmode(_fileno(stderr), _O_BINARY); #endif int is_interactive = isatty(fileno(stdin)); signal(SIGINT, term); setbuf(stdout, NULL); clax_options_init(&options); clax_ctx_init(&clax_ctx); clax_ctx.options = &options; int ok = clax_parse_options(&options, argc, argv); if (ok < 0) { const char *error = clax_strerror(ok); size_t len = strlen(error); if (is_interactive) { fprintf(stdout, "Error: %s\n", error); if (ok != -1) { fprintf(stdout, "\n"); clax_usage(); } clax_exit(255); } else { char buf[1024]; char *b = buf; snprintf(buf, sizeof(buf), "HTTP/1.1 500 System Error\r\n" "Content-Type: text/plain\r\n" "Content-Length: %d\r\n" "\r\n" "%s\n", (int)(len + 1), error ); #ifdef MVS b = clax_etoa_alloc(buf, strlen(buf)); #endif fprintf(stdout, "%s", b); #ifdef MVS free(b); #endif goto cleanup; } } clax_log("Option: root=%s", options.root); clax_log("Option: entropy_file=%s", options.entropy_file); clax_log("Option: log_file=%s", options.log_file); clax_log("Option: ssl=%d", options.ssl); if (!options.ssl) { clax_http_dispatch(&clax_ctx, clax_send, clax_recv, NULL); } else { clax_loop_ssl(&clax_ctx); } cleanup: fflush(stdout); fclose(stdout); clax_ctx_free(&clax_ctx); clax_options_free(&options); clax_exit(0); }