1 /*
2 * Copyright (c) 2022-2024 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(
__anonec9fe6f10102(const sptr<InputerGetData> &inputer) 54 [](const sptr<InputerGetData> &inputer) {
55 if (inputer != nullptr) {
56 InputerGetDataParam getDataParam = {
57 .mode = GET_DATA_MODE_ALL_IN_ONE_PIN_AUTH,
58 .authSubType = 10000,
59 .algoVersion = 0,
60 .algoParameter = {1, 2, 3, 4},
61 .challenge = {},
62 .inputerSetData = nullptr,
63 };
64 inputer->OnGetData(getDataParam);
65 }
66 return true;
67 }
68 );
69 EXPECT_CALL(*tempInputerGetData, OnGetData(_)).Times(1);
70
71 MessageParcel data;
72 MessageParcel reply;
73
74 EXPECT_TRUE(data.WriteInterfaceToken(PinAuthInterface::GetDescriptor()));
75 EXPECT_NE(tempInputerGetData->AsObject(), nullptr);
76 EXPECT_TRUE(data.WriteRemoteObject(tempInputerGetData->AsObject()));
77 uint32_t code = PinAuthInterfaceCode::REGISTER_INPUTER;
78 MessageOption option(MessageOption::TF_SYNC);
79
80 EXPECT_EQ(service.OnRemoteRequest(code, data, reply, option), UserAuth::SUCCESS);
81 bool result = false;
82 EXPECT_EQ(reply.ReadBool(result), true);
83 EXPECT_EQ(result, true);
84 }
85
86 HWTEST_F(PinAuthStubTest, PinAuthStubTestUnRegisterInputer001, TestSize.Level0)
87 {
88 MockPinAuthService service;
89 EXPECT_CALL(service, UnRegisterInputer()).Times(1);
90
91 MessageParcel data;
92 MessageParcel reply;
93
94 EXPECT_TRUE(data.WriteInterfaceToken(PinAuthInterface::GetDescriptor()));
95 uint32_t code = PinAuthInterfaceCode::UNREGISTER_INPUTER;
96 MessageOption option(MessageOption::TF_SYNC);
97
98 EXPECT_EQ(service.OnRemoteRequest(code, data, reply, option), UserAuth::SUCCESS);
99 }
100
101 HWTEST_F(PinAuthStubTest, PinAuthStubTestUnRegisterInputer002, TestSize.Level0)
102 {
103 MockPinAuthService service;
104
105 MessageParcel data;
106 MessageParcel reply;
107
108 uint32_t code = PinAuthInterfaceCode::UNREGISTER_INPUTER;
109 MessageOption option(MessageOption::TF_SYNC);
110
111 EXPECT_EQ(service.OnRemoteRequest(code, data, reply, option), UserAuth::GENERAL_ERROR);
112 }
113 } // namespace PinAuth
114 } // namespace UserIam
115 } // namespace OHOS
116