2
0

namemap_internal_test.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. #include <openssl/evp.h>
  10. #include "internal/namemap.h"
  11. #include "testutil.h"
  12. #define NAME1 "name1"
  13. #define NAME2 "name2"
  14. #define ALIAS1 "alias1"
  15. #define ALIAS1_UC "ALIAS1"
  16. static int test_namemap(OSSL_NAMEMAP *nm)
  17. {
  18. int num1 = ossl_namemap_add_name(nm, 0, NAME1);
  19. int num2 = ossl_namemap_add_name(nm, 0, NAME2);
  20. int num3 = ossl_namemap_add_name(nm, num1, ALIAS1);
  21. int num4 = ossl_namemap_add_name(nm, 0, ALIAS1_UC);
  22. int check1 = ossl_namemap_name2num(nm, NAME1);
  23. int check2 = ossl_namemap_name2num(nm, NAME2);
  24. int check3 = ossl_namemap_name2num(nm, ALIAS1);
  25. int check4 = ossl_namemap_name2num(nm, ALIAS1_UC);
  26. int false1 = ossl_namemap_name2num(nm, "foo");
  27. return TEST_int_ne(num1, 0)
  28. && TEST_int_ne(num2, 0)
  29. && TEST_int_eq(num1, num3)
  30. && TEST_int_eq(num3, num4)
  31. && TEST_int_eq(num1, check1)
  32. && TEST_int_eq(num2, check2)
  33. && TEST_int_eq(num3, check3)
  34. && TEST_int_eq(num4, check4)
  35. && TEST_int_eq(false1, 0);
  36. }
  37. static int test_namemap_independent(void)
  38. {
  39. OSSL_NAMEMAP *nm = ossl_namemap_new();
  40. int ok = nm != NULL && test_namemap(nm);
  41. ossl_namemap_free(nm);
  42. return ok;
  43. }
  44. static int test_namemap_stored(void)
  45. {
  46. OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL);
  47. return nm != NULL
  48. && test_namemap(nm);
  49. }
  50. /*
  51. * Test that EVP_get_digestbyname() will use the namemap when it can't find
  52. * entries in the legacy method database.
  53. */
  54. static int test_digestbyname(void)
  55. {
  56. int id;
  57. OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL);
  58. const EVP_MD *sha256, *foo;
  59. id = ossl_namemap_add_name(nm, 0, "SHA256");
  60. if (!TEST_int_ne(id, 0))
  61. return 0;
  62. if (!TEST_int_eq(ossl_namemap_add_name(nm, id, "foo"), id))
  63. return 0;
  64. sha256 = EVP_get_digestbyname("SHA256");
  65. if (!TEST_ptr(sha256))
  66. return 0;
  67. foo = EVP_get_digestbyname("foo");
  68. if (!TEST_ptr_eq(sha256, foo))
  69. return 0;
  70. return 1;
  71. }
  72. /*
  73. * Test that EVP_get_cipherbyname() will use the namemap when it can't find
  74. * entries in the legacy method database.
  75. */
  76. static int test_cipherbyname(void)
  77. {
  78. int id;
  79. OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL);
  80. const EVP_CIPHER *aes128, *bar;
  81. id = ossl_namemap_add_name(nm, 0, "AES-128-CBC");
  82. if (!TEST_int_ne(id, 0))
  83. return 0;
  84. if (!TEST_int_eq(ossl_namemap_add_name(nm, id, "bar"), id))
  85. return 0;
  86. aes128 = EVP_get_cipherbyname("AES-128-CBC");
  87. if (!TEST_ptr(aes128))
  88. return 0;
  89. bar = EVP_get_cipherbyname("bar");
  90. if (!TEST_ptr_eq(aes128, bar))
  91. return 0;
  92. return 1;
  93. }
  94. int setup_tests(void)
  95. {
  96. ADD_TEST(test_namemap_independent);
  97. ADD_TEST(test_namemap_stored);
  98. ADD_TEST(test_digestbyname);
  99. ADD_TEST(test_cipherbyname);
  100. return 1;
  101. }