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