Browse Source

New unit test for HTTP authentication (part 2/?)

bel 9 years ago
parent
commit
dcac52b0ea
2 changed files with 40 additions and 21 deletions
  1. 1 1
      test/private.c
  2. 39 20
      test/public_server.c

+ 1 - 1
test/private.c

@@ -700,7 +700,7 @@ make_private_suite(void)
 /* Used to debug test cases without using the check framework */
 
 void
-main(void)
+MAIN_PRIVATE(void)
 {
 	test_alloc_vprintf(0);
 	test_mg_vsnprintf(0);

+ 39 - 20
test/public_server.c

@@ -2042,46 +2042,61 @@ END_TEST
 START_TEST(test_http_auth)
 {
 #if !defined(NO_FILES)
+	const char *OPTIONS[] = {
+#if !defined(NO_FILES)
+		"document_root",
+		".",
+#endif
+		"listening_ports",
+		"8080",
+		"static_file_max_age",
+		"0",
+		NULL,
+	};
 	struct mg_context *ctx;
 	struct mg_connection *client_conn;
 	char client_err[256];
 	const struct mg_request_info *client_ri;
 	int client_res;
-	FILE * f;
+	FILE *f;
+	const char *passwd_file = ".htpasswd";
+	const char *test_file = "test_http_auth.test_file.txt";
 	const char *test_content = "test_http_auth test_file content";
 	const char *domain;
+	const char *doc_root;
 	size_t len;
 
-	memset(errmsg, 0, sizeof(errmsg));
-
+	/* Start with default options */
 	mark_point();
-        /* Start with default options */
-	ctx = mg_start(NULL, NULL, NULL);
+	ctx = mg_start(NULL, NULL, OPTIONS);
 	test_sleep(1);
 
 	ck_assert(ctx != NULL);
 	domain = mg_get_option(ctx, "authentication_domain");
 	ck_assert(domain != NULL);
 	len = strlen(domain);
-	ck_assert_uint_gt(domain, 0);
-	ck_assert_uint_lt(domain, 64);
+	ck_assert_uint_gt(len, 0);
+	ck_assert_uint_lt(len, 64);
+	doc_root = mg_get_option(ctx, "document_root");
 
-        /* Create a default file in the document root */
-        f = fopen("test_http_auth.test_file.txt", "w");
-        if (f) {
+	/* Create a default file in the document root */
+	f = fopen(test_file, "w");
+	if (f) {
 		fprintf(f, "%s", test_content);
-		fclose(f);		
+		fclose(f);
 	} else {
-		ck_assert_abort_msg(f, "Cannot create file test_http_auth.test_file.txt");
+		ck_abort_msg("Cannot create file %s", test_file);
 	}
 
+	remove(passwd_file);
+
 	/* Read file before a .htpasswd file has been created */
 	memset(client_err, 0, sizeof(client_err));
 	client_conn =
 	    mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
 	ck_assert(client_conn != NULL);
 	ck_assert_str_eq(client_err, "");
-	mg_printf(client_conn, "GET / HTTP/1.0\r\n\r\n");
+	mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
 	client_res =
 	    mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
 	ck_assert_int_ge(client_res, 0);
@@ -2093,13 +2108,13 @@ START_TEST(test_http_auth)
 	client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
 	ck_assert_int_gt(client_res, 0);
 	ck_assert_int_le(client_res, sizeof(client_err));
-	ck_assert_str_eq(client_res, test_content);
+	ck_assert_str_eq(client_err, test_content);
 	mg_close_connection(client_conn);
 
 	test_sleep(1);
 
 	/* Create a .htpasswd file */
-	client_res = mg_modify_passwords_file(".htpasswd", domain, "user", "pass");
+	client_res = mg_modify_passwords_file(passwd_file, domain, "user", "pass");
 	ck_assert_int_eq(client_res, 1);
 
 	client_res = mg_modify_passwords_file(NULL, domain, "user", "pass");
@@ -2113,7 +2128,7 @@ START_TEST(test_http_auth)
 	    mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
 	ck_assert(client_conn != NULL);
 	ck_assert_str_eq(client_err, "");
-	mg_printf(client_conn, "GET / HTTP/1.0\r\n\r\n");
+	mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file);
 	client_res =
 	    mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
 	ck_assert_int_ge(client_res, 0);
@@ -2121,15 +2136,18 @@ START_TEST(test_http_auth)
 	client_ri = mg_get_request_info(client_conn);
 	ck_assert(client_ri != NULL);
 
-	ck_assert_str_eq(client_ri->uri, "401");
+	/* ck_assert_str_eq(client_ri->uri, "401"); */
 	mg_close_connection(client_conn);
 
 	test_sleep(1);
 
 
+	/* Stop the server and clean up */
 	mg_stop(ctx);
-#endif
+	remove(passwd_file); /* Don't leave it here for the next test */
+	remove(test_file);
 
+#endif
 }
 END_TEST
 
@@ -2192,7 +2210,7 @@ static int chk_failed = 0;
 
 
 void
-xmain(void)
+MAIN_PUBLIC_SERVER(void)
 {
 	/*
 	    test_the_test_environment(0);
@@ -2201,8 +2219,9 @@ xmain(void)
 	    test_mg_start_stop_https_server(0);
 	    test_request_handlers(0);
 	    test_mg_server_and_client_tls(0);
+	    test_handle_form(0);
 	*/
-	test_handle_form(0);
+	test_http_auth(0);
 
 	printf("\nok: %i\nfailed: %i\n\n", chk_ok, chk_failed);
 }