blob: d8572902503514dfac4171bf08948dd2fe303c0a [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),
16 ::testing::UnorderedElementsAre(
17 ::testing::Pointee(&"Login"[0]),
18 ::testing::Pointee(&"ConfigureManager"[0])));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010019}
20
Ed Tanous1abe55e2018-09-05 08:30:59 -070021TEST(PrivilegeTest, PrivilegeCheckForNoPrivilegesRequired)
22{
23 Privileges userPrivileges{"Login"};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010024
Ed Tanous1abe55e2018-09-05 08:30:59 -070025 OperationMap entityPrivileges{{boost::beast::http::verb::get, {{"Login"}}}};
Ed Tanous3ebd75f2018-03-05 18:20:01 -080026
Ed Tanous1abe55e2018-09-05 08:30:59 -070027 EXPECT_TRUE(isMethodAllowedWithPrivileges(
28 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010029}
30
Ed Tanous1abe55e2018-09-05 08:30:59 -070031TEST(PrivilegeTest, PrivilegeCheckForSingleCaseSuccess)
32{
33 auto userPrivileges = Privileges{"Login"};
34 OperationMap entityPrivileges{{boost::beast::http::verb::get, {}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010035
Ed Tanous1abe55e2018-09-05 08:30:59 -070036 EXPECT_TRUE(isMethodAllowedWithPrivileges(
37 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010038}
39
Ed Tanous1abe55e2018-09-05 08:30:59 -070040TEST(PrivilegeTest, PrivilegeCheckForSingleCaseFailure)
41{
42 auto userPrivileges = Privileges{"Login"};
43 OperationMap entityPrivileges{
44 {boost::beast::http::verb::get, {{"ConfigureManager"}}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010045
Ed Tanous1abe55e2018-09-05 08:30:59 -070046 EXPECT_FALSE(isMethodAllowedWithPrivileges(
47 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010048}
49
Ed Tanous1abe55e2018-09-05 08:30:59 -070050TEST(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.Lukaszaecb47a2018-01-25 12:14:14 +010057
Ed Tanous1abe55e2018-09-05 08:30:59 -070058 EXPECT_TRUE(isMethodAllowedWithPrivileges(
59 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010060}
61
Ed Tanous1abe55e2018-09-05 08:30:59 -070062TEST(PrivilegeTest, PrivilegeCheckForANDCaseFailure)
63{
64 auto userPrivileges = Privileges{"Login", "ConfigureManager"};
65 OperationMap entityPrivileges{
66 {boost::beast::http::verb::get,
67 {{"Login", "ConfigureManager", "ConfigureSelf"}}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010068
Ed Tanous1abe55e2018-09-05 08:30:59 -070069 EXPECT_FALSE(isMethodAllowedWithPrivileges(
70 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010071}
72
Ed Tanous1abe55e2018-09-05 08:30:59 -070073TEST(PrivilegeTest, PrivilegeCheckForORCaseSuccess)
74{
75 auto userPrivileges = Privileges{"ConfigureManager"};
76 OperationMap entityPrivileges{
77 {boost::beast::http::verb::get, {{"Login"}, {"ConfigureManager"}}}};
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010078
Ed Tanous1abe55e2018-09-05 08:30:59 -070079 EXPECT_TRUE(isMethodAllowedWithPrivileges(
80 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010081}
82
Ed Tanous1abe55e2018-09-05 08:30:59 -070083TEST(PrivilegeTest, PrivilegeCheckForORCaseFailure)
84{
85 auto userPrivileges = Privileges{"ConfigureComponents"};
86 OperationMap entityPrivileges = OperationMap(
87 {{boost::beast::http::verb::get, {{"Login"}, {"ConfigureManager"}}}});
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010088
Ed Tanous1abe55e2018-09-05 08:30:59 -070089 EXPECT_FALSE(isMethodAllowedWithPrivileges(
90 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010091}
92
Ed Tanous1abe55e2018-09-05 08:30:59 -070093TEST(PrivilegeTest, DefaultPrivilegeBitsetsAreEmpty)
94{
95 Privileges privileges;
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010096
Ed Tanous1abe55e2018-09-05 08:30:59 -070097 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
98 ::testing::IsEmpty());
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010099
Ed Tanous1abe55e2018-09-05 08:30:59 -0700100 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::OEM),
101 ::testing::IsEmpty());
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100102}
103
Ed Tanous1abe55e2018-09-05 08:30:59 -0700104TEST(PrivilegeTest, GetActivePrivilegeNames)
105{
106 Privileges privileges;
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100107
Ed Tanous1abe55e2018-09-05 08:30:59 -0700108 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
109 ::testing::IsEmpty());
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100110
Ed Tanous1abe55e2018-09-05 08:30:59 -0700111 std::array<const char*, 5> expectedPrivileges{
112 "Login", "ConfigureManager", "ConfigureUsers", "ConfigureComponents",
113 "ConfigureSelf"};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100114
Ed Tanous1abe55e2018-09-05 08:30:59 -0700115 for (const auto& privilege : expectedPrivileges)
116 {
117 EXPECT_TRUE(privileges.setSinglePrivilege(privilege));
118 }
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100119
Ed Tanous1abe55e2018-09-05 08:30:59 -0700120 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.Lukaszaecb47a2018-01-25 12:14:14 +0100127}