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 "pin_auth_service_test.h"
17
18 #include "accesstoken_kit.h"
19 #include "nativetoken_kit.h"
20 #include "token_setproc.h"
21
22 #include "iam_ptr.h"
23 #include "mock_inputer_get_data.h"
24 #include "pin_auth_service.h"
25
26 namespace OHOS {
27 namespace UserIam {
28 namespace PinAuth {
29 using namespace testing;
30 using namespace testing::ext;
31
SetUpTestCase()32 void PinAuthServiceTest::SetUpTestCase()
33 {
34 }
35
TearDownTestCase()36 void PinAuthServiceTest::TearDownTestCase()
37 {
38 }
39
SetUp()40 void PinAuthServiceTest::SetUp()
41 {
42 }
43
TearDown()44 void PinAuthServiceTest::TearDown()
45 {
46 }
47
48 HWTEST_F(PinAuthServiceTest, RegisterInputerTest001, TestSize.Level0)
49 {
50 auto service = Common::MakeShared<PinAuthService>();
51 EXPECT_NE(service, nullptr);
52 EXPECT_EQ(service->RegisterInputer(nullptr), false);
53 }
54
55 HWTEST_F(PinAuthServiceTest, UnRegisterInputerTest001, TestSize.Level0)
56 {
57 auto service = Common::MakeShared<PinAuthService>();
58 EXPECT_NE(service, nullptr);
59 EXPECT_NO_THROW(service->UnRegisterInputer());
60 }
61
62 HWTEST_F(PinAuthServiceTest, OnStartTest001, TestSize.Level0)
63 {
64 auto service = Common::MakeShared<PinAuthService>();
65 EXPECT_NE(service, nullptr);
66 EXPECT_NO_THROW(service->OnStart());
67 EXPECT_NO_THROW(service->OnStart());
68 }
69
70 HWTEST_F(PinAuthServiceTest, OnStopTest001, TestSize.Level0)
71 {
72 auto service = Common::MakeShared<PinAuthService>();
73 EXPECT_NE(service, nullptr);
74 EXPECT_NO_THROW(service->OnStop());
75 }
76
77 HWTEST_F(PinAuthServiceTest, PinAuthServiceTest001, TestSize.Level0)
78 {
79 static const char *PERMS[] = {
80 "ohos.permission.ACCESS_PIN_AUTH"
81 };
82 NativeTokenInfoParams infoInstance = {
83 .dcapsNum = 0,
84 .permsNum = 1,
85 .aclsNum = 0,
86 .dcaps = nullptr,
87 .perms = PERMS,
88 .acls = nullptr,
89 .processName = "pin_auth_service_test",
90 .aplStr = "system_core",
91 };
92 uint64_t tokenId = GetAccessTokenId(&infoInstance);
93 SetSelfTokenID(tokenId);
94 Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
95 auto service = Common::MakeShared<PinAuthService>();
96 EXPECT_NE(service, nullptr);
97 sptr<InputerGetData> testInputerGetData(nullptr);
98 EXPECT_EQ(service->RegisterInputer(testInputerGetData), false);
99 service->UnRegisterInputer();
100 }
101
102 HWTEST_F(PinAuthServiceTest, PinAuthServiceTest002, TestSize.Level0)
103 {
104 static const char *PERMS[] = {
105 "ohos.permission.ACCESS_PIN_AUTH"
106 };
107 NativeTokenInfoParams infoInstance = {
108 .dcapsNum = 0,
109 .permsNum = 1,
110 .aclsNum = 0,
111 .dcaps = nullptr,
112 .perms = PERMS,
113 .acls = nullptr,
114 .processName = "pin_auth_service_test",
115 .aplStr = "system_core",
116 };
117 uint64_t tokenId = GetAccessTokenId(&infoInstance);
118 SetSelfTokenID(tokenId);
119 Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
120 auto service = Common::MakeShared<PinAuthService>();
121 EXPECT_NE(service, nullptr);
122 sptr<InputerGetData> testInputerGetData(new (std::nothrow) MockInputerGetData());
123 EXPECT_NE(testInputerGetData, nullptr);
124 EXPECT_EQ(service->RegisterInputer(testInputerGetData), true);
125 service->UnRegisterInputer();
126 }
127 } // namespace PinAuth
128 } // namespace UserIam
129 } // namespace OHOS
130