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/CommandlineParser/CyadCommand.h>
35 #include <cyad/CommandsDispatcher.h>
37 #include "CyadCommandlineDispatcherTest.h"
38 #include "FakeAdminApiWrapper.h"
41 * @brief Dispatcher should not touch admin API on help or error
43 * - Prepare some parsing results not requiring API calls
44 * - Check if no API calls were made
46 TEST_F(CyadCommandlineDispatcherTest, noApi) {
48 using ::testing::Return;
50 FakeAdminApiWrapper adminApi;
52 EXPECT_CALL(adminApi, cynara_admin_initialize(_)).WillOnce(Return(CYNARA_API_SUCCESS));
53 EXPECT_CALL(adminApi, cynara_admin_finish(_)).WillOnce(Return(CYNARA_API_SUCCESS));
55 Cynara::CommandsDispatcher dispatcher(m_io, adminApi);
57 Cynara::CyadCommand result;
58 Cynara::HelpCyadCommand helpResult;
59 Cynara::ErrorCyadCommand errorResult("Fake error");
61 dispatcher.execute(result);
62 dispatcher.execute(helpResult);
63 dispatcher.execute(errorResult);
66 TEST_F(CyadCommandlineDispatcherTest, deleteBucket) {
68 using ::testing::Return;
69 using ::testing::StrEq;
70 using ::testing::IsNull;
72 FakeAdminApiWrapper adminApi;
74 EXPECT_CALL(adminApi, cynara_admin_initialize(_)).WillOnce(Return(CYNARA_API_SUCCESS));
75 EXPECT_CALL(adminApi, cynara_admin_finish(_)).WillOnce(Return(CYNARA_API_SUCCESS));
77 Cynara::CommandsDispatcher dispatcher(m_io, adminApi);
78 Cynara::DeleteBucketCyadCommand result("test-bucket");
81 cynara_admin_set_bucket(_, StrEq("test-bucket"), CYNARA_ADMIN_DELETE, IsNull()))
82 .WillOnce(Return(CYNARA_API_SUCCESS));
84 dispatcher.execute(result);
87 TEST_F(CyadCommandlineDispatcherTest, setBucket) {
89 using ::testing::Return;
90 using ::testing::StrEq;
91 using ::testing::IsNull;
92 using Cynara::PolicyBucketId;
93 using Cynara::PolicyType;
94 using Cynara::PolicyResult;
96 FakeAdminApiWrapper adminApi;
98 EXPECT_CALL(adminApi, cynara_admin_initialize(_)).WillOnce(Return(CYNARA_API_SUCCESS));
99 EXPECT_CALL(adminApi, cynara_admin_finish(_)).WillOnce(Return(CYNARA_API_SUCCESS));
101 Cynara::CommandsDispatcher dispatcher(m_io, adminApi);
103 typedef std::tuple<PolicyBucketId, PolicyResult> BucketData;
104 typedef std::vector<BucketData> Buckets;
105 const Buckets buckets = { BucketData("test-bucket-1", { CYNARA_ADMIN_ALLOW, "" }),
106 BucketData("test-bucket-2", { CYNARA_ADMIN_DENY, "" }),
107 BucketData("test-bucket-3", { CYNARA_ADMIN_BUCKET, "other-bucket" }),
108 BucketData("test-bucket-2", { CYNARA_ADMIN_NONE, "" }),
109 BucketData("test-bucket-4", { 42, "douglas-noel-adams" }) };
111 for (const auto &bucket : buckets) {
112 const auto &bucketId = std::get<0>(bucket);
113 const auto &policyResult = std::get<1>(bucket);
115 SCOPED_TRACE(bucketId);
117 Cynara::SetBucketCyadCommand result(bucketId, policyResult);
119 if (policyResult.metadata().empty() == false) {
120 EXPECT_CALL(adminApi,
121 cynara_admin_set_bucket(_, StrEq(bucketId.c_str()), policyResult.policyType(),
122 StrEq(policyResult.metadata().c_str())))
123 .WillOnce(Return(CYNARA_API_SUCCESS));
125 EXPECT_CALL(adminApi,
126 cynara_admin_set_bucket(_, StrEq(bucketId.c_str()), policyResult.policyType(),
128 .WillOnce(Return(CYNARA_API_SUCCESS));
131 dispatcher.execute(result);