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