blob: 92cd6c42a4128559295be574e69f80383a6c3474 [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) {
Ed Tanous3ebd75f2018-03-05 18:20:01 -080010 Privileges privileges{"Login", "ConfigureManager"};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010011
Ed Tanous3ebd75f2018-03-05 18:20:01 -080012 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
13 ::testing::UnorderedElementsAre(
14 ::testing::Pointee(&"Login"[0]),
15 ::testing::Pointee(&"ConfigureManager"[0])));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010016}
17
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010018TEST(PrivilegeTest, PrivilegeCheckForNoPrivilegesRequired) {
Ed Tanous3ebd75f2018-03-05 18:20:01 -080019 Privileges userPrivileges{"Login"};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010020
Ed Tanouse0d918b2018-03-27 17:41:04 -070021 OperationMap entityPrivileges{{boost::beast::http::verb::get, {{"Login"}}}};
Ed Tanous3ebd75f2018-03-05 18:20:01 -080022
Ed Tanouse0d918b2018-03-27 17:41:04 -070023 EXPECT_TRUE(isMethodAllowedWithPrivileges(boost::beast::http::verb::get,
Ed Tanous3ebd75f2018-03-05 18:20:01 -080024 entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010025}
26
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010027TEST(PrivilegeTest, PrivilegeCheckForSingleCaseSuccess) {
28 auto userPrivileges = Privileges{"Login"};
Ed Tanouse0d918b2018-03-27 17:41:04 -070029 OperationMap entityPrivileges{{boost::beast::http::verb::get, {}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010030
Ed Tanouse0d918b2018-03-27 17:41:04 -070031 EXPECT_TRUE(isMethodAllowedWithPrivileges(boost::beast::http::verb::get,
Ed Tanous3ebd75f2018-03-05 18:20:01 -080032 entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010033}
34
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010035TEST(PrivilegeTest, PrivilegeCheckForSingleCaseFailure) {
36 auto userPrivileges = Privileges{"Login"};
Ed Tanous3ebd75f2018-03-05 18:20:01 -080037 OperationMap entityPrivileges{
Ed Tanouse0d918b2018-03-27 17:41:04 -070038 {boost::beast::http::verb::get, {{"ConfigureManager"}}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010039
Ed Tanouse0d918b2018-03-27 17:41:04 -070040 EXPECT_FALSE(isMethodAllowedWithPrivileges(boost::beast::http::verb::get,
Ed Tanous3ebd75f2018-03-05 18:20:01 -080041 entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010042}
43
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010044TEST(PrivilegeTest, PrivilegeCheckForANDCaseSuccess) {
45 auto userPrivileges =
46 Privileges{"Login", "ConfigureManager", "ConfigureSelf"};
Ed Tanous3ebd75f2018-03-05 18:20:01 -080047 OperationMap entityPrivileges{
Ed Tanouse0d918b2018-03-27 17:41:04 -070048 {boost::beast::http::verb::get,
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010049 {{"Login", "ConfigureManager", "ConfigureSelf"}}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010050
Ed Tanouse0d918b2018-03-27 17:41:04 -070051 EXPECT_TRUE(isMethodAllowedWithPrivileges(boost::beast::http::verb::get,
Ed Tanous3ebd75f2018-03-05 18:20:01 -080052 entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010053}
54
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010055TEST(PrivilegeTest, PrivilegeCheckForANDCaseFailure) {
56 auto userPrivileges = Privileges{"Login", "ConfigureManager"};
Ed Tanous3ebd75f2018-03-05 18:20:01 -080057 OperationMap entityPrivileges{
Ed Tanouse0d918b2018-03-27 17:41:04 -070058 {boost::beast::http::verb::get,
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010059 {{"Login", "ConfigureManager", "ConfigureSelf"}}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010060
Ed Tanouse0d918b2018-03-27 17:41:04 -070061 EXPECT_FALSE(isMethodAllowedWithPrivileges(boost::beast::http::verb::get,
Ed Tanous3ebd75f2018-03-05 18:20:01 -080062 entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010063}
64
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010065TEST(PrivilegeTest, PrivilegeCheckForORCaseSuccess) {
66 auto userPrivileges = Privileges{"ConfigureManager"};
Ed Tanous3ebd75f2018-03-05 18:20:01 -080067 OperationMap entityPrivileges{
Ed Tanouse0d918b2018-03-27 17:41:04 -070068 {boost::beast::http::verb::get, {{"Login"}, {"ConfigureManager"}}}};
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010069
Ed Tanouse0d918b2018-03-27 17:41:04 -070070 EXPECT_TRUE(isMethodAllowedWithPrivileges(boost::beast::http::verb::get,
Ed Tanous3ebd75f2018-03-05 18:20:01 -080071 entityPrivileges, userPrivileges));
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010072}
73
74TEST(PrivilegeTest, PrivilegeCheckForORCaseFailure) {
75 auto userPrivileges = Privileges{"ConfigureComponents"};
Ed Tanous3ebd75f2018-03-05 18:20:01 -080076 OperationMap entityPrivileges = OperationMap(
Ed Tanouse0d918b2018-03-27 17:41:04 -070077 {{boost::beast::http::verb::get, {{"Login"}, {"ConfigureManager"}}}});
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010078
Ed Tanouse0d918b2018-03-27 17:41:04 -070079 EXPECT_FALSE(isMethodAllowedWithPrivileges(boost::beast::http::verb::get,
Ed Tanous3ebd75f2018-03-05 18:20:01 -080080 entityPrivileges, userPrivileges));
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010081}
82
83TEST(PrivilegeTest, DefaultPrivilegeBitsetsAreEmpty) {
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010084 Privileges privileges;
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010085
Ed Tanous3ebd75f2018-03-05 18:20:01 -080086 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
87 ::testing::IsEmpty());
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010088
Ed Tanous3ebd75f2018-03-05 18:20:01 -080089 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::OEM),
90 ::testing::IsEmpty());
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010091}
92
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010093TEST(PrivilegeTest, GetActivePrivilegeNames) {
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010094 Privileges privileges;
95
Ed Tanous3ebd75f2018-03-05 18:20:01 -080096 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
97 ::testing::IsEmpty());
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010098
Ed Tanous3ebd75f2018-03-05 18:20:01 -080099 std::array<const char*, 5> expectedPrivileges{
Borawski.Lukasz43a095a2018-02-19 15:39:01 +0100100 "Login", "ConfigureManager", "ConfigureUsers", "ConfigureComponents",
101 "ConfigureSelf"};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100102
Borawski.Lukasz43a095a2018-02-19 15:39:01 +0100103 for (const auto& privilege : expectedPrivileges) {
Ed Tanous3ebd75f2018-03-05 18:20:01 -0800104 EXPECT_TRUE(privileges.setSinglePrivilege(privilege));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100105 }
106
Ed Tanous3ebd75f2018-03-05 18:20:01 -0800107 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
108 ::testing::UnorderedElementsAre(
109 ::testing::Pointee(expectedPrivileges[0]),
110 ::testing::Pointee(expectedPrivileges[1]),
111 ::testing::Pointee(expectedPrivileges[2]),
112 ::testing::Pointee(expectedPrivileges[3]),
113 ::testing::Pointee(expectedPrivileges[4])));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100114}