Commit d836d71b authored by Emilia Kasper's avatar Emilia Kasper
Browse files

Simplify tests part 2

1) Remove some unnecessary fixtures
2) Add EXECUTE_TEST_NO_TEARDOWN shorthand when a fixture exists but has
no teardown.
3) Fix return values in ct_test.c (introduced by an earlier refactoring,
oops)

Note that for parameterized tests, the index (test vector) usually holds all the
customization, and there should be no need for a separate test
fixture. The CTS test is an exception: it demonstrates how to combine
customization with parameterization.

Reviewed-by: default avatarRichard Levitte <levitte@openssl.org>
parent 8e47ee18
Loading
Loading
Loading
Loading
+4 −4
Changes for test/ct_test.c: 4 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -507,20 +507,20 @@ static int test_encode_tls_sct()
    SCT *sct = SCT_new();
    if (!SCT_set_version(sct, SCT_VERSION_V1)) {
        fprintf(stderr, "Failed to set SCT version\n");
        return 1;
        return 0;
    }
    if (!SCT_set1_log_id(sct, log_id, 32)) {
        fprintf(stderr, "Failed to set SCT log ID\n");
        return 1;
        return 0;
    }
    SCT_set_timestamp(sct, 1);
    if (!SCT_set_signature_nid(sct, NID_ecdsa_with_SHA256)) {
        fprintf(stderr, "Failed to set SCT signature NID\n");
        return 1;
        return 0;
    }
    if (!SCT_set1_signature(sct, signature, 71)) {
        fprintf(stderr, "Failed to set SCT signature\n");
        return 1;
        return 0;
    }
    sk_SCT_push(sct_list, sct);

+21 −44
Changes for test/mdc2_internal_test.c: 21 added lines, 44 removed lines.
Original line number Diff line number Diff line
@@ -21,74 +21,51 @@ typedef struct {
    const unsigned char expected[MDC2_DIGEST_LENGTH];
} TESTDATA;

typedef struct {
    const char *case_name;
    int num;
    const TESTDATA *data;
} SIMPLE_FIXTURE;

/**********************************************************************
 *
 * Test of mdc2 internal functions
 * Test driver
 *
 ***/

static SIMPLE_FIXTURE setup_mdc2(const char *const test_case_name)
static TESTDATA tests[] = {
    {
        "Now is the time for all ",
        {
    SIMPLE_FIXTURE fixture;
    fixture.case_name = test_case_name;
    return fixture;
            0x42, 0xE5, 0x0C, 0xD2, 0x24, 0xBA, 0xCE, 0xBA,
            0x76, 0x0B, 0xDD, 0x2B, 0xD4, 0x09, 0x28, 0x1A
        }
    }
};

static int execute_mdc2(SIMPLE_FIXTURE fixture)
/**********************************************************************
 *
 * Test of mdc2 internal functions
 *
 ***/

static int test_mdc2(int idx)
{
    unsigned char md[MDC2_DIGEST_LENGTH];
    MDC2_CTX c;
    const TESTDATA testdata = tests[idx];

    MDC2_Init(&c);
    MDC2_Update(&c, (const unsigned char *)fixture.data->input,
                strlen(fixture.data->input));
    MDC2_Update(&c, (const unsigned char *)testdata.input,
                strlen(testdata.input));
    MDC2_Final(&(md[0]), &c);

    if (memcmp(fixture.data->expected, md, MDC2_DIGEST_LENGTH)) {
        fprintf(stderr, "mdc2 test %d: unexpected output\n", fixture.num);
    if (memcmp(testdata.expected, md, MDC2_DIGEST_LENGTH)) {
        fprintf(stderr, "mdc2 test %d: unexpected output\n", idx);
        return 0;
    }

    return 1;
}

static void teardown_mdc2(SIMPLE_FIXTURE fixture)
{
}

/**********************************************************************
 *
 * Test driver
 *
 ***/

static TESTDATA tests[] = {
    {
        "Now is the time for all ",
        {
            0x42, 0xE5, 0x0C, 0xD2, 0x24, 0xBA, 0xCE, 0xBA,
            0x76, 0x0B, 0xDD, 0x2B, 0xD4, 0x09, 0x28, 0x1A
        }
    }
};

static int drive_tests(int idx)
{
    SETUP_TEST_FIXTURE(SIMPLE_FIXTURE, setup_mdc2);
    fixture.num = idx;
    fixture.data = &tests[idx];
    EXECUTE_TEST(execute_mdc2, teardown_mdc2);
}

int main(int argc, char **argv)
{
    ADD_ALL_TESTS(drive_tests, OSSL_NELEM(tests));
    ADD_ALL_TESTS(test_mdc2, OSSL_NELEM(tests));

    return run_tests(argv[0]);
}
+258 −345

File changed.

Preview size limit exceeded, changes collapsed.

+83 −115

File changed.

Preview size limit exceeded, changes collapsed.

+6 −32

File changed.

Preview size limit exceeded, changes collapsed.

Loading