asn1_internal_test.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* Internal tests for the asn1 module */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <openssl/asn1.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/objects.h>
  15. #include "testutil.h"
  16. #include "internal/nelem.h"
  17. /**********************************************************************
  18. *
  19. * Test of a_strnid's tbl_standard
  20. *
  21. ***/
  22. #include "../crypto/asn1/tbl_standard.h"
  23. static int test_tbl_standard(void)
  24. {
  25. const ASN1_STRING_TABLE *tmp;
  26. int last_nid = -1;
  27. size_t i;
  28. for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++) {
  29. if (tmp->nid < last_nid) {
  30. last_nid = 0;
  31. break;
  32. }
  33. last_nid = tmp->nid;
  34. }
  35. if (TEST_int_ne(last_nid, 0)) {
  36. TEST_info("asn1 tbl_standard: Table order OK");
  37. return 1;
  38. }
  39. TEST_info("asn1 tbl_standard: out of order");
  40. for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++)
  41. TEST_note("asn1 tbl_standard: Index %zu, NID %d, Name=%s",
  42. i, tmp->nid, OBJ_nid2ln(tmp->nid));
  43. return 0;
  44. }
  45. /**********************************************************************
  46. *
  47. * Test of ameth_lib's standard_methods
  48. *
  49. ***/
  50. #include "internal/asn1_int.h"
  51. #include "../crypto/asn1/standard_methods.h"
  52. static int test_standard_methods(void)
  53. {
  54. const EVP_PKEY_ASN1_METHOD **tmp;
  55. int last_pkey_id = -1;
  56. size_t i;
  57. int ok = 1;
  58. for (tmp = standard_methods, i = 0; i < OSSL_NELEM(standard_methods);
  59. i++, tmp++) {
  60. if ((*tmp)->pkey_id < last_pkey_id) {
  61. last_pkey_id = 0;
  62. break;
  63. }
  64. last_pkey_id = (*tmp)->pkey_id;
  65. /*
  66. * One of the following must be true:
  67. *
  68. * pem_str == NULL AND ASN1_PKEY_ALIAS is set
  69. * pem_str != NULL AND ASN1_PKEY_ALIAS is clear
  70. *
  71. * Anything else is an error and may lead to a corrupt ASN1 method table
  72. */
  73. if (!TEST_true(((*tmp)->pem_str == NULL && ((*tmp)->pkey_flags & ASN1_PKEY_ALIAS) != 0)
  74. || ((*tmp)->pem_str != NULL && ((*tmp)->pkey_flags & ASN1_PKEY_ALIAS) == 0))) {
  75. TEST_note("asn1 standard methods: Index %zu, pkey ID %d, Name=%s",
  76. i, (*tmp)->pkey_id, OBJ_nid2sn((*tmp)->pkey_id));
  77. ok = 0;
  78. }
  79. }
  80. if (TEST_int_ne(last_pkey_id, 0)) {
  81. TEST_info("asn1 standard methods: Table order OK");
  82. return ok;
  83. }
  84. TEST_note("asn1 standard methods: out of order");
  85. for (tmp = standard_methods, i = 0; i < OSSL_NELEM(standard_methods);
  86. i++, tmp++)
  87. TEST_note("asn1 standard methods: Index %zu, pkey ID %d, Name=%s",
  88. i, (*tmp)->pkey_id, OBJ_nid2sn((*tmp)->pkey_id));
  89. return 0;
  90. }
  91. int setup_tests(void)
  92. {
  93. ADD_TEST(test_tbl_standard);
  94. ADD_TEST(test_standard_methods);
  95. return 1;
  96. }