blob: c5a7a87964772b7767488a8a377b4b954c55ec4c [file] [log] [blame]
Ed Tanous1abe55e2018-09-05 08:30:59 -07001#include "nlohmann/json.hpp"
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +01002#include "privileges.hpp"
Ed Tanous1abe55e2018-09-05 08:30:59 -07003
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +01004#include <fstream>
5#include <string>
Ed Tanous1abe55e2018-09-05 08:30:59 -07006
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +01007#include "gmock/gmock.h"
8
Nan Zhou38ead5e2022-07-03 23:07:27 +00009namespace redfish
10{
11namespace
12{
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010013
Nan Zhoub5a10a22022-07-04 01:18:14 +000014using ::testing::IsEmpty;
15using ::testing::UnorderedElementsAre;
16
Ed Tanous1abe55e2018-09-05 08:30:59 -070017TEST(PrivilegeTest, PrivilegeConstructor)
18{
19 Privileges privileges{"Login", "ConfigureManager"};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010020
Ed Tanous1abe55e2018-09-05 08:30:59 -070021 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
Nan Zhoub5a10a22022-07-04 01:18:14 +000022 UnorderedElementsAre("Login", "ConfigureManager"));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010023}
24
Ed Tanous1abe55e2018-09-05 08:30:59 -070025TEST(PrivilegeTest, PrivilegeCheckForNoPrivilegesRequired)
26{
27 Privileges userPrivileges{"Login"};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010028
Ed Tanous1abe55e2018-09-05 08:30:59 -070029 OperationMap entityPrivileges{{boost::beast::http::verb::get, {{"Login"}}}};
Ed Tanous3ebd75f2018-03-05 18:20:01 -080030
Ed Tanous1abe55e2018-09-05 08:30:59 -070031 EXPECT_TRUE(isMethodAllowedWithPrivileges(
32 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010033}
34
Ed Tanous1abe55e2018-09-05 08:30:59 -070035TEST(PrivilegeTest, PrivilegeCheckForSingleCaseSuccess)
36{
37 auto userPrivileges = Privileges{"Login"};
38 OperationMap entityPrivileges{{boost::beast::http::verb::get, {}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010039
Ed Tanous1abe55e2018-09-05 08:30:59 -070040 EXPECT_TRUE(isMethodAllowedWithPrivileges(
41 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010042}
43
Ed Tanous1abe55e2018-09-05 08:30:59 -070044TEST(PrivilegeTest, PrivilegeCheckForSingleCaseFailure)
45{
46 auto userPrivileges = Privileges{"Login"};
47 OperationMap entityPrivileges{
48 {boost::beast::http::verb::get, {{"ConfigureManager"}}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010049
Ed Tanous1abe55e2018-09-05 08:30:59 -070050 EXPECT_FALSE(isMethodAllowedWithPrivileges(
51 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010052}
53
Ed Tanous1abe55e2018-09-05 08:30:59 -070054TEST(PrivilegeTest, PrivilegeCheckForANDCaseSuccess)
55{
56 auto userPrivileges =
57 Privileges{"Login", "ConfigureManager", "ConfigureSelf"};
58 OperationMap entityPrivileges{
59 {boost::beast::http::verb::get,
60 {{"Login", "ConfigureManager", "ConfigureSelf"}}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010061
Ed Tanous1abe55e2018-09-05 08:30:59 -070062 EXPECT_TRUE(isMethodAllowedWithPrivileges(
63 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010064}
65
Ed Tanous1abe55e2018-09-05 08:30:59 -070066TEST(PrivilegeTest, PrivilegeCheckForANDCaseFailure)
67{
68 auto userPrivileges = Privileges{"Login", "ConfigureManager"};
69 OperationMap entityPrivileges{
70 {boost::beast::http::verb::get,
71 {{"Login", "ConfigureManager", "ConfigureSelf"}}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010072
Ed Tanous1abe55e2018-09-05 08:30:59 -070073 EXPECT_FALSE(isMethodAllowedWithPrivileges(
74 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010075}
76
Ed Tanous1abe55e2018-09-05 08:30:59 -070077TEST(PrivilegeTest, PrivilegeCheckForORCaseSuccess)
78{
79 auto userPrivileges = Privileges{"ConfigureManager"};
80 OperationMap entityPrivileges{
81 {boost::beast::http::verb::get, {{"Login"}, {"ConfigureManager"}}}};
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010082
Ed Tanous1abe55e2018-09-05 08:30:59 -070083 EXPECT_TRUE(isMethodAllowedWithPrivileges(
84 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010085}
86
Ed Tanous1abe55e2018-09-05 08:30:59 -070087TEST(PrivilegeTest, PrivilegeCheckForORCaseFailure)
88{
89 auto userPrivileges = Privileges{"ConfigureComponents"};
90 OperationMap entityPrivileges = OperationMap(
91 {{boost::beast::http::verb::get, {{"Login"}, {"ConfigureManager"}}}});
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010092
Ed Tanous1abe55e2018-09-05 08:30:59 -070093 EXPECT_FALSE(isMethodAllowedWithPrivileges(
94 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010095}
96
Ed Tanous1abe55e2018-09-05 08:30:59 -070097TEST(PrivilegeTest, DefaultPrivilegeBitsetsAreEmpty)
98{
99 Privileges privileges;
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100100
Ed Tanous1abe55e2018-09-05 08:30:59 -0700101 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
Nan Zhoub5a10a22022-07-04 01:18:14 +0000102 IsEmpty());
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100103
Ed Tanous1abe55e2018-09-05 08:30:59 -0700104 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::OEM),
Nan Zhoub5a10a22022-07-04 01:18:14 +0000105 IsEmpty());
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100106}
107
Ed Tanous1abe55e2018-09-05 08:30:59 -0700108TEST(PrivilegeTest, GetActivePrivilegeNames)
109{
110 Privileges privileges;
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100111
Ed Tanous1abe55e2018-09-05 08:30:59 -0700112 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
Nan Zhoub5a10a22022-07-04 01:18:14 +0000113 IsEmpty());
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100114
Ed Tanous1abe55e2018-09-05 08:30:59 -0700115 std::array<const char*, 5> expectedPrivileges{
116 "Login", "ConfigureManager", "ConfigureUsers", "ConfigureComponents",
117 "ConfigureSelf"};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100118
Ed Tanous1abe55e2018-09-05 08:30:59 -0700119 for (const auto& privilege : expectedPrivileges)
120 {
121 EXPECT_TRUE(privileges.setSinglePrivilege(privilege));
122 }
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100123
Nan Zhoub5a10a22022-07-04 01:18:14 +0000124 EXPECT_THAT(
125 privileges.getActivePrivilegeNames(PrivilegeType::BASE),
126 UnorderedElementsAre(expectedPrivileges[0], expectedPrivileges[1],
127 expectedPrivileges[2], expectedPrivileges[3],
128 expectedPrivileges[4]));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100129}
Nan Zhou38ead5e2022-07-03 23:07:27 +0000130} // namespace
131} // namespace redfish