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