1 /*
2 * Copyright (C) 2022 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 "co_auth_stub_test.h"
17
18 #include "mock_executor_callback.h"
19 #include "mock_co_auth_service.h"
20
21 namespace OHOS {
22 namespace UserIam {
23 namespace UserAuth {
24 using namespace testing;
25 using namespace testing::ext;
26
SetUpTestCase()27 void CoAuthStubTest::SetUpTestCase()
28 {
29 }
30
TearDownTestCase()31 void CoAuthStubTest::TearDownTestCase()
32 {
33 }
34
SetUp()35 void CoAuthStubTest::SetUp()
36 {
37 }
38
TearDown()39 void CoAuthStubTest::TearDown()
40 {
41 }
42
43 HWTEST_F(CoAuthStubTest, CoAuthStubTestExecutorRegister001, TestSize.Level0)
44 {
45 MessageParcel data;
46 MessageParcel reply;
47
48 CoAuthInterface::ExecutorRegisterInfo testInfo = {};
49 testInfo.authType = PIN;
50 testInfo.executorRole = SCHEDULER;
51 testInfo.executorSensorHint = 0;
52 testInfo.executorMatcher = 0;
53 testInfo.esl = ESL1;
54 testInfo.publicKey = {'a', 'b', 'c', 'd'};
55
56 uint64_t testContextId = 124545;
57
58 sptr<MockExecutorCallback> callback = new MockExecutorCallback();
59 EXPECT_NE(callback, nullptr);
60 MockCoAuthService service;
61 EXPECT_CALL(service, ExecutorRegister(_, _)).Times(1);
62 ON_CALL(service, ExecutorRegister)
63 .WillByDefault(
64 [&testInfo, &testContextId](const CoAuthInterface::ExecutorRegisterInfo &info,
__anoncc17f5fd0102(const CoAuthInterface::ExecutorRegisterInfo &info, sptr<ExecutorCallbackInterface> &callback) 65 sptr<ExecutorCallbackInterface> &callback) {
66 EXPECT_EQ(info.authType, testInfo.authType);
67 EXPECT_EQ(info.executorRole, testInfo.executorRole);
68 EXPECT_EQ(info.executorSensorHint, testInfo.executorSensorHint);
69 EXPECT_EQ(info.executorMatcher, testInfo.executorMatcher);
70 EXPECT_EQ(info.esl, testInfo.esl);
71 EXPECT_THAT(info.publicKey, ElementsAreArray(testInfo.publicKey));
72 return testContextId;
73 }
74 );
75
76 EXPECT_TRUE(data.WriteInterfaceToken(CoAuthInterface::GetDescriptor()));
77 EXPECT_TRUE(data.WriteInt32(testInfo.authType));
78 EXPECT_TRUE(data.WriteInt32(testInfo.executorRole));
79 EXPECT_TRUE(data.WriteUint32(testInfo.executorSensorHint));
80 EXPECT_TRUE(data.WriteUint32(testInfo.executorMatcher));
81 EXPECT_TRUE(data.WriteInt32(testInfo.esl));
82 EXPECT_TRUE(data.WriteUInt8Vector(testInfo.publicKey));
83 EXPECT_NE(callback->AsObject(), nullptr);
84 EXPECT_TRUE(data.WriteRemoteObject(callback->AsObject()));
85 uint32_t code = CoAuthInterface::CO_AUTH_EXECUTOR_REGISTER;
86 MessageOption option(MessageOption::TF_SYNC);
87
88 EXPECT_EQ(SUCCESS, service.OnRemoteRequest(code, data, reply, option));
89 uint64_t contextId = -1;
90 EXPECT_TRUE(reply.ReadUint64(contextId));
91 EXPECT_EQ(contextId, testContextId);
92 }
93
94 HWTEST_F(CoAuthStubTest, CoAuthStubTestExecutorRegister002, TestSize.Level0)
95 {
96 MessageParcel data;
97 MessageParcel reply;
98
99 uint32_t code = CoAuthInterface::CO_AUTH_EXECUTOR_REGISTER;
100 MessageOption option(MessageOption::TF_SYNC);
101
102 MockCoAuthService service;
103 EXPECT_EQ(GENERAL_ERROR, service.OnRemoteRequest(code, data, reply, option));
104 }
105
106 HWTEST_F(CoAuthStubTest, CoAuthStubTestExecutorRegister003, TestSize.Level0)
107 {
108 MessageParcel data;
109 MessageParcel reply;
110
111 EXPECT_TRUE(data.WriteInterfaceToken(CoAuthInterface::GetDescriptor()));
112 uint32_t code = CoAuthInterface::CO_AUTH_EXECUTOR_REGISTER;
113 MessageOption option(MessageOption::TF_SYNC);
114
115 MockCoAuthService service;
116 EXPECT_EQ(READ_PARCEL_ERROR, service.OnRemoteRequest(code, data, reply, option));
117 }
118 } // namespace UserAuth
119 } // namespace UserIam
120 } // namespace OHOS