2 * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file test/cyad/CyadCommandlineDispatcherTest.h
18 * @author Aleksander Zdyb <a.zdyb@samsung.com>
20 * @brief Test fixture for CyadCommandlineDispatcher
23 #ifndef TEST_CYAD_CYADCOMMANDLINEDISPATCHERTEST_H_
24 #define TEST_CYAD_CYADCOMMANDLINEDISPATCHERTEST_H_
29 #include <gmock/gmock.h>
30 #include <gtest/gtest.h>
32 #include <cynara-admin-types.h>
33 #include <cynara-error.h>
35 #include "FakeAdminApiWrapper.h"
36 #include "FakeDispatcherIO.h"
37 #include "FakeErrorApiWrapper.h"
39 #define MAX_POLICY_DESCRIPTIONS 15
41 class CyadCommandlineDispatcherTest : public ::testing::Test {
43 typedef cynara_admin_policy_descr DescrType;
47 auto arrSize = sizeof(DescrType *) * (MAX_POLICY_DESCRIPTIONS + 1);
48 m_policyDescs = static_cast<DescrType **>(malloc(arrSize));
50 if (m_policyDescs == nullptr)
51 throw std::bad_alloc();
53 m_policyDescs[0] = nullptr;
54 setupInitFinishExpectations();
55 setupDescriptionsExpectation();
59 if (m_policyDescs != nullptr)
63 void addDescriptions(std::vector<std::pair<int, std::string>> toAdd) {
64 if (m_descCount + toAdd.size() > MAX_POLICY_DESCRIPTIONS) {
65 throw std::length_error("Descriptions count would exceed "
66 + std::to_string(MAX_POLICY_DESCRIPTIONS));
69 auto addDesc = [] (DescrType **desc, int result, const std::string &name) {
70 (*desc) = static_cast<DescrType *>(malloc(sizeof(DescrType)));
71 (*desc)->result = result;
72 (*desc)->name = strdup(name.data());
75 for (const auto &it : toAdd) {
76 addDesc(m_policyDescs + m_descCount, it.first, it.second);
80 m_policyDescs[m_descCount] = nullptr;
84 void setupInitFinishExpectations() {
86 using ::testing::Return;
88 EXPECT_CALL(m_adminApi, cynara_admin_initialize(_)).WillOnce(Return(CYNARA_API_SUCCESS));
89 EXPECT_CALL(m_adminApi, cynara_admin_finish(_)).WillOnce(Return(CYNARA_API_SUCCESS));
92 void setupDescriptionsExpectation() {
94 using ::testing::Return;
95 using ::testing::NotNull;
96 using ::testing::DoAll;
97 using ::testing::SetArgPointee;
98 using ::testing::Assign;
105 EXPECT_CALL(m_adminApi, cynara_admin_list_policies_descriptions(_, NotNull()))
106 .WillOnce(DoAll(SetArgPointee<1>(m_policyDescs),
107 Assign(&m_policyDescs, nullptr),
108 Return(CYNARA_API_SUCCESS)));
111 FakeDispatcherIO m_io;
112 FakeAdminApiWrapper m_adminApi;
113 FakeErrorApiWrapper m_errorApi;
116 void freeDescriptions() {
117 auto freePolicyDesc = [] (DescrType *pd) {
122 for (int i = 0; m_policyDescs[i] != nullptr; ++i) {
123 freePolicyDesc(m_policyDescs[i]);
128 DescrType **m_policyDescs = nullptr;
129 std::size_t m_descCount = 0;
132 #endif /* TEST_CYAD_CYADCOMMANDLINEDISPATCHERTEST_H_ */