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_stub_test.h"
17 
18 #include "message_parcel.h"
19 
20 #include "iam_ptr.h"
21 #include "mock_executor_callback_service.h"
22 #include "mock_executor_messenger_service.h"
23 
24 namespace OHOS {
25 namespace UserIam {
26 namespace UserAuth {
27 using namespace testing;
28 using namespace testing::ext;
29 
SetUpTestCase()30 void ExecutorCallbackStubTest::SetUpTestCase()
31 {
32 }
33 
TearDownTestCase()34 void ExecutorCallbackStubTest::TearDownTestCase()
35 {
36 }
37 
SetUp()38 void ExecutorCallbackStubTest::SetUp()
39 {
40 }
41 
TearDown()42 void ExecutorCallbackStubTest::TearDown()
43 {
44 }
45 
46 HWTEST_F(ExecutorCallbackStubTest, TestOnMessengerReadyStub_001, TestSize.Level0)
47 {
48     sptr<MockExecutorMessengerService> messenger(new (std::nothrow) MockExecutorMessengerService());
49     EXPECT_NE(messenger, nullptr);
50     std::vector<uint8_t> publicKey;
51     std::vector<uint64_t> templateIdList;
52 
53     MessageParcel data;
54     MessageParcel reply;
55     MessageOption option(MessageOption::TF_SYNC);
56     uint32_t code = ExecutorCallbackInterfaceCode::ON_MESSENGER_READY;
57 
58     EXPECT_TRUE(data.WriteInterfaceToken(ExecutorCallbackInterface::GetDescriptor()));
59     EXPECT_TRUE(data.WriteRemoteObject(messenger->AsObject()));
60     EXPECT_TRUE(data.WriteUInt8Vector(publicKey));
61     EXPECT_TRUE(data.WriteUInt64Vector(templateIdList));
62 
63     auto service = Common::MakeShared<MockExecutorCallbackService>();
64     EXPECT_NE(service, nullptr);
65     EXPECT_CALL(*service, OnMessengerReady(_, _, _)).Times(1);
66 
67     EXPECT_EQ(service->OnRemoteRequest(code, data, reply, option), SUCCESS);
68 }
69 
70 HWTEST_F(ExecutorCallbackStubTest, TestOnBeginExecuteStub_001, TestSize.Level0)
71 {
72     uint64_t scheduleId = 231527;
73     std::vector<uint8_t> publicKey;
74     std::vector<uint8_t> command;
75 
76     MessageParcel data;
77     MessageParcel reply;
78     MessageOption option(MessageOption::TF_SYNC);
79     uint32_t code = ExecutorCallbackInterfaceCode::ON_BEGIN_EXECUTE;
80 
81     EXPECT_TRUE(data.WriteInterfaceToken(ExecutorCallbackInterface::GetDescriptor()));
82     EXPECT_TRUE(data.WriteUint64(scheduleId));
83     EXPECT_TRUE(data.WriteUInt8Vector(publicKey));
84     EXPECT_TRUE(data.WriteUInt8Vector(command));
85 
86     auto service = Common::MakeShared<MockExecutorCallbackService>();
87     EXPECT_NE(service, nullptr);
88     EXPECT_CALL(*service, OnBeginExecute(_, _, _)).Times(1);
89 
90     EXPECT_EQ(service->OnRemoteRequest(code, data, reply, option), SUCCESS);
91 }
92 
93 HWTEST_F(ExecutorCallbackStubTest, TestOnEndExecuteStub_001, TestSize.Level0)
94 {
95     uint64_t scheduleId = 231527;
96     std::vector<uint8_t> command;
97 
98     MessageParcel data;
99     MessageParcel reply;
100     MessageOption option(MessageOption::TF_SYNC);
101     uint32_t code = ExecutorCallbackInterfaceCode::ON_END_EXECUTE;
102 
103     EXPECT_TRUE(data.WriteInterfaceToken(ExecutorCallbackInterface::GetDescriptor()));
104     EXPECT_TRUE(data.WriteUint64(scheduleId));
105     EXPECT_TRUE(data.WriteUInt8Vector(command));
106 
107     auto service = Common::MakeShared<MockExecutorCallbackService>();
108     EXPECT_NE(service, nullptr);
109     EXPECT_CALL(*service, OnEndExecute(_, _)).Times(1);
110 
111     EXPECT_EQ(service->OnRemoteRequest(code, data, reply, option), SUCCESS);
112 }
113 
114 HWTEST_F(ExecutorCallbackStubTest, TestOnSetPropertyStub_001, TestSize.Level0)
115 {
116     std::vector<uint8_t> properties;
117 
118     MessageParcel data;
119     MessageParcel reply;
120     MessageOption option(MessageOption::TF_SYNC);
121     uint32_t code = ExecutorCallbackInterfaceCode::ON_SET_PROPERTY;
122 
123     EXPECT_TRUE(data.WriteInterfaceToken(ExecutorCallbackInterface::GetDescriptor()));
124     EXPECT_TRUE(data.WriteUInt8Vector(properties));
125 
126     auto service = Common::MakeShared<MockExecutorCallbackService>();
127     EXPECT_NE(service, nullptr);
128     EXPECT_CALL(*service, OnSetProperty(_)).Times(1);
129 
130     EXPECT_EQ(service->OnRemoteRequest(code, data, reply, option), SUCCESS);
131 }
132 
133 HWTEST_F(ExecutorCallbackStubTest, TestOnGetPropertyStub_001, TestSize.Level0)
134 {
135     std::vector<uint8_t> condition;
136 
137     MessageParcel data;
138     MessageParcel reply;
139     MessageOption option(MessageOption::TF_SYNC);
140     uint32_t code = ExecutorCallbackInterfaceCode::ON_GET_PROPERTY;
141 
142     EXPECT_TRUE(data.WriteInterfaceToken(ExecutorCallbackInterface::GetDescriptor()));
143     EXPECT_TRUE(data.WriteUInt8Vector(condition));
144 
145     auto service = Common::MakeShared<MockExecutorCallbackService>();
146     EXPECT_NE(service, nullptr);
147     EXPECT_CALL(*service, OnGetProperty(_, _)).Times(1);
148 
149     EXPECT_EQ(service->OnRemoteRequest(code, data, reply, option), SUCCESS);
150 }
151 } // namespace UserAuth
152 } // namespace UserIam
153 } // namespace OHOS
154