iPXE
asn1_test.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2016 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * ASN.1 self-tests
00029  *
00030  */
00031 
00032 /* Forcibly enable assertions */
00033 #undef NDEBUG
00034 
00035 #include <stdlib.h>
00036 #include <assert.h>
00037 #include <ipxe/image.h>
00038 #include <ipxe/asn1.h>
00039 #include <ipxe/test.h>
00040 #include "asn1_test.h"
00041 
00042 /**
00043  * Report ASN.1 test result
00044  *
00045  * @v test              ASN.1 test
00046  * @v file              Test code file
00047  * @v line              Test code line
00048  */
00049 void asn1_okx ( struct asn1_test *test, const char *file, unsigned int line ) {
00050         struct digest_algorithm *digest = &asn1_test_digest_algorithm;
00051         struct asn1_cursor *cursor;
00052         uint8_t ctx[digest->ctxsize];
00053         uint8_t out[ASN1_TEST_DIGEST_SIZE];
00054         unsigned int i;
00055         size_t offset;
00056         int next;
00057 
00058         /* Sanity check */
00059         assert ( sizeof ( out ) == digest->digestsize );
00060 
00061         /* Correct image data pointer */
00062         test->image->data = virt_to_user ( ( void * ) test->image->data );
00063 
00064         /* Check that image is detected as correct type */
00065         okx ( register_image ( test->image ) == 0, file, line );
00066         okx ( test->image->type == test->type, file, line );
00067 
00068         /* Check that all ASN.1 objects can be extracted */
00069         for ( offset = 0, i = 0 ; i < test->count ; offset = next, i++ ) {
00070 
00071                 /* Extract ASN.1 object */
00072                 next = image_asn1 ( test->image, offset, &cursor );
00073                 okx ( next >= 0, file, line );
00074                 okx ( ( ( size_t ) next ) > offset, file, line );
00075                 if ( next > 0 ) {
00076 
00077                         /* Calculate digest of ASN.1 object */
00078                         digest_init ( digest, ctx );
00079                         digest_update ( digest, ctx, cursor->data,
00080                                         cursor->len );
00081                         digest_final ( digest, ctx, out );
00082 
00083                         /* Compare against expected digest */
00084                         okx ( memcmp ( out, test->expected[i].digest,
00085                                        sizeof ( out ) ) == 0, file, line );
00086 
00087                         /* Free ASN.1 object */
00088                         free ( cursor );
00089                 }
00090         }
00091 
00092         /* Check that we have reached the end of the image */
00093         okx ( offset == test->image->len, file, line );
00094 
00095         /* Unregister image */
00096         unregister_image ( test->image );
00097 }