blob: 1613fbb8cfdd99257383d4c41bd5fc0ab6d73a8c [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
9using namespace redfish;
10
Ed Tanous1abe55e2018-09-05 08:30:59 -070011TEST(PrivilegeTest, PrivilegeConstructor)
12{
13 Privileges privileges{"Login", "ConfigureManager"};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010014
Ed Tanous1abe55e2018-09-05 08:30:59 -070015 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
Ed Tanous23a21a12020-07-25 04:45:05 +000016 ::testing::UnorderedElementsAre("Login", "ConfigureManager"));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010017}
18
Ed Tanous1abe55e2018-09-05 08:30:59 -070019TEST(PrivilegeTest, PrivilegeCheckForNoPrivilegesRequired)
20{
21 Privileges userPrivileges{"Login"};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010022
Ed Tanous1abe55e2018-09-05 08:30:59 -070023 OperationMap entityPrivileges{{boost::beast::http::verb::get, {{"Login"}}}};
Ed Tanous3ebd75f2018-03-05 18:20:01 -080024
Ed Tanous1abe55e2018-09-05 08:30:59 -070025 EXPECT_TRUE(isMethodAllowedWithPrivileges(
26 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010027}
28
Ed Tanous1abe55e2018-09-05 08:30:59 -070029TEST(PrivilegeTest, PrivilegeCheckForSingleCaseSuccess)
30{
31 auto userPrivileges = Privileges{"Login"};
32 OperationMap entityPrivileges{{boost::beast::http::verb::get, {}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010033
Ed Tanous1abe55e2018-09-05 08:30:59 -070034 EXPECT_TRUE(isMethodAllowedWithPrivileges(
35 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010036}
37
Ed Tanous1abe55e2018-09-05 08:30:59 -070038TEST(PrivilegeTest, PrivilegeCheckForSingleCaseFailure)
39{
40 auto userPrivileges = Privileges{"Login"};
41 OperationMap entityPrivileges{
42 {boost::beast::http::verb::get, {{"ConfigureManager"}}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010043
Ed Tanous1abe55e2018-09-05 08:30:59 -070044 EXPECT_FALSE(isMethodAllowedWithPrivileges(
45 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010046}
47
Ed Tanous1abe55e2018-09-05 08:30:59 -070048TEST(PrivilegeTest, PrivilegeCheckForANDCaseSuccess)
49{
50 auto userPrivileges =
51 Privileges{"Login", "ConfigureManager", "ConfigureSelf"};
52 OperationMap entityPrivileges{
53 {boost::beast::http::verb::get,
54 {{"Login", "ConfigureManager", "ConfigureSelf"}}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010055
Ed Tanous1abe55e2018-09-05 08:30:59 -070056 EXPECT_TRUE(isMethodAllowedWithPrivileges(
57 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010058}
59
Ed Tanous1abe55e2018-09-05 08:30:59 -070060TEST(PrivilegeTest, PrivilegeCheckForANDCaseFailure)
61{
62 auto userPrivileges = Privileges{"Login", "ConfigureManager"};
63 OperationMap entityPrivileges{
64 {boost::beast::http::verb::get,
65 {{"Login", "ConfigureManager", "ConfigureSelf"}}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010066
Ed Tanous1abe55e2018-09-05 08:30:59 -070067 EXPECT_FALSE(isMethodAllowedWithPrivileges(
68 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010069}
70
Ed Tanous1abe55e2018-09-05 08:30:59 -070071TEST(PrivilegeTest, PrivilegeCheckForORCaseSuccess)
72{
73 auto userPrivileges = Privileges{"ConfigureManager"};
74 OperationMap entityPrivileges{
75 {boost::beast::http::verb::get, {{"Login"}, {"ConfigureManager"}}}};
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010076
Ed Tanous1abe55e2018-09-05 08:30:59 -070077 EXPECT_TRUE(isMethodAllowedWithPrivileges(
78 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010079}
80
Ed Tanous1abe55e2018-09-05 08:30:59 -070081TEST(PrivilegeTest, PrivilegeCheckForORCaseFailure)
82{
83 auto userPrivileges = Privileges{"ConfigureComponents"};
84 OperationMap entityPrivileges = OperationMap(
85 {{boost::beast::http::verb::get, {{"Login"}, {"ConfigureManager"}}}});
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010086
Ed Tanous1abe55e2018-09-05 08:30:59 -070087 EXPECT_FALSE(isMethodAllowedWithPrivileges(
88 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010089}
90
Ed Tanous1abe55e2018-09-05 08:30:59 -070091TEST(PrivilegeTest, DefaultPrivilegeBitsetsAreEmpty)
92{
93 Privileges privileges;
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010094
Ed Tanous1abe55e2018-09-05 08:30:59 -070095 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
96 ::testing::IsEmpty());
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010097
Ed Tanous1abe55e2018-09-05 08:30:59 -070098 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::OEM),
99 ::testing::IsEmpty());
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100100}
101
Ed Tanous1abe55e2018-09-05 08:30:59 -0700102TEST(PrivilegeTest, GetActivePrivilegeNames)
103{
104 Privileges privileges;
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100105
Ed Tanous1abe55e2018-09-05 08:30:59 -0700106 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
107 ::testing::IsEmpty());
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100108
Ed Tanous1abe55e2018-09-05 08:30:59 -0700109 std::array<const char*, 5> expectedPrivileges{
110 "Login", "ConfigureManager", "ConfigureUsers", "ConfigureComponents",
111 "ConfigureSelf"};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100112
Ed Tanous1abe55e2018-09-05 08:30:59 -0700113 for (const auto& privilege : expectedPrivileges)
114 {
115 EXPECT_TRUE(privileges.setSinglePrivilege(privilege));
116 }
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100117
Ed Tanous1abe55e2018-09-05 08:30:59 -0700118 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
119 ::testing::UnorderedElementsAre(
Ed Tanous23a21a12020-07-25 04:45:05 +0000120 expectedPrivileges[0], expectedPrivileges[1],
121 expectedPrivileges[2], expectedPrivileges[3],
122 expectedPrivileges[4]));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100123}