iPXE
Data Structures | Macros | 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...
 

Macros

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

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. More...
 

Macro Definition Documentation

◆ asn1_test_digest_algorithm

#define asn1_test_digest_algorithm   sha1_algorithm

Digest algorithm used for ASN.1 tests.

Definition at line 12 of file asn1_test.h.

◆ ASN1_TEST_DIGEST_SIZE

#define ASN1_TEST_DIGEST_SIZE   SHA1_DIGEST_SIZE

Digest size used for ASN.1 tests.

Definition at line 15 of file asn1_test.h.

◆ ASN1

#define ASN1 (   _name,
  _type,
  _file,
  ... 
)
Value:
static const char _name ## __file[] = _file; \
static struct image _name ## __image = { \
.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] ) ), \
};
An executable image.
Definition: image.h:24
struct image_type * type
Image type.
Definition: asn1_test.h:26
An ASN.1 test.
Definition: asn1_test.h:24
An ASN.1 test digest.
Definition: asn1_test.h:18
#define REF_INIT(free_fn)
Initialise a static reference counter.
Definition: refcnt.h:77
void ref_no_free(struct refcnt *refcnt __unused)
Do not free reference-counted object.
Definition: refcnt.c:101
unsigned long userptr_t
A pointer to a user buffer.
Definition: uaccess.h:33
struct refcnt refcnt
Reference count.
Definition: image.h:26

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.

◆ asn1_ok

#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.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ asn1_okx()

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.

49  {
51  struct asn1_cursor *cursor;
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 }
uint32_t next
Next descriptor address.
Definition: myson.h:18
const void * data
Start of data.
Definition: asn1.h:22
int image_asn1(struct image *image, size_t offset, struct asn1_cursor **cursor)
Extract ASN.1 object from image.
Definition: asn1.c:880
__be32 out[4]
Definition: CIB_PRM.h:36
size_t len
Length of data.
Definition: asn1.h:24
#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
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static void struct digest_algorithm * digest
HMAC-MD5 digest.
Definition: crypto.h:308
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
int register_image(struct image *image)
Register executable image.
Definition: image.c:267
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
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
unsigned char uint8_t
Definition: stdint.h:10
void unregister_image(struct image *image)
Unregister executable image.
Definition: image.c:303
size_t ctxsize
Context size.
Definition: crypto.h:21
size_t digestsize
Digest size.
Definition: crypto.h:25
userptr_t virt_to_user(volatile const void *addr)
Convert virtual address to user pointer.
A message digest algorithm.
Definition: crypto.h:17
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114
An ASN.1 object cursor.
Definition: asn1.h:20
static int test
Definition: epic100.c:73

References asn1_test_digest_algorithm, ASN1_TEST_DIGEST_SIZE, assert(), ctx, digest_algorithm::ctxsize, asn1_cursor::data, digest, digest_algorithm::digestsize, free, image_asn1(), asn1_cursor::len, memcmp(), next, offset, okx, out, register_image(), test, unregister_image(), and virt_to_user().