Browse Source

add file-iterator to `test_process_dir()`

Signed-off-by: Steffen Jaeckel <[email protected]>
Steffen Jaeckel 3 years ago
parent
commit
a76447f7aa
5 changed files with 52 additions and 31 deletions
  1. 27 12
      tests/common.c
  2. 3 2
      tests/common.h
  3. 1 1
      tests/der_test.c
  4. 19 14
      tests/pem_test.c
  5. 2 2
      tests/rsa_test.c

+ 27 - 12
tests/common.c

@@ -95,16 +95,30 @@ static DIR *s_opendir(const char *path, char *mypath, unsigned long l)
    return d;
    return d;
 }
 }
 
 
-int test_process_dir(const char *path, void *ctx, dir_iter_cb process, dir_cleanup_cb cleanup, const char *test)
+static int s_read_and_process(FILE *f, unsigned long sz, void *ctx, dir_iter_cb process)
+{
+   int err = CRYPT_OK;
+   void* buf = XMALLOC(sz);
+   if (buf == NULL)
+      return CRYPT_MEM;
+   if (fread(buf, 1, sz, f) != sz) {
+      err = CRYPT_ERROR;
+      goto out;
+   }
+   err = process(buf, sz, ctx);
+out:
+   XFREE(buf);
+   return err;
+}
+
+int test_process_dir(const char *path, void *ctx, dir_iter_cb iter, dir_fiter_cb fiter, dir_cleanup_cb cleanup, const char *test)
 {
 {
    char mypath[PATH_MAX];
    char mypath[PATH_MAX];
    DIR *d = s_opendir(path, mypath, sizeof(mypath));
    DIR *d = s_opendir(path, mypath, sizeof(mypath));
    struct dirent *de;
    struct dirent *de;
    char fname[PATH_MAX];
    char fname[PATH_MAX];
-   void* buf = NULL;
    FILE *f = NULL;
    FILE *f = NULL;
    off_t fsz;
    off_t fsz;
-   unsigned long sz;
    int err = CRYPT_FILE_NOTFOUND;
    int err = CRYPT_FILE_NOTFOUND;
    if (d == NULL)
    if (d == NULL)
       return CRYPT_FILE_NOTFOUND;
       return CRYPT_FILE_NOTFOUND;
@@ -124,14 +138,18 @@ int test_process_dir(const char *path, void *ctx, dir_iter_cb process, dir_clean
       fprintf(stderr, "%s: Try to process %s\n", test, fname);
       fprintf(stderr, "%s: Try to process %s\n", test, fname);
 #endif
 #endif
       f = fopen(fname, "rb");
       f = fopen(fname, "rb");
-      sz = fsz;
-      buf = XMALLOC(fsz);
-      if (fread(buf, 1, sz, f) != sz) {
-         err = CRYPT_ERROR;
-         break;
+
+      if (iter) {
+         err = s_read_and_process(f, fsz, ctx, iter);
+      } else if (fiter) {
+         err = fiter(f, ctx);
+      } else {
+         err = CRYPT_NOP;
+#if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
+         fprintf(stderr, "%s: No call-back set for %s\n", test, fname);
+#endif
       }
       }
 
 
-      err = process(buf, sz, ctx);
       if (err == CRYPT_NOP) {
       if (err == CRYPT_NOP) {
 #if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
 #if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
          fprintf(stderr, "%s: Skip: %s\n", test, fname);
          fprintf(stderr, "%s: Skip: %s\n", test, fname);
@@ -150,12 +168,9 @@ int test_process_dir(const char *path, void *ctx, dir_iter_cb process, dir_clean
       }
       }
 
 
 continue_loop:
 continue_loop:
-      XFREE(buf);
-      buf = NULL;
       fclose(f);
       fclose(f);
       f = NULL;
       f = NULL;
    }
    }
-   if (buf != NULL) XFREE(buf);
    if (f != NULL) fclose(f);
    if (f != NULL) fclose(f);
    closedir(d);
    closedir(d);
    return err;
    return err;

+ 3 - 2
tests/common.h

@@ -40,13 +40,14 @@ int ecc_key_cmp(const int should_type, const ecc_key *should, const ecc_key *is)
 
 
 #define COMPARE_TESTVECTOR(i, il, s, sl, wa, wi) do { DO(do_compare_testvector((i), (il), (s), (sl), (wa), (wi))); } while(0)
 #define COMPARE_TESTVECTOR(i, il, s, sl, wa, wi) do { DO(do_compare_testvector((i), (il), (s), (sl), (wa), (wi))); } while(0)
 
 
-#if !((defined(_WIN32) || defined(_WIN32_WCE)) && !defined(__GNUC__))
+#if !((defined(_WIN32) || defined(_WIN32_WCE)) && !defined(__GNUC__)) && !defined(LTC_NO_FILE)
 #define LTC_TEST_READDIR
 #define LTC_TEST_READDIR
 
 
 typedef int (*dir_iter_cb)(const void *d, unsigned long l, void* ctx);
 typedef int (*dir_iter_cb)(const void *d, unsigned long l, void* ctx);
+typedef int (*dir_fiter_cb)(FILE *f, void* ctx);
 typedef void (*dir_cleanup_cb)(void* ctx);
 typedef void (*dir_cleanup_cb)(void* ctx);
 
 
-int test_process_dir(const char *path, void *ctx, dir_iter_cb iter, dir_cleanup_cb cleanup, const char *test);
+int test_process_dir(const char *path, void *ctx, dir_iter_cb iter, dir_fiter_cb fiter, dir_cleanup_cb cleanup, const char *test);
 #endif
 #endif
 
 
 void run_cmd(int res, int line, const char *file, const char *cmd, const char *algorithm);
 void run_cmd(int res, int line, const char *file, const char *cmd, const char *algorithm);

+ 1 - 1
tests/der_test.c

@@ -1664,7 +1664,7 @@ int der_test(void)
    der_Xcode_test();
    der_Xcode_test();
 
 
 #ifdef LTC_TEST_READDIR
 #ifdef LTC_TEST_READDIR
-   DO(test_process_dir("tests/asn1", &list, s_der_decode_sequence_flexi, NULL, "DER ASN.1 special cases"));
+   DO(test_process_dir("tests/asn1", &list, s_der_decode_sequence_flexi, NULL, NULL, "DER ASN.1 special cases"));
 #endif
 #endif
 
 
    der_custom_test();
    der_custom_test();

+ 19 - 14
tests/pem_test.c

@@ -22,29 +22,22 @@ static rsa_key s_rsa_key_should;
 static ecc_key s_ecc_key_should;
 static ecc_key s_ecc_key_should;
 #endif
 #endif
 
 
-static int s_pem_decode_filehandle(const void *in, unsigned long inlen, void *key)
+static int s_key_cmp(ltc_pka_key *key)
 {
 {
-   password_ctx pw_ctx;
-   ltc_pka_key *key_ = key;
-   int err;
-   pw_ctx.callback = password_get;
-   if ((err = pem_decode(in, inlen, key_, &pw_ctx)) != CRYPT_OK) {
-      return err;
-   }
-   switch (key_->id) {
+   switch (key->id) {
       case LTC_PKA_DSA:
       case LTC_PKA_DSA:
 #if defined(LTC_MDSA)
 #if defined(LTC_MDSA)
-         return dsa_key_cmp(PK_PRIVATE, &s_dsa_key_should, &key_->u.dsa);
+         return dsa_key_cmp(PK_PRIVATE, &s_dsa_key_should, &key->u.dsa);
 #endif
 #endif
          break;
          break;
       case LTC_PKA_RSA:
       case LTC_PKA_RSA:
 #if defined(LTC_MRSA)
 #if defined(LTC_MRSA)
-         return rsa_key_cmp(PK_PRIVATE, &s_rsa_key_should, &key_->u.rsa);
+         return rsa_key_cmp(PK_PRIVATE, &s_rsa_key_should, &key->u.rsa);
 #endif
 #endif
          break;
          break;
       case LTC_PKA_EC:
       case LTC_PKA_EC:
 #if defined(LTC_MECC)
 #if defined(LTC_MECC)
-         return ecc_key_cmp(PK_PRIVATE, &s_ecc_key_should, &key_->u.ecc);
+         return ecc_key_cmp(PK_PRIVATE, &s_ecc_key_should, &key->u.ecc);
 #endif
 #endif
          break;
          break;
       case LTC_PKA_CURVE25519:
       case LTC_PKA_CURVE25519:
@@ -55,6 +48,17 @@ static int s_pem_decode_filehandle(const void *in, unsigned long inlen, void *ke
    return CRYPT_INVALID_ARG;
    return CRYPT_INVALID_ARG;
 }
 }
 
 
+static int s_pem_decode(const void *in, unsigned long inlen, void *key)
+{
+   password_ctx pw_ctx;
+   int err;
+   pw_ctx.callback = password_get;
+   if ((err = pem_decode(in, inlen, key, &pw_ctx)) != CRYPT_OK) {
+      return err;
+   }
+   return s_key_cmp(key);
+}
+
 static void s_pem_free_key(ltc_pka_key *key)
 static void s_pem_free_key(ltc_pka_key *key)
 {
 {
    switch (key->id) {
    switch (key->id) {
@@ -95,8 +99,9 @@ int pem_test(void)
 #endif
 #endif
 
 
 
 
-   DO(test_process_dir("tests/pem", &key, s_pem_decode_filehandle, (dir_cleanup_cb)s_pem_free_key, "pem_test"));
-   DO(test_process_dir("tests/pem-ecc-pkcs8", &key, s_pem_decode_filehandle, (dir_cleanup_cb)s_pem_free_key, "pem_test+ecc"));
+   DO(test_process_dir("tests/pem", &key, s_pem_decode, NULL, (dir_cleanup_cb)s_pem_free_key, "pem_test"));
+   DO(test_process_dir("tests/pem-ecc-pkcs8", &key, s_pem_decode, NULL, (dir_cleanup_cb)s_pem_free_key, "pem_test+ecc"));
+   DO(test_process_dir("tests/ssh", &key, s_pem_decode_ssh, NULL, (dir_cleanup_cb)s_pem_free_key, "pem_test+ssh"));
 
 
 #if defined(LTC_MDSA)
 #if defined(LTC_MDSA)
    dsa_free(&s_dsa_key_should);
    dsa_free(&s_dsa_key_should);

+ 2 - 2
tests/rsa_test.c

@@ -475,9 +475,9 @@ int rsa_test(void)
    }
    }
 
 
 #ifdef LTC_TEST_READDIR
 #ifdef LTC_TEST_READDIR
-   DO(test_process_dir("tests/rsa", &key, s_rsa_import_x509, (dir_cleanup_cb)rsa_free, "rsa_test"));
+   DO(test_process_dir("tests/rsa", &key, s_rsa_import_x509, NULL, (dir_cleanup_cb)rsa_free, "rsa_test"));
 #if defined(LTC_MD2) && defined(LTC_MD5) && defined(LTC_RC2)
 #if defined(LTC_MD2) && defined(LTC_MD5) && defined(LTC_RC2)
-   DO(test_process_dir("tests/rsa-pkcs8", &key, s_rsa_import_pkcs8, (dir_cleanup_cb)rsa_free, "rsa_pkcs8_test"));
+   DO(test_process_dir("tests/rsa-pkcs8", &key, s_rsa_import_pkcs8, NULL, (dir_cleanup_cb)rsa_free, "rsa_pkcs8_test"));
 #endif
 #endif
 #endif
 #endif