summaryrefslogtreecommitdiff
path: root/redfish-core/ut/privileges_test.cpp
blob: 92cd6c42a4128559295be574e69f80383a6c3474 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "privileges.hpp"
#include <fstream>
#include <string>
#include "nlohmann/json.hpp"
#include "gmock/gmock.h"

using namespace redfish;

TEST(PrivilegeTest, PrivilegeConstructor) {
  Privileges privileges{"Login", "ConfigureManager"};

  EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
              ::testing::UnorderedElementsAre(
                  ::testing::Pointee(&"Login"[0]),
                  ::testing::Pointee(&"ConfigureManager"[0])));
}

TEST(PrivilegeTest, PrivilegeCheckForNoPrivilegesRequired) {
  Privileges userPrivileges{"Login"};

  OperationMap entityPrivileges{{boost::beast::http::verb::get, {{"Login"}}}};

  EXPECT_TRUE(isMethodAllowedWithPrivileges(boost::beast::http::verb::get,
                                            entityPrivileges, userPrivileges));
}

TEST(PrivilegeTest, PrivilegeCheckForSingleCaseSuccess) {
  auto userPrivileges = Privileges{"Login"};
  OperationMap entityPrivileges{{boost::beast::http::verb::get, {}}};

  EXPECT_TRUE(isMethodAllowedWithPrivileges(boost::beast::http::verb::get,
                                            entityPrivileges, userPrivileges));
}

TEST(PrivilegeTest, PrivilegeCheckForSingleCaseFailure) {
  auto userPrivileges = Privileges{"Login"};
  OperationMap entityPrivileges{
      {boost::beast::http::verb::get, {{"ConfigureManager"}}}};

  EXPECT_FALSE(isMethodAllowedWithPrivileges(boost::beast::http::verb::get,
                                             entityPrivileges, userPrivileges));
}

TEST(PrivilegeTest, PrivilegeCheckForANDCaseSuccess) {
  auto userPrivileges =
      Privileges{"Login", "ConfigureManager", "ConfigureSelf"};
  OperationMap entityPrivileges{
      {boost::beast::http::verb::get,
       {{"Login", "ConfigureManager", "ConfigureSelf"}}}};

  EXPECT_TRUE(isMethodAllowedWithPrivileges(boost::beast::http::verb::get,
                                            entityPrivileges, userPrivileges));
}

TEST(PrivilegeTest, PrivilegeCheckForANDCaseFailure) {
  auto userPrivileges = Privileges{"Login", "ConfigureManager"};
  OperationMap entityPrivileges{
      {boost::beast::http::verb::get,
       {{"Login", "ConfigureManager", "ConfigureSelf"}}}};

  EXPECT_FALSE(isMethodAllowedWithPrivileges(boost::beast::http::verb::get,
                                             entityPrivileges, userPrivileges));
}

TEST(PrivilegeTest, PrivilegeCheckForORCaseSuccess) {
  auto userPrivileges = Privileges{"ConfigureManager"};
  OperationMap entityPrivileges{
      {boost::beast::http::verb::get, {{"Login"}, {"ConfigureManager"}}}};

  EXPECT_TRUE(isMethodAllowedWithPrivileges(boost::beast::http::verb::get,
                                            entityPrivileges, userPrivileges));
}

TEST(PrivilegeTest, PrivilegeCheckForORCaseFailure) {
  auto userPrivileges = Privileges{"ConfigureComponents"};
  OperationMap entityPrivileges = OperationMap(
      {{boost::beast::http::verb::get, {{"Login"}, {"ConfigureManager"}}}});

  EXPECT_FALSE(isMethodAllowedWithPrivileges(boost::beast::http::verb::get,
                                             entityPrivileges, userPrivileges));
}

TEST(PrivilegeTest, DefaultPrivilegeBitsetsAreEmpty) {
  Privileges privileges;

  EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
              ::testing::IsEmpty());

  EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::OEM),
              ::testing::IsEmpty());
}

TEST(PrivilegeTest, GetActivePrivilegeNames) {
  Privileges privileges;

  EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
              ::testing::IsEmpty());

  std::array<const char*, 5> expectedPrivileges{
      "Login", "ConfigureManager", "ConfigureUsers", "ConfigureComponents",
      "ConfigureSelf"};

  for (const auto& privilege : expectedPrivileges) {
    EXPECT_TRUE(privileges.setSinglePrivilege(privilege));
  }

  EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
              ::testing::UnorderedElementsAre(
                  ::testing::Pointee(expectedPrivileges[0]),
                  ::testing::Pointee(expectedPrivileges[1]),
                  ::testing::Pointee(expectedPrivileges[2]),
                  ::testing::Pointee(expectedPrivileges[3]),
                  ::testing::Pointee(expectedPrivileges[4])));
}