2b0eff5d7b84eb5aa1b64237f4a075963f7fe985
[platform/core/test/security-tests.git] / src / cynara-tests / test_cases_agent.cpp
1 /*
2  * Copyright (c) 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 /**
18  * @file        test_cases_agent.cpp
19  * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
20  * @version     1.0
21  * @brief       Tests for libcynara-agent
22  */
23
24 #include <chrono>
25 #include <string>
26
27 #include <cynara-error.h>
28 #include <cynara-admin-types.h>
29 #include <cynara-client-async.h>
30
31 #include <dpl/test/test_runner.h>
32 #include <cynara_test_admin.h>
33 #include <cynara_test_agent.h>
34 #include <cynara_test_agent_request.h>
35 #include <cynara_test_agent_response.h>
36 #include <cynara_test_client_async_client.h>
37 #include <cynara_test_commons.h>
38 #include <cynara_test_env.h>
39 #include <service_manager.h>
40 #include <timeout.h>
41
42 using namespace CynaraTestAdmin;
43 using namespace CynaraTestAgent;
44 using namespace CynaraTestClientAsync;
45 using namespace CynaraTestPlugins;
46
47 void loadAgentPlugin()
48 {
49     DirectoryPaths paths;
50     paths.push_back(TEST_PLUGIN_PATH + TEST_AGENT);
51     loadServicePlugins(paths);
52 }
53
54 void setAgentPolicy(int expectedResult = CYNARA_API_SUCCESS)
55 {
56     const char *bucket = CYNARA_ADMIN_DEFAULT_BUCKET;
57     const char *wildcard = CYNARA_ADMIN_WILDCARD;
58     const char *extra = nullptr;
59 // collection of policy descriptions defined by plugin that causes use of TestAgent
60     auto testAgentPolicies = POLICY_DESCRIPTIONS.at(TEST_AGENT);
61 // any policy type from above collection
62     auto policyType = testAgentPolicies[0].type;
63
64     CynaraPoliciesContainer cp;
65     cp.add(bucket, wildcard, wildcard, wildcard, policyType, extra);
66
67     Admin admin;
68     admin.setPolicies(cp, expectedResult);
69 }
70
71 void restartCynara()
72 {
73     ServiceManager serviceManager(CynaraTestConsts::SERVICE);
74     serviceManager.restartService();
75 }
76
77 void getAgentRequest(Agent &agent, AgentRequest &request, Client &client)
78 {
79     auto timeLimit = std::chrono::seconds(2);
80     auto hangOnGetRequest = [&agent, &request]() {
81         agent.getRequest(request);
82     };
83     Timeout::CancelFunction sendClientRequest = [&client]() {
84         client.process();
85         client.assertStatus(READ);
86     };
87
88     Timeout::callAndWait(timeLimit, Timeout::ExpectMode::TIMEOUT,
89                          sendClientRequest, hangOnGetRequest);
90 }
91
92 void tcag01_set_agent_type_policy_without_plugin_func()
93 {
94     loadServicePlugins(DirectoryPaths());
95     setAgentPolicy(CYNARA_API_INVALID_PARAM);
96 }
97
98 void tcag02_set_agent_type_policy_with_plugin_loaded_func()
99 {
100     loadAgentPlugin();
101     setAgentPolicy();
102 }
103
104 void tcag03_check_with_no_agent_func()
105 {
106     std::string testNo("03");
107     cynara_check_id id;
108     RequestEntity callbackData = {RequestFunction(),
109                                   CYNARA_API_ACCESS_DENIED,
110                                   CYNARA_CALL_CAUSE_ANSWER};
111
112     loadAgentPlugin();
113     setAgentPolicy();
114
115     Client client;
116     client.createRequest({testNo}, id, callbackData);
117     client.assertStatus(READWRITE);
118
119     //send requests
120     client.process();
121     client.process(CYNARA_API_SUCCESS, Client::IGNORE_TIMEOUT);
122 }
123
124 void tcag04_agent_initialize_func()
125 {
126     Agent();
127 }
128
129 void tcag05_agent_request_timeout_func()
130 {
131     Agent agent;
132     AgentRequest request;
133
134     auto testTimeLimit = std::chrono::seconds(2);
135     auto hangOnGetRequest = [&agent, &request]() {
136         agent.getRequest(request, CYNARA_API_SERVICE_NOT_AVAILABLE);
137     };
138
139     Timeout::callAndWait(testTimeLimit, Timeout::ExpectMode::TIMEOUT,
140                          restartCynara, hangOnGetRequest);
141 }
142
143 void tcag06_check_with_unregistered_agent_func()
144 {
145     std::string testNo("06");
146     cynara_check_id id;
147     RequestEntity callbackData = {RequestFunction(),
148                                   CYNARA_API_ACCESS_DENIED,
149                                   CYNARA_CALL_CAUSE_ANSWER};
150
151     loadAgentPlugin();
152     setAgentPolicy();
153
154     Agent agent;
155
156     Client client;
157     client.createRequest({testNo}, id, callbackData);
158     client.assertStatus(READWRITE);
159
160     //send requests
161     client.process();
162     client.process(CYNARA_API_SUCCESS, Client::IGNORE_TIMEOUT);
163 }
164
165 void tcag07_get_request_func()
166 {
167     std::string testNo("07");
168     CheckData data(testNo);
169     cynara_check_id id;
170     RequestEntity callbackData = {RequestFunction(),
171                                   CYNARA_API_ACCESS_ALLOWED,
172                                   CYNARA_CALL_CAUSE_ANSWER};
173
174     loadAgentPlugin();
175     setAgentPolicy();
176
177     Agent agent;
178     AgentRequest agentRequest;
179     Client client;
180     client.createRequest(data, id, callbackData);
181     client.assertStatus(READWRITE);
182
183     auto testTimeLimit = std::chrono::seconds(5);
184     Timeout::callAndWait(testTimeLimit, Timeout::ExpectMode::FINISHED,
185                          restartCynara, getAgentRequest, std::ref(agent), std::ref(agentRequest),
186                          std::ref(client));
187
188     agentRequest.assertAction(data.m_client, data.m_user, data.m_privilege);
189     agent.putResponse(AgentResponse::createAllow(agentRequest.id()));
190     client.process();
191 }
192
193 RUNNER_TEST_GROUP_INIT(cynara_agent_tests)
194
195 RUN_CYNARA_TEST(tcag01_set_agent_type_policy_without_plugin)
196 RUN_CYNARA_TEST(tcag02_set_agent_type_policy_with_plugin_loaded)
197 RUN_CYNARA_TEST(tcag03_check_with_no_agent)
198 RUN_CYNARA_TEST(tcag04_agent_initialize)
199 RUN_CYNARA_TEST(tcag05_agent_request_timeout)
200 RUN_CYNARA_TEST(tcag06_check_with_unregistered_agent)
201 RUN_CYNARA_TEST(tcag07_get_request)