Skip to content

Commit fa86229

Browse files
simonsjcryptomilk
authored andcommitted
pkd: a few improvements and fixups
Summary: Hello, resending this patch series for the `pkd` tests, originally sent to the mailing list here: * https://www.libssh.org/archive/libssh/2017-07/0000011.html Here are a few improvements and fixups for the `pkd` tests, including a new flag `-m` that can be used to run only certain subsets of the test passes. Jon Simons (5): pkd: rename AES192 cipher suite -> OPENSSHONLY pkd_daemon.c: mark `pkd_ready` field as volatile pkd: fixups for updated CMocka CMUnitTest struct pkd: refactor -t testname lookup-by-name pkd: support -m to match multiple tests tests/pkd/pkd_daemon.c | 2 +- tests/pkd/pkd_daemon.h | 1 + tests/pkd/pkd_hello.c | 84 +++++++++++++++++++++++++++++++++----------------- 3 files changed, 58 insertions(+), 29 deletions(-) -- Test Plan: * I've been using the new `-m` mode locally for a long time to run only certain groups of tests. * The CMocka struct fixes can be seen in the pkd output before and after: after, there are no more extraneous test output strings. * The fix for the `pkd_ready` field can be observed when building the libssh tests with `-Os` on a Debian system (before the fix, pkd would hang, after the fix, it runs as intended). Reviewers: asn Reviewed By: asn Tags: #libssh Differential Revision: https://bugs.libssh.org/D2
1 parent c317d95 commit fa86229

File tree

3 files changed

+58
-29
lines changed

3 files changed

+58
-29
lines changed

tests/pkd/pkd_daemon.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static struct {
5858
int rc;
5959
pthread_t tid;
6060
int keep_going;
61-
int pkd_ready;
61+
volatile int pkd_ready;
6262
} ctx;
6363

6464
static struct {

tests/pkd/pkd_daemon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct pkd_daemon_args {
2626
int libssh_log_level;
2727

2828
const char *testname;
29+
const char *testmatch;
2930
unsigned int iterations;
3031
} opts;
3132
};

tests/pkd/pkd_hello.c

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* pkd_hello.c --
33
*
4-
* (c) 2014 Jon Simons
4+
* (c) 2014, 2017 Jon Simons <[email protected]>
55
*/
66

