Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1 | #include "nlohmann/json.hpp" |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 2 | #include "privileges.hpp" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 3 | |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 4 | #include <fstream> |
| 5 | #include <string> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 6 | |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 7 | #include "gmock/gmock.h" |
| 8 | |
Nan Zhou | 38ead5e | 2022-07-03 23:07:27 +0000 | [diff] [blame] | 9 | namespace redfish |
| 10 | { |
| 11 | namespace |
| 12 | { |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 13 | |
Nan Zhou | b5a10a2 | 2022-07-04 01:18:14 +0000 | [diff] [blame] | 14 | using ::testing::IsEmpty; |
| 15 | using ::testing::UnorderedElementsAre; |
| 16 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 17 | TEST(PrivilegeTest, PrivilegeConstructor) |
| 18 | { |
| 19 | Privileges privileges{"Login", "ConfigureManager"}; |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 20 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 21 | EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE), |
Nan Zhou | b5a10a2 | 2022-07-04 01:18:14 +0000 | [diff] [blame] | 22 | UnorderedElementsAre("Login", "ConfigureManager")); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 23 | } |
| 24 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 25 | TEST(PrivilegeTest, PrivilegeCheckForNoPrivilegesRequired) |
| 26 | { |
| 27 | Privileges userPrivileges{"Login"}; |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 28 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 29 | OperationMap entityPrivileges{{boost::beast::http::verb::get, {{"Login"}}}}; |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 30 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 31 | EXPECT_TRUE(isMethodAllowedWithPrivileges( |
| 32 | boost::beast::http::verb::get, entityPrivileges, userPrivileges)); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 33 | } |
| 34 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 35 | TEST(PrivilegeTest, PrivilegeCheckForSingleCaseSuccess) |
| 36 | { |
| 37 | auto userPrivileges = Privileges{"Login"}; |
| 38 | OperationMap entityPrivileges{{boost::beast::http::verb::get, {}}}; |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 39 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 40 | EXPECT_TRUE(isMethodAllowedWithPrivileges( |
| 41 | boost::beast::http::verb::get, entityPrivileges, userPrivileges)); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 42 | } |
| 43 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 44 | TEST(PrivilegeTest, PrivilegeCheckForSingleCaseFailure) |
| 45 | { |
| 46 | auto userPrivileges = Privileges{"Login"}; |
| 47 | OperationMap entityPrivileges{ |
| 48 | {boost::beast::http::verb::get, {{"ConfigureManager"}}}}; |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 49 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 50 | EXPECT_FALSE(isMethodAllowedWithPrivileges( |
| 51 | boost::beast::http::verb::get, entityPrivileges, userPrivileges)); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 54 | TEST(PrivilegeTest, PrivilegeCheckForANDCaseSuccess) |
| 55 | { |
| 56 | auto userPrivileges = |
| 57 | Privileges{"Login", "ConfigureManager", "ConfigureSelf"}; |
| 58 | OperationMap entityPrivileges{ |
| 59 | {boost::beast::http::verb::get, |
| 60 | {{"Login", "ConfigureManager", "ConfigureSelf"}}}}; |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 61 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 62 | EXPECT_TRUE(isMethodAllowedWithPrivileges( |
| 63 | boost::beast::http::verb::get, entityPrivileges, userPrivileges)); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 66 | TEST(PrivilegeTest, PrivilegeCheckForANDCaseFailure) |
| 67 | { |
| 68 | auto userPrivileges = Privileges{"Login", "ConfigureManager"}; |
| 69 | OperationMap entityPrivileges{ |
| 70 | {boost::beast::http::verb::get, |
| 71 | {{"Login", "ConfigureManager", "ConfigureSelf"}}}}; |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 72 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 73 | EXPECT_FALSE(isMethodAllowedWithPrivileges( |
| 74 | boost::beast::http::verb::get, entityPrivileges, userPrivileges)); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 77 | TEST(PrivilegeTest, PrivilegeCheckForORCaseSuccess) |
| 78 | { |
| 79 | auto userPrivileges = Privileges{"ConfigureManager"}; |
| 80 | OperationMap entityPrivileges{ |
| 81 | {boost::beast::http::verb::get, {{"Login"}, {"ConfigureManager"}}}}; |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 82 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 83 | EXPECT_TRUE(isMethodAllowedWithPrivileges( |
| 84 | boost::beast::http::verb::get, entityPrivileges, userPrivileges)); |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 87 | TEST(PrivilegeTest, PrivilegeCheckForORCaseFailure) |
| 88 | { |
| 89 | auto userPrivileges = Privileges{"ConfigureComponents"}; |
| 90 | OperationMap entityPrivileges = OperationMap( |
| 91 | {{boost::beast::http::verb::get, {{"Login"}, {"ConfigureManager"}}}}); |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 92 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 93 | EXPECT_FALSE(isMethodAllowedWithPrivileges( |
| 94 | boost::beast::http::verb::get, entityPrivileges, userPrivileges)); |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 97 | TEST(PrivilegeTest, DefaultPrivilegeBitsetsAreEmpty) |
| 98 | { |
| 99 | Privileges privileges; |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 100 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 101 | EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE), |
Nan Zhou | b5a10a2 | 2022-07-04 01:18:14 +0000 | [diff] [blame] | 102 | IsEmpty()); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 103 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 104 | EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::OEM), |
Nan Zhou | b5a10a2 | 2022-07-04 01:18:14 +0000 | [diff] [blame] | 105 | IsEmpty()); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 108 | TEST(PrivilegeTest, GetActivePrivilegeNames) |
| 109 | { |
| 110 | Privileges privileges; |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 111 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 112 | EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE), |
Nan Zhou | b5a10a2 | 2022-07-04 01:18:14 +0000 | [diff] [blame] | 113 | IsEmpty()); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 114 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 115 | std::array<const char*, 5> expectedPrivileges{ |
| 116 | "Login", "ConfigureManager", "ConfigureUsers", "ConfigureComponents", |
| 117 | "ConfigureSelf"}; |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 118 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 119 | for (const auto& privilege : expectedPrivileges) |
| 120 | { |
| 121 | EXPECT_TRUE(privileges.setSinglePrivilege(privilege)); |
| 122 | } |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 123 | |
Nan Zhou | b5a10a2 | 2022-07-04 01:18:14 +0000 | [diff] [blame] | 124 | EXPECT_THAT( |
| 125 | privileges.getActivePrivilegeNames(PrivilegeType::BASE), |
| 126 | UnorderedElementsAre(expectedPrivileges[0], expectedPrivileges[1], |
| 127 | expectedPrivileges[2], expectedPrivileges[3], |
| 128 | expectedPrivileges[4])); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 129 | } |
Nan Zhou | 38ead5e | 2022-07-03 23:07:27 +0000 | [diff] [blame] | 130 | } // namespace |
| 131 | } // namespace redfish |