1 /*
2  * Copyright (c) 2021 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 <functional>
17 #include <gtest/gtest.h>
18 #include "accesstoken_kit.h"
19 #define private public
20 #define protected public
21 #include "access_token_helper.h"
22 #include "ans_log_wrapper.h"
23 #include "ipc_skeleton.h"
24 #undef private
25 #undef protected
26 using namespace testing::ext;
27 using namespace OHOS::Security::AccessToken;
28 
29 namespace OHOS {
30 namespace Notification {
31 
32 extern void MockGetTokenTypeFlag(ATokenTypeEnum mockRet);
33 extern void MockDlpType(DlpType mockRet);
34 extern void MockApl(ATokenAplEnum mockRet);
35 class AccessTokenHelperTest : public testing::Test {
36 public:
37     static void SetUpTestCase();
38     static void TearDownTestCase();
39     void SetUp() override;
40     void TearDown() override;
41 
42     std::shared_ptr<AccessTokenHelper> stub_;
43 };
44 
SetUpTestCase()45 void AccessTokenHelperTest::SetUpTestCase()
46 {
47 }
48 
TearDownTestCase()49 void AccessTokenHelperTest::TearDownTestCase()
50 {
51 }
52 
SetUp()53 void AccessTokenHelperTest::SetUp()
54 {
55     stub_ = std::make_shared<AccessTokenHelper>();
56 }
57 
TearDown()58 void AccessTokenHelperTest::TearDown()
59 {
60 }
61 
62 /**
63  * @tc.number    : AccessTokenHelperTest
64  * @tc.name      : VerifyCallerPermission_00100
65  * @tc.desc      : VerifyCallerPermission success
66  */
67 HWTEST_F(AccessTokenHelperTest, VerifyCallerPermission_00100, Function | SmallTest | Level1)
68 {
69     AccessTokenID tokenID = 0;
70     string permission;
71     EXPECT_TRUE(stub_->VerifyCallerPermission(tokenID, permission));
72 }
73 
74 /**
75  * @tc.number    : AccessTokenHelperTest
76  * @tc.name      : VerifyNativeToken_00100
77  * @tc.desc      : VerifyNativeToken success
78  */
79 HWTEST_F(AccessTokenHelperTest, VerifyNativeToken_00100, Function | SmallTest | Level1)
80 {
81     AccessTokenID tokenID = 0;
82     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
83     EXPECT_TRUE(stub_->VerifyNativeToken(tokenID));
84 }
85 
86 /**
87  * @tc.number    : AccessTokenHelperTest
88  * @tc.name      : IsSystemApp_00100
89  * @tc.desc      : IsSystemApp Token Type TOKEN_NATIVE
90  */
91 HWTEST_F(AccessTokenHelperTest, IsSystemApp_00100, Function | SmallTest | Level1)
92 {
93     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
94     EXPECT_FALSE(stub_->IsSystemApp());
95 }
96 
97 /**
98  * @tc.number    : AccessTokenHelperTest
99  * @tc.name      : IsSystemApp_00200
100  * @tc.desc      : IsSystemApp Token Type TOKEN_HAP
101  */
102 HWTEST_F(AccessTokenHelperTest, IsSystemApp_00200, Function | SmallTest | Level1)
103 {
104     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
105     EXPECT_TRUE(stub_->IsSystemApp());
106 }
107 
108 /**
109  * @tc.number    : AccessTokenHelperTest
110  * @tc.name      : IsDlpHap_00100
111  * @tc.desc      : IsDlpHap Token Type TOKEN_HAP
112  */
113 HWTEST_F(AccessTokenHelperTest, IsDlpHap_00100, Function | SmallTest | Level1)
114 {
115     AccessTokenID tokenID = 0;
116     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
117     MockDlpType(DlpType::DLP_READ);
118     EXPECT_TRUE(stub_->IsDlpHap(tokenID));
119 }
120 
121 /**
122  * @tc.number    : AccessTokenHelperTest
123  * @tc.name      : IsDlpHap_00200
124  * @tc.desc      : IsDlpHap Token Type TOKEN_NATIVE
125  */
126 HWTEST_F(AccessTokenHelperTest, IsDlpHap_00200, Function | SmallTest | Level1)
127 {
128     AccessTokenID tokenID = 0;
129     MockDlpType(DlpType::DLP_COMMON);
130     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
131     EXPECT_FALSE(stub_->IsDlpHap(tokenID));
132 }
133 
134 
135 /**
136  * @tc.number  : AdvancedNotificationService_01300
137  * @tc.name    : AdvancedNotificationService_01300
138  * @tc.desc    : Test CheckPermission function and result is false
139  */
140 HWTEST_F(AccessTokenHelperTest, CheckPermission_00100, Function | SmallTest | Level1)
141 {
142     std::string permission = "<permission>";
143     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
144     ASSERT_EQ(stub_->CheckPermission(permission), true);
145 }
146 }  // namespace Notification
147 }  // namespace OHOS
148