Skip to content

PSA tests: use a few common test macros #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jan 2, 2019

Conversation

gilles-peskine-arm
Copy link
Collaborator

Declare a few macros in helpers.function and use them in the PSA tests. I haven't touched non-PSA tests.

  • TEST_EQUAL (slightly more readable than TEST_ASSERT(a==b), but the real payoff will come later when we instrument it to print the values on failure)
  • PSA_ASSERT (TEST_ASSERT(…==PSA_SUCCESS))
  • ARRAY_LENGTH (with protection against usage on a pointer when compiling with GCC)
  • MIN, MAX

There's some refactoring going on, so I recommend reviewing commit by commit.

@Patater Patater added the enhancement New feature or request label Dec 18, 2018
@gilles-peskine-arm gilles-peskine-arm added the needs: review The pull request is ready for review. This generally means that it has no known issues. label Dec 18, 2018
Copy link
Contributor

@NirSonnenschein NirSonnenschein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

#endif

/* A compile-time constant with the value 0. If `const_expr` is not a
compile-time constant with a nonzero value, cause a compile-time error. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: begin continued comment lines with a *.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in the amended commit.

#else
/* On platforms where we don't know how to implement this check,
* omit it. Oh well, a non-portable check is better than nothing. */
#define STATIC_ASSERT_EXPR_IF_POSSIBLE( const_expr ) ( (void) 0 )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is IS_ARRAY_NOT_POINTER defined for non GNUC platforms? Should STATIC_ASSERT_EXPR_IF_POSSIBLE instead be IS_ARRAY_NOT_POINTER? Where is STATIC_ASSERT_EXPR_IF_POSSIBLE used?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover from a slightly different design that I abandoned without committing. I have amended the corresponding commit.

Cause a compilation error on ARRAY_LENGTH(p) where p is a pointer as
opposed to an array. This only works under GCC and compatible
compilers such as Clang. On other compilers, ARRAY_LENGTH works but
doesn't check the type of its argument.
It's potentially useful in all PSA test suites, of which there are now
several.
TEST_EQUAL(expr1, expr2) is just TEST_ASSERT((expr1) == (expr2)) for
now, but in the future I hope that it will print out the differing
values.
This guarantees that they'll be indented as desired under most
indentation rules.
Only whitespace changes in this commit.
This commit is the result of the following command, followed by
reindenting (but not wrapping lines):

perl -00 -i -pe 's/^( *)TEST_ASSERT\(([^;=]*)(?: |\n *)==\s*PSA_SUCCESS\s*\);$/${1}PSA_ASSERT($2 );/gm' tests/suites/test_suite_psa_*.function
This commit is the result of the following command, followed by
reindenting (but not wrapping lines):

perl -00 -i -pe 's/^( *)TEST_ASSERT\(([^;=]*)(?: |\n *)==([^;=]*)\);$/${1}TEST_EQUAL($2,$3);/gm' tests/suites/test_suite_psa_*.function
Change the way some lines are wrapped to cut at a more logical place.
This commit mainly rewrites multi-line calls to TEST_EQUAL, and also a
few calls to PSA_ASSERT.
Our code base doesn't even support 16-bit platforms, so those checks
are always trivially true.
The test framework never passes NULL for a data_t* parameter, so
testing them against NULL is clutter.
To allocate memory dynamically in a test, call ASSERT_ALLOC which
takes care of calling calloc and of checking for NULL.
@gilles-peskine-arm
Copy link
Collaborator Author

I've amended the commit "Add a safety check to ARRAY_LENGTH" to address the issues that @Patater pointed out. Previous history at https://github.com/gilles-peskine-arm/mbed-crypto/tree/psa-test_macros-1

Copy link
Contributor

@Patater Patater left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Patater
Copy link
Contributor

Patater commented Dec 21, 2018

CI failure is test_suite_timing on FreeBSD, a known issue.

@gilles-peskine-arm
Copy link
Collaborator Author

gilles-peskine-arm commented Dec 21, 2018

@Patater
Copy link
Contributor

Patater commented Dec 21, 2018

Other half of the release job: https://jenkins-internal.mbed.com/job/mbedtls-psa-release-new/202/

@Patater
Copy link
Contributor

Patater commented Dec 21, 2018

Basic build and test passed. Mbed OS testing went badly because the test is broke: not this PR's fault.

Copy link
Contributor

@NirSonnenschein NirSonnenschein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-checked after rebase - still look fine.

@Patater
Copy link
Contributor

Patater commented Jan 2, 2019

The test failure is the FreeBSD timing test, a known issue.

