From: Aleksander Zdyb Date: Fri, 20 Jun 2014 13:01:14 +0000 (+0200) Subject: Add tests for Cynara::PolicyBucket X-Git-Tag: accepted/tizen/common/20140722.142604~96 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f881b995b1165d34458a3f2c448df14f3f5779c6;p=platform%2Fcore%2Fsecurity%2Fcynara.git Add tests for Cynara::PolicyBucket Added some convenience methods to PolicyType and PolicyResult Change-Id: Ibcc1200403132cd16879c6afbc859e851c2a3dac --- diff --git a/src/common/types/PolicyResult.h b/src/common/types/PolicyResult.h index b0d0420..a8febc2 100644 --- a/src/common/types/PolicyResult.h +++ b/src/common/types/PolicyResult.h @@ -25,6 +25,8 @@ #include "types/PolicyType.h" +#include + namespace Cynara { class PolicyResult { @@ -56,6 +58,10 @@ public: bool operator ==(const PolicyResult &other) const { return std::tie(m_type, m_metadata) == std::tie(other.m_type, other.m_metadata); } + + bool operator ==(const PolicyType &policyType) const { + return (m_type == policyType) && m_metadata.empty(); + } }; } // namespace Cynara diff --git a/src/common/types/PolicyType.cpp b/src/common/types/PolicyType.cpp new file mode 100644 index 0000000..bf022f5 --- /dev/null +++ b/src/common/types/PolicyType.cpp @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2014 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file PolicyType.cpp + * @author Aleksander Zdyb + * @version 1.0 + * @brief Implementation of functions for Cynara::PolicyType + */ + +#include "PolicyType.h" +#include "PolicyResult.h" + +namespace Cynara { + +bool operator ==(const PolicyType &policyType, const PolicyResult &policyResult) { + return policyResult == policyType; +} + +} // namespace Cynara diff --git a/src/common/types/PolicyType.h b/src/common/types/PolicyType.h index 82d4740..9131b1b 100644 --- a/src/common/types/PolicyType.h +++ b/src/common/types/PolicyType.h @@ -37,6 +37,9 @@ namespace PredefinedPolicyType { const PolicyType ALLOW = 0xFFFF; }; +class PolicyResult; +bool operator ==(const PolicyType &policyType, const PolicyResult &policyResult); + } // namespace Cynara #endif /* SRC_COMMON_TYPES_POLICYTYPE_H_ */ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d9ad4bd..fddc664 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -28,6 +28,7 @@ SET(CYNARA_SOURCES_FOR_TESTS ${CYNARA_SRC}/service/storage/InMemoryStorageBackend.cpp ${CYNARA_SRC}/service/storage/StorageSerializer.cpp ${CYNARA_SRC}/common/types/PolicyKey.cpp + ${CYNARA_SRC}/common/types/PolicyType.cpp ) SET(CYNARA_TESTS_SOURCES @@ -39,6 +40,7 @@ SET(CYNARA_TESTS_SOURCES storage/inmemorystoragebackend/search.cpp storage/inmemorystoragebackend/buckets.cpp storage/serializer/dump.cpp + common/types/policybucket.cpp helpers.cpp ) diff --git a/test/common/types/policybucket.cpp b/test/common/types/policybucket.cpp new file mode 100644 index 0000000..06afbbb --- /dev/null +++ b/test/common/types/policybucket.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2014 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file policybucket.cpp + * @author Aleksander Zdyb + * @version 1.0 + * @brief Tests for Cynara::PolicyBucket + */ + +#include +#include + +#include "common/types/PolicyBucket.h" +#include "common/types/PolicyKey.h" +#include "common/types/PolicyCollection.h" + +#include "../../helpers.h" + +using namespace Cynara; + +class PolicyBucketFixture : public ::testing::Test { +public: + virtual ~PolicyBucketFixture() {} + +protected: + const PolicyKey pk1 = Helpers::generatePolicyKey("1"); + const PolicyKey otherPk = Helpers::generatePolicyKey("_"); + + const PolicyCollection pk1Policies = { + Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW), + Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW), + Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW) + }; +}; + +TEST_F(PolicyBucketFixture, filtered) { + using ::testing::UnorderedElementsAreArray; + using ::testing::UnorderedElementsAre; + using ::testing::IsEmpty; + + PolicyBucket bucket(pk1Policies); + bucket.setDefaultPolicy(PredefinedPolicyType::DENY); + auto filtered = bucket.filtered(pk1); + + // Elements match + ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(pk1Policies)); + + // default policy matches + ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy()); +} + +TEST_F(PolicyBucketFixture, filtered_other) { + using ::testing::UnorderedElementsAreArray; + using ::testing::UnorderedElementsAre; + using ::testing::IsEmpty; + + PolicyBucket bucket(pk1Policies); + bucket.setDefaultPolicy(PredefinedPolicyType::DENY); + auto filtered = bucket.filtered(otherPk); + + // No policies should be found + ASSERT_THAT(filtered.policyCollection(), IsEmpty()); + + // default policy should be preserved + ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy()); +}