2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file test/cyad/commands_dispatcher.cpp
18 * @author Aleksander Zdyb <a.zdyb@samsung.com>
20 * @brief Tests for CommandsDispatcher
26 #include <gmock/gmock.h>
27 #include <gtest/gtest.h>
29 #include <cynara-error.h>
30 #include <cynara-policy-types.h>
32 #include <common/types/PolicyKey.h>
33 #include <common/types/PolicyResult.h>
34 #include <cyad/CynaraAdminPolicies.h>
35 #include <cyad/CommandlineParser/CyadCommand.h>
36 #include <cyad/CommandsDispatcher.h>
38 #include "CyadCommandlineDispatcherTest.h"
39 #include "FakeAdminApiWrapper.h"
43 * @brief Dispatcher should not touch admin API on help or error
45 * - Prepare some parsing results not requiring API calls
46 * - Check if no API calls were made
48 TEST_F(CyadCommandlineDispatcherTest, noApi) {
50 using ::testing::Return;
52 FakeAdminApiWrapper adminApi;
54 EXPECT_CALL(adminApi, cynara_admin_initialize(_)).WillOnce(Return(CYNARA_API_SUCCESS));
55 EXPECT_CALL(adminApi, cynara_admin_finish(_)).WillOnce(Return(CYNARA_API_SUCCESS));
57 Cynara::CommandsDispatcher dispatcher(m_io, adminApi);
59 Cynara::CyadCommand result;
60 Cynara::HelpCyadCommand helpResult;
61 Cynara::ErrorCyadCommand errorResult("Fake error");
63 dispatcher.execute(result);
64 dispatcher.execute(helpResult);
65 dispatcher.execute(errorResult);
68 TEST_F(CyadCommandlineDispatcherTest, deleteBucket) {
70 using ::testing::Return;
71 using ::testing::StrEq;
72 using ::testing::IsNull;
74 FakeAdminApiWrapper adminApi;
76 EXPECT_CALL(adminApi, cynara_admin_initialize(_)).WillOnce(Return(CYNARA_API_SUCCESS));
77 EXPECT_CALL(adminApi, cynara_admin_finish(_)).WillOnce(Return(CYNARA_API_SUCCESS));
79 Cynara::CommandsDispatcher dispatcher(m_io, adminApi);
80 Cynara::DeleteBucketCyadCommand result("test-bucket");
83 cynara_admin_set_bucket(_, StrEq("test-bucket"), CYNARA_ADMIN_DELETE, IsNull()))
84 .WillOnce(Return(CYNARA_API_SUCCESS));
86 dispatcher.execute(result);
89 TEST_F(CyadCommandlineDispatcherTest, setBucket) {
91 using ::testing::Return;
92 using ::testing::StrEq;
93 using ::testing::IsNull;
94 using Cynara::PolicyBucketId;
95 using Cynara::PolicyType;
96 using Cynara::PolicyResult;
98 FakeAdminApiWrapper adminApi;
100 EXPECT_CALL(adminApi, cynara_admin_initialize(_)).WillOnce(Return(CYNARA_API_SUCCESS));
101 EXPECT_CALL(adminApi, cynara_admin_finish(_)).WillOnce(Return(CYNARA_API_SUCCESS));
103 Cynara::CommandsDispatcher dispatcher(m_io, adminApi);
105 typedef std::tuple<PolicyBucketId, PolicyResult> BucketData;
106 typedef std::vector<BucketData> Buckets;
107 const Buckets buckets = { BucketData("test-bucket-1", { CYNARA_ADMIN_ALLOW, "" }),
108 BucketData("test-bucket-2", { CYNARA_ADMIN_DENY, "" }),
109 BucketData("test-bucket-3", { CYNARA_ADMIN_BUCKET, "other-bucket" }),
110 BucketData("test-bucket-2", { CYNARA_ADMIN_NONE, "" }),
111 BucketData("test-bucket-4", { 42, "douglas-noel-adams" }) };
113 for (const auto &bucket : buckets) {
114 const auto &bucketId = std::get<0>(bucket);
115 const auto &policyResult = std::get<1>(bucket);
117 SCOPED_TRACE(bucketId);
119 Cynara::SetBucketCyadCommand result(bucketId, policyResult);
121 if (policyResult.metadata().empty() == false) {
122 EXPECT_CALL(adminApi,
123 cynara_admin_set_bucket(_, StrEq(bucketId.c_str()), policyResult.policyType(),
124 StrEq(policyResult.metadata().c_str())))
125 .WillOnce(Return(CYNARA_API_SUCCESS));
127 EXPECT_CALL(adminApi,
128 cynara_admin_set_bucket(_, StrEq(bucketId.c_str()), policyResult.policyType(),
130 .WillOnce(Return(CYNARA_API_SUCCESS));
133 dispatcher.execute(result);
137 TEST_F(CyadCommandlineDispatcherTest, setPolicy) {
139 using ::testing::Return;
141 FakeAdminApiWrapper adminApi;
143 EXPECT_CALL(adminApi, cynara_admin_initialize(_)).WillOnce(Return(CYNARA_API_SUCCESS));
144 EXPECT_CALL(adminApi, cynara_admin_finish(_)).WillOnce(Return(CYNARA_API_SUCCESS));
146 Cynara::CommandsDispatcher dispatcher(m_io, adminApi);
147 Cynara::SetPolicyCyadCommand result("test-bucket", { CYNARA_ADMIN_ALLOW, "" },
148 { "client", "user", "privilege" });
150 Cynara::CynaraAdminPolicies expectedPolicies;
151 expectedPolicies.add("test-bucket", { CYNARA_ADMIN_ALLOW, "" },
152 { "client", "user", "privilege"} );
153 expectedPolicies.seal();
155 EXPECT_CALL(adminApi, cynara_admin_set_policies(_, AdmPolicyListEq(expectedPolicies.data())))
156 .WillOnce(Return(CYNARA_API_SUCCESS));
158 dispatcher.execute(result);
161 TEST_F(CyadCommandlineDispatcherTest, setPolicyWithMetadata) {
163 using ::testing::Return;
165 FakeAdminApiWrapper adminApi;
167 EXPECT_CALL(adminApi, cynara_admin_initialize(_)).WillOnce(Return(CYNARA_API_SUCCESS));
168 EXPECT_CALL(adminApi, cynara_admin_finish(_)).WillOnce(Return(CYNARA_API_SUCCESS));
170 Cynara::CommandsDispatcher dispatcher(m_io, adminApi);
171 Cynara::SetPolicyCyadCommand result("test-bucket", { CYNARA_ADMIN_ALLOW, "metadata" },
172 Cynara::PolicyKey("client", "user", "privilege"));
174 Cynara::CynaraAdminPolicies expectedPolicies;
175 expectedPolicies.add("test-bucket", { CYNARA_ADMIN_ALLOW, "metadata" },
176 { "client", "user", "privilege"} );
177 expectedPolicies.seal();
179 EXPECT_CALL(adminApi, cynara_admin_set_policies(_, AdmPolicyListEq(expectedPolicies.data())))
180 .WillOnce(Return(CYNARA_API_SUCCESS));
182 dispatcher.execute(result);