@Patater Patater merged commit c9a0722 into ARMmbed:development Jan 2, 2019
Patater pushed a commit that referenced this pull request Jul 24, 2019
option name: LINK_WITH_TRUSTED_STORAGE
default value: ON
gilles-peskine-arm added a commit to gilles-peskine-arm/mbed-crypto that referenced this pull request Jul 31, 2019
Patater added a commit to Patater/mbed-crypto that referenced this pull request Aug 16, 2019
* crypto/pr/212: (337 commits)
  Make TODO comments consistent
  Fix PSA tests
  Fix psa_generate_random for >1024 bytes
  Add tests to generate more random than MBEDTLS_CTR_DRBG_MAX_REQUEST
  Fix double free in psa_generate_key when psa_generate_random fails
  Fix copypasta in test data
  Avoid a lowercase letter in a macro name
  Correct some comments
  Fix PSA init/deinit in mbedtls_xxx tests when using PSA
  Make psa_calculate_key_bits return psa_key_bits_t
  Adjust secure element code to the new ITS interface
  More refactoring: consolidate attribute validation
  Fix policy validity check on key creation.
  Add test function for import with a bad policy
  Test key creation with an invalid type (0 and nonzero)
  Remove "allocated" flag from key slots
  Take advantage of psa_core_key_attributes_t internally ARMmbed#2
  Store the key size in the slot in memory
  Take advantage of psa_core_key_attributes_t internally: key loading
  Switch storage functions over to psa_core_key_attributes_t
  ...
Patater added a commit that referenced this pull request Aug 16, 2019
* crypto/pr/212: (337 commits)
  Make TODO comments consistent
  Fix PSA tests
  Fix psa_generate_random for >1024 bytes
  Add tests to generate more random than MBEDTLS_CTR_DRBG_MAX_REQUEST
  Fix double free in psa_generate_key when psa_generate_random fails
  Fix copypasta in test data
  Avoid a lowercase letter in a macro name
  Correct some comments
  Fix PSA init/deinit in mbedtls_xxx tests when using PSA
  Make psa_calculate_key_bits return psa_key_bits_t
  Adjust secure element code to the new ITS interface
  More refactoring: consolidate attribute validation
  Fix policy validity check on key creation.
  Add test function for import with a bad policy
  Test key creation with an invalid type (0 and nonzero)
  Remove "allocated" flag from key slots
  Take advantage of psa_core_key_attributes_t internally #2
  Store the key size in the slot in memory
  Take advantage of psa_core_key_attributes_t internally: key loading
  Switch storage functions over to psa_core_key_attributes_t
  ...
gilles-peskine-arm added a commit to gilles-peskine-arm/mbed-crypto that referenced this pull request Sep 19, 2019
Make check-test-cases.py pass.

Prior to this commit, there were many repeated test descriptions, but
none with the same test data and dependencies and comments, as checked
with the following command:

    for x in tests/suites/*.data; do perl -00 -ne 'warn "$ARGV: $. = $seen{$_}\n" if $seen{$_}; $seen{$_}=$.' $x; done

Wherever a test suite contains multiple test cases with the exact same
description, add " [ARMmbed#1]", " [ARMmbed#2]", etc. to make the descriptions
unique. We don't currently use this particular arrangement of
punctuation, so all occurrences of " [#" were added by this script.

I used the following ad hoc code:

import sys

def fix_test_suite(data_file_name):
    in_paragraph = False
    total = {}
    index = {}
    lines = None
    with open(data_file_name) as data_file:
        lines = list(data_file.readlines())
        for line in lines:
            if line == '\n':
                in_paragraph = False
                continue
            if line.startswith('#'):
                continue
            if not in_paragraph:
                # This is a test case description line.
                total[line] = total.get(line, 0) + 1
                index[line] = 0
            in_paragraph = True
    with open(data_file_name, 'w') as data_file:
        for line in lines:
            if line in total and total[line] > 1:
                index[line] += 1
                line = '%s [#%d]\n' % (line[:-1], index[line])
            data_file.write(line)

for data_file_name in sys.argv[1:]:
    fix_test_suite(data_file_name)
gilles-peskine-arm added a commit to gilles-peskine-arm/mbed-crypto that referenced this pull request Sep 20, 2019
Make check-test-cases.py pass.

Prior to this commit, there were many repeated test descriptions, but
none with the same test data and dependencies and comments, as checked
with the following command:

    for x in tests/suites/*.data; do perl -00 -ne 'warn "$ARGV: $. = $seen{$_}\n" if $seen{$_}; $seen{$_}=$.' $x; done

Wherever a test suite contains multiple test cases with the exact same
description, add " [ARMmbed#1]", " [ARMmbed#2]", etc. to make the descriptions
unique. We don't currently use this particular arrangement of
punctuation, so all occurrences of " [#" were added by this script.

I used the following ad hoc code:

import sys

def fix_test_suite(data_file_name):
    in_paragraph = False
    total = {}
    index = {}
    lines = None
    with open(data_file_name) as data_file:
        lines = list(data_file.readlines())
        for line in lines:
            if line == '\n':
                in_paragraph = False
                continue
            if line.startswith('#'):
                continue
            if not in_paragraph:
                # This is a test case description line.
                total[line] = total.get(line, 0) + 1
                index[line] = 0
            in_paragraph = True
    with open(data_file_name, 'w') as data_file:
        for line in lines:
            if line in total and total[line] > 1:
                index[line] += 1
                line = '%s [#%d]\n' % (line[:-1], index[line])
            data_file.write(line)

for data_file_name in sys.argv[1:]:
    fix_test_suite(data_file_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs: review The pull request is ready for review. This generally means that it has no known issues.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants