Add --set-bucket and --delete-bucket to Cyad
[platform/core/security/cynara.git] / test / cyad / commands_dispatcher.cpp
1 /*
2  * Copyright (c) 2014 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  * @file        test/cyad/commands_dispatcher.cpp
18  * @author      Aleksander Zdyb <a.zdyb@samsung.com>
19  * @version     1.0
20  * @brief       Tests for CommandsDispatcher
21  */
22
23 #include <tuple>
24 #include <vector>
25
26 #include <gmock/gmock.h>
27 #include <gtest/gtest.h>
28
29 #include <cynara-error.h>
30 #include <cynara-policy-types.h>
31
32 #include <common/types/PolicyKey.h>
33 #include <common/types/PolicyResult.h>
34 #include <cyad/CommandlineParser/CyadCommand.h>
35 #include <cyad/CommandsDispatcher.h>
36
37 #include "CyadCommandlineDispatcherTest.h"
38 #include "FakeAdminApiWrapper.h"
39
40 /**
41  * @brief   Dispatcher should not touch admin API on help or error
42  * @test    Scenario:
43  * - Prepare some parsing results not requiring API calls
44  * - Check if no API calls were made
45  */
46 TEST_F(CyadCommandlineDispatcherTest, noApi) {
47     using ::testing::_;
48     using ::testing::Return;
49
50     FakeAdminApiWrapper adminApi;
51
52     EXPECT_CALL(adminApi, cynara_admin_initialize(_)).WillOnce(Return(CYNARA_API_SUCCESS));
53     EXPECT_CALL(adminApi, cynara_admin_finish(_)).WillOnce(Return(CYNARA_API_SUCCESS));
54
55     Cynara::CommandsDispatcher dispatcher(m_io, adminApi);
56
57     Cynara::CyadCommand result;
58     Cynara::HelpCyadCommand helpResult;
59     Cynara::ErrorCyadCommand errorResult("Fake error");
60
61     dispatcher.execute(result);
62     dispatcher.execute(helpResult);
63     dispatcher.execute(errorResult);
64 }
65
66 TEST_F(CyadCommandlineDispatcherTest, deleteBucket) {
67     using ::testing::_;
68     using ::testing::Return;
69     using ::testing::StrEq;
70     using ::testing::IsNull;
71
72     FakeAdminApiWrapper adminApi;
73
74     EXPECT_CALL(adminApi, cynara_admin_initialize(_)).WillOnce(Return(CYNARA_API_SUCCESS));
75     EXPECT_CALL(adminApi, cynara_admin_finish(_)).WillOnce(Return(CYNARA_API_SUCCESS));
76
77     Cynara::CommandsDispatcher dispatcher(m_io, adminApi);
78     Cynara::DeleteBucketCyadCommand result("test-bucket");
79
80     EXPECT_CALL(adminApi,
81             cynara_admin_set_bucket(_, StrEq("test-bucket"), CYNARA_ADMIN_DELETE, IsNull()))
82         .WillOnce(Return(CYNARA_API_SUCCESS));
83
84     dispatcher.execute(result);
85 }
86
87 TEST_F(CyadCommandlineDispatcherTest, setBucket) {
88     using ::testing::_;
89     using ::testing::Return;
90     using ::testing::StrEq;
91     using ::testing::IsNull;
92     using Cynara::PolicyBucketId;
93     using Cynara::PolicyType;
94     using Cynara::PolicyResult;
95
96     FakeAdminApiWrapper adminApi;
97
98     EXPECT_CALL(adminApi, cynara_admin_initialize(_)).WillOnce(Return(CYNARA_API_SUCCESS));
99     EXPECT_CALL(adminApi, cynara_admin_finish(_)).WillOnce(Return(CYNARA_API_SUCCESS));
100
101     Cynara::CommandsDispatcher dispatcher(m_io, adminApi);
102
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" }) };
110
111     for (const auto &bucket : buckets) {
112         const auto &bucketId = std::get<0>(bucket);
113         const auto &policyResult = std::get<1>(bucket);
114
115         SCOPED_TRACE(bucketId);
116
117         Cynara::SetBucketCyadCommand result(bucketId, policyResult);
118
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));
124         } else {
125             EXPECT_CALL(adminApi,
126                     cynara_admin_set_bucket(_, StrEq(bucketId.c_str()), policyResult.policyType(),
127                                            IsNull()))
128                 .WillOnce(Return(CYNARA_API_SUCCESS));
129         }
130
131         dispatcher.execute(result);
132     }
133 }