summaryrefslogtreecommitdiffstats
path: root/test/ldap_mapper_test.cpp
blob: 2b06cce5e7c6bc445009636fc0b7aac1e9e69805 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <gtest/gtest.h>
#include <experimental/filesystem>
#include <stdlib.h>
#include <sdbusplus/bus.hpp>
#include "phosphor-ldap-mapper/ldap_mapper_entry.hpp"
#include "phosphor-ldap-mapper/ldap_mapper_mgr.hpp"
#include "phosphor-ldap-mapper/ldap_mapper_serialize.hpp"
#include <xyz/openbmc_project/Common/error.hpp>
#include <xyz/openbmc_project/User/Common/error.hpp>
#include "config.h"

namespace phosphor
{
namespace user
{

namespace fs = std::experimental::filesystem;

class TestSerialization : public testing::Test
{
  public:
    TestSerialization() : bus(sdbusplus::bus::new_default())
    {
    }

    void SetUp() override
    {
        char tempDir[] = "/tmp/privmapper_test.XXXXXX";
        dir = fs::path(mkdtemp(tempDir));
    }

    void TearDown() override
    {
        fs::remove_all(dir);
    }

    fs::path dir;
    sdbusplus::bus::bus bus;
};

TEST_F(TestSerialization, testPersistPath)
{
    LDAPMapperMgr manager(TestSerialization::bus, mapperMgrRoot,
                          TestSerialization::dir.c_str());
    std::string groupName = "admin";
    std::string privilege = "priv-admin";
    size_t entryId = 1;
    auto dbusPath = std::string(mapperMgrRoot) + '/' + std::to_string(entryId);

    auto entry = std::make_unique<LDAPMapperEntry>(
        TestSerialization::bus, dbusPath.c_str(),
        (TestSerialization::dir).c_str(), groupName, privilege, manager);
    auto outPath = serialize(*entry, entryId, TestSerialization::dir);
    EXPECT_EQ(outPath, TestSerialization::dir / std::to_string(entryId));
}

TEST_F(TestSerialization, testPersistData)
{
    std::string groupName = "admin";
    std::string privilege = "priv-admin";
    size_t entryId = 1;
    auto dbusPath = std::string(mapperMgrRoot) + '/' + std::to_string(entryId);
    LDAPMapperMgr manager(TestSerialization::bus, mapperMgrRoot,
                          TestSerialization::dir.c_str());

    auto input = std::make_unique<LDAPMapperEntry>(
        bus, dbusPath.c_str(), TestSerialization::dir.c_str(), groupName,
        privilege, manager);
    auto outPath = serialize(*input, entryId, TestSerialization::dir);

    auto output = std::make_unique<LDAPMapperEntry>(
        bus, dbusPath.c_str(), (TestSerialization::dir).c_str(), manager);
    auto rc = deserialize(outPath, *output);

    EXPECT_EQ(rc, true);
    EXPECT_EQ(output->groupName(), groupName);
    EXPECT_EQ(output->privilege(), privilege);
}

TEST_F(TestSerialization, testRestore)
{
    std::string groupName = "admin";
    std::string privilege = "priv-admin";
    namespace fs = std::experimental::filesystem;
    size_t entryId = 1;
    LDAPMapperMgr manager1(TestSerialization::bus, mapperMgrRoot,
                           (TestSerialization::dir).c_str());
    EXPECT_NO_THROW(manager1.create(groupName, privilege));

    EXPECT_EQ(fs::exists(TestSerialization::dir / std::to_string(entryId)),
              true);
    LDAPMapperMgr manager2(TestSerialization::bus, mapperMgrRoot,
                           (TestSerialization::dir).c_str());
    EXPECT_NO_THROW(manager2.restore());
    EXPECT_NO_THROW(manager2.deletePrivilegeMapper(entryId));
    EXPECT_EQ(fs::exists(TestSerialization::dir / std::to_string(entryId)),
              false);
}

TEST_F(TestSerialization, testPrivilegeMapperCreation)
{
    std::string groupName = "admin";
    std::string privilege = "priv-admin";
    LDAPMapperMgr manager(TestSerialization::bus, mapperMgrRoot,
                          (TestSerialization::dir).c_str());
    EXPECT_NO_THROW(manager.create(groupName, privilege));
}

TEST_F(TestSerialization, testDuplicateGroupName)
{
    std::string groupName = "admin";
    std::string privilege = "priv-admin";
    using PrivilegeMappingExists = sdbusplus::xyz::openbmc_project::User::
        Common::Error::PrivilegeMappingExists;
    LDAPMapperMgr manager(TestSerialization::bus, mapperMgrRoot,
                          (TestSerialization::dir).c_str());
    auto objectPath = manager.create(groupName, privilege);
    EXPECT_THROW(manager.create(groupName, privilege), PrivilegeMappingExists);
}

TEST_F(TestSerialization, testValidPrivilege)
{
    std::string groupName = "admin";
    std::string privilege = "priv-admin";
    size_t entryId = 1;
    auto dbusPath = std::string(mapperMgrRoot) + '/' + std::to_string(entryId);
    LDAPMapperMgr manager(TestSerialization::bus, mapperMgrRoot,
                          TestSerialization::dir.c_str());

    auto entry = std::make_unique<LDAPMapperEntry>(
        TestSerialization::bus, dbusPath.c_str(),
        (TestSerialization::dir).c_str(), groupName, privilege, manager);

    EXPECT_NO_THROW(entry->privilege("priv-operator"));
    EXPECT_NO_THROW(entry->privilege("priv-user"));
    EXPECT_NO_THROW(entry->privilege("priv-callback"));
}

TEST_F(TestSerialization, testInvalidPrivilege)
{
    std::string groupName = "admin";
    std::string privilege = "priv-test";
    using InvalidArgument =
        sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
    LDAPMapperMgr manager(TestSerialization::bus, mapperMgrRoot,
                          (TestSerialization::dir).c_str());
    EXPECT_THROW(manager.create(groupName, privilege), InvalidArgument);
}

} // namespace user
} // namespace phosphor
OpenPOWER on IntegriCloud