• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /*!
18  * \file
19  * \brief Basename management unit tests.
20  */
21 #ifndef SHARED
22 #include "epid/common-testhelper/epid_gtest-testhelper.h"
23 #include "gtest/gtest.h"
24 
25 extern "C" {
26 #include "epid/member/tiny/src/allowed_basenames.h"
27 }
28 
29 namespace {
30 
TEST(AllowedBasenames,GetBasenamesSizeSucceeds)31 TEST(AllowedBasenames, GetBasenamesSizeSucceeds) {
32   size_t bsn_count = 10;
33   size_t expected_bsn_size =
34       sizeof(size_t)                     // current_bsn_number
35       + sizeof(size_t)                   // max_bsn_number
36       + sizeof(sha_digest) * bsn_count;  // digest * max_bsn_number
37   size_t bsn_size = BasenamesGetSize(bsn_count);
38   EXPECT_EQ(expected_bsn_size, bsn_size);
39 }
40 
TEST(AllowedBasenames,ReportsRegisteredBasename)41 TEST(AllowedBasenames, ReportsRegisteredBasename) {
42   const std::vector<uint8_t> bsn0 = {'b', 's', 'n', '0'};
43   const std::vector<uint8_t> bsn1 = {'b', 's', 'n', '1'};
44 
45   AllowedBasenames* base_names = nullptr;
46   std::vector<uint8_t> base_names_buf;
47   size_t bsn_size = BasenamesGetSize(2);
48   base_names_buf.resize(bsn_size);
49   memset(base_names_buf.data(), 1, bsn_size);
50   base_names = (AllowedBasenames*)&base_names_buf[0];
51   InitBasenames(base_names, 2);
52 
53   EXPECT_TRUE(AllowBasename(base_names, bsn0.data(), bsn0.size()));
54   EXPECT_TRUE(AllowBasename(base_names, bsn1.data(), bsn1.size()));
55   EXPECT_TRUE(IsBasenameAllowed(base_names, bsn0.data(), bsn0.size()));
56 }
57 
TEST(AllowedBasenames,ReportsUnregisteredBasename)58 TEST(AllowedBasenames, ReportsUnregisteredBasename) {
59   const std::vector<uint8_t> bsn0 = {'b', 's', 'n', '0'};
60   const std::vector<uint8_t> bsn1 = {'b', 's', 'n', '1'};
61 
62   AllowedBasenames* base_names = nullptr;
63   std::vector<uint8_t> base_names_buf;
64   size_t bsn_size = BasenamesGetSize(1);
65   base_names_buf.resize(bsn_size);
66   base_names = (AllowedBasenames*)&base_names_buf[0];
67   InitBasenames(base_names, 1);
68 
69   EXPECT_TRUE(AllowBasename(base_names, bsn0.data(), bsn0.size()));
70   EXPECT_FALSE(IsBasenameAllowed(base_names, bsn1.data(), bsn1.size()));
71 }
72 
TEST(AllowedBasenames,IsBasenameAllowedReturnsFalseWhenNoBasenamesRegistered)73 TEST(AllowedBasenames, IsBasenameAllowedReturnsFalseWhenNoBasenamesRegistered) {
74   const std::vector<uint8_t> bsn0 = {'b', 's', 'n', '0'};
75 
76   AllowedBasenames* base_names = nullptr;
77   std::vector<uint8_t> base_names_buf;
78   size_t bsn_size = BasenamesGetSize(1);
79   base_names_buf.resize(bsn_size);
80   base_names = (AllowedBasenames*)&base_names_buf[0];
81   InitBasenames(base_names, 1);
82 
83   EXPECT_FALSE(IsBasenameAllowed(base_names, bsn0.data(), bsn0.size()));
84 }
85 
TEST(AllowedBasenames,AllowBasenameReturnsFalseForMoreThanMaxBasenames)86 TEST(AllowedBasenames, AllowBasenameReturnsFalseForMoreThanMaxBasenames) {
87   const std::vector<uint8_t> bsn0 = {'b', 's', 'n', '0'};
88 
89   AllowedBasenames* base_names = nullptr;
90   std::vector<uint8_t> base_names_buf;
91   size_t bsn_size = BasenamesGetSize(5);
92   base_names_buf.resize(bsn_size);
93   base_names = (AllowedBasenames*)&base_names_buf[0];
94   InitBasenames(base_names, 5);
95 
96   for (int n = 0; n < 5; n++) {
97     EXPECT_TRUE(AllowBasename(base_names, &n, sizeof(n)));
98   }
99   // One more basename should not fit into storage
100   EXPECT_FALSE(AllowBasename(base_names, bsn0.data(), bsn0.size()));
101 }
102 
TEST(AllowedBasenames,AllowBasenameCanReturnTrue)103 TEST(AllowedBasenames, AllowBasenameCanReturnTrue) {
104   const std::vector<uint8_t> bsn0 = {'b', 's', 'n', '0'};
105 
106   AllowedBasenames* base_names = nullptr;
107   std::vector<uint8_t> base_names_buf;
108   size_t bsn_size = BasenamesGetSize(1);
109   base_names_buf.resize(bsn_size);
110   base_names = (AllowedBasenames*)&base_names_buf[0];
111   InitBasenames(base_names, 1);
112 
113   EXPECT_TRUE(AllowBasename(base_names, bsn0.data(), bsn0.size()));
114 }
115 
116 }  // namespace
117 #endif  // SHARED
118