unit tests: use the unit test infrastructure better

Allow UNITTEST_STOP to return the error code, use the fail & abort
macros to indicate test failure and return success instead of fail if
the unit test can't test anything because of missing features at
compile-time.  A couple of tests could never fail because they were
overriding the failure return code.
This commit is contained in:
Dan Fandrich 2023-04-01 08:59:38 -07:00
parent 419a745da6
commit a13ef31d0f
7 changed files with 60 additions and 79 deletions

View File

@ -47,7 +47,7 @@ static void unit_stop(void)
} }
UNITTEST_START UNITTEST_START
{
int i; int i;
CURLcode code; CURLcode code;
struct Curl_addrinfo *addrhead = addrs; struct Curl_addrinfo *addrhead = addrs;
@ -71,6 +71,4 @@ UNITTEST_START
abort_unless(addrhead != addrs, "addresses are not being reordered"); abort_unless(addrhead != addrs, "addresses are not being reordered");
return 0;
}
UNITTEST_STOP UNITTEST_STOP

View File

@ -41,9 +41,6 @@ static void unit_stop(void)
(!defined(HAVE_FSETXATTR) && \ (!defined(HAVE_FSETXATTR) && \
(!defined(__FreeBSD_version) || (__FreeBSD_version < 500000))) (!defined(__FreeBSD_version) || (__FreeBSD_version < 500000)))
UNITTEST_START UNITTEST_START
{
return 0;
}
UNITTEST_STOP UNITTEST_STOP
#else #else
@ -68,7 +65,6 @@ static const struct checkthis tests[] = {
UNITTEST_START UNITTEST_START
{ {
int i; int i;
int rc = 0;
for(i = 0; tests[i].input; i++) { for(i = 0; tests[i].input; i++) {
const char *url = tests[i].input; const char *url = tests[i].input;
@ -76,15 +72,10 @@ UNITTEST_START
printf("Test %u got input \"%s\", output: \"%s\"\n", printf("Test %u got input \"%s\", output: \"%s\"\n",
i, tests[i].input, stripped); i, tests[i].input, stripped);
if(stripped && strcmp(tests[i].output, stripped)) { fail_if(stripped && strcmp(tests[i].output, stripped),
fprintf(stderr, "Test %u got input \"%s\", expected output \"%s\"\n" tests[i].output);
" Actual output: \"%s\"\n", i, tests[i].input, tests[i].output,
stripped);
rc++;
}
curl_free(stripped); curl_free(stripped);
} }
return rc;
} }
UNITTEST_STOP UNITTEST_STOP
#endif #endif

View File

@ -159,25 +159,27 @@ UNITTEST_START
unsigned char buffer[256]; unsigned char buffer[256];
size_t i; size_t i;
unsigned char *p; unsigned char *p;
for(i = 0; i < sizeof(req) / sizeof(req[0]); i++) { for(i = 0; i < sizeof(req) / sizeof(req[0]); i++) {
int rc = doh_encode(req[i].name, req[i].type, int rc = doh_encode(req[i].name, req[i].type,
buffer, sizeof(buffer), &size); buffer, sizeof(buffer), &size);
if(rc != req[i].rc) { if(rc != req[i].rc) {
fprintf(stderr, "req %zu: Expected return code %d got %d\n", i, fprintf(stderr, "req %zu: Expected return code %d got %d\n", i,
req[i].rc, rc); req[i].rc, rc);
return 1; abort_if(rc != req[i].rc, "return code");
} }
else if(size != req[i].size) { if(size != req[i].size) {
fprintf(stderr, "req %zu: Expected size %zu got %zu\n", i, fprintf(stderr, "req %zu: Expected size %zu got %zu\n", i,
req[i].size, size); req[i].size, size);
fprintf(stderr, "DNS encode made: %s\n", hexdump(buffer, size)); fprintf(stderr, "DNS encode made: %s\n", hexdump(buffer, size));
return 2; abort_if(size != req[i].size, "size");
} }
else if(req[i].packet && memcmp(req[i].packet, buffer, size)) { if(req[i].packet && memcmp(req[i].packet, buffer, size)) {
fprintf(stderr, "DNS encode made: %s\n", hexdump(buffer, size)); fprintf(stderr, "DNS encode made: %s\n", hexdump(buffer, size));
fprintf(stderr, "... instead of: %s\n", fprintf(stderr, "... instead of: %s\n",
hexdump((unsigned char *)req[i].packet, size)); hexdump((unsigned char *)req[i].packet, size));
return 3; abort_if(req[i].packet && memcmp(req[i].packet, buffer, size),
"contents");
} }
} }
@ -193,7 +195,7 @@ UNITTEST_START
if(rc != resp[i].rc) { if(rc != resp[i].rc) {
fprintf(stderr, "resp %zu: Expected return code %d got %d\n", i, fprintf(stderr, "resp %zu: Expected return code %d got %d\n", i,
resp[i].rc, rc); resp[i].rc, rc);
return 4; abort_if(rc != resp[i].rc, "return code");
} }
len = sizeof(buffer); len = sizeof(buffer);
ptr = (char *)buffer; ptr = (char *)buffer;
@ -234,11 +236,10 @@ UNITTEST_START
if(resp[i].out && strcmp((char *)buffer, resp[i].out)) { if(resp[i].out && strcmp((char *)buffer, resp[i].out)) {
fprintf(stderr, "resp %zu: Expected %s got %s\n", i, fprintf(stderr, "resp %zu: Expected %s got %s\n", i,
resp[i].out, buffer); resp[i].out, buffer);
return 1; abort_if(resp[i].out && strcmp((char *)buffer, resp[i].out), "content");
} }
} }
{
/* pass all sizes into the decoder until full */ /* pass all sizes into the decoder until full */
for(i = 0; i < sizeof(full49)-1; i++) { for(i = 0; i < sizeof(full49)-1; i++) {
struct dohentry d; struct dohentry d;
@ -248,9 +249,10 @@ UNITTEST_START
if(!rc) { if(!rc) {
/* none of them should work */ /* none of them should work */
fprintf(stderr, "%zu: %d\n", i, rc); fprintf(stderr, "%zu: %d\n", i, rc);
return 5; abort_if(!rc, "error rc");
} }
} }
/* and try all pieces from the other end of the packet */ /* and try all pieces from the other end of the packet */
for(i = 1; i < sizeof(full49); i++) { for(i = 1; i < sizeof(full49); i++) {
struct dohentry d; struct dohentry d;
@ -261,9 +263,10 @@ UNITTEST_START
if(!rc) { if(!rc) {
/* none of them should work */ /* none of them should work */
fprintf(stderr, "2 %zu: %d\n", i, rc); fprintf(stderr, "2 %zu: %d\n", i, rc);
return 7; abort_if(!rc, "error rc");
} }
} }
{ {
int rc; int rc;
struct dohentry d; struct dohentry d;
@ -278,19 +281,16 @@ UNITTEST_START
"%u.%u.%u.%u", p[0], p[1], p[2], p[3]); "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
if(rc || strcmp((char *)buffer, "127.0.0.1")) { if(rc || strcmp((char *)buffer, "127.0.0.1")) {
fprintf(stderr, "bad address decoded: %s, rc == %d\n", buffer, rc); fprintf(stderr, "bad address decoded: %s, rc == %d\n", buffer, rc);
return 7; abort_if(rc || strcmp((char *)buffer, "127.0.0.1"), "bad address");
} }
fail_if(d.numcname, "bad cname counter"); fail_if(d.numcname, "bad cname counter");
} }
} }
}
UNITTEST_STOP UNITTEST_STOP
#else /* CURL_DISABLE_DOH */ #else /* CURL_DISABLE_DOH */
UNITTEST_START UNITTEST_START
{ /* nothing to do, just succeed */
return 1; /* nothing to do, just fail */
}
UNITTEST_STOP UNITTEST_STOP

