iPXE
asn1_test.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /** @file
27  *
28  * ASN.1 self-tests
29  *
30  */
31 
32 /* Forcibly enable assertions */
33 #undef NDEBUG
34 
35 #include <stdlib.h>
36 #include <assert.h>
37 #include <ipxe/image.h>
38 #include <ipxe/asn1.h>
39 #include <ipxe/test.h>
40 #include "asn1_test.h"
41 
42 /**
43  * Report ASN.1 test result
44  *
45  * @v test ASN.1 test
46  * @v file Test code file
47  * @v line Test code line
48  */
49 void asn1_okx ( struct asn1_test *test, const char *file, unsigned int line ) {
51  struct asn1_cursor *cursor;
52  uint8_t ctx[digest->ctxsize];
54  unsigned int i;
55  size_t offset;
56  int next;
57 
58  /* Sanity check */
59  assert ( sizeof ( out ) == digest->digestsize );
60 
61  /* Correct image data pointer */
62  test->image->data = virt_to_user ( ( void * ) test->image->data );
63 
64  /* Check that image is detected as correct type */
65  okx ( register_image ( test->image ) == 0, file, line );
66  okx ( test->image->type == test->type, file, line );
67 
68  /* Check that all ASN.1 objects can be extracted */
69  for ( offset = 0, i = 0 ; i < test->count ; offset = next, i++ ) {
70 
71  /* Extract ASN.1 object */
72  next = image_asn1 ( test->image, offset, &cursor );
73  okx ( next >= 0, file, line );
74  okx ( ( ( size_t ) next ) > offset, file, line );
75  if ( next > 0 ) {
76 
77  /* Calculate digest of ASN.1 object */
78  digest_init ( digest, ctx );
79  digest_update ( digest, ctx, cursor->data,
80  cursor->len );
81  digest_final ( digest, ctx, out );
82 
83  /* Compare against expected digest */
84  okx ( memcmp ( out, test->expected[i].digest,
85  sizeof ( out ) ) == 0, file, line );
86 
87  /* Free ASN.1 object */
88  free ( cursor );
89  }
90  }
91 
92  /* Check that we have reached the end of the image */
93  okx ( offset == test->image->len, file, line );
94 
95  /* Unregister image */
96  unregister_image ( test->image );
97 }
static void digest_update(struct digest_algorithm *digest, void *ctx, const void *data, size_t len)
Definition: crypto.h:177
uint32_t next
Next descriptor address.
Definition: myson.h:18
void asn1_okx(struct asn1_test *test, const char *file, unsigned int line)
Report ASN.1 test result.
Definition: asn1_test.c:49
static void digest_final(struct digest_algorithm *digest, void *ctx, void *out)
Definition: crypto.h:182
const void * data
Start of data.
Definition: asn1.h:21
Self-test infrastructure.
int image_asn1(struct image *image, size_t offset, struct asn1_cursor **cursor)
Extract ASN.1 object from image.
Definition: asn1.c:854
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
struct md4_digest digest
Digest of data already processed.
Definition: md4.h:12
size_t len
Length of data.
Definition: asn1.h:23
#define ASN1_TEST_DIGEST_SIZE
Digest size used for ASN.1 tests.
Definition: asn1_test.h:15
#define okx(success, file, line)
Report test result.
Definition: test.h:44
Assertions.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
Executable images.
ASN.1 encoding.
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
__be32 out[4]
Definition: CIB_PRM.h:36
static void digest_init(struct digest_algorithm *digest, void *ctx)
Definition: crypto.h:172
int register_image(struct image *image)
Register executable image.
Definition: image.c:210
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
#define asn1_test_digest_algorithm
Digest algorithm used for ASN.1 tests.
Definition: asn1_test.h:12
An ASN.1 test.
Definition: asn1_test.h:24
unsigned char uint8_t
Definition: stdint.h:10
void unregister_image(struct image *image)
Unregister executable image.
Definition: image.c:252
userptr_t virt_to_user(volatile const void *addr)
Convert virtual address to user pointer.
A message digest algorithm.
Definition: crypto.h:16
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:98
An ASN.1 object cursor.
Definition: asn1.h:19
static int test
Definition: epic100.c:73