blob: 3813b54fa2a2124d371708e8bf0713a1545f3d1a [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
Nan Zhou38ead5e2022-07-03 23:07:27 +00009namespace redfish
10{
11namespace
12{
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010013
Ed Tanous1abe55e2018-09-05 08:30:59 -070014TEST(PrivilegeTest, PrivilegeConstructor)
15{
16 Privileges privileges{"Login", "ConfigureManager"};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010017
Ed Tanous1abe55e2018-09-05 08:30:59 -070018 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
Ed Tanous23a21a12020-07-25 04:45:05 +000019 ::testing::UnorderedElementsAre("Login", "ConfigureManager"));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010020}
21
Ed Tanous1abe55e2018-09-05 08:30:59 -070022TEST(PrivilegeTest, PrivilegeCheckForNoPrivilegesRequired)
23{
24 Privileges userPrivileges{"Login"};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010025
Ed Tanous1abe55e2018-09-05 08:30:59 -070026 OperationMap entityPrivileges{{boost::beast::http::verb::get, {{"Login"}}}};
Ed Tanous3ebd75f2018-03-05 18:20:01 -080027
Ed Tanous1abe55e2018-09-05 08:30:59 -070028 EXPECT_TRUE(isMethodAllowedWithPrivileges(
29 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010030}
31
Ed Tanous1abe55e2018-09-05 08:30:59 -070032TEST(PrivilegeTest, PrivilegeCheckForSingleCaseSuccess)
33{
34 auto userPrivileges = Privileges{"Login"};
35 OperationMap entityPrivileges{{boost::beast::http::verb::get, {}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010036
Ed Tanous1abe55e2018-09-05 08:30:59 -070037 EXPECT_TRUE(isMethodAllowedWithPrivileges(
38 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010039}
40
Ed Tanous1abe55e2018-09-05 08:30:59 -070041TEST(PrivilegeTest, PrivilegeCheckForSingleCaseFailure)
42{
43 auto userPrivileges = Privileges{"Login"};
44 OperationMap entityPrivileges{
45 {boost::beast::http::verb::get, {{"ConfigureManager"}}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010046
Ed Tanous1abe55e2018-09-05 08:30:59 -070047 EXPECT_FALSE(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, PrivilegeCheckForANDCaseSuccess)
52{
53 auto userPrivileges =
54 Privileges{"Login", "ConfigureManager", "ConfigureSelf"};
55 OperationMap entityPrivileges{
56 {boost::beast::http::verb::get,
57 {{"Login", "ConfigureManager", "ConfigureSelf"}}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010058
Ed Tanous1abe55e2018-09-05 08:30:59 -070059 EXPECT_TRUE(isMethodAllowedWithPrivileges(
60 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010061}
62
Ed Tanous1abe55e2018-09-05 08:30:59 -070063TEST(PrivilegeTest, PrivilegeCheckForANDCaseFailure)
64{
65 auto userPrivileges = Privileges{"Login", "ConfigureManager"};
66 OperationMap entityPrivileges{
67 {boost::beast::http::verb::get,
68 {{"Login", "ConfigureManager", "ConfigureSelf"}}}};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010069
Ed Tanous1abe55e2018-09-05 08:30:59 -070070 EXPECT_FALSE(isMethodAllowedWithPrivileges(
71 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010072}
73
Ed Tanous1abe55e2018-09-05 08:30:59 -070074TEST(PrivilegeTest, PrivilegeCheckForORCaseSuccess)
75{
76 auto userPrivileges = Privileges{"ConfigureManager"};
77 OperationMap entityPrivileges{
78 {boost::beast::http::verb::get, {{"Login"}, {"ConfigureManager"}}}};
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010079
Ed Tanous1abe55e2018-09-05 08:30:59 -070080 EXPECT_TRUE(isMethodAllowedWithPrivileges(
81 boost::beast::http::verb::get, entityPrivileges, userPrivileges));
Borawski.Lukasz43a095a2018-02-19 15:39:01 +010082}
83
Ed Tanous1abe55e2018-09-05 08:30:59 -070084TEST(PrivilegeTest, PrivilegeCheckForORCaseFailure)
85{
86 auto userPrivileges = Privileges{"ConfigureComponents"};
87 OperationMap entityPrivileges = OperationMap(
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_FALSE(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, DefaultPrivilegeBitsetsAreEmpty)
95{
96 Privileges privileges;
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +010097
Ed Tanous1abe55e2018-09-05 08:30:59 -070098 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
99 ::testing::IsEmpty());
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100100
Ed Tanous1abe55e2018-09-05 08:30:59 -0700101 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::OEM),
102 ::testing::IsEmpty());
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100103}
104
Ed Tanous1abe55e2018-09-05 08:30:59 -0700105TEST(PrivilegeTest, GetActivePrivilegeNames)
106{
107 Privileges privileges;
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100108
Ed Tanous1abe55e2018-09-05 08:30:59 -0700109 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
110 ::testing::IsEmpty());
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100111
Ed Tanous1abe55e2018-09-05 08:30:59 -0700112 std::array<const char*, 5> expectedPrivileges{
113 "Login", "ConfigureManager", "ConfigureUsers", "ConfigureComponents",
114 "ConfigureSelf"};
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100115
Ed Tanous1abe55e2018-09-05 08:30:59 -0700116 for (const auto& privilege : expectedPrivileges)
117 {
118 EXPECT_TRUE(privileges.setSinglePrivilege(privilege));
119 }
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100120
Ed Tanous1abe55e2018-09-05 08:30:59 -0700121 EXPECT_THAT(privileges.getActivePrivilegeNames(PrivilegeType::BASE),
122 ::testing::UnorderedElementsAre(
Ed Tanous23a21a12020-07-25 04:45:05 +0000123 expectedPrivileges[0], expectedPrivileges[1],
124 expectedPrivileges[2], expectedPrivileges[3],
125 expectedPrivileges[4]));
Borawski.Lukaszaecb47a2018-01-25 12:14:14 +0100126}
Nan Zhou38ead5e2022-07-03 23:07:27 +0000127} // namespace
128} // namespace redfish