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