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_messenger_proxy_test.h"
17 
18 #include "executor_messenger_proxy.h"
19 #include "iam_ptr.h"
20 #include "mock_remote_object.h"
21 
22 namespace OHOS {
23 namespace UserIam {
24 namespace UserAuth {
25 using namespace testing;
26 using namespace testing::ext;
27 
SetUpTestCase()28 void ExecutorMessengerProxyTest::SetUpTestCase()
29 {
30 }
31 
TearDownTestCase()32 void ExecutorMessengerProxyTest::TearDownTestCase()
33 {
34 }
35 
SetUp()36 void ExecutorMessengerProxyTest::SetUp()
37 {
38 }
39 
TearDown()40 void ExecutorMessengerProxyTest::TearDown()
41 {
42 }
43 
44 HWTEST_F(ExecutorMessengerProxyTest, TestSendData_001, TestSize.Level0)
45 {
46     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
47     EXPECT_NE(obj, nullptr);
48     EXPECT_CALL(*obj, SendRequest(_, _, _, _))
49         .WillOnce(
__anon7e60407d0102(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 50             [](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
51                 EXPECT_EQ(code, ExecutorMessengerInterfaceCode::CO_AUTH_SEND_DATA);
52                 EXPECT_TRUE(reply.WriteInt32(SUCCESS));
53                 return OHOS::NO_ERROR;
54             }
55         );
56 
57     uint64_t scheduleId = 6598;
58     ExecutorRole dstRole = COLLECTOR;
59     std::vector<uint8_t> message = {1, 2, 4, 6};
60 
61     auto proxy = Common::MakeShared<ExecutorMessengerProxy>(obj);
62     EXPECT_NE(proxy, nullptr);
63     EXPECT_EQ(proxy->SendData(scheduleId, dstRole, message), SUCCESS);
64 }
65 
66 HWTEST_F(ExecutorMessengerProxyTest, TestFinish_001, TestSize.Level0)
67 {
68     uint64_t scheduleId = 6598;
69     ResultCode resultCode = SUCCESS;
70     std::shared_ptr<Attributes> finalResult = nullptr;
71 
72     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
73     EXPECT_NE(obj, nullptr);
74     auto proxy = Common::MakeShared<ExecutorMessengerProxy>(obj);
75     EXPECT_NE(proxy, nullptr);
76     EXPECT_EQ(proxy->Finish(scheduleId, resultCode, finalResult), INVALID_PARAMETERS);
77 }
78 
79 HWTEST_F(ExecutorMessengerProxyTest, TestFinish_002, TestSize.Level0)
80 {
81     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
82     EXPECT_NE(obj, nullptr);
83     EXPECT_CALL(*obj, SendRequest(_, _, _, _))
84         .WillOnce(
__anon7e60407d0202(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 85             [](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
86                 EXPECT_EQ(code, ExecutorMessengerInterfaceCode::CO_AUTH_FINISH);
87                 EXPECT_TRUE(reply.WriteInt32(SUCCESS));
88                 return OHOS::NO_ERROR;
89             }
90         );
91 
92     uint64_t scheduleId = 6598;
93     ResultCode resultCode = SUCCESS;
94     std::shared_ptr<Attributes> finalResult = Common::MakeShared<Attributes>();
95     EXPECT_NE(finalResult, nullptr);
96 
97     auto proxy = Common::MakeShared<ExecutorMessengerProxy>(obj);
98     EXPECT_NE(proxy, nullptr);
99     EXPECT_EQ(proxy->Finish(scheduleId, resultCode, finalResult), SUCCESS);
100 }
101 } // namespace UserAuth
102 } // namespace UserIam
103 } // namespace OHOS
104