-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_testlib.h
278 lines (215 loc) · 6.96 KB
/
_testlib.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#ifndef CGILIB_TESTING_LIBRARY_H_
#define CGILIB_TESTING_LIBRARY_H_
#include <string>
#include <cstdlib>
#include <sstream>
#include <iostream>
#include <vector>
#include <utility>
#include <sys/time.h>
#include <ctime> // In conjunction with <cstdlib>, we have pseudo-random numbers
#include "cgilib.h"
extern char** environ;
namespace cgi {
namespace test {
long __timeval_to_seconds(timeval tv) {
return (tv.tv_sec * 1000000) + tv.tv_usec;
}
// Create a class that inherits from this class to write a test.
class unit_test {
public:
unit_test(std::string name) {
name_ = name;
srand(time(NULL)); // Seed number gen
clear_environment(); // Start with a fresh slate
}
void build_environment() {
// Build headers
for(int i = 0; i < headers.size(); ++i) {
std::string name = headers[i].first;
for(int j = 0; j < name.size(); ++j) {
if(name[j] == '-') {
name[j] == '_';
} else {
name[j] = ::cgi::__string_asciitoupper__(name[j]);
}
}
setenv((std::string("HTTP_") + name).c_str(), read_header(headers[i].first).c_str(), 1);
}
}
void print_request_headers() {
for(int i = 0; i < headers.size(); ++i) {
std::string name = headers[i].first;
for(int j = 0; j < headers[i].second.size(); ++j) {
std::cout << "\033[48;33m" << name << ": \033[48;31m" << headers[i].second[j] << "\033[0m" << std::endl;
}
}
}
std::string get_name() {
return name_;
}
void clear_environment() {
// SEE #15 TO UNDERSTAND THIS
for(char** env = environ; *env; ++env) {
std::string varline = *env;
std::string varname = varline.substr(0, varline.find('='));
unsetenv(varname.c_str());
}
}
void clear_headers() {
headers = {};
}
virtual int run() = 0;
virtual void initialize() = 0;
private:
std::string name_;
std::vector<std::pair<std::string, std::vector<std::string>>> headers;
protected:
bool gen_chance(int numerator, int denominator) {
return rand() <= (((RAND_MAX)/denominator) * numerator);
}
void create_header(std::string name, std::string field) {
std::vector<std::string> emptyvec{};
int entry_index = -1;
for(int i = 0; i < headers.size(); ++i) {
if(headers[i].first == name) {
entry_index = i;
break;
}
}
if(entry_index == -1) {
headers.push_back(std::make_pair(name, emptyvec));
entry_index = headers.size()-1;
}
headers[entry_index].second.push_back(field);
}
std::string read_header(std::string name) {
int entry_index = -1;
for(int i = 0; i < headers.size(); ++i) {
if(headers[i].first == name) {
entry_index = i;
break;
}
}
if(entry_index == -1) {
return "";
}
std::string result;
std::string sep = ", ";
// To reduce the use of memory reallocation, we'll determine result size beforehand
int alloc_size = headers[entry_index].second.size()*2 - 2;
for(int i = 0; i < headers[entry_index].second.size(); ++i) {
alloc_size += headers[entry_index].second[i].size();
}
result.reserve(alloc_size);
for(int i = 0; i < headers[entry_index].second.size(); ++i) {
if(name == "Cookie") { // Cookie has a semicolon (;) separator, not comma (,).
// Still good practice to test both ways, since it could be given either way.
if(rand() > RAND_MAX/2) {
sep = "; ";
}
}
if(i != 0) {
result += sep;
}
result += headers[entry_index].second[i];
}
return result;
}
// Begin setters for metavariables [rfc3875 section 4.1]
void set_auth_type(std::string type) {
setenv("AUTH_TYPE", type.c_str(), 1);
}
void set_content_length(int length) {
setenv("CONTENT_LENGTH", std::to_string(length).c_str(), 1);
}
void set_content_type(std::string type) {
setenv("CONTENT_TYPE", type.c_str(), 1);
}
void set_cgi_version(int major, int minor) {
setenv("GATEWAY_INTERFACE", (std::string("CGI/") + std::to_string(major) + "." + std::to_string(minor)).c_str(), 1);
}
void set_path_info(std::string info) {
setenv("PATH_INFO", info.c_str(), 1);
}
void set_translated_path(std::string path) {
setenv("PATH_TRANSLATED", path.c_str(), 1);
}
void set_query_string(std::string query) {
setenv("QUERY_STRING", query.c_str(), 1);
}
void set_client_address(std::string remote) {
setenv("REMOTE_ADDR", remote.c_str(), 1);
}
void set_client_name(std::string name) {
setenv("REMOTE_HOST", name.c_str(), 1);
}
void set_client_identity(std::string identity) {
setenv("REMOTE_IDENT", identity.c_str(), 1);
}
void set_user_string(std::string user) {
setenv("REMOTE_USER", user.c_str(), 1);
}
void set_request_method(std::string method) {
setenv("REQUEST_METHOD", method.c_str(), 1);
}
void set_script_name(std::string name) {
setenv("SCRIPT_NAME", name.c_str(), 1);
}
void set_address(std::string address) {
setenv("SERVER_NAME", address.c_str(), 1);
}
void set_port(int port) {
setenv("SERVER_PORT", std::to_string(port).c_str(), 1);
}
void use_http() {
set_port(80);
}
void use_https() {
set_port(443);
}
void set_http_version(int major, int minor) {
setenv("SERVER_PROTOCOL", (std::string("HTTP/") + std::to_string(major) + "." + std::to_string(minor)).c_str(), 1);
}
void set_server_software(std::string software) {
setenv("SERVER_SOFTWARE", software.c_str(), 1);
}
void set_request_body(std::string body) {
std::istringstream b(body);
std::cin.rdbuf(b.rdbuf());
}
};
void execute_test(unit_test& test) {
int trial_count = 25;
int max_avg_time = 20000; // measured in microseconds
long usec_sum = 0;
for(int i = 0; i < trial_count; ++i) {
std::cout << "\033[48;34;1m" << "Running test: " << test.get_name() << " (" << i+1 << " of " << trial_count << ")" << "\033[0m" << std::endl;
::cgi::headers.clear();
::cgi::out.str("");
test.clear_environment();
test.clear_headers();
test.initialize();
test.build_environment();
::cgi::request_headers.regenerate_list();
std::cout << "\033[48;35;1m" << "Request headers given to script: " << "\033[0m" << std::endl;
test.print_request_headers();
std::cout << std::endl << "\033[48;34;1m" << "Script Output:" << "\033[0m" << std::endl;
timeval tv;
gettimeofday(&tv, 0);
long t_0 = __timeval_to_seconds(tv);
int res = test.run();
gettimeofday(&tv, 0);
long t_f = __timeval_to_seconds(tv);
usec_sum += t_f - t_0;
if(res != 0) {
std::cout << "\033[48;31;1m" << "Error occured in test (via return) " << test.get_name() << "\033[0m" << std::endl;
}
}
if(usec_sum >= trial_count * max_avg_time) {
std::cout << "\033[48;33;1m" << "WARNING: Average execution time was longer than " << max_avg_time/1000 << "ms (" << usec_sum/1000 << "ms)" << "\033[0m" << std::endl;
}
}
} // namespace test
} // namespace cgi
#endif // CGILIB_TESTING_LIBRARY_H_