From e86ba301a094b20c1c2ea77e6a5c0f6975a24f93 Mon Sep 17 00:00:00 2001 From: Lukasz Wojciechowski Date: Wed, 31 Dec 2014 14:54:55 +0100 Subject: [PATCH] Add wrap for cynara_admin_list_policies in Admin class Add method listPolicies executing cynara_admin_list_policies and checking if received result match expected. Enhance CynaraPoliciesContainer class by: * removing copy and move constructors; * adding sort method; * adding operator<<. Change-Id: Icff4a6aa0e27efa191b7eb9e9c2be79c21066cbf --- tests/cynara-tests/common/cynara_test_admin.cpp | 155 +++++++++++++++++++++--- tests/cynara-tests/common/cynara_test_admin.h | 21 +++- 2 files changed, 160 insertions(+), 16 deletions(-) diff --git a/tests/cynara-tests/common/cynara_test_admin.cpp b/tests/cynara-tests/common/cynara_test_admin.cpp index 19362ad..40f0009 100644 --- a/tests/cynara-tests/common/cynara_test_admin.cpp +++ b/tests/cynara-tests/common/cynara_test_admin.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,8 @@ #include #include +#include +#include #include #include #include @@ -27,33 +29,90 @@ namespace CynaraTestAdmin { namespace { -std::ostream& operator<<(std::ostream& os, const cynara_admin_policy *const *policies) +std::ostream& operator<<(std::ostream& os, const cynara_admin_policy &policy) { + os << "{"; + os << " " << formatCstr(policy.bucket) << ","; + os << " " << formatCstr(policy.client) << ","; + os << " " << formatCstr(policy.user) << ","; + os << " " << formatCstr(policy.privilege) << ","; + os << " " << policy.result << ","; + os << " " << formatCstr(policy.result_extra); + os << " }" << std::endl; + return os; +} +std::ostream& operator<<(std::ostream& os, const cynara_admin_policy *const *policies) +{ os << "{" << std::endl; - for (size_t i=0; policies[i] != nullptr; ++i) { - os << "{"; - os << " " << formatCstr(policies[i]->bucket); - os << " " << formatCstr(policies[i]->client); - os << " " << formatCstr(policies[i]->user); - os << " " << formatCstr(policies[i]->privilege); - os << " " << policies[i]->result; - os << " " << formatCstr(policies[i]->result_extra); - if (policies[i+1] != nullptr) - os << " }," << std::endl; - else - os << " }" << std::endl; - } + for (size_t i = 0; policies[i] != nullptr; ++i) + os << *policies[i]; os << "}"; return os; } +int string_compare(const char *s1, const char *s2) +{ + if (!s2) + { + if (!s1) + return 0; + return 1; + } + if (!s1) + return -1; + return strcmp(s1, s2); +} + +bool policy_less(const cynara_admin_policy &p1, const cynara_admin_policy &p2) +{ + auto sc = string_compare(p1.bucket, p2.bucket); + if (sc != 0) + return (sc < 0); + sc = string_compare(p1.client, p2.client); + if (sc != 0) + return (sc < 0); + sc = string_compare(p1.user, p2.user); + if (sc != 0) + return (sc < 0); + sc = string_compare(p1.privilege, p2.privilege); + if (sc != 0) + return (sc < 0); + sc = string_compare(p1.result_extra, p2.result_extra); + if (sc != 0) + return (sc < 0); + return p1.result < p2.result; +} + +bool policy_equal(const cynara_admin_policy &p1, const cynara_admin_policy &p2) +{ + return (p1.result == p2.result + && string_compare(p1.bucket, p2.bucket) == 0 + && string_compare(p1.client, p2.client) == 0 + && string_compare(p1.user, p2.user) == 0 + && string_compare(p1.privilege, p2.privilege) == 0 + && string_compare(p1.result_extra, p2.result_extra) == 0); } +} // namespace anonymous + CynaraPoliciesContainer::CynaraPoliciesContainer() { } +CynaraPoliciesContainer::CynaraPoliciesContainer(struct cynara_admin_policy **policies) +{ + if (!policies) + return; + + for (int i = 0; policies[i]; ++i) { + auto policyPtr = policies[i]; + m_policies.push_back(*policyPtr); + free(policyPtr); + } + free(policies); +} + CynaraPoliciesContainer::~CynaraPoliciesContainer() { for (struct cynara_admin_policy &policy : m_policies) { @@ -95,6 +154,20 @@ void CynaraPoliciesContainer::add(const char *bucket, add(bucket, checkKey.m_client, checkKey.m_user, checkKey.m_privilege, result, resultExtra); } +void CynaraPoliciesContainer::sort() +{ + std::sort(m_policies.begin(), m_policies.end(), policy_less); +} + +std::ostream& operator<<(std::ostream& os, const CynaraPoliciesContainer &policies) +{ + os << "{" << std::endl; + for (const auto & policy : policies.m_policies) + os << policy; + os << "}"; + return os; +} + Admin::Admin() : m_admin(nullptr) { @@ -188,4 +261,56 @@ void Admin::adminCheck(const char *startBucket, int recursive, << dump()); } +void Admin::listPolicies(const char *startBucket, + const char *client, const char *user, const char *privilege, + CynaraPoliciesContainer &expectedPolicyList, + int expectedResult) { + + struct cynara_admin_policy **policies; + + int ret = cynara_admin_list_policies(m_admin, + startBucket, + client, user, privilege, + &policies); + + CynaraPoliciesContainer receivedPolicyList(policies); + receivedPolicyList.sort(); + expectedPolicyList.sort(); + + auto dump = [&]() -> std::string + { + std::stringstream s; + s << " functionReturn: " << ret << "," + << " functionExpectedReturn: " << expectedResult << ","; + + s << " startBucket: " << formatCstr(startBucket) << "," + << " client: " << formatCstr(client) << "," + << " user: " << formatCstr(user) << "," + << " privilege: " << formatCstr(privilege) << ","; + + s << " receivedPolicyList: " << receivedPolicyList << "," + << " expectedPolicyList: " << expectedPolicyList; + return s.str(); + }; + + RUNNER_ASSERT_MSG(ret == expectedResult, + "cynara_admin_list_policies returned wrong value: " + << ret << " != " << expectedResult << "." + << dump()); + + RUNNER_ASSERT_MSG(receivedPolicyList.m_policies.size() == expectedPolicyList.m_policies.size(), + "size of list returned by cynara_admin_list_policies: " + << receivedPolicyList.m_policies.size() + << " doesn't match expected list size: " + << expectedPolicyList.m_policies.size() << "." + << dump()); + + RUNNER_ASSERT_MSG(std::equal(receivedPolicyList.m_policies.begin(), + receivedPolicyList.m_policies.end(), + expectedPolicyList.m_policies.begin(), + policy_equal), + "list returned by cynara_admin_list_policies doesn't match expected: " + << dump()); +} + } // namespace CynaraTestAdmin diff --git a/tests/cynara-tests/common/cynara_test_admin.h b/tests/cynara-tests/common/cynara_test_admin.h index b153a64..26d910c 100644 --- a/tests/cynara-tests/common/cynara_test_admin.h +++ b/tests/cynara-tests/common/cynara_test_admin.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ #include #include +#include #include namespace CynaraTestAdmin { @@ -30,6 +31,17 @@ class CynaraPoliciesContainer { public: CynaraPoliciesContainer(); + +/** + * \par Description: + * A special constructor stealing all data from all structures cynara_admin_policy + * arranged in a null-terminated list. + * It moves all data from inside structures to own vector, + * but release input list by freeing memory of list elements and list itself. + */ + CynaraPoliciesContainer(struct cynara_admin_policy **policies); + CynaraPoliciesContainer(const CynaraPoliciesContainer&) = delete; + CynaraPoliciesContainer(const CynaraPoliciesContainer&&) = delete; virtual ~CynaraPoliciesContainer(); void add(const char *bucket, @@ -42,6 +54,9 @@ public: const CheckKey &checkKey, const int result, const char *resultExtra = nullptr); + void sort(); + + friend std::ostream& operator<<(std::ostream& os, const CynaraPoliciesContainer &policies); private: friend class Admin; @@ -63,6 +78,10 @@ public: const char *client, const char *user, const char *privilege, int expectedCheckResult, const char *expectedCheckResultExtra, int expectedResult = CYNARA_API_SUCCESS); + void listPolicies(const char *startBucket, + const char *client, const char *user, const char *privilege, + CynaraPoliciesContainer &expectedPolicyList, + int expectedResult = CYNARA_API_SUCCESS); private: struct cynara_admin *m_admin; -- 2.7.4