blob: 2d0da021eb45a55b16c3c7d7c6c4521a851d0018 [file] [log] [blame]
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +01001#include "privileges.hpp"
Ed Tanous1abe55e2018-09-05 08:30:59 -07002
Nan Zhoud5c80ad2022-07-11 01:16:31 +00003#include <boost/beast/http/verb.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07004
Nan Zhoud5c80ad2022-07-11 01:16:31 +00005#include <array>
6
7#include <gmock/gmock.h> // IWYU pragma: keep
8#include <gtest/gtest.h> // IWYU pragma: keep
9
10// IWYU pragma: no_include <gtest/gtest-message.h>
11// IWYU pragma: no_include <gtest/gtest-test-part.h>
12// IWYU pragma: no_include "gtest/gtest_pred_impl.h"
13// IWYU pragma: no_include <gmock/gmock-matchers.h>
14// IWYU pragma: no_include <gmock/gmock-more-matchers.h>
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010015
Nan Zhou38ead5e2022-07-03 23:07:27 +000016namespace redfish
17{
18namespace
19{
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010020
Nan Zhoub5a10a22022-07-04 01:18:14 +000021using ::testing::IsEmpty;
22using ::testing::UnorderedElementsAre;
23
Ed Tanous1abe55e2018-09-05 08:30:59 -070024TEST(PrivilegeTest, PrivilegeConstructor)
25{
26 Privileges privileges{"Login", "ConfigureManager"};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010027
Ed Tanous1abe55e2018-09-05 08:30:59 -070028 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
Nan Zhoub5a10a22022-07-04 01:18:14 +000029 UnorderedElementsAre("Login", "ConfigureManager"));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010030}
31
Ed Tanous1abe55e2018-09-05 08:30:59 -070032TEST(PrivilegeTest, PrivilegeCheckForNoPrivilegesRequired)
33{
34 Privileges userPrivileges{"Login"};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010035
Ed Tanous1abe55e2018-09-05 08:30:59 -070036 OperationMap entityPrivileges{{boost::beast::http::verb::get, {{"Login"}}}};
Ed Tanous3ebd75f2018-03-05 18:20:01 -080037
Ed Tanous1abe55e2018-09-05 08:30:59 -070038 EXPECT_TRUE(isMethodAllowedWithPrivileges(
39 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010040}
41
Ed Tanous1abe55e2018-09-05 08:30:59 -070042TEST(PrivilegeTest, PrivilegeCheckForSingleCaseSuccess)
43{
44 auto userPrivileges = Privileges{"Login"};
45 OperationMap entityPrivileges{{boost::beast::http::verb::get, {}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010046
Ed Tanous1abe55e2018-09-05 08:30:59 -070047 EXPECT_TRUE(isMethodAllowedWithPrivileges(
48 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010049}
50
Ed Tanous1abe55e2018-09-05 08:30:59 -070051TEST(PrivilegeTest, PrivilegeCheckForSingleCaseFailure)
52{
53 auto userPrivileges = Privileges{"Login"};
54 OperationMap entityPrivileges{
55 {boost::beast::http::verb::get, {{"ConfigureManager"}}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010056
Ed Tanous1abe55e2018-09-05 08:30:59 -070057 EXPECT_FALSE(isMethodAllowedWithPrivileges(
58 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010059}
60
Ed Tanous1abe55e2018-09-05 08:30:59 -070061TEST(PrivilegeTest, PrivilegeCheckForANDCaseSuccess)
62{
63 auto userPrivileges =
64 Privileges{"Login", "ConfigureManager", "ConfigureSelf"};
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_TRUE(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, PrivilegeCheckForANDCaseFailure)
74{
75 auto userPrivileges = Privileges{"Login", "ConfigureManager"};
76 OperationMap entityPrivileges{
77 {boost::beast::http::verb::get,
78 {{"Login", "ConfigureManager", "ConfigureSelf"}}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010079
Ed Tanous1abe55e2018-09-05 08:30:59 -070080 EXPECT_FALSE(isMethodAllowedWithPrivileges(
81 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010082}
83
Ed Tanous1abe55e2018-09-05 08:30:59 -070084TEST(PrivilegeTest, PrivilegeCheckForORCaseSuccess)
85{
86 auto userPrivileges = Privileges{"ConfigureManager"};
87 OperationMap entityPrivileges{
88 {boost::beast::http::verb::get, {{"Login"}, {"ConfigureManager"}}}};
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010089
Ed Tanous1abe55e2018-09-05 08:30:59 -070090 EXPECT_TRUE(isMethodAllowedWithPrivileges(
91 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010092}
93
Ed Tanous1abe55e2018-09-05 08:30:59 -070094TEST(PrivilegeTest, PrivilegeCheckForORCaseFailure)
95{
96 auto userPrivileges = Privileges{"ConfigureComponents"};
97 OperationMap entityPrivileges = OperationMap(
98 {{boost::beast::http::verb::get, {{"Login"}, {"ConfigureManager"}}}});
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010099
Ed Tanous1abe55e2018-09-05 08:30:59 -0700100 EXPECT_FALSE(isMethodAllowedWithPrivileges(
101 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukasz43a095a2018-02-19 15:39:01 +0100102}
103
Ed Tanous1abe55e2018-09-05 08:30:59 -0700104TEST(PrivilegeTest, DefaultPrivilegeBitsetsAreEmpty)
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),
Nan Zhoub5a10a22022-07-04 01:18:14 +0000109 IsEmpty());
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100110
Ed Tanous1abe55e2018-09-05 08:30:59 -0700111 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::OEM),
Nan Zhoub5a10a22022-07-04 01:18:14 +0000112 IsEmpty());
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100113}
114
Ed Tanous1abe55e2018-09-05 08:30:59 -0700115TEST(PrivilegeTest, GetActivePrivilegeNames)
116{
117 Privileges privileges;
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100118
Ed Tanous1abe55e2018-09-05 08:30:59 -0700119 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
Nan Zhoub5a10a22022-07-04 01:18:14 +0000120 IsEmpty());
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100121
Ed Tanous1abe55e2018-09-05 08:30:59 -0700122 std::array<const char*, 5> expectedPrivileges{
123 "Login", "ConfigureManager", "ConfigureUsers", "ConfigureComponents",
124 "ConfigureSelf"};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100125
Ed Tanous1abe55e2018-09-05 08:30:59 -0700126 for (const auto& privilege : expectedPrivileges)
127 {
128 EXPECT_TRUE(privileges.setSinglePrivilege(privilege));
129 }
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100130
Nan Zhoub5a10a22022-07-04 01:18:14 +0000131 EXPECT_THAT(
132 privileges.getActivePrivilegeNames(PrivilegeType::BASE),
133 UnorderedElementsAre(expectedPrivileges[0], expectedPrivileges[1],
134 expectedPrivileges[2], expectedPrivileges[3],
135 expectedPrivileges[4]));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100136}
Nan Zhou38ead5e2022-07-03 23:07:27 +0000137} // namespace
138} // namespace redfish