1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "executor_callback_service_test.h"
17 
18 #include "executor_callback_service.h"
19 #include "iam_ptr.h"
20 #include "mock_executor_register_callback.h"
21 
22 namespace OHOS {
23 namespace UserIam {
24 namespace UserAuth {
25 using namespace testing;
26 using namespace testing::ext;
27 
SetUpTestCase()28 void ExecutorCallbackServiceTest::SetUpTestCase()
29 {
30 }
31 
TearDownTestCase()32 void ExecutorCallbackServiceTest::TearDownTestCase()
33 {
34 }
35 
SetUp()36 void ExecutorCallbackServiceTest::SetUp()
37 {
38 }
39 
TearDown()40 void ExecutorCallbackServiceTest::TearDown()
41 {
42 }
43 
44 HWTEST_F(ExecutorCallbackServiceTest, ExecutorCallbackServiceTestOnMessengerReady001, TestSize.Level0)
45 {
46     sptr<ExecutorMessengerInterface> testMessenger(nullptr);
47     std::vector<uint8_t> testPublicKey = {1, 2, 3, 4};
48     std::vector<uint64_t> testTemplateIdList = {12, 13, 14, 15};
49 
50     std::shared_ptr<ExecutorRegisterCallback> testCallback = nullptr;
51     auto service = Common::MakeShared<ExecutorCallbackService>(testCallback);
52     EXPECT_NE(service, nullptr);
53     service->OnMessengerReady(testMessenger, testPublicKey, testTemplateIdList);
54 }
55 
56 HWTEST_F(ExecutorCallbackServiceTest, ExecutorCallbackServiceTestOnMessengerReady002, TestSize.Level0)
57 {
58     sptr<ExecutorMessengerInterface> testMessenger(nullptr);
59     std::vector<uint8_t> testPublicKey = {1, 2, 3, 4};
60     std::vector<uint64_t> testTemplateIdList = {12, 13, 14, 15};
61 
62     auto testCallback = Common::MakeShared<MockExecutorRegisterCallback>();
63     EXPECT_NE(testCallback, nullptr);
64     EXPECT_CALL(*testCallback, OnMessengerReady(_, _, _)).Times(1);
65     ON_CALL(*testCallback, OnMessengerReady)
66         .WillByDefault(
67             [&testPublicKey, &testTemplateIdList](
68             const std::shared_ptr<ExecutorMessenger> &messenger, const std::vector<uint8_t> &publicKey,
__anon4997f8b00102( const std::shared_ptr<ExecutorMessenger> &messenger, const std::vector<uint8_t> &publicKey, const std::vector<uint64_t> &templateIdList) 69             const std::vector<uint64_t> &templateIdList) {
70                 EXPECT_NE(messenger, nullptr);
71                 EXPECT_THAT(publicKey, ElementsAreArray(testPublicKey));
72                 EXPECT_THAT(templateIdList, ElementsAreArray(testTemplateIdList));
73             }
74         );
75     auto service = Common::MakeShared<ExecutorCallbackService>(testCallback);
76     EXPECT_NE(service, nullptr);
77     service->OnMessengerReady(testMessenger, testPublicKey, testTemplateIdList);
78 }
79 
80 HWTEST_F(ExecutorCallbackServiceTest, ExecutorCallbackServiceTestOnBeginExecute001, TestSize.Level0)
81 {
82     uint64_t testScheduleId = 57875;
83     std::vector<uint8_t> testPublicKey = {1, 2, 3, 4};
84     Attributes testCommand;
85 
86     std::shared_ptr<ExecutorRegisterCallback> testCallback = nullptr;
87     auto service = Common::MakeShared<ExecutorCallbackService>(testCallback);
88     EXPECT_NE(service, nullptr);
89     int32_t result = service->OnBeginExecute(testScheduleId, testPublicKey, testCommand);
90     EXPECT_EQ(result, GENERAL_ERROR);
91 }
92 
93 HWTEST_F(ExecutorCallbackServiceTest, ExecutorCallbackServiceTestOnBeginExecute002, TestSize.Level0)
94 {
95     uint64_t testScheduleId = 57875;
96     std::vector<uint8_t> testPublicKey = {1, 2, 3, 4};
97     Attributes testCommand;
98 
99     auto testCallback = Common::MakeShared<MockExecutorRegisterCallback>();
100     EXPECT_NE(testCallback, nullptr);
101     EXPECT_CALL(*testCallback, OnBeginExecute(_, _, _)).Times(1);
102     ON_CALL(*testCallback, OnBeginExecute)
103         .WillByDefault(
104             [&testScheduleId, &testPublicKey](uint64_t scheduleId, const std::vector<uint8_t> &publicKey,
__anon4997f8b00202(uint64_t scheduleId, const std::vector<uint8_t> &publicKey, const Attributes &commandAttrs) 105                 const Attributes &commandAttrs) {
106                 EXPECT_EQ(scheduleId, testScheduleId);
107                 EXPECT_THAT(publicKey, ElementsAreArray(testPublicKey));
108                 return SUCCESS;
109             }
110         );
111     auto service = Common::MakeShared<ExecutorCallbackService>(testCallback);
112     EXPECT_NE(service, nullptr);
113     int32_t result = service->OnBeginExecute(testScheduleId, testPublicKey, testCommand);
114     EXPECT_EQ(result, SUCCESS);
115 }
116 
117 HWTEST_F(ExecutorCallbackServiceTest, ExecutorCallbackServiceTestOnEndExecute001, TestSize.Level0)
118 {
119     uint64_t testScheduleId = 57875;
120     Attributes testCommand;
121 
122     std::shared_ptr<ExecutorRegisterCallback> testCallback = nullptr;
123     auto service = Common::MakeShared<ExecutorCallbackService>(testCallback);
124     EXPECT_NE(service, nullptr);
125     int32_t result = service->OnEndExecute(testScheduleId, testCommand);
126     EXPECT_EQ(result, GENERAL_ERROR);
127 }
128 
129 HWTEST_F(ExecutorCallbackServiceTest, ExecutorCallbackServiceTestOnEndExecute002, TestSize.Level0)
130 {
131     uint64_t testScheduleId = 57875;
132     Attributes testCommand;
133 
134     auto testCallback = Common::MakeShared<MockExecutorRegisterCallback>();
135     EXPECT_NE(testCallback, nullptr);
136     EXPECT_CALL(*testCallback, OnEndExecute(_, _)).Times(1);
137     ON_CALL(*testCallback, OnEndExecute)
138         .WillByDefault(
__anon4997f8b00302(uint64_t scheduleId, const Attributes &commandAttrs) 139             [&testScheduleId](uint64_t scheduleId, const Attributes &commandAttrs) {
140                 EXPECT_EQ(scheduleId, testScheduleId);
141                 return SUCCESS;
142             }
143         );
144     auto service = Common::MakeShared<ExecutorCallbackService>(testCallback);
145     EXPECT_NE(service, nullptr);
146     int32_t result = service->OnEndExecute(testScheduleId, testCommand);
147     EXPECT_EQ(result, SUCCESS);
148 }
149 
150 HWTEST_F(ExecutorCallbackServiceTest, ExecutorCallbackServiceTestOnSetProperty001, TestSize.Level0)
151 {
152     Attributes testProperties;
153 
154     std::shared_ptr<ExecutorRegisterCallback> testCallback = nullptr;
155     auto service = Common::MakeShared<ExecutorCallbackService>(testCallback);
156     EXPECT_NE(service, nullptr);
157     int32_t result = service->OnSetProperty(testProperties);
158     EXPECT_EQ(result, GENERAL_ERROR);
159 }
160 
161 HWTEST_F(ExecutorCallbackServiceTest, ExecutorCallbackServiceTestOnSetProperty002, TestSize.Level0)
162 {
163     Attributes testProperties;
164 
165     auto testCallback = Common::MakeShared<MockExecutorRegisterCallback>();
166     EXPECT_NE(testCallback, nullptr);
167     EXPECT_CALL(*testCallback, OnSetProperty(_)).Times(1);
168     ON_CALL(*testCallback, OnSetProperty)
169         .WillByDefault(
__anon4997f8b00402(const Attributes &properties) 170             [](const Attributes &properties) {
171                 return SUCCESS;
172             }
173         );
174     auto service = Common::MakeShared<ExecutorCallbackService>(testCallback);
175     EXPECT_NE(service, nullptr);
176     int32_t result = service->OnSetProperty(testProperties);
177     EXPECT_EQ(result, SUCCESS);
178 }
179 
180 HWTEST_F(ExecutorCallbackServiceTest, ExecutorCallbackServiceTestOnGetProperty001, TestSize.Level0)
181 {
182     Attributes testCondition;
183     Attributes testValues;
184 
185     std::shared_ptr<ExecutorRegisterCallback> testCallback = nullptr;
186     auto service = Common::MakeShared<ExecutorCallbackService>(testCallback);
187     EXPECT_NE(service, nullptr);
188     int32_t result = service->OnGetProperty(testCondition, testValues);
189     EXPECT_EQ(result, GENERAL_ERROR);
190 }
191 
192 HWTEST_F(ExecutorCallbackServiceTest, ExecutorCallbackServiceTestOnGetProperty002, TestSize.Level0)
193 {
194     Attributes testCondition;
195     Attributes testValues;
196     int32_t testCode = 544857;
197 
198     auto testCallback = Common::MakeShared<MockExecutorRegisterCallback>();
199     EXPECT_NE(testCallback, nullptr);
200     EXPECT_CALL(*testCallback, OnGetProperty(_, _)).Times(1);
201     ON_CALL(*testCallback, OnGetProperty)
202         .WillByDefault(
__anon4997f8b00502(const Attributes &conditions, Attributes &results) 203             [&testCode](const Attributes &conditions, Attributes &results) {
204                 EXPECT_TRUE(results.SetInt32Value(Attributes::ATTR_RESULT_CODE, testCode));
205                 return SUCCESS;
206             }
207         );
208     auto service = Common::MakeShared<ExecutorCallbackService>(testCallback);
209     EXPECT_NE(service, nullptr);
210     int32_t result = service->OnGetProperty(testCondition, testValues);
211     EXPECT_EQ(result, SUCCESS);
212     int32_t code = 0;
213     EXPECT_TRUE(testValues.GetInt32Value(Attributes::ATTR_RESULT_CODE, code));
214     EXPECT_EQ(code, testCode);
215 }
216 } // namespace UserAuth
217 } // namespace UserIam
218 } // namespace OHOS