77
#include <setjmp.h> // for cmocka
@@ -25,9 +25,9 @@ static struct pkd_daemon_args pkd_dargs;
2525
#include <argp.h>
2626
#define PROGNAME "pkd_hello"
2727
#define ARGP_PROGNAME "libssh " PROGNAME
28-
const char *argp_program_version = ARGP_PROGNAME " 2014-04-12";
28+
const char *argp_program_version = ARGP_PROGNAME " 2017-07-12";
2929
const char *argp_program_bug_address = "Jon Simons <[email protected]>";
30-
//static char **cmdline;
30+
3131
static char doc[] = \
3232
"\nExample usage:\n\n"
3333
" " PROGNAME "\n"
@@ -36,6 +36,8 @@ static char doc[] = \
3636
" List available individual test names.\n"
3737
" " PROGNAME " -i 1000 -t torture_pkd_rsa_ecdh_sha2_nistp256\n"
3838
" Run only the torture_pkd_rsa_ecdh_sha2_nistp256 testcase 1000 times.\n"
39+
" " PROGNAME " -i 1000 -m curve25519\n"
40+
" Run all tests with the string 'curve25519' 1000 times.\n"
3941
" " PROGNAME " -v -v -v -v -e -o\n"
4042
" Run all tests with maximum libssh and pkd logging.\n"
4143
;
@@ -47,6 +49,8 @@ static struct argp_option options[] = {
4749
"List available individual test names", 0 },
4850
{ "iterations", 'i', "number", 0,
4951
"Run each test for the given number of iterations (default is 10)", 0 },
52+
{ "match", 'm', "testmatch", 0,
53+
"Run all tests with the given string", 0 },
5054
{ "stdout", 'o', NULL, 0,
5155
"Emit pkd stdout messages", 0 },
5256
{ "test", 't', "testname", 0,
@@ -71,6 +75,9 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) {
7175
case 'i':
7276
pkd_dargs.opts.iterations = atoi(arg);
7377
break;
78+
case 'm':
79+
pkd_dargs.opts.testmatch = arg;
80+
break;
7481
case 'o':
7582
pkd_dargs.opts.log_stdout = 1;
7683
break;
@@ -235,7 +242,7 @@ static int torture_pkd_setup_ecdsa_521(void **state) {
235242
f(client, ecdsa_521_aes256_ctr, ciphercmd("aes256-ctr"), setup_ecdsa_521, teardown) \
236243
f(client, ecdsa_521_blowfish_cbc, ciphercmd("blowfish-cbc"), setup_ecdsa_521, teardown)
237244

238-
#define PKDTESTS_CIPHER_AES192(f, client, ciphercmd) \
245+
#define PKDTESTS_CIPHER_OPENSSHONLY(f, client, ciphercmd) \
239246
/* Ciphers. */ \
240247
f(client, rsa_aes192_cbc, ciphercmd("aes192-cbc"), setup_rsa, teardown) \
241248
f(client, rsa_aes192_ctr, ciphercmd("aes192-ctr"), setup_rsa, teardown) \
@@ -315,23 +322,23 @@ static void torture_pkd_runtest(const char *testname,
315322
PKDTESTS_DEFAULT(emit_keytest, openssh_dsa, OPENSSH_CMD)
316323
PKDTESTS_KEX(emit_keytest, openssh_dsa, OPENSSH_KEX_CMD)
317324
PKDTESTS_CIPHER(emit_keytest, openssh_dsa, OPENSSH_CIPHER_CMD)
318-
PKDTESTS_CIPHER_AES192(emit_keytest, openssh_dsa, OPENSSH_CIPHER_CMD)
325+
PKDTESTS_CIPHER_OPENSSHONLY(emit_keytest, openssh_dsa, OPENSSH_CIPHER_CMD)
319326
PKDTESTS_MAC(emit_keytest, openssh_dsa, OPENSSH_MAC_CMD)
320327
#undef CLIENT_ID_FILE
321328

322329
#define CLIENT_ID_FILE OPENSSH_RSA_TESTKEY
323330
PKDTESTS_DEFAULT(emit_keytest, openssh_rsa, OPENSSH_CMD)
324331
PKDTESTS_KEX(emit_keytest, openssh_rsa, OPENSSH_KEX_CMD)
325332
PKDTESTS_CIPHER(emit_keytest, openssh_rsa, OPENSSH_CIPHER_CMD)
326-
PKDTESTS_CIPHER_AES192(emit_keytest, openssh_rsa, OPENSSH_CIPHER_CMD)
333+
PKDTESTS_CIPHER_OPENSSHONLY(emit_keytest, openssh_rsa, OPENSSH_CIPHER_CMD)
327334
PKDTESTS_MAC(emit_keytest, openssh_rsa, OPENSSH_MAC_CMD)
328335
#undef CLIENT_ID_FILE
329336

330337
#define CLIENT_ID_FILE OPENSSH_ECDSA256_TESTKEY
331338
PKDTESTS_DEFAULT(emit_keytest, openssh_e256, OPENSSH_CMD)
332339
PKDTESTS_KEX(emit_keytest, openssh_e256, OPENSSH_KEX_CMD)
333340
PKDTESTS_CIPHER(emit_keytest, openssh_e256, OPENSSH_CIPHER_CMD)
334-
PKDTESTS_CIPHER_AES192(emit_keytest, openssh_e256, OPENSSH_CIPHER_CMD)
341+
PKDTESTS_CIPHER_OPENSSHONLY(emit_keytest, openssh_e256, OPENSSH_CIPHER_CMD)
335342
PKDTESTS_MAC(emit_keytest, openssh_e256, OPENSSH_MAC_CMD)
336343
#undef CLIENT_ID_FILE
337344

@@ -343,7 +350,7 @@ PKDTESTS_MAC(emit_keytest, openssh_e256, OPENSSH_MAC_CMD)
343350
PKDTESTS_DEFAULT(emit_keytest, openssh_ed, OPENSSH_CMD)
344351
PKDTESTS_KEX(emit_keytest, openssh_ed, OPENSSH_KEX_CMD)
345352
PKDTESTS_CIPHER(emit_keytest, openssh_ed, OPENSSH_CIPHER_CMD)
346-
PKDTESTS_CIPHER_AES192(emit_keytest, openssh_ed, OPENSSH_CIPHER_CMD)
353+
PKDTESTS_CIPHER_OPENSSHONLY(emit_keytest, openssh_ed, OPENSSH_CIPHER_CMD)
347354
PKDTESTS_MAC(emit_keytest, openssh_ed, OPENSSH_MAC_CMD)
348355
#undef CLIENT_ID_FILE
349356

@@ -361,7 +368,7 @@ PKDTESTS_MAC(emit_keytest, dropbear, DROPBEAR_MAC_CMD)
361368

362369
#define emit_testmap(client, testname, sshcmd, setup, teardown) \
363370
{ "torture_pkd_" #client "_" #testname, \
364-
{ emit_unit_test(client, testname, sshcmd, setup, teardown) } },
371+
emit_unit_test(client, testname, sshcmd, setup, teardown) },
365372

366373
#define emit_unit_test(client, testname, sshcmd, setup, teardown) \
367374
cmocka_unit_test_setup_teardown(torture_pkd_ ## client ## _ ## testname, \
@@ -373,31 +380,31 @@ PKDTESTS_MAC(emit_keytest, dropbear, DROPBEAR_MAC_CMD)
373380

374381
struct {
375382
const char *testname;
376-
const struct CMUnitTest test[3]; /* requires setup + test + teardown */
383+
const struct CMUnitTest test;
377384
} testmap[] = {
378385
/* OpenSSH */
379386
PKDTESTS_DEFAULT(emit_testmap, openssh_dsa, OPENSSH_CMD)
380387
PKDTESTS_KEX(emit_testmap, openssh_dsa, OPENSSH_KEX_CMD)
381388
PKDTESTS_CIPHER(emit_testmap, openssh_dsa, OPENSSH_CIPHER_CMD)
382-
PKDTESTS_CIPHER_AES192(emit_testmap, openssh_dsa, OPENSSH_CIPHER_CMD)
389+
PKDTESTS_CIPHER_OPENSSHONLY(emit_testmap, openssh_dsa, OPENSSH_CIPHER_CMD)
383390
PKDTESTS_MAC(emit_testmap, openssh_dsa, OPENSSH_MAC_CMD)
384391

385392
PKDTESTS_DEFAULT(emit_testmap, openssh_rsa, OPENSSH_CMD)
386393
PKDTESTS_KEX(emit_testmap, openssh_rsa, OPENSSH_KEX_CMD)
387394
PKDTESTS_CIPHER(emit_testmap, openssh_rsa, OPENSSH_CIPHER_CMD)
388-
PKDTESTS_CIPHER_AES192(emit_testmap, openssh_rsa, OPENSSH_CIPHER_CMD)
395+
PKDTESTS_CIPHER_OPENSSHONLY(emit_testmap, openssh_rsa, OPENSSH_CIPHER_CMD)
389396
PKDTESTS_MAC(emit_testmap, openssh_rsa, OPENSSH_MAC_CMD)
390397

391398
PKDTESTS_DEFAULT(emit_testmap, openssh_e256, OPENSSH_CMD)
392399
PKDTESTS_KEX(emit_testmap, openssh_e256, OPENSSH_KEX_CMD)
393400
PKDTESTS_CIPHER(emit_testmap, openssh_e256, OPENSSH_CIPHER_CMD)
394-
PKDTESTS_CIPHER_AES192(emit_testmap, openssh_e256, OPENSSH_CIPHER_CMD)
401+
PKDTESTS_CIPHER_OPENSSHONLY(emit_testmap, openssh_e256, OPENSSH_CIPHER_CMD)
395402
PKDTESTS_MAC(emit_testmap, openssh_e256, OPENSSH_MAC_CMD)
396403

397404
PKDTESTS_DEFAULT(emit_testmap, openssh_ed, OPENSSH_CMD)
398405
PKDTESTS_KEX(emit_testmap, openssh_ed, OPENSSH_KEX_CMD)
399406
PKDTESTS_CIPHER(emit_testmap, openssh_ed, OPENSSH_CIPHER_CMD)
400-
PKDTESTS_CIPHER_AES192(emit_testmap, openssh_ed, OPENSSH_CIPHER_CMD)
407+
PKDTESTS_CIPHER_OPENSSHONLY(emit_testmap, openssh_ed, OPENSSH_CIPHER_CMD)
401408
PKDTESTS_MAC(emit_testmap, openssh_ed, OPENSSH_MAC_CMD)
402409

403410
/* Dropbear */
@@ -409,8 +416,11 @@ struct {
409416
emit_testmap(client, noop, "", setup_noop, teardown)
410417

411418
/* NULL tail entry */
412-
{ .testname = NULL, {
413-
{ .name = NULL, }, { .name = NULL }, { .name = NULL } } }
419+
{ .testname = NULL,
420+
.test = { .name = NULL,
421+
.test_func = NULL,
422+
.setup_func = NULL,
423+
.teardown_func = NULL } }
414424
};
415425

416426
static int pkd_run_tests(void) {
@@ -421,25 +431,25 @@ static int pkd_run_tests(void) {
421431
PKDTESTS_DEFAULT(emit_unit_test_comma, openssh_dsa, OPENSSH_CMD)
422432
PKDTESTS_KEX(emit_unit_test_comma, openssh_dsa, OPENSSH_KEX_CMD)
423433
PKDTESTS_CIPHER(emit_unit_test_comma, openssh_dsa, OPENSSH_CIPHER_CMD)
424-
PKDTESTS_CIPHER_AES192(emit_unit_test_comma, openssh_dsa, OPENSSH_CIPHER_CMD)
434+
PKDTESTS_CIPHER_OPENSSHONLY(emit_unit_test_comma, openssh_dsa, OPENSSH_CIPHER_CMD)
425435
PKDTESTS_MAC(emit_unit_test_comma, openssh_dsa, OPENSSH_MAC_CMD)
426436

427437
PKDTESTS_DEFAULT(emit_unit_test_comma, openssh_rsa, OPENSSH_CMD)
428438
PKDTESTS_KEX(emit_unit_test_comma, openssh_rsa, OPENSSH_KEX_CMD)
429439
PKDTESTS_CIPHER(emit_unit_test_comma, openssh_rsa, OPENSSH_CIPHER_CMD)
430-
PKDTESTS_CIPHER_AES192(emit_unit_test_comma, openssh_rsa, OPENSSH_CIPHER_CMD)
440+
PKDTESTS_CIPHER_OPENSSHONLY(emit_unit_test_comma, openssh_rsa, OPENSSH_CIPHER_CMD)
431441
PKDTESTS_MAC(emit_unit_test_comma, openssh_rsa, OPENSSH_MAC_CMD)
432442

433443
PKDTESTS_DEFAULT(emit_unit_test_comma, openssh_e256, OPENSSH_CMD)
434444
PKDTESTS_KEX(emit_unit_test_comma, openssh_e256, OPENSSH_KEX_CMD)
435445
PKDTESTS_CIPHER(emit_unit_test_comma, openssh_e256, OPENSSH_CIPHER_CMD)
436-
PKDTESTS_CIPHER_AES192(emit_unit_test_comma, openssh_e256, OPENSSH_CIPHER_CMD)
446+
PKDTESTS_CIPHER_OPENSSHONLY(emit_unit_test_comma, openssh_e256, OPENSSH_CIPHER_CMD)
437447
PKDTESTS_MAC(emit_unit_test_comma, openssh_e256, OPENSSH_MAC_CMD)
438448

439449
PKDTESTS_DEFAULT(emit_unit_test_comma, openssh_ed, OPENSSH_CMD)
440450
PKDTESTS_KEX(emit_unit_test_comma, openssh_ed, OPENSSH_KEX_CMD)
441451
PKDTESTS_CIPHER(emit_unit_test_comma, openssh_ed, OPENSSH_CIPHER_CMD)
442-
PKDTESTS_CIPHER_AES192(emit_unit_test_comma, openssh_ed, OPENSSH_CIPHER_CMD)
452+
PKDTESTS_CIPHER_OPENSSHONLY(emit_unit_test_comma, openssh_ed, OPENSSH_CIPHER_CMD)
443453
PKDTESTS_MAC(emit_unit_test_comma, openssh_ed, OPENSSH_MAC_CMD)
444454
};
445455

@@ -455,8 +465,8 @@ static int pkd_run_tests(void) {
455465

456466
/* Test list is populated depending on which clients are enabled. */
457467
struct CMUnitTest all_tests[(sizeof(openssh_tests) / sizeof(openssh_tests[0])) +
458-
(sizeof(dropbear_tests) / sizeof(dropbear_tests[0])) +
459-
(sizeof(noop_tests) / sizeof(noop_tests[0]))];
468+
(sizeof(dropbear_tests) / sizeof(dropbear_tests[0])) +
469+
(sizeof(noop_tests) / sizeof(noop_tests[0]))];
460470
memset(&all_tests[0], 0x0, sizeof(all_tests));
461471

462472
/* Generate client keys and populate test list for each enabled client. */
@@ -475,23 +485,41 @@ static int pkd_run_tests(void) {
475485
memcpy(&all_tests[tindex], &noop_tests[0], sizeof(noop_tests));
476486
tindex += (sizeof(noop_tests) / sizeof(noop_tests[0]));
477487

478-
if (pkd_dargs.opts.testname == NULL) {
488+
if ((pkd_dargs.opts.testname == NULL) &&
489+
(pkd_dargs.opts.testmatch == NULL)) {
479490
rc = _cmocka_run_group_tests("all tests", all_tests, tindex, NULL, NULL);
480491
} else {
481492
int i = 0;
482-
const struct CMUnitTest *found = NULL;
493+
int num_found = 0;
483494
const char *testname = pkd_dargs.opts.testname;
495+
const char *testmatch = pkd_dargs.opts.testmatch;
496+
497+
struct CMUnitTest matching_tests[sizeof(all_tests)];
498+
memset(&matching_tests[0], 0x0, sizeof(matching_tests));
484499

485500
while (testmap[i].testname != NULL) {
486-
if (strcmp(testmap[i].testname, testname) == 0) {
487-
found = &testmap[i].test[0];
501+
if ((testname != NULL) &&
502+
(strcmp(testmap[i].testname, testname) == 0)) {
503+
memcpy(&matching_tests[0],
504+
&testmap[i].test,
505+
sizeof(struct CMUnitTest));
506+
num_found += 1;
488507
break;
489508
}
509+
510+
if ((testmatch != NULL) &&
511+
(strstr(testmap[i].testname, testmatch) != NULL)) {
512+
memcpy(&matching_tests[num_found],
513+
&testmap[i].test,
514+
sizeof(struct CMUnitTest));
515+
num_found += 1;
516+
}
517+
490518
i += 1;
491519
}
492520

493-
if (found != NULL) {
494-
rc = _cmocka_run_group_tests("found", found, 3, NULL, NULL);
521+
if (num_found > 0) {
522+
rc = _cmocka_run_group_tests("found", matching_tests, num_found, NULL, NULL);
495523
} else {
496524
fprintf(stderr, "Did not find test '%s'\n", testname);
497525
}

0 commit comments

Comments
 (0)