allowed_basenames-test.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*############################################################################
  2. # Copyright 2017 Intel Corporation
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################*/
  16. /*!
  17. * \file
  18. * \brief Basename management unit tests.
  19. */
  20. #ifndef SHARED
  21. #include "epid/common-testhelper/epid_gtest-testhelper.h"
  22. #include "gtest/gtest.h"
  23. extern "C" {
  24. #include "epid/member/tiny/src/allowed_basenames.h"
  25. }
  26. namespace {
  27. TEST(AllowedBasenames, GetBasenamesSizeSucceeds) {
  28. size_t bsn_count = 10;
  29. size_t expected_bsn_size =
  30. sizeof(size_t) // current_bsn_number
  31. + sizeof(size_t) // max_bsn_number
  32. + sizeof(sha_digest) * bsn_count; // digest * max_bsn_number
  33. size_t bsn_size = BasenamesGetSize(bsn_count);
  34. EXPECT_EQ(expected_bsn_size, bsn_size);
  35. }
  36. TEST(AllowedBasenames, ReportsRegisteredBasename) {
  37. const std::vector<uint8_t> bsn0 = {'b', 's', 'n', '0'};
  38. const std::vector<uint8_t> bsn1 = {'b', 's', 'n', '1'};
  39. AllowedBasenames* base_names = nullptr;
  40. std::vector<uint8_t> base_names_buf;
  41. size_t bsn_size = BasenamesGetSize(2);
  42. base_names_buf.resize(bsn_size);
  43. memset(base_names_buf.data(), 1, bsn_size);
  44. base_names = (AllowedBasenames*)&base_names_buf[0];
  45. InitBasenames(base_names, 2);
  46. EXPECT_TRUE(AllowBasename(base_names, bsn0.data(), bsn0.size()));
  47. EXPECT_TRUE(AllowBasename(base_names, bsn1.data(), bsn1.size()));
  48. EXPECT_TRUE(IsBasenameAllowed(base_names, bsn0.data(), bsn0.size()));
  49. }
  50. TEST(AllowedBasenames, ReportsUnregisteredBasename) {
  51. const std::vector<uint8_t> bsn0 = {'b', 's', 'n', '0'};
  52. const std::vector<uint8_t> bsn1 = {'b', 's', 'n', '1'};
  53. AllowedBasenames* base_names = nullptr;
  54. std::vector<uint8_t> base_names_buf;
  55. size_t bsn_size = BasenamesGetSize(1);
  56. base_names_buf.resize(bsn_size);
  57. base_names = (AllowedBasenames*)&base_names_buf[0];
  58. InitBasenames(base_names, 1);
  59. EXPECT_TRUE(AllowBasename(base_names, bsn0.data(), bsn0.size()));
  60. EXPECT_FALSE(IsBasenameAllowed(base_names, bsn1.data(), bsn1.size()));
  61. }
  62. TEST(AllowedBasenames, IsBasenameAllowedReturnsFalseWhenNoBasenamesRegistered) {
  63. const std::vector<uint8_t> bsn0 = {'b', 's', 'n', '0'};
  64. AllowedBasenames* base_names = nullptr;
  65. std::vector<uint8_t> base_names_buf;
  66. size_t bsn_size = BasenamesGetSize(1);
  67. base_names_buf.resize(bsn_size);
  68. base_names = (AllowedBasenames*)&base_names_buf[0];
  69. InitBasenames(base_names, 1);
  70. EXPECT_FALSE(IsBasenameAllowed(base_names, bsn0.data(), bsn0.size()));
  71. }
  72. TEST(AllowedBasenames, AllowBasenameReturnsFalseForMoreThanMaxBasenames) {
  73. const std::vector<uint8_t> bsn0 = {'b', 's', 'n', '0'};
  74. AllowedBasenames* base_names = nullptr;
  75. std::vector<uint8_t> base_names_buf;
  76. size_t bsn_size = BasenamesGetSize(5);
  77. base_names_buf.resize(bsn_size);
  78. base_names = (AllowedBasenames*)&base_names_buf[0];
  79. InitBasenames(base_names, 5);
  80. for (int n = 0; n < 5; n++) {
  81. EXPECT_TRUE(AllowBasename(base_names, &n, sizeof(n)));
  82. }
  83. // One more basename should not fit into storage
  84. EXPECT_FALSE(AllowBasename(base_names, bsn0.data(), bsn0.size()));
  85. }
  86. TEST(AllowedBasenames, AllowBasenameCanReturnTrue) {
  87. const std::vector<uint8_t> bsn0 = {'b', 's', 'n', '0'};
  88. AllowedBasenames* base_names = nullptr;
  89. std::vector<uint8_t> base_names_buf;
  90. size_t bsn_size = BasenamesGetSize(1);
  91. base_names_buf.resize(bsn_size);
  92. base_names = (AllowedBasenames*)&base_names_buf[0];
  93. InitBasenames(base_names, 1);
  94. EXPECT_TRUE(AllowBasename(base_names, bsn0.data(), bsn0.size()));
  95. }
  96. } // namespace
  97. #endif // SHARED