tests: introduce preprocessed test cases

The runtests script now always performs variable replacement on the
entire test source file before the test gets executed, and saves the
updated version in a temporary file (log/test[num]) so that all test
case readers/servers can use that version (if present) and thus enjoy
the powers of test case variable substitution.

This is necessary to allow complete port number freedom.

Test 309 is updated to work with a non-fixed port number thanks to this.
This commit is contained in:
Daniel Stenberg 2020-04-17 09:58:42 +02:00
parent 5e2f4a33fe
commit d009bc2e56
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
10 changed files with 171 additions and 177 deletions

View file

@ -6,7 +6,7 @@
* \___|\___/|_| \_\_____|
*
* Copyright (C) 2010, Mandy Wu, <mandy.wu@intel.com>
* Copyright (C) 2011 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 2011 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@ -112,7 +112,6 @@ int main(int argc, char *argv[])
char buf[1024];
char logfilename[256];
FILE *stream;
char *filename;
int error;
char *type1_input = NULL, *type3_input = NULL;
char *type1_output = NULL, *type3_output = NULL;
@ -186,12 +185,10 @@ int main(int argc, char *argv[])
path = env;
}
filename = test2file(testnum);
stream = fopen(filename, "rb");
stream = test2fopen(testnum);
if(!stream) {
error = errno;
logmsg("fopen() failed with error: %d %s", error, strerror(error));
logmsg("Error opening file: %s", filename);
logmsg("Couldn't open test file %ld", testnum);
exit(1);
}
@ -205,13 +202,11 @@ int main(int argc, char *argv[])
}
}
stream = fopen(filename, "rb");
stream = test2fopen(testnum);
if(!stream) {
error = errno;
logmsg("fopen() failed with error: %d %s", error, strerror(error));
logmsg("Error opening file: %s", filename);
logmsg("Couldn't open test file %ld", testnum);
exit(1);
}
else {
size = 0;
@ -225,11 +220,10 @@ int main(int argc, char *argv[])
while(fgets(buf, sizeof(buf), stdin)) {
if(strcmp(buf, type1_input) == 0) {
stream = fopen(filename, "rb");
stream = test2fopen(testnum);
if(!stream) {
error = errno;
logmsg("fopen() failed with error: %d %s", error, strerror(error));
logmsg("Error opening file: %s", filename);
logmsg("Couldn't open test file %ld", testnum);
exit(1);
}
@ -247,11 +241,10 @@ int main(int argc, char *argv[])
fflush(stdout);
}
else if(strncmp(buf, type3_input, strlen(type3_input)) == 0) {
stream = fopen(filename, "rb");
stream = test2fopen(testnum);
if(!stream) {
error = errno;
logmsg("fopen() failed with error: %d %s", error, strerror(error));
logmsg("Error opening file: %s", filename);
logmsg("Couldn't open test file %ld", testnum);
exit(1);
}

View file

@ -446,7 +446,6 @@ static curl_socket_t mqttit(curl_socket_t fd)
size_t remaining_length = 0;
size_t bytes = 0; /* remaining length field size in bytes */
char client_id[MAX_CLIENT_ID_LENGTH];
char *filename;
long testno;
static const char protocol[7] = {
@ -550,8 +549,7 @@ static curl_socket_t mqttit(curl_socket_t fd)
char *data;
size_t datalen;
logmsg("Found test number %ld", testno);
filename = test2file(testno);
stream = fopen(filename, "rb");
stream = test2fopen(testno);
error = getpart(&data, &datalen, "reply", "data", stream);
if(!error)
publish(dump, fd, packet_id, topic, data, datalen);

View file

@ -247,8 +247,6 @@ static int ProcessRequest(struct httprequest *req)
/* get the number after it */
if(ptr) {
FILE *stream;
char *filename;
if((strlen(doc) + strlen(request)) < 200)
msnprintf(logbuf, sizeof(logbuf), "Got request: %s %s %s/%d.%d",
request, doc, prot_str, prot_major, prot_minor);
@ -288,13 +286,11 @@ static int ProcessRequest(struct httprequest *req)
req->testno, req->partno);
logmsg("%s", logbuf);
filename = test2file(req->testno);
stream = test2fopen(req->testno);
stream = fopen(filename, "rb");
if(!stream) {
int error = errno;
logmsg("fopen() failed with error: %d %s", error, strerror(error));
logmsg("Error opening file: %s", filename);
logmsg("Couldn't open test file %ld", req->testno);
req->open = FALSE; /* closes connection */
return 1; /* done */
@ -849,17 +845,13 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
count = strlen(buffer);
}
else {
char *filename = test2file(req->testno);
FILE *stream = test2fopen(req->testno);
char partbuf[80]="data";
FILE *stream;
if(0 != req->partno)
msnprintf(partbuf, sizeof(partbuf), "data%ld", req->partno);
stream = fopen(filename, "rb");
if(!stream) {
error = errno;
logmsg("fopen() failed with error: %d %s", error, strerror(error));
logmsg("Error opening file: %s", filename);
logmsg("Couldn't open test file");
return 0;
}
@ -879,11 +871,10 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
}
/* re-open the same file again */
stream = fopen(filename, "rb");
stream = test2fopen(req->testno);
if(!stream) {
error = errno;
logmsg("fopen() failed with error: %d %s", error, strerror(error));
logmsg("Error opening file: %s", filename);
logmsg("Couldn't open test file");
free(ptr);
return 0;

View file

@ -235,18 +235,15 @@ static bool socket_domain_is_ip(void)
static int parse_servercmd(struct httprequest *req)
{
FILE *stream;
char *filename;
int error;
filename = test2file(req->testno);
stream = test2fopen(req->testno);
req->close = FALSE;
req->connmon = FALSE;
stream = fopen(filename, "rb");
if(!stream) {
error = errno;
logmsg("fopen() failed with error: %d %s", error, strerror(error));
logmsg(" [1] Error opening file: %s", filename);
logmsg(" Couldn't open test file %ld", req->testno);
req->open = FALSE; /* closes connection */
return 1; /* done */
@ -991,7 +988,6 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
}
else {
char partbuf[80];
char *filename = test2file(req->testno);
/* select the <data> tag for "normal" requests and the <connect> one
for CONNECT requests (within the <reply> section) */
@ -1004,11 +1000,10 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
logmsg("Send response test%ld section <%s>", req->testno, partbuf);
stream = fopen(filename, "rb");
stream = test2fopen(req->testno);
if(!stream) {
error = errno;
logmsg("fopen() failed with error: %d %s", error, strerror(error));
logmsg(" [3] Error opening file: %s", filename);
return 0;
}
else {
@ -1027,11 +1022,10 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
}
/* re-open the same file again */
stream = fopen(filename, "rb");
stream = test2fopen(req->testno);
if(!stream) {
error = errno;
logmsg("fopen() failed with error: %d %s", error, strerror(error));
logmsg(" [4] Error opening file: %s", filename);
free(ptr);
return 0;
}

View file

@ -944,16 +944,12 @@ static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size)
static int parse_servercmd(struct testcase *req)
{
FILE *stream;
char *filename;
int error;
filename = test2file(req->testno);
stream = fopen(filename, "rb");
stream = test2fopen(req->testno);
if(!stream) {
error = errno;
logmsg("fopen() failed with error: %d %s", error, strerror(error));
logmsg(" [1] Error opening file: %s", filename);
logmsg(" Couldn't open test file %ld", req->testno);
return 1; /* done */
}
@ -1036,7 +1032,7 @@ static int validate_access(struct testcase *test,
char partbuf[80]="data";
long partno;
long testno;
char *file;
FILE *stream;
ptr++; /* skip the slash */
@ -1061,40 +1057,33 @@ static int validate_access(struct testcase *test,
(void)parse_servercmd(test);
file = test2file(testno);
stream = test2fopen(testno);
if(0 != partno)
msnprintf(partbuf, sizeof(partbuf), "data%ld", partno);
if(file) {
FILE *stream = fopen(file, "rb");
if(!stream) {
int error = errno;
logmsg("fopen() failed with error: %d %s", error, strerror(error));
logmsg("Error opening file: %s", file);
logmsg("Couldn't open test file: %s", file);
if(!stream) {
int error = errno;
logmsg("fopen() failed with error: %d %s", error, strerror(error));
logmsg("Couldn't open test file for test : %d", testno);
return EACCESS;
}
else {
size_t count;
int error = getpart(&test->buffer, &count, "reply", partbuf, stream);
fclose(stream);
if(error) {
logmsg("getpart() failed with error: %d", error);
return EACCESS;
}
else {
size_t count;
int error = getpart(&test->buffer, &count, "reply", partbuf, stream);
fclose(stream);
if(error) {
logmsg("getpart() failed with error: %d", error);
return EACCESS;
}
if(test->buffer) {
test->rptr = test->buffer; /* set read pointer */
test->bufsize = count; /* set total count */
test->rcount = count; /* set data left to read */
}
else
return EACCESS;
if(test->buffer) {
test->rptr = test->buffer; /* set read pointer */
test->bufsize = count; /* set total count */
test->rcount = count; /* set data left to read */
}
else
return EACCESS;
}
else
return EACCESS;
}
else {
logmsg("no slash found in path");

View file

@ -194,11 +194,21 @@ void win32_cleanup(void)
/* set by the main code to point to where the test dir is */
const char *path = ".";
char *test2file(long testno)
FILE *test2fopen(long testno)
{
static char filename[256];
FILE *stream;
char filename[256];
/* first try the alternative, preprocessed, file */
msnprintf(filename, sizeof(filename), ALTTEST_DATA_PATH, path, testno);
stream = fopen(filename, "rb");
if(stream)
return stream;
/* then try the source version */
msnprintf(filename, sizeof(filename), TEST_DATA_PATH, path, testno);
return filename;
stream = fopen(filename, "rb");
return stream;
}
/*

View file

@ -28,6 +28,7 @@ void logmsg(const char *msg, ...);
long timediff(struct timeval newer, struct timeval older);
#define TEST_DATA_PATH "%s/data/test%ld"
#define ALTTEST_DATA_PATH "%s/log/test%ld"
#define SERVERLOGS_LOCK "log/serverlogs.lock"
@ -53,8 +54,9 @@ void win32_init(void);
void win32_cleanup(void);
#endif /* USE_WINSOCK */
/* returns the path name to the test case file */
char *test2file(long testno);
/* fopens the test case file */
FILE *test2fopen(long testno);
int wait_ms(int timeout_ms);
int write_pidfile(const char *filename);
int write_portfile(const char *filename, int port);