Bladeren bron

Unit test: create a function for locating the ssl cert in different test environments

bel 10 jaren geleden
bovenliggende
commit
052f7d33b3
1 gewijzigde bestanden met toevoegingen van 57 en 29 verwijderingen
  1. 57 29
      test/public_server.c

+ 57 - 29
test/public_server.c

@@ -46,6 +46,30 @@
  */
  */
 
 
 
 
+const char *locate_ssl_cert(void)
+{
+	return
+#ifdef _WIN32
+#ifdef LOCAL_TEST
+	    "resources\\ssl_cert.pem";
+#else
+	    /* Appveyor */
+	    "..\\..\\..\\resources\\ssl_cert.pem"; /* TODO: the different paths
+                                                * used in the different test 
+                                                * system is an unsolved 
+                                                * problem */
+#endif
+#else
+#ifdef LOCAL_TEST
+        "../resources/ssl_cert.pem";
+#else
+	    /* Travis */
+	    "../../resources/ssl_cert.pem"; // TODO: fix path in CI test environment
+#endif
+#endif
+}
+
+
 START_TEST(test_the_test_environment)
 START_TEST(test_the_test_environment)
 {
 {
 	char wd[300];
 	char wd[300];
@@ -53,10 +77,12 @@ START_TEST(test_the_test_environment)
 	FILE *f;
 	FILE *f;
 	struct stat st;
 	struct stat st;
 	int ret;
 	int ret;
+	const char *ssl_cert = locate_ssl_cert();
 
 
 	memset(wd, 0, sizeof(wd));
 	memset(wd, 0, sizeof(wd));
 	memset(buf, 0, sizeof(buf));
 	memset(buf, 0, sizeof(buf));
 
 
+
 /* Get the current working directory */
 /* Get the current working directory */
 #ifdef _WIN32
 #ifdef _WIN32
 	(void)GetCurrentDirectoryA(sizeof(wd), wd);
 	(void)GetCurrentDirectoryA(sizeof(wd), wd);
@@ -69,11 +95,13 @@ START_TEST(test_the_test_environment)
 /* Check the pem file */
 /* Check the pem file */
 #ifdef _WIN32
 #ifdef _WIN32
 	strcpy(buf, wd);
 	strcpy(buf, wd);
-	strcat(buf, "..\\..\\..\\resources\\ssl_cert.pem");
+    strcat(buf, "\\");
+	strcat(buf, ssl_cert);
 	f = fopen(buf, "rb");
 	f = fopen(buf, "rb");
 #else
 #else
 	strcpy(buf, wd);
 	strcpy(buf, wd);
-	strcat(buf, "../../resources/ssl_cert.pem");
+    strcat(buf, "/");
+	strcat(buf, ssl_cert);
 	f = fopen(buf, "r");
 	f = fopen(buf, "r");
 #endif
 #endif
 
 
@@ -163,30 +191,36 @@ END_TEST
 
 
 START_TEST(test_mg_start_stop_https_server)
 START_TEST(test_mg_start_stop_https_server)
 {
 {
+#ifndef NO_SSL
+
 	struct mg_context *ctx;
 	struct mg_context *ctx;
-	const char *OPTIONS[] = {
-#if !defined(NO_FILES)
-		"document_root",
-		".",
-#endif
-		"listening_ports",
-		"8080r,8443s",
-		"ssl_certificate",
-#ifdef _WIN32
-		"..\\..\\..\\resources/ssl_cert.pem", // TODO: the different paths used
-                                              // in the different test system is
-                                              // an unsolved problem
-#else
-		"../../resources/ssl_cert.pem", // TODO: fix path in CI test environment
-#endif
-		NULL,
-	};
+
 	size_t ports_cnt;
 	size_t ports_cnt;
 	int ports[16];
 	int ports[16];
 	int ssl[16];
 	int ssl[16];
 	struct mg_callbacks callbacks;
 	struct mg_callbacks callbacks;
 	char errmsg[256];
 	char errmsg[256];
 
 
+	const char *OPTIONS[8]; /* initializer list here is rejected by CI test */
+	int opt_idx = 0;
+	const char *ssl_cert = locate_ssl_cert();
+	ck_assert(ssl_cert != NULL);
+
+	memset((void *)OPTIONS, 0, sizeof(OPTIONS));
+#if !defined(NO_FILES)
+	OPTIONS[opt_idx++] = "document_root";
+	OPTIONS[opt_idx++] = ".";
+#endif
+	OPTIONS[opt_idx++] = "listening_ports";
+	OPTIONS[opt_idx++] = "8080r,8443s";
+	OPTIONS[opt_idx++] = "ssl_certificate";
+	OPTIONS[opt_idx++] = ssl_cert;
+
+	ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
+	ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
+	ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
+
+
 	memset(ports, 0, sizeof(ports));
 	memset(ports, 0, sizeof(ports));
 	memset(ssl, 0, sizeof(ssl));
 	memset(ssl, 0, sizeof(ssl));
 	memset(&callbacks, 0, sizeof(callbacks));
 	memset(&callbacks, 0, sizeof(callbacks));
@@ -210,6 +244,7 @@ START_TEST(test_mg_start_stop_https_server)
 
 
 	mg_Sleep(1);
 	mg_Sleep(1);
 	mg_stop(ctx);
 	mg_stop(ctx);
+#endif
 }
 }
 END_TEST
 END_TEST
 
 
@@ -294,6 +329,7 @@ START_TEST(test_request_handlers)
 	const char *opt;
 	const char *opt;
 	FILE *f;
 	FILE *f;
 	int opt_idx = 0;
 	int opt_idx = 0;
+	const char *ssl_cert = locate_ssl_cert();
 
 
 	memset((void *)OPTIONS, 0, sizeof(OPTIONS));
 	memset((void *)OPTIONS, 0, sizeof(OPTIONS));
 	OPTIONS[opt_idx++] = "listening_ports";
 	OPTIONS[opt_idx++] = "listening_ports";
@@ -303,17 +339,9 @@ START_TEST(test_request_handlers)
 	OPTIONS[opt_idx++] = ".";
 	OPTIONS[opt_idx++] = ".";
 #endif
 #endif
 #ifndef NO_SSL
 #ifndef NO_SSL
+	ck_assert(ssl_cert != NULL);
 	OPTIONS[opt_idx++] = "ssl_certificate";
 	OPTIONS[opt_idx++] = "ssl_certificate";
-#ifdef _WIN32
-	OPTIONS[opt_idx++] =
-	    "..\\..\\..\\resources/ssl_cert.pem"; // TODO: the different
-                                              // paths used in the
-                                              // different test system
-                                              // is an unsolved problem
-#else
-	OPTIONS[opt_idx++] =
-	    "../../resources/ssl_cert.pem"; // TODO: fix path in CI test environment
-#endif
+	OPTIONS[opt_idx++] = ssl_cert;
 #endif
 #endif
 	ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
 	ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
 	ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
 	ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);