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) { |
| 10 | Privileges privileges = {"Login", "ConfigureManager"}; |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 11 | |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame^] | 12 | auto activePrivileges = |
| 13 | privileges.getActivePrivilegeNames(PrivilegeType::BASE); |
| 14 | std::vector<std::string> expectedPrivileges{"Login", "ConfigureManager"}; |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 15 | |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame^] | 16 | std::sort(expectedPrivileges.begin(), expectedPrivileges.end()); |
| 17 | std::sort(activePrivileges.begin(), activePrivileges.end()); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 18 | |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame^] | 19 | EXPECT_EQ(expectedPrivileges, activePrivileges); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 20 | } |
| 21 | |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame^] | 22 | TEST(PrivilegeTest, PrivilegeCheckForNoPrivilegesRequired) { |
| 23 | auto userPrivileges = Privileges{"Login"}; |
| 24 | OperationMap operationMap = {{crow::HTTPMethod::GET, {{}}}}; |
| 25 | auto entityPrivileges = EntityPrivileges(std::move(operationMap)); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 26 | |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 27 | EXPECT_TRUE(entityPrivileges.isMethodAllowedWithPrivileges( |
| 28 | crow::HTTPMethod::GET, userPrivileges)); |
| 29 | } |
| 30 | |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame^] | 31 | TEST(PrivilegeTest, PrivilegeCheckForSingleCaseSuccess) { |
| 32 | auto userPrivileges = Privileges{"Login"}; |
| 33 | OperationMap operationMap = {{crow::HTTPMethod::GET, {{"Login"}}}}; |
| 34 | auto entityPrivileges = EntityPrivileges(std::move(operationMap)); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 35 | |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 36 | EXPECT_TRUE(entityPrivileges.isMethodAllowedWithPrivileges( |
| 37 | crow::HTTPMethod::GET, userPrivileges)); |
| 38 | } |
| 39 | |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame^] | 40 | TEST(PrivilegeTest, PrivilegeCheckForSingleCaseFailure) { |
| 41 | auto userPrivileges = Privileges{"Login"}; |
| 42 | OperationMap operationMap = {{crow::HTTPMethod::GET, {{"ConfigureManager"}}}}; |
| 43 | auto entityPrivileges = EntityPrivileges(std::move(operationMap)); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 44 | |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 45 | EXPECT_FALSE(entityPrivileges.isMethodAllowedWithPrivileges( |
| 46 | crow::HTTPMethod::GET, userPrivileges)); |
| 47 | } |
| 48 | |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame^] | 49 | TEST(PrivilegeTest, PrivilegeCheckForANDCaseSuccess) { |
| 50 | auto userPrivileges = |
| 51 | Privileges{"Login", "ConfigureManager", "ConfigureSelf"}; |
| 52 | OperationMap operationMap = { |
| 53 | {crow::HTTPMethod::GET, |
| 54 | {{"Login", "ConfigureManager", "ConfigureSelf"}}}}; |
| 55 | auto entityPrivileges = EntityPrivileges(std::move(operationMap)); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 56 | |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 57 | EXPECT_TRUE(entityPrivileges.isMethodAllowedWithPrivileges( |
| 58 | crow::HTTPMethod::GET, userPrivileges)); |
| 59 | } |
| 60 | |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame^] | 61 | TEST(PrivilegeTest, PrivilegeCheckForANDCaseFailure) { |
| 62 | auto userPrivileges = Privileges{"Login", "ConfigureManager"}; |
| 63 | OperationMap operationMap = { |
| 64 | {crow::HTTPMethod::GET, |
| 65 | {{"Login", "ConfigureManager", "ConfigureSelf"}}}}; |
| 66 | auto entityPrivileges = EntityPrivileges(std::move(operationMap)); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 67 | |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 68 | EXPECT_FALSE(entityPrivileges.isMethodAllowedWithPrivileges( |
| 69 | crow::HTTPMethod::GET, userPrivileges)); |
| 70 | } |
| 71 | |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame^] | 72 | TEST(PrivilegeTest, PrivilegeCheckForORCaseSuccess) { |
| 73 | auto userPrivileges = Privileges{"ConfigureManager"}; |
| 74 | OperationMap operationMap = { |
| 75 | {crow::HTTPMethod::GET, {{"Login"}, {"ConfigureManager"}}}}; |
| 76 | auto entityPrivileges = EntityPrivileges(std::move(operationMap)); |
| 77 | |
| 78 | EXPECT_TRUE(entityPrivileges.isMethodAllowedWithPrivileges( |
| 79 | crow::HTTPMethod::GET, userPrivileges)); |
| 80 | } |
| 81 | |
| 82 | TEST(PrivilegeTest, PrivilegeCheckForORCaseFailure) { |
| 83 | auto userPrivileges = Privileges{"ConfigureComponents"}; |
| 84 | OperationMap operationMap = { |
| 85 | {crow::HTTPMethod::GET, {{"Login"}, {"ConfigureManager"}}}}; |
| 86 | auto entityPrivileges = EntityPrivileges(std::move(operationMap)); |
| 87 | |
| 88 | EXPECT_FALSE(entityPrivileges.isMethodAllowedWithPrivileges( |
| 89 | crow::HTTPMethod::GET, userPrivileges)); |
| 90 | } |
| 91 | |
| 92 | TEST(PrivilegeTest, DefaultPrivilegeBitsetsAreEmpty) { |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 93 | Privileges privileges; |
| 94 | EXPECT_TRUE(privileges.getBasePrivilegeBitset() == 0); |
| 95 | EXPECT_TRUE(privileges.getOEMPrivilegeBitset() == 0); |
| 96 | } |
| 97 | |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame^] | 98 | TEST(PrivilegeTest, UniqueBitsAssignedForAllPrivilegeNames) { |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 99 | Privileges privileges; |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame^] | 100 | std::vector<std::string> expectedPrivileges{ |
| 101 | "Login", "ConfigureManager", "ConfigureUsers", "ConfigureComponents", |
| 102 | "ConfigureSelf"}; |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 103 | |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame^] | 104 | for (const auto& privilege : expectedPrivileges) { |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 105 | privileges.setSinglePrivilege(privilege); |
| 106 | } |
| 107 | |
| 108 | EXPECT_EQ(privileges.getBasePrivilegeBitset().count(), |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame^] | 109 | expectedPrivileges.size()); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame^] | 112 | TEST(PrivilegeTest, GetActivePrivilegeNames) { |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 113 | Privileges privileges; |
| 114 | |
| 115 | EXPECT_EQ(privileges.getActivePrivilegeNames(PrivilegeType::BASE), |
| 116 | std::vector<std::string>()); |
| 117 | |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame^] | 118 | std::vector<std::string> expectedPrivileges{ |
| 119 | "Login", "ConfigureManager", "ConfigureUsers", "ConfigureComponents", |
| 120 | "ConfigureSelf"}; |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 121 | |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame^] | 122 | for (const auto& privilege : expectedPrivileges) { |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 123 | privileges.setSinglePrivilege(privilege); |
| 124 | } |
| 125 | |
| 126 | std::vector<std::string> activePrivileges = |
| 127 | privileges.getActivePrivilegeNames(PrivilegeType::BASE); |
| 128 | |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame^] | 129 | std::sort(expectedPrivileges.begin(), expectedPrivileges.end()); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 130 | std::sort(activePrivileges.begin(), activePrivileges.end()); |
| 131 | |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame^] | 132 | EXPECT_EQ(activePrivileges, expectedPrivileges); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame^] | 135 | TEST(PrivilegeTest, PropertyOverrideConstructor) { |
| 136 | OperationMap operationMap = { |
| 137 | {crow::HTTPMethod::GET, {{"Login"}, {"ConfigureManager"}}}}; |
| 138 | PropertyOverride propertyOverride(std::move(operationMap), |
| 139 | {"Password", "Id"}); |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 140 | } |