blob: 36e02c67f27b82cea283db83204863c84d37b352 [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 Tanous3ebd75f2018-03-05 18:20:01 -080021 OperationMap entityPrivileges{{crow::HTTPMethod::GET, {{"Login"}}}};
22
23 EXPECT_TRUE(isMethodAllowedWithPrivileges(crow::HTTPMethod::GET,
24 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 Tanous3ebd75f2018-03-05 18:20:01 -080029 OperationMap entityPrivileges{{crow::HTTPMethod::GET, {}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010030
Ed Tanous3ebd75f2018-03-05 18:20:01 -080031 EXPECT_TRUE(isMethodAllowedWithPrivileges(crow::HTTPMethod::GET,
32 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{
38 {crow::HTTPMethod::GET, {{"ConfigureManager"}}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010039
Ed Tanous3ebd75f2018-03-05 18:20:01 -080040 EXPECT_FALSE(isMethodAllowedWithPrivileges(crow::HTTPMethod::GET,
41 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{
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010048 {crow::HTTPMethod::GET,
49 {{"Login", "ConfigureManager", "ConfigureSelf"}}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010050
Ed Tanous3ebd75f2018-03-05 18:20:01 -080051 EXPECT_TRUE(isMethodAllowedWithPrivileges(crow::HTTPMethod::GET,
52 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{
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010058 {crow::HTTPMethod::GET,
59 {{"Login", "ConfigureManager", "ConfigureSelf"}}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010060
Ed Tanous3ebd75f2018-03-05 18:20:01 -080061 EXPECT_FALSE(isMethodAllowedWithPrivileges(crow::HTTPMethod::GET,
62 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{
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010068 {crow::HTTPMethod::GET, {{"Login"}, {"ConfigureManager"}}}};
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010069
Ed Tanous3ebd75f2018-03-05 18:20:01 -080070 EXPECT_TRUE(isMethodAllowedWithPrivileges(crow::HTTPMethod::GET,
71 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(
77 {{crow::HTTPMethod::GET, {{"Login"}, {"ConfigureManager"}}}});
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010078
Ed Tanous3ebd75f2018-03-05 18:20:01 -080079 EXPECT_FALSE(isMethodAllowedWithPrivileges(crow::HTTPMethod::GET,
80 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}