iPXE
Functions
asn1_test.c File Reference

ASN.1 self-tests. More...

#include <stdlib.h>
#include <assert.h>
#include <ipxe/image.h>
#include <ipxe/asn1.h>
#include <ipxe/test.h>
#include "asn1_test.h"

Go to the source code of this file.

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.

Detailed Description

ASN.1 self-tests.

Definition in file asn1_test.c.


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 );
}