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_register_test.h"
17
18 #include "file_ex.h"
19 #include "accesstoken_kit.h"
20 #include "nativetoken_kit.h"
21 #include "token_setproc.h"
22
23 #include "iam_ptr.h"
24 #include "mock_inputer.h"
25 #include "pinauth_register.h"
26
27 namespace OHOS {
28 namespace UserIam {
29 namespace PinAuth {
30 using namespace testing;
31 using namespace testing::ext;
32
33 static uint64_t tokenId;
34
SetUpTestCase()35 void PinAuthRegisterTest::SetUpTestCase()
36 {
37 static const char *PERMS[] = {
38 "ohos.permission.ACCESS_PIN_AUTH"
39 };
40 string isEnforcing;
41 NativeTokenInfoParams infoInstance = {
42 .dcapsNum = 0,
43 .permsNum = 1,
44 .aclsNum = 0,
45 .dcaps = nullptr,
46 .perms = PERMS,
47 .acls = nullptr,
48 .processName = "pin_auth_service_test",
49 .aplStr = "system_core",
50 };
51 tokenId = GetAccessTokenId(&infoInstance);
52 SetSelfTokenID(tokenId);
53 Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
54 LoadStringFromFile("/sys/fs/selinux/enforce", isEnforcing);
55 if (isEnforcing.compare("1") == 0) {
56 PinAuthRegisterTest::isEnforcing_ = true;
57 SaveStringToFile("/sys/fs/selinux/enforce", "0");
58 }
59 }
60
TearDownTestCase()61 void PinAuthRegisterTest::TearDownTestCase()
62 {
63 Security::AccessToken::AccessTokenKit::DeleteToken(tokenId);
64 Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
65 if (PinAuthRegisterTest::isEnforcing_) {
66 SaveStringToFile("/sys/fs/selinux/enforce", "1");
67 }
68 }
69
SetUp()70 void PinAuthRegisterTest::SetUp()
71 {
72 }
73
TearDown()74 void PinAuthRegisterTest::TearDown()
75 {
76 }
77
78 bool PinAuthRegisterTest::isEnforcing_ = false;
79
80 HWTEST_F(PinAuthRegisterTest, PinAuthRegisterTest001, TestSize.Level0)
81 {
82 std::shared_ptr<IInputer> testInputer = nullptr;
83 EXPECT_EQ(PinAuthRegister::GetInstance().RegisterInputer(testInputer), false);
84 PinAuthRegister::GetInstance().UnRegisterInputer();
85 }
86
87 HWTEST_F(PinAuthRegisterTest, PinAuthRegisterTest002, TestSize.Level0)
88 {
89 std::shared_ptr<IInputer> testInputer = Common::MakeShared<MockInputer>();
90 EXPECT_NE(testInputer, nullptr);
91 EXPECT_EQ(PinAuthRegister::GetInstance().RegisterInputer(testInputer), true);
92 PinAuthRegister::GetInstance().UnRegisterInputer();
93 }
94 } // namespace PinAuth
95 } // namespace UserIam
96 } // namespace OHOS
97