View File

@ -184,9 +184,7 @@ UNITTEST_STOP
#else /* CURL_DISABLE_DOH */ #else /* CURL_DISABLE_DOH */
UNITTEST_START UNITTEST_START
{ /* nothing to do, just succeed */
return 1; /* nothing to do, just fail */
}
UNITTEST_STOP UNITTEST_STOP
#endif #endif

View File

@ -118,7 +118,6 @@ static void showsts(struct stsentry *e, const char *chost)
} }
UNITTEST_START UNITTEST_START
{
CURLcode result; CURLcode result;
struct stsentry *e; struct stsentry *e;
struct hsts *h = Curl_hsts_init(); struct hsts *h = Curl_hsts_init();
@ -126,15 +125,15 @@ UNITTEST_START
const char *chost; const char *chost;
CURL *easy; CURL *easy;
char savename[256]; char savename[256];
if(!h)
return 1; abort_unless(h, "Curl_hsts_init()");
curl_global_init(CURL_GLOBAL_ALL); curl_global_init(CURL_GLOBAL_ALL);
easy = curl_easy_init(); easy = curl_easy_init();
if(!easy) { if(!easy) {
Curl_hsts_cleanup(&h); Curl_hsts_cleanup(&h);
curl_global_cleanup(); curl_global_cleanup();
return 1; abort_unless(easy, "curl_easy_init()");
} }
Curl_hsts_loadfile(easy, h, arg); Curl_hsts_loadfile(easy, h, arg);
@ -175,7 +174,6 @@ UNITTEST_START
Curl_hsts_cleanup(&h); Curl_hsts_cleanup(&h);
curl_easy_cleanup(easy); curl_easy_cleanup(easy);
curl_global_cleanup(); curl_global_cleanup();
return unitfail;
}
UNITTEST_STOP UNITTEST_STOP
#endif #endif

View File

@ -243,5 +243,4 @@ UNITTEST_START
check_bufq(8, 8000, 10, 1234, 1234, BUFQ_OPT_NONE); check_bufq(8, 8000, 10, 1234, 1234, BUFQ_OPT_NONE);
check_bufq(8, 1024, 4, 129, 127, BUFQ_OPT_NO_SPARES); check_bufq(8, 1024, 4, 129, 127, BUFQ_OPT_NO_SPARES);
return 0;
UNITTEST_STOP UNITTEST_STOP

View File

@ -79,7 +79,6 @@ static const char *filecontents[] = {
UNITTEST_START UNITTEST_START
{
size_t i; size_t i;
for(i = 0; i < NUMTESTS; i++) { for(i = 0; i < NUMTESTS; i++) {
FILE *fp; FILE *fp;
@ -157,6 +156,4 @@ UNITTEST_START
fclose(fp); fclose(fp);
fprintf(stderr, "OK\n"); fprintf(stderr, "OK\n");
} }
return 0;
}
UNITTEST_STOP UNITTEST_STOP