Skip to content

Commit 4491b37

Browse files
committed
waitress: Move testcases to separate file
Now the test-enabled waitress.o does not conflict with pianobar’s waitress.o any more, thus running `make test` without `make clean` works fine.
1 parent b8e3899 commit 4491b37

File tree

4 files changed

+149
-123
lines changed

4 files changed

+149
-123
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
pianobar
77
libpiano.a
88
libpiano.so*
9+
waitress-test

Makefile

+5-3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ LIBWAITRESS_OBJ:=${LIBWAITRESS_SRC:.c=.o}
6868
LIBWAITRESS_RELOBJ:=${LIBWAITRESS_SRC:.c=.lo}
6969
LIBWAITRESS_INCLUDE:=${LIBWAITRESS_DIR}
7070

71+
LIBWAITRESS_TEST_SRC=${LIBWAITRESS_DIR}/waitress-test.c
72+
LIBWAITRESS_TEST_OBJ:=${LIBWAITRESS_TEST_SRC:.c=.o}
73+
7174
ifeq (${DISABLE_FAAD}, 1)
7275
LIBFAAD_CFLAGS:=
7376
LIBFAAD_LDFLAGS:=
@@ -189,9 +192,8 @@ debug: CFLAGS=-pedantic -ggdb -Wall -Wmissing-declarations -Wshadow -Wcast-qual
189192
# -Wstack-protector: we don't use stack protector
190193
# -Woverlength-strings: over-portability-ish
191194

192-
waitress-test: CFLAGS+= -DTEST
193-
waitress-test: ${LIBWAITRESS_OBJ}
194-
${CC} ${LDFLAGS} ${LIBWAITRESS_OBJ} ${LIBGNUTLS_LDFLAGS} -o waitress-test
195+
waitress-test: ${LIBWAITRESS_TEST_OBJ}
196+
${CC} ${LDFLAGS} ${LIBWAITRESS_TEST_OBJ} ${LIBGNUTLS_LDFLAGS} -o waitress-test
195197

196198
test: waitress-test
197199
./waitress-test

