You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

asn1_test.c 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * ASN.1 self-tests
  27. *
  28. */
  29. /* Forcibly enable assertions */
  30. #undef NDEBUG
  31. #include <stdlib.h>
  32. #include <assert.h>
  33. #include <ipxe/image.h>
  34. #include <ipxe/asn1.h>
  35. #include <ipxe/test.h>
  36. #include "asn1_test.h"
  37. /**
  38. * Report ASN.1 test result
  39. *
  40. * @v test ASN.1 test
  41. * @v file Test code file
  42. * @v line Test code line
  43. */
  44. void asn1_okx ( struct asn1_test *test, const char *file, unsigned int line ) {
  45. struct digest_algorithm *digest = &asn1_test_digest_algorithm;
  46. struct asn1_cursor *cursor;
  47. uint8_t ctx[digest->ctxsize];
  48. uint8_t out[ASN1_TEST_DIGEST_SIZE];
  49. unsigned int i;
  50. size_t offset;
  51. int next;
  52. /* Sanity check */
  53. assert ( sizeof ( out ) == digest->digestsize );
  54. /* Correct image data pointer */
  55. test->image->data = virt_to_user ( ( void * ) test->image->data );
  56. /* Check that image is detected as correct type */
  57. okx ( register_image ( test->image ) == 0, file, line );
  58. okx ( test->image->type == test->type, file, line );
  59. /* Check that all ASN.1 objects can be extracted */
  60. for ( offset = 0, i = 0 ; i < test->count ; offset = next, i++ ) {
  61. /* Extract ASN.1 object */
  62. next = image_asn1 ( test->image, offset, &cursor );
  63. okx ( next >= 0, file, line );
  64. okx ( ( ( size_t ) next ) > offset, file, line );
  65. if ( next > 0 ) {
  66. /* Calculate digest of ASN.1 object */
  67. digest_init ( digest, ctx );
  68. digest_update ( digest, ctx, cursor->data,
  69. cursor->len );
  70. digest_final ( digest, ctx, out );
  71. /* Compare against expected digest */
  72. okx ( memcmp ( out, test->expected[i].digest,
  73. sizeof ( out ) ) == 0, file, line );
  74. /* Free ASN.1 object */
  75. free ( cursor );
  76. }
  77. }
  78. /* Check that we have reached the end of the image */
  79. okx ( offset == test->image->len, file, line );
  80. /* Unregister image */
  81. unregister_image ( test->image );
  82. }