Merging tizen into ckm. Stage 1.
[platform/core/test/security-tests.git] / tests / cynara-tests / common / cynara_test_admin.cpp
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 #include <cynara_test_admin.h>
18 #include <memory.h>
19 #include <plugins.h>
20 #include <tests_common.h>
21
22 #include <algorithm>
23 #include <cstring>
24 #include <cstdlib>
25 #include <ostream>
26 #include <string>
27 #include <sstream>
28
29 namespace CynaraTestAdmin {
30
31 namespace
32 {
33
34 std::ostream& operator<<(std::ostream& os, const cynara_admin_policy &policy)
35 {
36     os << "{";
37     os << " " << formatCstr(policy.bucket) << ",";
38     os << " " << formatCstr(policy.client) << ",";
39     os << " " << formatCstr(policy.user) << ",";
40     os << " " << formatCstr(policy.privilege) << ",";
41     os << " " << policy.result << ",";
42     os << " " << formatCstr(policy.result_extra);
43     os << " }" << std::endl;
44     return os;
45 }
46
47 std::ostream& operator<<(std::ostream& os, const cynara_admin_policy *const *policies)
48 {
49     os << "{" << std::endl;
50     for (size_t i = 0; policies[i] != nullptr; ++i)
51         os << *policies[i];
52     os << "}";
53     return os;
54 }
55
56 int string_compare(const char *s1, const char *s2)
57 {
58     if (!s2)
59     {
60         if (!s1)
61             return 0;
62         return 1;
63     }
64     if (!s1)
65         return -1;
66     return strcmp(s1, s2);
67 }
68
69 bool policy_less(const cynara_admin_policy &p1, const cynara_admin_policy &p2)
70 {
71     auto sc = string_compare(p1.bucket, p2.bucket);
72     if (sc != 0)
73         return (sc < 0);
74     sc = string_compare(p1.client, p2.client);
75     if (sc != 0)
76         return (sc < 0);
77     sc = string_compare(p1.user, p2.user);
78     if (sc != 0)
79         return (sc < 0);
80     sc = string_compare(p1.privilege, p2.privilege);
81     if (sc != 0)
82         return (sc < 0);
83     sc = string_compare(p1.result_extra, p2.result_extra);
84     if (sc != 0)
85         return (sc < 0);
86     return p1.result < p2.result;
87 }
88
89 bool policy_equal(const cynara_admin_policy &p1, const cynara_admin_policy &p2)
90 {
91     return (p1.result == p2.result
92             && string_compare(p1.bucket, p2.bucket) == 0
93             && string_compare(p1.client, p2.client) == 0
94             && string_compare(p1.user, p2.user) == 0
95             && string_compare(p1.privilege, p2.privilege) == 0
96             && string_compare(p1.result_extra, p2.result_extra) == 0);
97 }
98
99 std::ostream& operator<<(std::ostream& os, const CynaraTestPlugins::Descriptions &descriptions)
100 {
101     os << "{" << std::endl;
102     for (const auto &desc : descriptions)
103         os << "{ [" << desc.type << "], <" << desc.name << "> }" << std::endl;
104     os << "}";
105     return os;
106 }
107
108 } // namespace anonymous
109
110 CynaraPoliciesContainer::CynaraPoliciesContainer()
111 {
112 }
113
114 CynaraPoliciesContainer::CynaraPoliciesContainer(struct cynara_admin_policy **policies)
115 {
116     if (!policies)
117         return;
118
119     for (int i = 0; policies[i]; ++i) {
120         auto policyPtr = policies[i];
121         m_policies.push_back(*policyPtr);
122         free(policyPtr);
123     }
124     free(policies);
125 }
126
127 CynaraPoliciesContainer::~CynaraPoliciesContainer()
128 {
129     for (struct cynara_admin_policy &policy : m_policies) {
130         free(policy.bucket);
131         free(policy.client);
132         free(policy.user);
133         free(policy.privilege);
134         free(policy.result_extra);
135     }
136 }
137
138 void CynaraPoliciesContainer::add(const char *bucket,
139                                   const char *client,
140                                   const char *user,
141                                   const char *privilege,
142                                   const int result,
143                                   const char *resultExtra)
144 {
145     m_policies.push_back({ nullptr, nullptr, nullptr, nullptr, 0, nullptr });
146     struct cynara_admin_policy &policy = m_policies.back();
147     if (bucket)
148         policy.bucket = strdup(bucket);
149     if (client)
150         policy.client = strdup(client);
151     if (user)
152         policy.user = strdup(user);
153     if (privilege)
154         policy.privilege = strdup(privilege);
155     policy.result = result;
156     if (resultExtra)
157         policy.result_extra = strdup(resultExtra);
158 }
159
160 void CynaraPoliciesContainer::add(const char *bucket,
161                                   const CheckKey &checkKey,
162                                   const int result,
163                                   const char *resultExtra)
164 {
165     add(bucket, checkKey.m_client, checkKey.m_user, checkKey.m_privilege, result, resultExtra);
166 }
167
168 void CynaraPoliciesContainer::sort()
169 {
170     std::sort(m_policies.begin(), m_policies.end(), policy_less);
171 }
172
173 std::ostream& operator<<(std::ostream& os, const CynaraPoliciesContainer &policies)
174 {
175     os << "{" << std::endl;
176     for (const auto & policy : policies.m_policies)
177         os << policy;
178     os << "}";
179     return os;
180 }
181
182 Admin::Admin()
183     : m_admin(nullptr)
184 {
185     int ret = cynara_admin_initialize(&m_admin);
186     RUNNER_ASSERT_MSG(ret == CYNARA_API_SUCCESS,
187                          "cynara_admin_initialize failed. ret: " << ret);
188     RUNNER_ASSERT_MSG(m_admin != nullptr, "cynara_admin struct was not initialized");
189 }
190
191 Admin::~Admin()
192 {
193     cynara_admin_finish(m_admin);
194 }
195
196 void Admin::setPolicies(const CynaraPoliciesContainer &policiesContainer,
197                         int expectedResult)
198 {
199     const cynara_admin_policy *policies[policiesContainer.m_policies.size()+1];
200
201     for (size_t i = 0; i < policiesContainer.m_policies.size(); ++i) {
202         policies[i] = &policiesContainer.m_policies[i];
203     }
204     policies[policiesContainer.m_policies.size()] = nullptr;
205
206     int ret = cynara_admin_set_policies(m_admin, policies);
207     RUNNER_ASSERT_MSG(ret == expectedResult,
208                          "cynara_admin_set_policies returned wrong value: "
209                              << ret << " != " << expectedResult << ". "
210                              << "policies:\n" << policies);
211 }
212
213 void Admin::setBucket(const char *bucket, int operation, const char *extra,
214                       int expectedResult)
215 {
216     int ret = cynara_admin_set_bucket(m_admin, bucket, operation, extra);
217     RUNNER_ASSERT_MSG(ret == expectedResult,
218                          "cynara_admin_set_bucket returned wrong value: "
219                              << ret << " != " << expectedResult << "."
220                              << " bucket: " << formatCstr(bucket) << ","
221                              << " operation: " << operation << ","
222                              << " extra: " << formatCstr(extra));
223 }
224
225 void Admin::adminCheck(const char *startBucket, int recursive,
226                        const char *client, const char *user, const char *privilege,
227                        int expectedCheckResult, const char *expectedCheckResultExtra,
228                        int expectedResult)
229 {
230     int checkResult;
231     char *checkResultExtra = nullptr;
232
233     int ret = cynara_admin_check(m_admin,
234                                  startBucket, recursive,
235                                  client, user, privilege,
236                                  &checkResult, &checkResultExtra);
237     CStringPtr extra(checkResultExtra);
238
239     auto dump = [&]() -> std::string
240     {
241         std::stringstream s;
242         s  << " functionReturn: " << ret << ","
243            << " functionExpectedReturn: " << expectedResult << ",";
244
245         s  << " startBucket: " << formatCstr(startBucket) << ","
246            << " recursive: " << recursive << ","
247            << " client: " << formatCstr(client) << ","
248            << " user: " << formatCstr(user) << ","
249            << " privilege: " << formatCstr(privilege) << ",";
250
251         s  << " checkResult: " << checkResult << ","
252            << " expectedCheckResult: " << expectedCheckResult << ","
253            << " checkResultExtra: " << formatCstr(checkResultExtra) << ","
254            << " expectedCheckResultExtra: " << formatCstr(expectedCheckResultExtra);
255         return s.str();
256     };
257
258     RUNNER_ASSERT_MSG(ret == expectedResult,
259                          "cynara_admin_check returned wrong value: "
260                              << ret << " != " << expectedResult << "."
261                              << dump());
262
263     RUNNER_ASSERT_MSG(checkResult == expectedCheckResult,
264                          "cynara_admin_check returned wrong check result: "
265                              << checkResult << " != " << expectedCheckResult << "."
266                              << dump());
267
268     RUNNER_ASSERT_MSG(formatCstr(checkResultExtra) == formatCstr(expectedCheckResultExtra),
269                          "cynara_admin_check returned wrong check result extra: "
270                              << formatCstr(checkResultExtra) << " != "
271                              << formatCstr(expectedCheckResultExtra) << "."
272                              << dump());
273 }
274
275 void Admin::listPolicies(const char *startBucket,
276                          const char *client, const char *user, const char *privilege,
277                          CynaraPoliciesContainer &expectedPolicyList,
278                          int expectedResult) {
279
280     struct cynara_admin_policy **policies = nullptr;
281
282     int ret = cynara_admin_list_policies(m_admin,
283                                          startBucket,
284                                          client, user, privilege,
285                                          &policies);
286
287     CynaraPoliciesContainer receivedPolicyList(policies);
288     receivedPolicyList.sort();
289     expectedPolicyList.sort();
290
291     auto dump = [&]() -> std::string
292     {
293         std::stringstream s;
294         s  << " functionReturn: " << ret << ","
295            << " functionExpectedReturn: " << expectedResult << ",";
296
297         s  << " startBucket: " << formatCstr(startBucket) << ","
298            << " client: " << formatCstr(client) << ","
299            << " user: " << formatCstr(user) << ","
300            << " privilege: " << formatCstr(privilege) << ",";
301
302         s  << " receivedPolicyList: " << receivedPolicyList << ","
303            << " expectedPolicyList: " << expectedPolicyList;
304         return s.str();
305     };
306
307     RUNNER_ASSERT_MSG(ret == expectedResult,
308                          "cynara_admin_list_policies returned wrong value: "
309                              << ret << " != " << expectedResult << "."
310                              << dump());
311
312     RUNNER_ASSERT_MSG(receivedPolicyList.m_policies.size() == expectedPolicyList.m_policies.size(),
313                      "size of list returned by cynara_admin_list_policies: "
314                          << receivedPolicyList.m_policies.size()
315                          << " doesn't match expected list size: "
316                          << expectedPolicyList.m_policies.size() << "."
317                          << dump());
318
319     RUNNER_ASSERT_MSG(std::equal(receivedPolicyList.m_policies.begin(),
320                                  receivedPolicyList.m_policies.end(),
321                                  expectedPolicyList.m_policies.begin(),
322                                  policy_equal),
323                      "list returned by cynara_admin_list_policies doesn't match expected: "
324                          << dump());
325 }
326
327 void Admin::erasePolicies(const char *startBucket, int recursive,
328                           const char *client, const char *user, const char *privilege,
329                           int expectedResult)
330 {
331     int ret = cynara_admin_erase(m_admin,
332                                  startBucket, recursive,
333                                  client, user, privilege);
334
335     auto dump = [&]() -> std::string
336     {
337         std::stringstream s;
338         s  << " functionReturn: " << ret << ","
339            << " functionExpectedReturn: " << expectedResult << ",";
340
341         s  << " startBucket: " << formatCstr(startBucket) << ","
342            << " recursive: " << recursive << ","
343            << " client: " << formatCstr(client) << ","
344            << " user: " << formatCstr(user) << ","
345            << " privilege: " << formatCstr(privilege);
346
347         return s.str();
348     };
349
350     RUNNER_ASSERT_MSG(ret == expectedResult,
351                          "cynara_admin_erase returned wrong value: "
352                              << ret << " != " << expectedResult << "."
353                              << dump());
354 }
355
356 CynaraTestPlugins::Descriptions parseAndRelease(cynara_admin_policy_descr **descriptions)
357 {
358     CynaraTestPlugins::Descriptions ret;
359
360     if (descriptions) {
361         for (size_t i = 0; descriptions[i] != nullptr; ++i) {
362             auto descPtr = descriptions[i];
363             ret.push_back({ static_cast<Cynara::PolicyType>(descPtr->result),
364                             std::string(descPtr->name) });
365             free(descPtr->name);
366             free(descPtr);
367         }
368         free(descriptions);
369     }
370     return ret;
371 }
372
373 void Admin::listPoliciesDescriptions(CynaraTestPlugins::Descriptions &expectedDescriptions,
374                                      int expectedResult)
375 {
376     struct cynara_admin_policy_descr **descriptions = nullptr;
377
378     int ret = cynara_admin_list_policies_descriptions(m_admin, &descriptions);
379
380     CynaraTestPlugins::Descriptions receivedDescriptions = parseAndRelease(descriptions);
381
382     auto description_less = [](const Cynara::PolicyDescription &d1,
383                                const Cynara::PolicyDescription &d2) -> bool {
384         return d1.type != d2.type ? d1.type < d2.type : d1.name < d2.name;
385     };
386
387     auto description_equal = [](const Cynara::PolicyDescription &d1,
388                                 const Cynara::PolicyDescription &d2) -> bool {
389         return d1.type == d2.type && d1.name == d2.name;
390     };
391
392     std::sort(receivedDescriptions.begin(), receivedDescriptions.end(), description_less);
393     std::sort(expectedDescriptions.begin(), expectedDescriptions.end(), description_less);
394
395     auto dump = [&]() -> std::string
396     {
397         std::stringstream s;
398         s  << " functionReturn: " << ret << ","
399            << " functionExpectedReturn: " << expectedResult << ",";
400
401         s  << " receivedPolicyDescriptionList: " << receivedDescriptions << ","
402            << " expectedPolicyDescriptionList: " << expectedDescriptions << ".";
403         return s.str();
404     };
405
406     RUNNER_ASSERT_MSG(ret == expectedResult,
407                          "cynara_admin_list_policies_descriptions returned wrong value: "
408                              << ret << " != " << expectedResult << "."
409                              << dump());
410
411     RUNNER_ASSERT_MSG(receivedDescriptions.size() == expectedDescriptions.size(),
412                      "size of list returned by cynara_admin_list_policies_descriptions: "
413                          << receivedDescriptions.size()
414                          << " doesn't match expected list size: "
415                          << expectedDescriptions.size() << "."
416                          << dump());
417
418     RUNNER_ASSERT_MSG(std::equal(receivedDescriptions.begin(),
419                                  receivedDescriptions.end(),
420                                  expectedDescriptions.begin(),
421                                  description_equal),
422                      "list returned by cynara_admin_list_policies_descriptions "
423                          "doesn't match expected. " << dump());
424 }
425
426 } // namespace CynaraTestAdmin