• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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](const std::shared_ptr<ExecutorMessenger> &messenger,
__anoncc8256f80102(const std::shared_ptr<ExecutorMessenger> &messenger, const std::vector<uint8_t> &publicKey, const std::vector<uint64_t> &templateIds) 68                 const std::vector<uint8_t> &publicKey, const std::vector<uint64_t> &templateIds) {
69                 EXPECT_NE(messenger, nullptr);
70                 EXPECT_THAT(publicKey, ElementsAreArray(testPublicKey));
71                 EXPECT_THAT(templateIds, ElementsAreArray(testTemplateIdList));
72             }
73         );
74     auto service = Common::MakeShared<ExecutorCallbackService>(testCallback);
75     EXPECT_NE(service, nullptr);
76     service->OnMessengerReady(testMessenger, testPublicKey, testTemplateIdList);
77 }
78 
79 HWTEST_F(ExecutorCallbackServiceTest, ExecutorCallbackServiceTestOnBeginExecute001, TestSize.Level0)
80 {
81     uint64_t testScheduleId = 57875;
82     std::vector<uint8_t> testPublicKey = {1, 2, 3, 4};
83     Attributes testCommand;
84 
85     std::shared_ptr<ExecutorRegisterCallback> testCallback = nullptr;
86     auto service = Common::MakeShared<ExecutorCallbackService>(testCallback);
87     EXPECT_NE(service, nullptr);
88     int32_t result = service->OnBeginExecute(testScheduleId, testPublicKey, testCommand);
89     EXPECT_EQ(result, GENERAL_ERROR);
90 }
91 
92 HWTEST_F(ExecutorCallbackServiceTest, ExecutorCallbackServiceTestOnBeginExecute002, TestSize.Level0)
93 {
94     uint64_t testScheduleId = 57875;
95     std::vector<uint8_t> testPublicKey = {1, 2, 3, 4};
96     Attributes testCommand;
97 
98     auto testCallback = Common::MakeShared<MockExecutorRegisterCallback>();
99     EXPECT_NE(testCallback, nullptr);
100     EXPECT_CALL(*testCallback, OnBeginExecute(_, _, _)).Times(1);
101     ON_CALL(*testCallback, OnBeginExecute)
102         .WillByDefault(
103             [&testScheduleId, &testPublicKey](uint64_t scheduleId, const std::vector<uint8_t> &publicKey,
__anoncc8256f80202(uint64_t scheduleId, const std::vector<uint8_t> &publicKey, const Attributes &commandAttrs) 104                 const Attributes &commandAttrs) {
105                 EXPECT_EQ(scheduleId, testScheduleId);
106                 EXPECT_THAT(publicKey, ElementsAreArray(testPublicKey));
107                 return SUCCESS;
108             }
109         );
110     auto service = Common::MakeShared<ExecutorCallbackService>(testCallback);
111     EXPECT_NE(service, nullptr);
112     int32_t result = service->OnBeginExecute(testScheduleId, testPublicKey, testCommand);
113     EXPECT_EQ(result, SUCCESS);
114 }
115 
116 HWTEST_F(ExecutorCallbackServiceTest, ExecutorCallbackServiceTestOnEndExecute001, TestSize.Level0)
117 {
118     uint64_t testScheduleId = 57875;
119     Attributes testCommand;
120 
121     std::shared_ptr<ExecutorRegisterCallback> testCallback = nullptr;
122     auto service = Common::MakeShared<ExecutorCallbackService>(testCallback);
123     EXPECT_NE(service, nullptr);
124     int32_t result = service->OnEndExecute(testScheduleId, testCommand);
125     EXPECT_EQ(result, GENERAL_ERROR);
126 }
127 
128 HWTEST_F(ExecutorCallbackServiceTest, ExecutorCallbackServiceTestOnEndExecute002, TestSize.Level0)
129 {
130     uint64_t testScheduleId = 57875;
131     Attributes testCommand;
132 
133     auto testCallback = Common::MakeShared<MockExecutorRegisterCallback>();
134     EXPECT_NE(testCallback, nullptr);
135     EXPECT_CALL(*testCallback, OnEndExecute(_, _)).Times(1);
136     ON_CALL(*testCallback, OnEndExecute)
137         .WillByDefault(
__anoncc8256f80302(uint64_t scheduleId, const Attributes &commandAttrs) 138             [&testScheduleId](uint64_t scheduleId, const Attributes &commandAttrs) {
139                 EXPECT_EQ(scheduleId, testScheduleId);
140                 return SUCCESS;
141             }
142         );
143     auto service = Common::MakeShared<ExecutorCallbackService>(testCallback);
144     EXPECT_NE(service, nullptr);
145     int32_t result = service->OnEndExecute(testScheduleId, testCommand);
146     EXPECT_EQ(result, SUCCESS);
147 }
148 
149 HWTEST_F(ExecutorCallbackServiceTest, ExecutorCallbackServiceTestOnSetProperty001, TestSize.Level0)
150 {
151     Attributes testProperties;
152 
153     std::shared_ptr<ExecutorRegisterCallback> testCallback = nullptr;
154     auto service = Common::MakeShared<ExecutorCallbackService>(testCallback);
155     EXPECT_NE(service, nullptr);
156     int32_t result = service->OnSetProperty(testProperties);
157     EXPECT_EQ(result, GENERAL_ERROR);
158 }
159 
160 HWTEST_F(ExecutorCallbackServiceTest, ExecutorCallbackServiceTestOnSetProperty002, TestSize.Level0)
161 {
162     Attributes testProperties;
163 
164     auto testCallback = Common::MakeShared<MockExecutorRegisterCallback>();
165     EXPECT_NE(testCallback, nullptr);
166     EXPECT_CALL(*testCallback, OnSetProperty(_)).Times(1);
167     ON_CALL(*testCallback, OnSetProperty)
168         .WillByDefault(
__anoncc8256f80402(const Attributes &properties) 169             [](const Attributes &properties) {
170                 return SUCCESS;
171             }
172         );
173     auto service = Common::MakeShared<ExecutorCallbackService>(testCallback);
174     EXPECT_NE(service, nullptr);
175     int32_t result = service->OnSetProperty(testProperties);
176     EXPECT_EQ(result, SUCCESS);
177 }
178 
179 HWTEST_F(ExecutorCallbackServiceTest, ExecutorCallbackServiceTestOnGetProperty001, TestSize.Level0)
180 {
181     Attributes testCondition;
182     Attributes testValues;
183 
184     std::shared_ptr<ExecutorRegisterCallback> testCallback = nullptr;
185     auto service = Common::MakeShared<ExecutorCallbackService>(testCallback);
186     EXPECT_NE(service, nullptr);
187     int32_t result = service->OnGetProperty(testCondition, testValues);
188     EXPECT_EQ(result, GENERAL_ERROR);
189 }
190 
191 HWTEST_F(ExecutorCallbackServiceTest, ExecutorCallbackServiceTestOnGetProperty002, TestSize.Level0)
192 {
193     Attributes testCondition;
194     Attributes testValues;
195     int32_t testCode = 544857;
196 
197     auto testCallback = Common::MakeShared<MockExecutorRegisterCallback>();
198     EXPECT_NE(testCallback, nullptr);
199     EXPECT_CALL(*testCallback, OnGetProperty(_, _)).Times(1);
200     ON_CALL(*testCallback, OnGetProperty)
201         .WillByDefault(
__anoncc8256f80502(const Attributes &conditions, Attributes &results) 202             [&testCode](const Attributes &conditions, Attributes &results) {
203                 EXPECT_TRUE(results.SetInt32Value(Attributes::ATTR_RESULT_CODE, testCode));
204                 return SUCCESS;
205             }
206         );
207     auto service = Common::MakeShared<ExecutorCallbackService>(testCallback);
208     EXPECT_NE(service, nullptr);
209     int32_t result = service->OnGetProperty(testCondition, testValues);
210     EXPECT_EQ(result, SUCCESS);
211     int32_t code = 0;
212     EXPECT_TRUE(testValues.GetInt32Value(Attributes::ATTR_RESULT_CODE, code));
213     EXPECT_EQ(code, testCode);
214 }
215 } // namespace UserAuth
216 } // namespace UserIam
217 } // namespace OHOS