Skip to content

Commit 15fc300

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

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
@@ -80,15 +80,29 @@ static off_t fsize(const char *filename)
8080
return -1;
8181
}
8282

83-
int test_process_dir(const char *path, void *ctx, dir_iter_cb process, dir_cleanup_cb cleanup, const char *test)
83+
static int s_read_and_process(FILE *f, unsigned long sz, void *ctx, dir_iter_cb process)
84+
{
85+
int err = CRYPT_OK;
86+
void* buf = XMALLOC(sz);
87+
if (buf == NULL)
88+
return CRYPT_MEM;
89+
if (fread(buf, 1, sz, f) != sz) {
90+
err = CRYPT_ERROR;
91+
goto out;
92+
}
93+
err = process(buf, sz, ctx);
94+
out:
95+
XFREE(buf);
96+
return err;
97+
}
98+
99+
int test_process_dir(const char *path, void *ctx, dir_iter_cb iter, dir_fiter_cb fiter, dir_cleanup_cb cleanup, const char *test)
84100
{
85101
DIR *d = opendir(path);
86102
struct dirent *de;
87103
char fname[PATH_MAX];
88-
void* buf = NULL;
89104
FILE *f = NULL;
90105
off_t fsz;
91-
unsigned long sz;
92106
int err = CRYPT_FILE_NOTFOUND;
93107
if (d == NULL)
94108
return CRYPT_FILE_NOTFOUND;
@@ -108,14 +122,18 @@ int test_process_dir(const char *path, void *ctx, dir_iter_cb process, dir_clean
108122
fprintf(stderr, "%s: Try to process %s\n", test, fname);
109123
#endif
110124
f = fopen(fname, "rb");
111-
sz = fsz;
112-
buf = XMALLOC(fsz);
113-
if (fread(buf, 1, sz, f) != sz) {
114-
err = CRYPT_ERROR;
115-
break;
125+
126+
if (iter) {
127+
err = s_read_and_process(f, fsz, ctx, iter);
128+
} else if (fiter) {
129+
err = fiter(f, ctx);
130+
} else {
131+
err = CRYPT_NOP;
132+
#if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
133+
fprintf(stderr, "%s: No call-back set for %s\n", test, fname);
134+
#endif
116135
}
117136

118-
err = process(buf, sz, ctx);
119137
if (err == CRYPT_NOP) {
120138
#if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
121139
fprintf(stderr, "%s: Skip: %s\n", test, fname);
@@ -134,12 +152,9 @@ int test_process_dir(const char *path, void *ctx, dir_iter_cb process, dir_clean
134152
}
135153

136154
continue_loop:
137-
XFREE(buf);
138-
buf = NULL;
139155
fclose(f);
140156
f = NULL;
141157
}
142-
if (buf != NULL) XFREE(buf);
143158
if (f != NULL) fclose(f);
144159
closedir(d);
145160
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
@@ -1659,7 +1659,7 @@ int der_test(void)
16591659
der_Xcode_test();
16601660

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

16651665
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
@@ -476,9 +476,9 @@ int rsa_test(void)
476476
}
477477

478478
#ifdef LTC_TEST_READDIR
479-
DO(test_process_dir("tests/rsa", &key, s_rsa_import_x509, (dir_cleanup_cb)rsa_free, "rsa_test"));
479+
DO(test_process_dir("tests/rsa", &key, s_rsa_import_x509, NULL, (dir_cleanup_cb)rsa_free, "rsa_test"));
480480
#if defined(LTC_MD2) && defined(LTC_MD5) && defined(LTC_RC2)
481-
DO(test_process_dir("tests/rsa-pkcs8", &key, s_rsa_import_pkcs8, (dir_cleanup_cb)rsa_free, "rsa_pkcs8_test"));
481+
DO(test_process_dir("tests/rsa-pkcs8", &key, s_rsa_import_pkcs8, NULL, (dir_cleanup_cb)rsa_free, "rsa_pkcs8_test"));
482482
#endif
483483
#endif
484484

0 commit comments

Comments
 (0)