iPXE
Data Structures | Defines | Functions
asn1_test.h File Reference
#include <stdint.h>
#include <ipxe/image.h>
#include <ipxe/sha1.h>
#include <ipxe/test.h>

Go to the source code of this file.

Data Structures

struct  asn1_test_digest
 An ASN.1 test digest. More...
struct  asn1_test
 An ASN.1 test. More...

Defines

#define asn1_test_digest_algorithm   sha1_algorithm
 Digest algorithm used for ASN.1 tests.
#define ASN1_TEST_DIGEST_SIZE   SHA1_DIGEST_SIZE
 Digest size used for ASN.1 tests.
#define ASN1(_name, _type, _file,...)
 Define an ASN.1 test.
#define asn1_ok(test)   asn1_okx ( test, __FILE__, __LINE__ )
 Report ASN.1 test result.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
void asn1_okx (struct asn1_test *test, const char *file, unsigned int line)
 Report ASN.1 test result.

Define Documentation

Digest algorithm used for ASN.1 tests.

Definition at line 12 of file asn1_test.h.

Referenced by asn1_okx().

Digest size used for ASN.1 tests.

Definition at line 15 of file asn1_test.h.

Referenced by asn1_okx().

#define ASN1 (   _name,
  _type,
  _file,
  ... 
)
Value:
static const char _name ## __file[] = _file;                    \
        static struct image _name ## __image = {                        \
                .refcnt = REF_INIT ( ref_no_free ),                     \
                .name = #_name,                                         \
                .data = ( userptr_t ) ( _name ## __file ),              \
                .len = sizeof ( _name ## __file ),                      \
        };                                                              \
        static struct asn1_test_digest _name ## _expected[] = {         \
                __VA_ARGS__                                             \
        };                                                              \
        static struct asn1_test _name = {                               \
                .type = _type,                                          \
                .image = & _name ## __image,                            \
                .expected = _name ## _expected,                         \
                .count = ( sizeof ( _name ## _expected ) /              \
                           sizeof ( _name ## _expected[0] ) ),          \
        };

Define an ASN.1 test.

Parameters:
_nameTest name
_typeTest image file type
_fileTest image file data
...Expected ASN.1 object digests
Return values:
testASN.1 test

Definition at line 44 of file asn1_test.h.

#define asn1_ok (   test)    asn1_okx ( test, __FILE__, __LINE__ )

Report ASN.1 test result.

Parameters:
testASN.1 test

Definition at line 71 of file asn1_test.h.

Referenced by der_test_exec(), and pem_test_exec().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
void asn1_okx ( struct asn1_test test,
const char *  file,
unsigned int  line 
)

Report ASN.1 test result.

Parameters:
testASN.1 test
fileTest code file
lineTest code line

Definition at line 49 of file asn1_test.c.

References asn1_test_digest_algorithm, ASN1_TEST_DIGEST_SIZE, assert, asn1_test::count, ctx, digest_algorithm::ctxsize, asn1_cursor::data, image::data, asn1_test_digest::digest, digest, digest_final(), digest_init(), digest_update(), digest_algorithm::digestsize, asn1_test::expected, free, asn1_test::image, image_asn1(), asn1_cursor::len, image::len, memcmp(), next, offset, okx, out, register_image(), asn1_test::type, image::type, unregister_image(), and virt_to_user().

                                                                              {
        struct digest_algorithm *digest = &asn1_test_digest_algorithm;
        struct asn1_cursor *cursor;
        uint8_t ctx[digest->ctxsize];
        uint8_t out[ASN1_TEST_DIGEST_SIZE];
        unsigned int i;
        size_t offset;
        int next;

        /* Sanity check */
        assert ( sizeof ( out ) == digest->digestsize );

        /* Correct image data pointer */
        test->image->data = virt_to_user ( ( void * ) test->image->data );

        /* Check that image is detected as correct type */
        okx ( register_image ( test->image ) == 0, file, line );
        okx ( test->image->type == test->type, file, line );

        /* Check that all ASN.1 objects can be extracted */
        for ( offset = 0, i = 0 ; i < test->count ; offset = next, i++ ) {

                /* Extract ASN.1 object */
                next = image_asn1 ( test->image, offset, &cursor );
                okx ( next >= 0, file, line );
                okx ( ( ( size_t ) next ) > offset, file, line );
                if ( next > 0 ) {

                        /* Calculate digest of ASN.1 object */
                        digest_init ( digest, ctx );
                        digest_update ( digest, ctx, cursor->data,
                                        cursor->len );
                        digest_final ( digest, ctx, out );

                        /* Compare against expected digest */
                        okx ( memcmp ( out, test->expected[i].digest,
                                       sizeof ( out ) ) == 0, file, line );

                        /* Free ASN.1 object */
                        free ( cursor );
                }
        }

        /* Check that we have reached the end of the image */
        okx ( offset == test->image->len, file, line );

        /* Unregister image */
        unregister_image ( test->image );
}