Browse Source

Unit test: rework to be able to run API tests individually

Change API test cases to return a result.
Test success is now TEST_SUCCESS (1).
Test result can be returned with use of macro TEST_RES_CHECK().
Always print the id, name of the test and the result (skipped or
otherwise) before and after running the test case.

Changed test case output to go to stderr.
Fixed some formatting.

Add option to take index and/or name of test case to run.
Added option to list all API tests.
Added option to only run API tests.
Added options to show usage.
Sean Parkinson 1 year ago
parent
commit
e4e53ab7ca
3 changed files with 259 additions and 142 deletions
  1. 190 130
      tests/api.c
  2. 65 12
      tests/unit.c
  3. 4 0
      tests/unit.h

File diff suppressed because it is too large
+ 190 - 130
tests/api.c


+ 65 - 12
tests/unit.c

@@ -33,6 +33,7 @@
 #include <wolfssl/wolfcrypt/fips_test.h>
 
 
+int allTesting = 1;
 int myoptind = 0;
 char* myoptarg = NULL;
 int unit_test(int argc, char** argv);
@@ -44,6 +45,21 @@ int main(int argc, char** argv)
 }
 #endif
 
+/* Print usage options for unit test.
+ */
+static void UnitTest_Usage(void)
+{
+    printf("Usage: ./tests/unit.test <options>\n");
+    printf(" -?, --help     Display this usage information.\n");
+    printf(" --list         List the API tests.\n");
+    printf(" --api          Only perform API tests.\n");
+    printf(" -<number>      Run the API test identified by number.\n");
+    printf("                Can be specified multiple times.\n");
+    printf(" -<string>      Run the API test identified by name.\n");
+    printf("                Can be specified multiple times.\n");
+    printf(" <filename>     Name of cipher suite testing file.\n");
+}
+
 int unit_test(int argc, char** argv)
 {
     int ret = 0;
@@ -154,39 +170,76 @@ int unit_test(int argc, char** argv)
     }
 #endif
 #endif /* HAVE_FIPS && HAVE_FIPS_VERSION == 5 */
+
+    while (argc > 1) {
+        if (argv[1][0] != '-') {
+            break;
+        }
+
+        if (XSTRCMP(argv[1], "-?") == 0 || XSTRCMP(argv[1], "--help") == 0) {
+            UnitTest_Usage();
+            goto exit;
+        }
+        else if (XSTRCMP(argv[1], "--list") == 0) {
+            ApiTest_PrintTestCases();
+            goto exit;
+        }
+        else if (XSTRCMP(argv[1], "--api") == 0) {
+        }
+        else if (argv[1][1] >= '0' && argv[1][1] <= '9') {
+            ret = ApiTest_RunIdx(atoi(argv[1] + 1));
+            if (ret != 0) {
+                goto exit;
+            }
+        }
+        else {
+            ret = ApiTest_RunName(argv[1] + 1);
+            if (ret != 0) {
+                goto exit;
+            }
+        }
+
+        allTesting = 0;
+        argc--;
+        argv++;
+    }
+
 #ifdef WOLFSSL_ALLOW_SKIP_UNIT_TESTS
     if (argc == 1)
 #endif
     {
         ApiTest();
 
-        if ( (ret = HashTest()) != 0){
+        if (!allTesting) {
+            goto exit;
+        }
+
+        if ((ret = HashTest()) != 0) {
             fprintf(stderr, "hash test failed with %d\n", ret);
             goto exit;
         }
 
-#ifdef WOLFSSL_W64_WRAPPER
+    #ifdef WOLFSSL_W64_WRAPPER
         if ((ret = w64wrapper_test()) != 0) {
             fprintf(stderr, "w64wrapper test failed with %d\n", ret);
             goto exit;
         }
-#endif /* WOLFSSL_W64_WRAPPER */
+    #endif /* WOLFSSL_W64_WRAPPER */
 
-    }
+    #ifdef WOLFSSL_QUIC
+        if ((ret = QuicTest()) != 0) {
+            printf("quic test failed with %d\n", ret);
+            goto exit;
+        }
+    #endif
 
-#ifdef WOLFSSL_QUIC
-    if ( (ret = QuicTest()) != 0){
-        printf("quic test failed with %d\n", ret);
-        goto exit;
+        SrpTest();
     }
-#endif
-
-    SrpTest();
 
 #ifndef NO_WOLFSSL_CIPHER_SUITE_TEST
 #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
 #ifndef SINGLE_THREADED
-    if ( (ret = SuiteTest(argc, argv)) != 0){
+    if ((ret = SuiteTest(argc, argv)) != 0) {
         fprintf(stderr, "suite test failed with %d\n", ret);
         goto exit;
     }

+ 4 - 0
tests/unit.h

@@ -110,7 +110,11 @@
 #define AssertPtrLE(x, y) AssertPtr(x, y, <=,  >)
 
 
+void ApiTest_PrintTestCases(void);
+int ApiTest_RunIdx(int idx);
+int ApiTest_RunName(char* name);
 void ApiTest(void);
+
 int  SuiteTest(int argc, char** argv);
 int  HashTest(void);
 void SrpTest(void);

Some files were not shown because too many files changed in this diff