From: Dan Fandrich Date: Sat, 1 Apr 2023 15:59:38 +0000 (-0700) Subject: unit tests: use the unit test infrastructure better X-Git-Tag: curl-8_1_0~224 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a13ef31d0fbbf98120b711746bd8802acaba6b0a;p=thirdparty%2Fcurl.git 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. --- diff --git a/tests/unit/unit1608.c b/tests/unit/unit1608.c index 7a5ea5f4b3..31c6912695 100644 --- a/tests/unit/unit1608.c +++ b/tests/unit/unit1608.c @@ -47,7 +47,7 @@ static void unit_stop(void) } UNITTEST_START -{ + int i; CURLcode code; struct Curl_addrinfo *addrhead = addrs; @@ -71,6 +71,4 @@ UNITTEST_START abort_unless(addrhead != addrs, "addresses are not being reordered"); - return 0; -} UNITTEST_STOP diff --git a/tests/unit/unit1621.c b/tests/unit/unit1621.c index 7e8bac1374..c384e044d7 100644 --- a/tests/unit/unit1621.c +++ b/tests/unit/unit1621.c @@ -41,9 +41,6 @@ static void unit_stop(void) (!defined(HAVE_FSETXATTR) && \ (!defined(__FreeBSD_version) || (__FreeBSD_version < 500000))) UNITTEST_START -{ - return 0; -} UNITTEST_STOP #else @@ -68,7 +65,6 @@ static const struct checkthis tests[] = { UNITTEST_START { int i; - int rc = 0; for(i = 0; tests[i].input; i++) { const char *url = tests[i].input; @@ -76,15 +72,10 @@ UNITTEST_START printf("Test %u got input \"%s\", output: \"%s\"\n", i, tests[i].input, stripped); - if(stripped && strcmp(tests[i].output, stripped)) { - fprintf(stderr, "Test %u got input \"%s\", expected output \"%s\"\n" - " Actual output: \"%s\"\n", i, tests[i].input, tests[i].output, - stripped); - rc++; - } + fail_if(stripped && strcmp(tests[i].output, stripped), + tests[i].output); curl_free(stripped); } - return rc; } UNITTEST_STOP #endif diff --git a/tests/unit/unit1650.c b/tests/unit/unit1650.c index 8c60013d5a..1993616bf1 100644 --- a/tests/unit/unit1650.c +++ b/tests/unit/unit1650.c @@ -159,25 +159,27 @@ UNITTEST_START unsigned char buffer[256]; size_t i; unsigned char *p; + for(i = 0; i < sizeof(req) / sizeof(req[0]); i++) { int rc = doh_encode(req[i].name, req[i].type, buffer, sizeof(buffer), &size); if(rc != req[i].rc) { fprintf(stderr, "req %zu: Expected return code %d got %d\n", i, 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, req[i].size, 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, "... instead of: %s\n", 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) { fprintf(stderr, "resp %zu: Expected return code %d got %d\n", i, resp[i].rc, rc); - return 4; + abort_if(rc != resp[i].rc, "return code"); } len = sizeof(buffer); ptr = (char *)buffer; @@ -234,63 +236,61 @@ UNITTEST_START if(resp[i].out && strcmp((char *)buffer, resp[i].out)) { fprintf(stderr, "resp %zu: Expected %s got %s\n", i, 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 */ - for(i = 0; i < sizeof(full49)-1; i++) { - struct dohentry d; - int rc; - memset(&d, 0, sizeof(d)); - rc = doh_decode((const unsigned char *)full49, i, DNS_TYPE_A, &d); - if(!rc) { - /* none of them should work */ - fprintf(stderr, "%zu: %d\n", i, rc); - return 5; - } + /* pass all sizes into the decoder until full */ + for(i = 0; i < sizeof(full49)-1; i++) { + struct dohentry d; + int rc; + memset(&d, 0, sizeof(d)); + rc = doh_decode((const unsigned char *)full49, i, DNS_TYPE_A, &d); + if(!rc) { + /* none of them should work */ + fprintf(stderr, "%zu: %d\n", i, rc); + abort_if(!rc, "error rc"); } - /* and try all pieces from the other end of the packet */ - for(i = 1; i < sizeof(full49); i++) { - struct dohentry d; - int rc; - memset(&d, 0, sizeof(d)); - rc = doh_decode((const unsigned char *)&full49[i], sizeof(full49)-i-1, - DNS_TYPE_A, &d); - if(!rc) { - /* none of them should work */ - fprintf(stderr, "2 %zu: %d\n", i, rc); - return 7; - } + } + + /* and try all pieces from the other end of the packet */ + for(i = 1; i < sizeof(full49); i++) { + struct dohentry d; + int rc; + memset(&d, 0, sizeof(d)); + rc = doh_decode((const unsigned char *)&full49[i], sizeof(full49)-i-1, + DNS_TYPE_A, &d); + if(!rc) { + /* none of them should work */ + fprintf(stderr, "2 %zu: %d\n", i, rc); + abort_if(!rc, "error rc"); } - { - int rc; - struct dohentry d; - struct dohaddr *a; - memset(&d, 0, sizeof(d)); - rc = doh_decode((const unsigned char *)full49, sizeof(full49)-1, - DNS_TYPE_A, &d); - fail_if(d.numaddr != 1, "missing address"); - a = &d.addr[0]; - p = &a->ip.v4[0]; - msnprintf((char *)buffer, sizeof(buffer), - "%u.%u.%u.%u", p[0], p[1], p[2], p[3]); - if(rc || strcmp((char *)buffer, "127.0.0.1")) { - fprintf(stderr, "bad address decoded: %s, rc == %d\n", buffer, rc); - return 7; - } - fail_if(d.numcname, "bad cname counter"); + } + + { + int rc; + struct dohentry d; + struct dohaddr *a; + memset(&d, 0, sizeof(d)); + rc = doh_decode((const unsigned char *)full49, sizeof(full49)-1, + DNS_TYPE_A, &d); + fail_if(d.numaddr != 1, "missing address"); + a = &d.addr[0]; + p = &a->ip.v4[0]; + msnprintf((char *)buffer, sizeof(buffer), + "%u.%u.%u.%u", p[0], p[1], p[2], p[3]); + if(rc || strcmp((char *)buffer, "127.0.0.1")) { + fprintf(stderr, "bad address decoded: %s, rc == %d\n", buffer, rc); + abort_if(rc || strcmp((char *)buffer, "127.0.0.1"), "bad address"); } + fail_if(d.numcname, "bad cname counter"); } } UNITTEST_STOP #else /* CURL_DISABLE_DOH */ UNITTEST_START -{ - return 1; /* nothing to do, just fail */ -} +/* nothing to do, just succeed */ UNITTEST_STOP diff --git a/tests/unit/unit1655.c b/tests/unit/unit1655.c index 2457b74a15..ca88f71503 100644 --- a/tests/unit/unit1655.c +++ b/tests/unit/unit1655.c @@ -184,9 +184,7 @@ UNITTEST_STOP #else /* CURL_DISABLE_DOH */ UNITTEST_START -{ - return 1; /* nothing to do, just fail */ -} +/* nothing to do, just succeed */ UNITTEST_STOP #endif diff --git a/tests/unit/unit1660.c b/tests/unit/unit1660.c index 26c2bfa2df..938d1a185f 100644 --- a/tests/unit/unit1660.c +++ b/tests/unit/unit1660.c @@ -118,7 +118,6 @@ static void showsts(struct stsentry *e, const char *chost) } UNITTEST_START -{ CURLcode result; struct stsentry *e; struct hsts *h = Curl_hsts_init(); @@ -126,15 +125,15 @@ UNITTEST_START const char *chost; CURL *easy; char savename[256]; - if(!h) - return 1; + + abort_unless(h, "Curl_hsts_init()"); curl_global_init(CURL_GLOBAL_ALL); easy = curl_easy_init(); if(!easy) { Curl_hsts_cleanup(&h); curl_global_cleanup(); - return 1; + abort_unless(easy, "curl_easy_init()"); } Curl_hsts_loadfile(easy, h, arg); @@ -175,7 +174,6 @@ UNITTEST_START Curl_hsts_cleanup(&h); curl_easy_cleanup(easy); curl_global_cleanup(); - return unitfail; -} + UNITTEST_STOP #endif diff --git a/tests/unit/unit2601.c b/tests/unit/unit2601.c index b010274002..de2a9f9d66 100644 --- a/tests/unit/unit2601.c +++ b/tests/unit/unit2601.c @@ -243,5 +243,4 @@ UNITTEST_START check_bufq(8, 8000, 10, 1234, 1234, BUFQ_OPT_NONE); check_bufq(8, 1024, 4, 129, 127, BUFQ_OPT_NO_SPARES); - return 0; UNITTEST_STOP diff --git a/tests/unit/unit3200.c b/tests/unit/unit3200.c index 19e1005a03..58eaff90dd 100644 --- a/tests/unit/unit3200.c +++ b/tests/unit/unit3200.c @@ -79,7 +79,6 @@ static const char *filecontents[] = { UNITTEST_START -{ size_t i; for(i = 0; i < NUMTESTS; i++) { FILE *fp; @@ -157,6 +156,4 @@ UNITTEST_START fclose(fp); fprintf(stderr, "OK\n"); } - return 0; -} UNITTEST_STOP