]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
unit tests: use the unit test infrastructure better
authorDan Fandrich <dan@coneharvesters.com>
Sat, 1 Apr 2023 15:59:38 +0000 (08:59 -0700)
committerDan Fandrich <dan@coneharvesters.com>
Sat, 1 Apr 2023 15:59:38 +0000 (08:59 -0700)
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.

tests/unit/unit1608.c
tests/unit/unit1621.c
tests/unit/unit1650.c
tests/unit/unit1655.c
tests/unit/unit1660.c
tests/unit/unit2601.c
tests/unit/unit3200.c

index 7a5ea5f4b3a973c2b70301ae52c801f7f4dbc938..31c6912695aabe68c0f7fa5bd2d133f73a6e1d68 100644 (file)
@@ -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
index 7e8bac1374735db62e6c8ce1c2cad2767ca39d91..c384e044d7456e0f1a8188869467806816e9689a 100644 (file)
@@ -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
index 8c60013d5ade8d13c8a10333f9b4a1ed279e3ada..1993616bf112dd3af2543257ba81941be7ef3d77 100644 (file)
@@ -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
 
 
index 2457b74a151338403b7895a223ba8b627ac2a325..ca88f71503837fac69ae4d015e393fef66e199ce 100644 (file)
@@ -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
index 26c2bfa2df64f9bb8458c1f3530b7f7fd64773b1..938d1a185f7a6fbcb3d931e0d5c81b06e6850da2 100644 (file)
@@ -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
index b010274002fa9c3693c63532a5b9947f64d7510e..de2a9f9d66316803e0d1b5ae84d08731ee6b40c1 100644 (file)
@@ -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
index 19e1005a033f66c15a276af5aec5cae86ec45ecb..58eaff90ddd74c3d759f741f981cb29a81567d7a 100644 (file)
@@ -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