1 /* Copyright (c) 2015-2018 the Civetweb developers
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  */
21 
22 #if defined(_MSC_VER)
23 #define _CRT_SECURE_NO_WARNINGS /* Microsoft nonsense */
24 #endif
25 
26 #include "civetweb_check.h"
27 #include "private.h"
28 #include "private_exe.h"
29 #include "public_func.h"
30 #include "public_server.h"
31 #include "shared.h"
32 #include "timertest.h"
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 
38 /* This unit test file uses the excellent Check unit testing library.
39  * The API documentation is available here:
40  * http://check.sourceforge.net/doc/check_html/index.html
41  *
42  * Note: CivetWeb is tested using it's own fork of check:
43  * https://github.com/civetweb/check
44  * Required fixes from this fork are already available
45  * in the main repository:
46  * https://github.com/libcheck/check
47  */
48 
49 #define FILENAME_LEN (128)
50 
51 int
main(const int argc,char * argv[])52 main(const int argc, char *argv[])
53 {
54 	/* Supported command line arguments */
55 	const char *const suite_arg = "--suite=";
56 	const size_t suite_arg_size = strlen(suite_arg);
57 	const char *const test_case_arg = "--test-case=";
58 	const size_t test_case_arg_size = strlen(test_case_arg);
59 	const char *const test_dir_arg = "--test-dir=";
60 	const size_t test_dir_arg_size = strlen(test_dir_arg);
61 	const char *const test_log_arg = "--test-log=";
62 	const size_t test_log_arg_size = strlen(test_log_arg);
63 	const char *const help_arg = "--help";
64 
65 	/* Test variables */
66 	const char *suite = NULL;
67 	const char *test_case = NULL;
68 	SRunner *srunner;
69 	int number_run = 0;
70 	int number_failed = 0;
71 	const char *test_log_prefix = NULL;
72 	char test_log_name[FILENAME_LEN];
73 	char test_xml_name[FILENAME_LEN];
74 
75 	int i;
76 
77 	/* Check command line parameters for tests */
78 	for (i = 1; i < argc; ++i) {
79 		if (0 == strncmp(suite_arg, argv[i], suite_arg_size)
80 		    && (strlen(argv[i]) > suite_arg_size)) {
81 			suite = &argv[i][suite_arg_size];
82 		} else if (0 == strncmp(test_case_arg, argv[i], test_case_arg_size)
83 		           && (strlen(argv[i]) > test_case_arg_size)) {
84 			test_case = &argv[i][test_case_arg_size];
85 		} else if (0 == strncmp(test_dir_arg, argv[i], test_dir_arg_size)
86 		           && (strlen(argv[i]) > test_dir_arg_size)) {
87 			set_test_directory(&argv[i][test_dir_arg_size]);
88 		} else if (0 == strncmp(test_log_arg, argv[i], test_log_arg_size)
89 		           && (strlen(argv[i]) > test_log_arg_size)) {
90 			test_log_prefix = &argv[i][test_log_arg_size];
91 			if ((strlen(test_log_prefix) + 16) > FILENAME_LEN) {
92 				fprintf(stderr, "Argument too long: %s\n", argv[i]);
93 				exit(EXIT_FAILURE);
94 			}
95 		} else if (0 == strcmp(help_arg, argv[i])) {
96 			printf(
97 			    "Usage: %s [options]\n"
98 			    "  --suite=Suite            Determines the suite to run\n"
99 			    "  --test-case='Test Case'  Determines the test case to run\n"
100 			    "  --test-dir='folder/path' The location of the test directory "
101 			    "with the \n"
102 			    "                           'fixtures' and 'expected\n",
103 			    argv[0]);
104 			exit(EXIT_SUCCESS);
105 		} else {
106 			fprintf(stderr, "Invalid argument: %s\n", argv[i]);
107 			exit(EXIT_FAILURE);
108 		}
109 	}
110 
111 	/* Register all tests to run them later */
112 	srunner = srunner_create(make_public_func_suite());
113 	srunner_add_suite(srunner, make_public_server_suite());
114 	srunner_add_suite(srunner, make_private_suite());
115 	srunner_add_suite(srunner, make_private_exe_suite());
116 	srunner_add_suite(srunner, make_timertest_suite());
117 
118 	/* Write test logs to a file */
119 	if (test_log_prefix == NULL) {
120 		/* Find the next free log name */
121 		FILE *f;
122 		for (i = 1;; i++) {
123 			/* enumerate all log files (8.3 filename using 3 digits) */
124 			sprintf(test_log_name, "test-%03i.log", i);
125 			f = fopen(test_log_name, "r");
126 			if (f) {
127 				/* file already exists */
128 				fclose(f);
129 				/* try next name */
130 				continue;
131 			}
132 			/* file does not exist - use this name */
133 			srunner_set_log(srunner, test_log_name);
134 			/* use the same index for xml as for log */
135 			sprintf(test_xml_name, "test-%03i.xml", i);
136 			srunner_set_xml(srunner, test_xml_name);
137 			break;
138 		}
139 	} else {
140 		/* We got a test log name from the command line */
141 		sprintf(test_log_name, "%s.log", test_log_prefix);
142 		srunner_set_log(srunner, test_log_name);
143 		sprintf(test_xml_name, "%s.xml", test_log_prefix);
144 		srunner_set_xml(srunner, test_xml_name);
145 	}
146 
147 	/* Run tests, using log level CK_VERBOSE, since CK_NORMAL
148 	 * offers not enough diagnosis to analyze failed tests.
149 	 * see http://check.sourceforge.net/doc/check_html/check_3.html */
150 	srunner_run(srunner, suite, test_case, CK_VERBOSE);
151 
152 	/* Check passed / failed */
153 	number_run = srunner_ntests_run(srunner);
154 	number_failed = srunner_ntests_failed(srunner);
155 	srunner_free(srunner);
156 	return ((number_failed == 0) && (number_run != 0)) ? EXIT_SUCCESS
157 	                                                   : EXIT_FAILURE;
158 }
159