1 /*
2  * Copyright (C) 2022-2023 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_proxy_test.h"
17 
18 #include "iam_ptr.h"
19 #include "executor_callback_proxy.h"
20 #include "mock_executor_messenger_service.h"
21 #include "mock_remote_object.h"
22 
23 namespace OHOS {
24 namespace UserIam {
25 namespace UserAuth {
26 using namespace testing;
27 using namespace testing::ext;
28 
SetUpTestCase()29 void ExecutorCallbackProxyTest::SetUpTestCase()
30 {
31 }
32 
TearDownTestCase()33 void ExecutorCallbackProxyTest::TearDownTestCase()
34 {
35 }
36 
SetUp()37 void ExecutorCallbackProxyTest::SetUp()
38 {
39 }
40 
TearDown()41 void ExecutorCallbackProxyTest::TearDown()
42 {
43 }
44 
45 HWTEST_F(ExecutorCallbackProxyTest, TestOnMessengerReady_001, TestSize.Level0)
46 {
47     sptr<ExecutorMessengerInterface> messenger(nullptr);
48     std::vector<uint8_t> publicKey;
49     std::vector<uint64_t> templateIdList;
50 
51     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
52     EXPECT_NE(obj, nullptr);
53     auto proxy = Common::MakeShared<ExecutorCallbackProxy>(obj);
54     EXPECT_NE(proxy, nullptr);
55 
56     proxy->OnMessengerReady(messenger, publicKey, templateIdList);
57 }
58 
59 HWTEST_F(ExecutorCallbackProxyTest, TestOnMessengerReady_002, TestSize.Level0)
60 {
61     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
62     EXPECT_NE(obj, nullptr);
63     EXPECT_CALL(*obj, SendRequest(_, _, _, _))
64         .WillOnce(
__anon60e9bba10102(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 65             [](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
66                 EXPECT_EQ(code, ExecutorCallbackInterfaceCode::ON_MESSENGER_READY);
67                 return OHOS::NO_ERROR;
68             }
69         );
70 
71     auto proxy = Common::MakeShared<ExecutorCallbackProxy>(obj);
72     EXPECT_NE(proxy, nullptr);
73 
74     sptr<ExecutorMessengerInterface> messenger(new (std::nothrow) MockExecutorMessengerService());
75     EXPECT_NE(messenger, nullptr);
76     std::vector<uint8_t> publicKey;
77     std::vector<uint64_t> templateIdList;
78     proxy->OnMessengerReady(messenger, publicKey, templateIdList);
79 }
80 
81 HWTEST_F(ExecutorCallbackProxyTest, TestOnBeginExecute_001, TestSize.Level0)
82 {
83     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
84     EXPECT_NE(obj, nullptr);
85     EXPECT_CALL(*obj, SendRequest(_, _, _, _))
86         .WillOnce(
__anon60e9bba10202(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 87             [](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
88                 EXPECT_EQ(code, ExecutorCallbackInterfaceCode::ON_BEGIN_EXECUTE);
89                 EXPECT_TRUE(reply.WriteInt32(SUCCESS));
90                 return OHOS::NO_ERROR;
91             }
92         );
93 
94     auto proxy = Common::MakeShared<ExecutorCallbackProxy>(obj);
95     EXPECT_NE(proxy, nullptr);
96 
97     uint64_t scheduleId = 321562;
98     std::vector<uint8_t> publicKey;
99     Attributes command;
100 
101     EXPECT_EQ(proxy->OnBeginExecute(scheduleId, publicKey, command), SUCCESS);
102 }
103 
104 HWTEST_F(ExecutorCallbackProxyTest, TestOnEndExecute_001, TestSize.Level0)
105 {
106     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
107     EXPECT_NE(obj, nullptr);
108     EXPECT_CALL(*obj, SendRequest(_, _, _, _))
109         .WillOnce(
__anon60e9bba10302(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 110             [](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
111                 EXPECT_EQ(code, ExecutorCallbackInterfaceCode::ON_END_EXECUTE);
112                 EXPECT_TRUE(reply.WriteInt32(SUCCESS));
113                 return OHOS::NO_ERROR;
114             }
115         );
116 
117     auto proxy = Common::MakeShared<ExecutorCallbackProxy>(obj);
118     EXPECT_NE(proxy, nullptr);
119 
120     uint64_t scheduleId = 321562;
121     Attributes command;
122     EXPECT_EQ(proxy->OnEndExecute(scheduleId, command), SUCCESS);
123 }
124 
125 HWTEST_F(ExecutorCallbackProxyTest, TestOnSetProperty_001, TestSize.Level0)
126 {
127     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
128     EXPECT_NE(obj, nullptr);
129     EXPECT_CALL(*obj, SendRequest(_, _, _, _))
130         .WillOnce(
__anon60e9bba10402(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 131             [](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
132                 EXPECT_EQ(code, ExecutorCallbackInterfaceCode::ON_SET_PROPERTY);
133                 EXPECT_TRUE(reply.WriteInt32(SUCCESS));
134                 return OHOS::NO_ERROR;
135             }
136         );
137 
138     auto proxy = Common::MakeShared<ExecutorCallbackProxy>(obj);
139     EXPECT_NE(proxy, nullptr);
140 
141     Attributes properties;
142     EXPECT_EQ(proxy->OnSetProperty(properties), SUCCESS);
143 }
144 
145 HWTEST_F(ExecutorCallbackProxyTest, TestOnGetProperty_001, TestSize.Level0)
146 {
147     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
148     EXPECT_NE(obj, nullptr);
149     EXPECT_CALL(*obj, SendRequest(_, _, _, _))
150         .Times(2)
151         .WillOnce(
__anon60e9bba10502(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 152             [](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
153                 EXPECT_EQ(code, ExecutorCallbackInterfaceCode::ON_GET_PROPERTY);
154                 EXPECT_TRUE(reply.WriteInt32(SUCCESS));
155                 return OHOS::NO_ERROR;
156             }
157         )
158         .WillOnce(
__anon60e9bba10602(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 159             [](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
160                 EXPECT_EQ(code, ExecutorCallbackInterfaceCode::ON_GET_PROPERTY);
161                 return GENERAL_ERROR;
162             }
163         );
164 
165     auto proxy = Common::MakeShared<ExecutorCallbackProxy>(obj);
166     EXPECT_NE(proxy, nullptr);
167 
168     Attributes condition;
169     Attributes values;
170     EXPECT_EQ(proxy->OnGetProperty(condition, values), SUCCESS);
171     EXPECT_EQ(proxy->OnGetProperty(condition, values), GENERAL_ERROR);
172 }
173 } // namespace UserAuth
174 } // namespace UserIam
175 } // namespace OHOS
176