#ifndef SRC_SECURITYMANAGER_BASESECURITYMANAGERWRAPPER_H
#define SRC_SECURITYMANAGER_BASESECURITYMANAGERWRAPPER_H
+#include <sys/types.h>
+
namespace SecurityManager {
class BaseSecurityManagerWrapper {
public:
virtual ~BaseSecurityManagerWrapper() = default;
- virtual int security_manager_groups_get(char ***groups, size_t *groups_count) = 0;
- virtual void security_manager_groups_free(char **groups, size_t groups_count) = 0;
+ virtual int security_manager_groups_get(gid_t **groups, size_t *groups_count) = 0;
virtual int SECURITY_MANAGER_SUCCESS_CONST(void) const = 0;
};
* @version 1.0
*/
-#include <functional>
+#include <stdlib.h>
+
#include <memory>
+#include <string>
#include <Log/log.h>
Lad::DataProvider::GroupsList DataProvider::getResourceGroups(void) {
Lad::DataProvider::GroupsList retGroups;
- char **groups = nullptr;
+ gid_t *groups = nullptr;
size_t groupsLen = 0;
auto getRet = m_smApi.security_manager_groups_get(&groups, &groupsLen);
throw ErrorException("Could not get groups from Security Manager");
}
- auto groupsDeleter = std::bind(&BaseSecurityManagerWrapper::security_manager_groups_free,
- &m_smApi, std::placeholders::_1, groupsLen);
- std::unique_ptr<char *, std::function<void(char **)>> groupsPtr(groups, groupsDeleter);
+ std::unique_ptr<gid_t, decltype(&::free)> groupsPtr(groups, ::free);
for (size_t i = 0; i < groupsLen; ++i) {
- retGroups.push_back(groups[i]);
+ retGroups.push_back(std::to_string(groups[i]));
}
return retGroups;
namespace SecurityManager {
-int SecurityManagerWrapper::security_manager_groups_get(char ***groups, size_t *groups_count) {
+int SecurityManagerWrapper::security_manager_groups_get(gid_t **groups, size_t *groups_count) {
return ::security_manager_groups_get(groups, groups_count);
}
-void SecurityManagerWrapper::security_manager_groups_free(char **groups, size_t groups_count) {
- return ::security_manager_groups_free(groups, groups_count);
-}
-
int SecurityManagerWrapper::SECURITY_MANAGER_SUCCESS_CONST(void) const {
return ::SECURITY_MANAGER_SUCCESS;
}
public:
virtual ~SecurityManagerWrapper() = default;
- virtual int security_manager_groups_get(char ***groups, size_t *groups_count);
- virtual void security_manager_groups_free(char **groups, size_t groups_count);
+ virtual int security_manager_groups_get(gid_t **groups, size_t *groups_count);
virtual int SECURITY_MANAGER_SUCCESS_CONST(void) const;
};
public:
using BaseSecurityManagerWrapper::BaseSecurityManagerWrapper;
- MOCK_METHOD2(security_manager_groups_get, int(char ***groups, size_t *groups_count));
- MOCK_METHOD2(security_manager_groups_free, void(char **groups, size_t groups_count));
+ MOCK_METHOD2(security_manager_groups_get, int(gid_t **groups, size_t *groups_count));
MOCK_CONST_METHOD0(SECURITY_MANAGER_SUCCESS_CONST, int(void));
};
* @version 1.0
*/
+#include <stdlib.h>
+
#include <memory>
#include <new>
#include <string>
#include <vector>
+#include <algorithm>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
* - call SecurityManager::DataProvider::getResourceGroups()
* - check if security_manager_groups_get() was called with proper args
* - check if correct groups are returned by security_manager_groups_get()
- * - check if groups were freed with security_manager_groups_free()
*/
TEST(SecurityManager, getResourceGroups) {
using ::testing::DoAll;
using ::testing::ElementsAreArray;
using ::testing::NiceMock;
- std::vector<std::string> expectedGroups = {
- "group1", "group2"
- };
-
- auto groupsDeleter = [] (char **groups) {
- if (groups == nullptr)
- return;
- for (size_t i = 0; groups[i] != nullptr; ++i)
- free(groups[i]);
- free(groups);
- };
-
- std::unique_ptr<char *, decltype(groupsDeleter)> groups(
- static_cast<char **>(calloc(expectedGroups.size(), sizeof(char *))),
- groupsDeleter);
-
- if (groups == nullptr)
- throw std::bad_alloc();
-
- auto freeGroups = [&groups] () {
- groups.reset();
- };
-
- size_t i = 0;
- for (const auto &g : expectedGroups) {
- groups.get()[i] = strdup(g.c_str());
- if (groups.get()[i] == nullptr)
- throw std::bad_alloc();
- ++i;
- }
+ const size_t SIZE = 3;
+
+ // getResourceGroups should free it
+ gid_t *groups = static_cast<gid_t*>(calloc(SIZE, sizeof(gid_t)));
+ groups[0] = 3;
+ groups[1] = 45;
+ groups[2] = 21;
+
+ std::vector<std::string> expectedGroups(SIZE);
+ for (size_t i = 0; i < SIZE; i++)
+ expectedGroups[i] = std::to_string(groups[i]);
NiceMock<FakeSecurityManagerWrapper> smApi;
.WillByDefault(Return(SECURITY_MANAGER_SUCCESS_CONST));
EXPECT_CALL(smApi, security_manager_groups_get(NotNull(), NotNull()))
- .WillOnce(DoAll(SetArgPointee<0>(groups.get()),
- SetArgPointee<1>(expectedGroups.size()),
+ .WillOnce(DoAll(SetArgPointee<0>(groups),
+ SetArgPointee<1>(SIZE),
Return(SECURITY_MANAGER_SUCCESS_CONST)));
- EXPECT_CALL(smApi, security_manager_groups_free(groups.get(), expectedGroups.size()))
- .WillOnce(InvokeWithoutArgs(freeGroups));
SecurityManager::DataProvider dp(smApi);