Skip to content

Commit fb58ec7

Browse files
authored
Make more robust tests. (#312)
* Add a test verifying that the test content files are in fact readable. * Also check HTTP responce code. * Delete dead variable. * Add include. * Allow playing games with where the test_content files are (and thus relax what directory the test can be run from). * Allow playing games with where the test_content files are (and thus relax what directory the test can be run from). * Allow playing games with where the .pem files are (and thus relax what directory the test can be run from). * Allow flexability for TCP ports on all the rest of the tests. * Make sure register_resource calls don't fail. * Make lint happy. * Deal with cross thread error propogation. * Deal with some special cases for loggin in littletest.
1 parent 84996ea commit fb58ec7

File tree

10 files changed

+257
-155
lines changed

10 files changed

+257
-155
lines changed

test/integ/authentication.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ using httpserver::string_response;
4747
using httpserver::http_resource;
4848
using httpserver::http_request;
4949

50+
#ifdef HTTPSERVER_PORT
51+
#define PORT HTTPSERVER_PORT
52+
#else
53+
#define PORT 8080
54+
#endif // PORT
55+
56+
#define STR2(p) #p
57+
#define STR(p) STR2(p)
58+
#define PORT_STRING STR(PORT)
59+
5060
size_t writefunc(void *ptr, size_t size, size_t nmemb, std::string *s) {
5161
s->append(reinterpret_cast<char*>(ptr), size*nmemb);
5262
return size*nmemb;
@@ -86,10 +96,10 @@ LT_BEGIN_SUITE(authentication_suite)
8696
LT_END_SUITE(authentication_suite)
8797

8898
LT_BEGIN_AUTO_TEST(authentication_suite, base_auth)
89-
webserver ws = create_webserver(8080);
99+
webserver ws = create_webserver(PORT);
90100

91101
user_pass_resource user_pass;
92-
ws.register_resource("base", &user_pass);
102+
LT_ASSERT_EQ(true, ws.register_resource("base", &user_pass));
93103
ws.start(false);
94104

95105
curl_global_init(CURL_GLOBAL_ALL);
@@ -98,7 +108,7 @@ LT_BEGIN_AUTO_TEST(authentication_suite, base_auth)
98108
CURLcode res;
99109
curl_easy_setopt(curl, CURLOPT_USERNAME, "myuser");
100110
curl_easy_setopt(curl, CURLOPT_PASSWORD, "mypass");
101-
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
111+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
102112
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
103113
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
104114
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
@@ -111,10 +121,10 @@ LT_BEGIN_AUTO_TEST(authentication_suite, base_auth)
111121
LT_END_AUTO_TEST(base_auth)
112122

113123
LT_BEGIN_AUTO_TEST(authentication_suite, base_auth_fail)
114-
webserver ws = create_webserver(8080);
124+
webserver ws = create_webserver(PORT);
115125

116126
user_pass_resource user_pass;
117-
ws.register_resource("base", &user_pass);
127+
LT_ASSERT_EQ(true, ws.register_resource("base", &user_pass));
118128
ws.start(false);
119129

120130
curl_global_init(CURL_GLOBAL_ALL);
@@ -123,7 +133,7 @@ LT_BEGIN_AUTO_TEST(authentication_suite, base_auth_fail)
123133
CURLcode res;
124134
curl_easy_setopt(curl, CURLOPT_USERNAME, "myuser");
125135
curl_easy_setopt(curl, CURLOPT_PASSWORD, "wrongpass");
126-
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
136+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
127137
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
128138
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
129139
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
@@ -141,12 +151,12 @@ LT_END_AUTO_TEST(base_auth_fail)
141151
#ifndef _WINDOWS
142152

143153
LT_BEGIN_AUTO_TEST(authentication_suite, digest_auth)
144-
webserver ws = create_webserver(8080)
154+
webserver ws = create_webserver(PORT)
145155
.digest_auth_random("myrandom")
146156
.nonce_nc_size(300);
147157

148158
digest_resource digest;
149-
ws.register_resource("base", &digest);
159+
LT_ASSERT_EQ(true, ws.register_resource("base", &digest));
150160
ws.start(false);
151161

152162
#if defined(_WINDOWS)
@@ -164,7 +174,7 @@ LT_BEGIN_AUTO_TEST(authentication_suite, digest_auth)
164174
#else
165175
curl_easy_setopt(curl, CURLOPT_USERPWD, "myuser:mypass");
166176
#endif
167-
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
177+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
168178
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
169179
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
170180
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
@@ -181,12 +191,12 @@ LT_BEGIN_AUTO_TEST(authentication_suite, digest_auth)
181191
LT_END_AUTO_TEST(digest_auth)
182192

183193
LT_BEGIN_AUTO_TEST(authentication_suite, digest_auth_wrong_pass)
184-
webserver ws = create_webserver(8080)
194+
webserver ws = create_webserver(PORT)
185195
.digest_auth_random("myrandom")
186196
.nonce_nc_size(300);
187197

188198
digest_resource digest;
189-
ws.register_resource("base", &digest);
199+
LT_ASSERT_EQ(true, ws.register_resource("base", &digest));
190200
ws.start(false);
191201

192202
#if defined(_WINDOWS)
@@ -204,7 +214,7 @@ LT_BEGIN_AUTO_TEST(authentication_suite, digest_auth_wrong_pass)
204214
#else
205215
curl_easy_setopt(curl, CURLOPT_USERPWD, "myuser:wrongpass");
206216
#endif
207-
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
217+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
208218
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
209219
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
210220
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);

test/integ/ban_system.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ using httpserver::string_response;
3636
using httpserver::http_request;
3737
using httpserver::http::http_utils;
3838

39+
#ifdef HTTPSERVER_PORT
40+
#define PORT HTTPSERVER_PORT
41+
#else
42+
#define PORT 8080
43+
#endif // PORT
44+
45+
#define STR2(p) #p
46+
#define STR(p) STR2(p)
47+
#define PORT_STRING STR(PORT)
48+
3949
size_t writefunc(void *ptr, size_t size, size_t nmemb, std::string *s) {
4050
s->append(reinterpret_cast<char*>(ptr), size*nmemb);
4151
return size*nmemb;
@@ -57,19 +67,19 @@ LT_BEGIN_SUITE(ban_system_suite)
5767
LT_END_SUITE(ban_system_suite)
5868

5969
LT_BEGIN_AUTO_TEST(ban_system_suite, accept_default_ban_blocks)
60-
webserver ws = create_webserver(8080).default_policy(http_utils::ACCEPT);
70+
webserver ws = create_webserver(PORT).default_policy(http_utils::ACCEPT);
6171
ws.start(false);
6272

6373
ok_resource resource;
64-
ws.register_resource("base", &resource);
74+
LT_ASSERT_EQ(true, ws.register_resource("base", &resource));
6575

6676
curl_global_init(CURL_GLOBAL_ALL);
6777

6878
{
6979
std::string s;
7080
CURL *curl = curl_easy_init();
7181
CURLcode res;
72-
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
82+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
7383
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
7484
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
7585
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
@@ -84,7 +94,7 @@ LT_BEGIN_AUTO_TEST(ban_system_suite, accept_default_ban_blocks)
8494

8595
CURL *curl = curl_easy_init();
8696
CURLcode res;
87-
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
97+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
8898
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
8999
res = curl_easy_perform(curl);
90100
LT_ASSERT_NEQ(res, 0);
@@ -97,7 +107,7 @@ LT_BEGIN_AUTO_TEST(ban_system_suite, accept_default_ban_blocks)
97107
std::string s;
98108
CURL *curl = curl_easy_init();
99109
CURLcode res;
100-
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
110+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
101111
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
102112
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
103113
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
@@ -112,18 +122,18 @@ LT_BEGIN_AUTO_TEST(ban_system_suite, accept_default_ban_blocks)
112122
LT_END_AUTO_TEST(accept_default_ban_blocks)
113123

114124
LT_BEGIN_AUTO_TEST(ban_system_suite, reject_default_allow_passes)
115-
webserver ws = create_webserver(8080).default_policy(http_utils::REJECT);
125+
webserver ws = create_webserver(PORT).default_policy(http_utils::REJECT);
116126
ws.start(false);
117127

118128
ok_resource resource;
119-
ws.register_resource("base", &resource);
129+
LT_ASSERT_EQ(true, ws.register_resource("base", &resource));
120130

121131
curl_global_init(CURL_GLOBAL_ALL);
122132

123133
{
124134
CURL *curl = curl_easy_init();
125135
CURLcode res;
126-
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
136+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
127137
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
128138
res = curl_easy_perform(curl);
129139
LT_ASSERT_NEQ(res, 0);
@@ -136,7 +146,7 @@ LT_BEGIN_AUTO_TEST(ban_system_suite, reject_default_allow_passes)
136146
std::string s;
137147
CURL *curl = curl_easy_init();
138148
CURLcode res;
139-
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
149+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
140150
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
141151
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
142152
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
@@ -151,7 +161,7 @@ LT_BEGIN_AUTO_TEST(ban_system_suite, reject_default_allow_passes)
151161

152162
CURL *curl = curl_easy_init();
153163
CURLcode res;
154-
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
164+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/base");
155165
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
156166
res = curl_easy_perform(curl);
157167
LT_ASSERT_NEQ(res, 0);

0 commit comments

Comments
 (0)