blob: 193fd7d93e9b7ffd4b1f5957665aed429702db00 [file] [log] [blame]
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +01001#include "privileges.hpp"
2#include <fstream>
3#include <string>
4#include "nlohmann/json.hpp"
5#include "gmock/gmock.h"
6
7using namespace redfish;
8
Borawski.Lukasz43a095a2018-02-19 15:39:01 +01009TEST(PrivilegeTest, PrivilegeConstructor) {
10 Privileges privileges = {"Login", "ConfigureManager"};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010011
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010012 auto activePrivileges =
13 privileges.getActivePrivilegeNames(PrivilegeType::BASE);
14 std::vector<std::string> expectedPrivileges{"Login", "ConfigureManager"};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010015
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010016 std::sort(expectedPrivileges.begin(), expectedPrivileges.end());
17 std::sort(activePrivileges.begin(), activePrivileges.end());
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010018
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010019 EXPECT_EQ(expectedPrivileges, activePrivileges);
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010020}
21
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010022TEST(PrivilegeTest, PrivilegeCheckForNoPrivilegesRequired) {
23 auto userPrivileges = Privileges{"Login"};
24 OperationMap operationMap = {{crow::HTTPMethod::GET, {{}}}};
25 auto entityPrivileges = EntityPrivileges(std::move(operationMap));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010026
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010027 EXPECT_TRUE(entityPrivileges.isMethodAllowedWithPrivileges(
28 crow::HTTPMethod::GET, userPrivileges));
29}
30
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010031TEST(PrivilegeTest, PrivilegeCheckForSingleCaseSuccess) {
32 auto userPrivileges = Privileges{"Login"};
33 OperationMap operationMap = {{crow::HTTPMethod::GET, {{"Login"}}}};
34 auto entityPrivileges = EntityPrivileges(std::move(operationMap));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010035
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010036 EXPECT_TRUE(entityPrivileges.isMethodAllowedWithPrivileges(
37 crow::HTTPMethod::GET, userPrivileges));
38}
39
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010040TEST(PrivilegeTest, PrivilegeCheckForSingleCaseFailure) {
41 auto userPrivileges = Privileges{"Login"};
42 OperationMap operationMap = {{crow::HTTPMethod::GET, {{"ConfigureManager"}}}};
43 auto entityPrivileges = EntityPrivileges(std::move(operationMap));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010044
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010045 EXPECT_FALSE(entityPrivileges.isMethodAllowedWithPrivileges(
46 crow::HTTPMethod::GET, userPrivileges));
47}
48
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010049TEST(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.Lukaszaecb47a2018-01-25 12:14:14 +010056
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010057 EXPECT_TRUE(entityPrivileges.isMethodAllowedWithPrivileges(
58 crow::HTTPMethod::GET, userPrivileges));
59}
60
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010061TEST(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.Lukaszaecb47a2018-01-25 12:14:14 +010067
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010068 EXPECT_FALSE(entityPrivileges.isMethodAllowedWithPrivileges(
69 crow::HTTPMethod::GET, userPrivileges));
70}
71
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010072TEST(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
82TEST(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
92TEST(PrivilegeTest, DefaultPrivilegeBitsetsAreEmpty) {
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010093 Privileges privileges;
94 EXPECT_TRUE(privileges.getBasePrivilegeBitset() == 0);
95 EXPECT_TRUE(privileges.getOEMPrivilegeBitset() == 0);
96}
97
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010098TEST(PrivilegeTest, UniqueBitsAssignedForAllPrivilegeNames) {
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010099 Privileges privileges;
Borawski.Lukasz43a095a2018-02-19 15:39:01 +0100100 std::vector<std::string> expectedPrivileges{
101 "Login", "ConfigureManager", "ConfigureUsers", "ConfigureComponents",
102 "ConfigureSelf"};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100103
Borawski.Lukasz43a095a2018-02-19 15:39:01 +0100104 for (const auto& privilege : expectedPrivileges) {
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100105 privileges.setSinglePrivilege(privilege);
106 }
107
108 EXPECT_EQ(privileges.getBasePrivilegeBitset().count(),
Borawski.Lukasz43a095a2018-02-19 15:39:01 +0100109 expectedPrivileges.size());
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100110}
111
Borawski.Lukasz43a095a2018-02-19 15:39:01 +0100112TEST(PrivilegeTest, GetActivePrivilegeNames) {
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100113 Privileges privileges;
114
115 EXPECT_EQ(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
116 std::vector<std::string>());
117
Borawski.Lukasz43a095a2018-02-19 15:39:01 +0100118 std::vector<std::string> expectedPrivileges{
119 "Login", "ConfigureManager", "ConfigureUsers", "ConfigureComponents",
120 "ConfigureSelf"};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100121
Borawski.Lukasz43a095a2018-02-19 15:39:01 +0100122 for (const auto& privilege : expectedPrivileges) {
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100123 privileges.setSinglePrivilege(privilege);
124 }
125
126 std::vector<std::string> activePrivileges =
127 privileges.getActivePrivilegeNames(PrivilegeType::BASE);
128
Borawski.Lukasz43a095a2018-02-19 15:39:01 +0100129 std::sort(expectedPrivileges.begin(), expectedPrivileges.end());
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100130 std::sort(activePrivileges.begin(), activePrivileges.end());
131
Borawski.Lukasz43a095a2018-02-19 15:39:01 +0100132 EXPECT_EQ(activePrivileges, expectedPrivileges);
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100133}
134
Borawski.Lukasz43a095a2018-02-19 15:39:01 +0100135TEST(PrivilegeTest, PropertyOverrideConstructor) {
136 OperationMap operationMap = {
137 {crow::HTTPMethod::GET, {{"Login"}, {"ConfigureManager"}}}};
138 PropertyOverride propertyOverride(std::move(operationMap),
139 {"Password", "Id"});
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100140}