Introduce 'default' method type for credential helpers
[platform/core/security/cynara.git] / test / cyad / CyadCommandlineDispatcherTest.h
1 /*
2  * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file        test/cyad/CyadCommandlineDispatcherTest.h
18  * @author      Aleksander Zdyb <a.zdyb@samsung.com>
19  * @version     1.0
20  * @brief       Test fixture for CyadCommandlineDispatcher
21  */
22
23 #ifndef TEST_CYAD_CYADCOMMANDLINEDISPATCHERTEST_H_
24 #define TEST_CYAD_CYADCOMMANDLINEDISPATCHERTEST_H_
25
26 #include <new>
27 #include <stdexcept>
28
29 #include <gmock/gmock.h>
30 #include <gtest/gtest.h>
31
32 #include <cynara-admin-types.h>
33 #include <cynara-error.h>
34
35 #include "FakeAdminApiWrapper.h"
36 #include "FakeDispatcherIO.h"
37 #include "FakeErrorApiWrapper.h"
38
39 #define MAX_POLICY_DESCRIPTIONS 15
40
41 class CyadCommandlineDispatcherTest : public ::testing::Test {
42 private:
43     typedef cynara_admin_policy_descr DescrType;
44
45 public:
46     void SetUp(void) {
47         auto arrSize = sizeof(DescrType *) * (MAX_POLICY_DESCRIPTIONS + 1);
48         m_policyDescs = static_cast<DescrType **>(malloc(arrSize));
49
50         if (m_policyDescs == nullptr)
51             throw std::bad_alloc();
52
53         m_policyDescs[0] = nullptr;
54         setupInitFinishExpectations();
55         setupDescriptionsExpectation();
56     }
57
58     void TearDown(void) {
59         if (m_policyDescs != nullptr)
60             freeDescriptions();
61     }
62
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));
67         }
68
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());
73         };
74
75         for (const auto &it : toAdd) {
76             addDesc(m_policyDescs + m_descCount, it.first, it.second);
77             ++m_descCount;
78         }
79
80         m_policyDescs[m_descCount] = nullptr;
81     }
82
83 protected:
84     void setupInitFinishExpectations() {
85         using ::testing::_;
86         using ::testing::Return;
87
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));
90     }
91
92     void setupDescriptionsExpectation() {
93         using ::testing::_;
94         using ::testing::Return;
95         using ::testing::NotNull;
96         using ::testing::DoAll;
97         using ::testing::SetArgPointee;
98         using ::testing::Assign;
99
100         addDescriptions({
101             { 0, "DENY" },
102             { 0xFFFF, "ALLOW" },
103         });
104
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)));
109     }
110
111     FakeDispatcherIO m_io;
112     FakeAdminApiWrapper m_adminApi;
113     FakeErrorApiWrapper m_errorApi;
114
115 private:
116     void freeDescriptions() {
117         auto freePolicyDesc = [] (DescrType *pd) {
118            free(pd->name);
119            free(pd);
120         };
121
122         for (int i = 0; m_policyDescs[i] != nullptr; ++i) {
123            freePolicyDesc(m_policyDescs[i]);
124         }
125         free(m_policyDescs);
126     }
127
128     DescrType **m_policyDescs = nullptr;
129     std::size_t m_descCount = 0;
130 };
131
132 #endif /* TEST_CYAD_CYADCOMMANDLINEDISPATCHERTEST_H_ */