src/libwaitress/waitress-test.c

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
Copyright (c) 2009-2013
3+
Lars-Dominik Braun <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.
22+
*/
23+
24+
/* test cases for libwaitress */
25+
26+
/* we are testing static methods and therefore have to include the .c */
27+
#include "waitress.c"
28+
29+
#include <stdio.h>
30+
#include <stdbool.h>
31+
#include <string.h>
32+
33+
#define streq(a,b) (strcmp(a,b) == 0)
34+
35+
/* string equality test (memory location or content)
36+
*/
37+
static bool streqtest (const char *x, const char *y) {
38+
return (x == y) || (x != NULL && y != NULL && streq (x, y));
39+
}
40+
41+
/* test WaitressSplitUrl
42+
* @param tested url
43+
* @param expected user
44+
* @param expected password
45+
* @param expected host
46+
* @param expected port
47+
* @param expected path
48+
*/
49+
static void compareUrl (const char *url, const char *user,
50+
const char *password, const char *host, const char *port,
51+
const char *path) {
52+
WaitressUrl_t splitUrl;
53+
54+
memset (&splitUrl, 0, sizeof (splitUrl));
55+
56+
WaitressSplitUrl (url, &splitUrl);
57+
58+
bool userTest, passwordTest, hostTest, portTest, pathTest, overallTest;
59+
60+
userTest = streqtest (splitUrl.user, user);
61+
passwordTest = streqtest (splitUrl.password, password);
62+
hostTest = streqtest (splitUrl.host, host);
63+
portTest = streqtest (splitUrl.port, port);
64+
pathTest = streqtest (splitUrl.path, path);
65+
66+
overallTest = userTest && passwordTest && hostTest && portTest && pathTest;
67+
68+
if (!overallTest) {
69+
printf ("FAILED test(s) for %s\n", url);
70+
if (!userTest) {
71+
printf ("user: %s vs %s\n", splitUrl.user, user);
72+
}
73+
if (!passwordTest) {
74+
printf ("password: %s vs %s\n", splitUrl.password, password);
75+
}
76+
if (!hostTest) {
77+
printf ("host: %s vs %s\n", splitUrl.host, host);
78+
}
79+
if (!portTest) {
80+
printf ("port: %s vs %s\n", splitUrl.port, port);
81+
}
82+
if (!pathTest) {
83+
printf ("path: %s vs %s\n", splitUrl.path, path);
84+
}
85+
} else {
86+
printf ("OK for %s\n", url);
87+
}
88+
}
89+
90+
/* compare two strings
91+
*/
92+
void compareStr (const char *result, const char *expected) {
93+
if (!streq (result, expected)) {
94+
printf ("FAIL for %s, result was %s\n", expected, result);
95+
} else {
96+
printf ("OK for %s\n", expected);
97+
}
98+
}
99+
100+
/* test entry point
101+
*/
102+
int main () {
103+
/* WaitressSplitUrl tests */
104+
compareUrl ("http://www.example.com/", NULL, NULL, "www.example.com", NULL,
105+
"");
106+
compareUrl ("http://www.example.com", NULL, NULL, "www.example.com", NULL,
107+
NULL);
108+
compareUrl ("http://www.example.com:80/", NULL, NULL, "www.example.com",
109+
"80", "");
110+
compareUrl ("http://www.example.com:/", NULL, NULL, "www.example.com", "",
111+
"");
112+
compareUrl ("http://:80/", NULL, NULL, "", "80", "");
113+
compareUrl ("http://www.example.com/foobar/barbaz", NULL, NULL,
114+
"www.example.com", NULL, "foobar/barbaz");
115+
compareUrl ("http://www.example.com:80/foobar/barbaz", NULL, NULL,
116+
"www.example.com", "80", "foobar/barbaz");
117+
compareUrl ("http://foo:[email protected]:80/foobar/barbaz", "foo", "bar",
118+
"www.example.com", "80", "foobar/barbaz");
119+
compareUrl ("http://foo:@www.example.com:80/foobar/barbaz", "foo", "",
120+
"www.example.com", "80", "foobar/barbaz");
121+
compareUrl ("http://[email protected]:80/foobar/barbaz", "foo", NULL,
122+
"www.example.com", "80", "foobar/barbaz");
123+
compareUrl ("http://:[email protected]:80/foobar/barbaz", "", "foo",
124+
"www.example.com", "80", "foobar/barbaz");
125+
compareUrl ("http://:@:80", "", "", "", "80", NULL);
126+
compareUrl ("http://", NULL, NULL, NULL, NULL, NULL);
127+
compareUrl ("http:///", NULL, NULL, "", NULL, "");
128+
compareUrl ("http://foo:bar@", "foo", "bar", "", NULL, NULL);
129+
130+
/* WaitressBase64Encode tests */
131+
compareStr (WaitressBase64Encode ("M"), "TQ==");
132+
compareStr (WaitressBase64Encode ("Ma"), "TWE=");
133+
compareStr (WaitressBase64Encode ("Man"), "TWFu");
134+
compareStr (WaitressBase64Encode ("The quick brown fox jumped over the lazy dog."),
135+
"VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cu");
136+
compareStr (WaitressBase64Encode ("The quick brown fox jumped over the lazy dog"),
137+
"VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2c=");
138+
compareStr (WaitressBase64Encode ("The quick brown fox jumped over the lazy do"),
139+
"VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkbw==");
140+
141+
return EXIT_SUCCESS;
142+
}
143+

src/libwaitress/waitress.c

-120
Original file line numberDiff line numberDiff line change
@@ -1242,123 +1242,3 @@ const char *WaitressErrorToStr (WaitressReturn_t wRet) {
12421242
}
12431243
}
12441244

