Skip to content

Commit a8c1bec

Browse files
committed
add file-iterator to test_process_dir()
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
1 parent e7e1fd6 commit a8c1bec

File tree

5 files changed

+52
-31
lines changed

5 files changed

+52
-31
lines changed

tests/common.c

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,30 @@ static DIR *s_opendir(const char *path, char *mypath, unsigned long l)
9595
return d;
9696
}
9797

98-
int test_process_dir(const char *path, void *ctx, dir_iter_cb process, dir_cleanup_cb cleanup, const char *test)
98+
static int s_read_and_process(FILE *f, unsigned long sz, void *ctx, dir_iter_cb process)
99+
{
100+
int err = CRYPT_OK;
101+
void* buf = XMALLOC(sz);
102+
if (buf == NULL)
103+
return CRYPT_MEM;
104+
if (fread(buf, 1, sz, f) != sz) {
105+
err = CRYPT_ERROR;
106+
goto out;
107+
}
108+
err = process(buf, sz, ctx);
109+
out:
110+
XFREE(buf);
111+
return err;
112+
}
113+
114+
int test_process_dir(const char *path, void *ctx, dir_iter_cb iter, dir_fiter_cb fiter, dir_cleanup_cb cleanup, const char *test)
99115
{
100116
char mypath[PATH_MAX];
101117
DIR *d = s_opendir(path, mypath, sizeof(mypath));
102118
struct dirent *de;
103119
char fname[PATH_MAX];
104-
void* buf = NULL;
105120
FILE *f = NULL;
106121
off_t fsz;
107-
unsigned long sz;
108122
int err = CRYPT_FILE_NOTFOUND;
109123
if (d == NULL)
110124
return CRYPT_FILE_NOTFOUND;
@@ -124,14 +138,18 @@ int test_process_dir(const char *path, void *ctx, dir_iter_cb process, dir_clean
124138
fprintf(stderr, "%s: Try to process %s\n", test, fname);
125139
#endif
126140
f = fopen(fname, "rb");
127-
sz = fsz;
128-
buf = XMALLOC(fsz);
129-
if (fread(buf, 1, sz, f) != sz) {
130-
err = CRYPT_ERROR;
131-
break;
141+
142+
if (iter) {
143+
err = s_read_and_process(f, fsz, ctx, iter);
144+
} else if (fiter) {
145+
err = fiter(f, ctx);
146+
} else {
147+
err = CRYPT_NOP;
148+
#if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
149+
fprintf(stderr, "%s: No call-back set for %s\n", test, fname);
150+
#endif
132151
}
133152

134-
err = process(buf, sz, ctx);
135153
if (err == CRYPT_NOP) {
136154
#if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
137155
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
150168
}
151169

152170
continue_loop:
153-
XFREE(buf);
154-
buf = NULL;
155171
fclose(f);
156172
f = NULL;
157173
}
158-
if (buf != NULL) XFREE(buf);
159174
if (f != NULL) fclose(f);
160175
closedir(d);
161176
return err;

tests/common.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ int ecc_key_cmp(const int should_type, const ecc_key *should, const ecc_key *is)
4040

4141
#define COMPARE_TESTVECTOR(i, il, s, sl, wa, wi) do { DO(do_compare_testvector((i), (il), (s), (sl), (wa), (wi))); } while(0)
4242

43-
#if !((defined(_WIN32) || defined(_WIN32_WCE)) && !defined(__GNUC__))
43+
#if !((defined(_WIN32) || defined(_WIN32_WCE)) && !defined(__GNUC__)) && !defined(LTC_NO_FILE)
4444
#define LTC_TEST_READDIR
4545

4646
typedef int (*dir_iter_cb)(const void *d, unsigned long l, void* ctx);
47+
typedef int (*dir_fiter_cb)(FILE *f, void* ctx);
4748
typedef void (*dir_cleanup_cb)(void* ctx);
4849

49-
int test_process_dir(const char *path, void *ctx, dir_iter_cb iter, dir_cleanup_cb cleanup, const char *test);
50+
int test_process_dir(const char *path, void *ctx, dir_iter_cb iter, dir_fiter_cb fiter, dir_cleanup_cb cleanup, const char *test);
5051
#endif
5152

5253
void run_cmd(int res, int line, const char *file, const char *cmd, const char *algorithm);

tests/der_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1658,7 +1658,7 @@ int der_test(void)
16581658
der_Xcode_test();
16591659

16601660
#ifdef LTC_TEST_READDIR
1661-
DO(test_process_dir("tests/asn1", &list, s_der_decode_sequence_flexi, NULL, "DER ASN.1 special cases"));
1661+
DO(test_process_dir("tests/asn1", &list, s_der_decode_sequence_flexi, NULL, NULL, "DER ASN.1 special cases"));
16621662
#endif
16631663

16641664
der_custom_test();

tests/pem_test.c

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,22 @@ static rsa_key s_rsa_key_should;
2222
static ecc_key s_ecc_key_should;
2323
#endif
2424

25-
static int s_pem_decode_filehandle(const void *in, unsigned long inlen, void *key)
25+
static int s_key_cmp(ltc_pka_key *key)
2626
{
27-
password_ctx pw_ctx;
28-
ltc_pka_key *key_ = key;
29-
int err;
30-
pw_ctx.callback = password_get;
31-
if ((err = pem_decode(in, inlen, key_, &pw_ctx)) != CRYPT_OK) {
32-
return err;
33-
}
34-
switch (key_->id) {
27+
switch (key->id) {
3528
case LTC_PKA_DSA:
3629
#if defined(LTC_MDSA)
37-
return dsa_key_cmp(PK_PRIVATE, &s_dsa_key_should, &key_->u.dsa);
30+
return dsa_key_cmp(PK_PRIVATE, &s_dsa_key_should, &key->u.dsa);
3831
#endif
3932
break;
4033
case LTC_PKA_RSA:
4134
#if defined(LTC_MRSA)
42-
return rsa_key_cmp(PK_PRIVATE, &s_rsa_key_should, &key_->u.rsa);
35+
return rsa_key_cmp(PK_PRIVATE, &s_rsa_key_should, &key->u.rsa);
4336
#endif
4437
break;
4538
case LTC_PKA_EC:
4639
#if defined(LTC_MECC)
47-
return ecc_key_cmp(PK_PRIVATE, &s_ecc_key_should, &key_->u.ecc);
40+
return ecc_key_cmp(PK_PRIVATE, &s_ecc_key_should, &key->u.ecc);
4841
#endif
4942
break;
5043
case LTC_PKA_CURVE25519:
@@ -55,6 +48,17 @@ static int s_pem_decode_filehandle(const void *in, unsigned long inlen, void *ke
5548
return CRYPT_INVALID_ARG;
5649
}
5750

51+
static int s_pem_decode(const void *in, unsigned long inlen, void *key)
52+
{
53+
password_ctx pw_ctx;
54+
int err;
55+
pw_ctx.callback = password_get;
56+
if ((err = pem_decode(in, inlen, key, &pw_ctx)) != CRYPT_OK) {
57+
return err;
58+
}
59+
return s_key_cmp(key);
60+
}
61+
5862
static void s_pem_free_key(ltc_pka_key *key)
5963
{
6064
switch (key->id) {
@@ -95,8 +99,9 @@ int pem_test(void)
9599
#endif
96100

97101

98-
DO(test_process_dir("tests/pem", &key, s_pem_decode_filehandle, (dir_cleanup_cb)s_pem_free_key, "pem_test"));
99-
DO(test_process_dir("tests/pem-ecc-pkcs8", &key, s_pem_decode_filehandle, (dir_cleanup_cb)s_pem_free_key, "pem_test+ecc"));
102+
DO(test_process_dir("tests/pem", &key, s_pem_decode, NULL, (dir_cleanup_cb)s_pem_free_key, "pem_test"));
103+
DO(test_process_dir("tests/pem-ecc-pkcs8", &key, s_pem_decode, NULL, (dir_cleanup_cb)s_pem_free_key, "pem_test+ecc"));
104+
DO(test_process_dir("tests/ssh", &key, s_pem_decode_ssh, NULL, (dir_cleanup_cb)s_pem_free_key, "pem_test+ssh"));
100105

101106
#if defined(LTC_MDSA)
102107
dsa_free(&s_dsa_key_should);

tests/rsa_test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,9 @@ int rsa_test(void)
475475
}
476476

477477
#ifdef LTC_TEST_READDIR
478-
DO(test_process_dir("tests/rsa", &key, s_rsa_import_x509, (dir_cleanup_cb)rsa_free, "rsa_test"));
478+
DO(test_process_dir("tests/rsa", &key, s_rsa_import_x509, NULL, (dir_cleanup_cb)rsa_free, "rsa_test"));
479479
#if defined(LTC_MD2) && defined(LTC_MD5) && defined(LTC_RC2)
480-
DO(test_process_dir("tests/rsa-pkcs8", &key, s_rsa_import_pkcs8, (dir_cleanup_cb)rsa_free, "rsa_pkcs8_test"));
480+
DO(test_process_dir("tests/rsa-pkcs8", &key, s_rsa_import_pkcs8, NULL, (dir_cleanup_cb)rsa_free, "rsa_pkcs8_test"));
481481
#endif
482482
#endif
483483

0 commit comments

Comments
 (0)