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 "pin_auth_stub_test.h"
17
18 #include "message_parcel.h"
19
20 #include "iam_common_defines.h"
21 #include "mock_inputer_get_data.h"
22 #include "mock_pin_auth_service.h"
23
24 namespace OHOS {
25 namespace UserIam {
26 namespace PinAuth {
27 using namespace testing;
28 using namespace testing::ext;
29
SetUpTestCase()30 void PinAuthStubTest::SetUpTestCase()
31 {
32 }
33
TearDownTestCase()34 void PinAuthStubTest::TearDownTestCase()
35 {
36 }
37
SetUp()38 void PinAuthStubTest::SetUp()
39 {
40 }
41
TearDown()42 void PinAuthStubTest::TearDown()
43 {
44 }
45
46 HWTEST_F(PinAuthStubTest, PinAuthStubTestRegisterInputer, TestSize.Level0)
47 {
48 MockPinAuthService service;
49 sptr<MockInputerGetData> tempInputerGetData(new (std::nothrow) MockInputerGetData());
50 EXPECT_NE(tempInputerGetData, nullptr);
51 EXPECT_CALL(service, RegisterInputer(_)).Times(1);
52 ON_CALL(service, RegisterInputer)
53 .WillByDefault(
__anon1165ef190102(const sptr<InputerGetData> &inputer) 54 [](const sptr<InputerGetData> &inputer) {
55 if (inputer != nullptr) {
56 std::vector<uint8_t> salt = {1, 2, 3, 4};
57 inputer->OnGetData(10000, salt, nullptr);
58 }
59 return true;
60 }
61 );
62 EXPECT_CALL(*tempInputerGetData, OnGetData(_, _, _)).Times(1);
63
64 MessageParcel data;
65 MessageParcel reply;
66
67 EXPECT_TRUE(data.WriteInterfaceToken(PinAuthInterface::GetDescriptor()));
68 EXPECT_NE(tempInputerGetData->AsObject(), nullptr);
69 EXPECT_TRUE(data.WriteRemoteObject(tempInputerGetData->AsObject()));
70 uint32_t code = PinAuthInterfaceCode::REGISTER_INPUTER;
71 MessageOption option(MessageOption::TF_SYNC);
72
73 EXPECT_EQ(service.OnRemoteRequest(code, data, reply, option), UserAuth::SUCCESS);
74 bool result = false;
75 EXPECT_EQ(reply.ReadBool(result), true);
76 EXPECT_EQ(result, true);
77 }
78
79 HWTEST_F(PinAuthStubTest, PinAuthStubTestUnRegisterInputer001, TestSize.Level0)
80 {
81 MockPinAuthService service;
82 EXPECT_CALL(service, UnRegisterInputer()).Times(1);
83
84 MessageParcel data;
85 MessageParcel reply;
86
87 EXPECT_TRUE(data.WriteInterfaceToken(PinAuthInterface::GetDescriptor()));
88 uint32_t code = PinAuthInterfaceCode::UNREGISTER_INPUTER;
89 MessageOption option(MessageOption::TF_SYNC);
90
91 EXPECT_EQ(service.OnRemoteRequest(code, data, reply, option), UserAuth::SUCCESS);
92 }
93
94 HWTEST_F(PinAuthStubTest, PinAuthStubTestUnRegisterInputer002, TestSize.Level0)
95 {
96 MockPinAuthService service;
97
98 MessageParcel data;
99 MessageParcel reply;
100
101 uint32_t code = PinAuthInterfaceCode::UNREGISTER_INPUTER;
102 MessageOption option(MessageOption::TF_SYNC);
103
104 EXPECT_EQ(service.OnRemoteRequest(code, data, reply, option), UserAuth::GENERAL_ERROR);
105 }
106 } // namespace PinAuth
107 } // namespace UserIam
108 } // namespace OHOS
109