1245-
#ifdef TEST
1246-
/* test cases for libwaitress */
1247-
1248-
#include <stdio.h>
1249-
#include <stdbool.h>
1250-
#include <string.h>
1251-
#include "waitress.h"
1252-
1253-
#define streq(a,b) (strcmp(a,b) == 0)
1254-
1255-
/* string equality test (memory location or content)
1256-
*/
1257-
static bool streqtest (const char *x, const char *y) {
1258-
return (x == y) || (x != NULL && y != NULL && streq (x, y));
1259-
}
1260-
1261-
/* test WaitressSplitUrl
1262-
* @param tested url
1263-
* @param expected user
1264-
* @param expected password
1265-
* @param expected host
1266-
* @param expected port
1267-
* @param expected path
1268-
*/
1269-
static void compareUrl (const char *url, const char *user,
1270-
const char *password, const char *host, const char *port,
1271-
const char *path) {
1272-
WaitressUrl_t splitUrl;
1273-
1274-
memset (&splitUrl, 0, sizeof (splitUrl));
1275-
1276-
WaitressSplitUrl (url, &splitUrl);
1277-
1278-
bool userTest, passwordTest, hostTest, portTest, pathTest, overallTest;
1279-
1280-
userTest = streqtest (splitUrl.user, user);
1281-
passwordTest = streqtest (splitUrl.password, password);
1282-
hostTest = streqtest (splitUrl.host, host);
1283-
portTest = streqtest (splitUrl.port, port);
1284-
pathTest = streqtest (splitUrl.path, path);
1285-
1286-
overallTest = userTest && passwordTest && hostTest && portTest && pathTest;
1287-
1288-
if (!overallTest) {
1289-
printf ("FAILED test(s) for %s\n", url);
1290-
if (!userTest) {
1291-
printf ("user: %s vs %s\n", splitUrl.user, user);
1292-
}
1293-
if (!passwordTest) {
1294-
printf ("password: %s vs %s\n", splitUrl.password, password);
1295-
}
1296-
if (!hostTest) {
1297-
printf ("host: %s vs %s\n", splitUrl.host, host);
1298-
}
1299-
if (!portTest) {
1300-
printf ("port: %s vs %s\n", splitUrl.port, port);
1301-
}
1302-
if (!pathTest) {
1303-
printf ("path: %s vs %s\n", splitUrl.path, path);
1304-
}
1305-
} else {
1306-
printf ("OK for %s\n", url);
1307-
}
1308-
}
1309-
1310-
/* compare two strings
1311-
*/
1312-
void compareStr (const char *result, const char *expected) {
1313-
if (!streq (result, expected)) {
1314-
printf ("FAIL for %s, result was %s\n", expected, result);
1315-
} else {
1316-
printf ("OK for %s\n", expected);
1317-
}
1318-
}
1319-
1320-
/* test entry point
1321-
*/
1322-
int main () {
1323-
/* WaitressSplitUrl tests */
1324-
compareUrl ("http://www.example.com/", NULL, NULL, "www.example.com", NULL,
1325-
"");
1326-
compareUrl ("http://www.example.com", NULL, NULL, "www.example.com", NULL,
1327-
NULL);
1328-
compareUrl ("http://www.example.com:80/", NULL, NULL, "www.example.com",
1329-
"80", "");
1330-
compareUrl ("http://www.example.com:/", NULL, NULL, "www.example.com", "",
1331-
"");
1332-
compareUrl ("http://:80/", NULL, NULL, "", "80", "");
1333-
compareUrl ("http://www.example.com/foobar/barbaz", NULL, NULL,
1334-
"www.example.com", NULL, "foobar/barbaz");
1335-
compareUrl ("http://www.example.com:80/foobar/barbaz", NULL, NULL,
1336-
"www.example.com", "80", "foobar/barbaz");
1337-
compareUrl ("http://foo:[email protected]:80/foobar/barbaz", "foo", "bar",
1338-
"www.example.com", "80", "foobar/barbaz");
1339-
compareUrl ("http://foo:@www.example.com:80/foobar/barbaz", "foo", "",
1340-
"www.example.com", "80", "foobar/barbaz");
1341-
compareUrl ("http://[email protected]:80/foobar/barbaz", "foo", NULL,
1342-
"www.example.com", "80", "foobar/barbaz");
1343-
compareUrl ("http://:[email protected]:80/foobar/barbaz", "", "foo",
1344-
"www.example.com", "80", "foobar/barbaz");
1345-
compareUrl ("http://:@:80", "", "", "", "80", NULL);
1346-
compareUrl ("http://", NULL, NULL, NULL, NULL, NULL);
1347-
compareUrl ("http:///", NULL, NULL, "", NULL, "");
1348-
compareUrl ("http://foo:bar@", "foo", "bar", "", NULL, NULL);
1349-
1350-
/* WaitressBase64Encode tests */
1351-
compareStr (WaitressBase64Encode ("M"), "TQ==");
1352-
compareStr (WaitressBase64Encode ("Ma"), "TWE=");
1353-
compareStr (WaitressBase64Encode ("Man"), "TWFu");
1354-
compareStr (WaitressBase64Encode ("The quick brown fox jumped over the lazy dog."),
1355-
"VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cu");
1356-
compareStr (WaitressBase64Encode ("The quick brown fox jumped over the lazy dog"),
1357-
"VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2c=");
1358-
compareStr (WaitressBase64Encode ("The quick brown fox jumped over the lazy do"),
1359-
"VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkbw==");
1360-
1361-
return EXIT_SUCCESS;
1362-
}
1363-
#endif /* TEST */
1364-

0 commit comments

Comments
 (0)