1 /*
2 * Copyright (c) 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 "dump_token_info_test.h"
17 #include "gtest/gtest.h"
18 #include <thread>
19 #include <unistd.h>
20
21 #include "access_token.h"
22 #include "access_token_error.h"
23 #include "accesstoken_common_log.h"
24 #include "iaccess_token_manager.h"
25 #include "permission_grant_info.h"
26 #include "permission_state_change_info_parcel.h"
27 #include "string_ex.h"
28 #include "test_common.h"
29 #include "tokenid_kit.h"
30 #include "token_setproc.h"
31
32 using namespace testing::ext;
33 namespace OHOS {
34 namespace Security {
35 namespace AccessToken {
36 namespace {
37 static uint64_t g_selfTokenId = 0;
38 static const std::string TEST_BUNDLE_NAME = "ohos";
39 static int32_t g_selfUid;
40
41 static HapPolicyParams g_PolicyPrams = {
42 .apl = APL_NORMAL,
43 .domain = "test.domain",
44 };
45
46 static HapInfoParams g_InfoParms = {
47 .userID = 1,
48 .bundleName = "ohos.test.bundle",
49 .instIndex = 0,
50 .appIDDesc = "test.bundle",
51 .isSystemApp = true
52 };
53 };
54
SetUpTestCase()55 void DumpTokenInfoTest::SetUpTestCase()
56 {
57 g_selfTokenId = GetSelfTokenID();
58 g_selfUid = getuid();
59 TestCommon::SetTestEvironment(g_selfTokenId);
60 }
61
TearDownTestCase()62 void DumpTokenInfoTest::TearDownTestCase()
63 {
64 setuid(g_selfUid);
65 SetSelfTokenID(g_selfTokenId);
66 TestCommon::ResetTestEvironment();
67 }
68
SetUp()69 void DumpTokenInfoTest::SetUp()
70 {
71 LOGI(ATM_DOMAIN, ATM_TAG, "SetUp ok.");
72
73 setuid(0);
74 }
75
TearDown()76 void DumpTokenInfoTest::TearDown()
77 {
78 }
79
80 /**
81 * @tc.name: DumpTokenInfoAbnormalTest001
82 * @tc.desc: Verify the DumpTokenInfo abnormal branch return nullptr proxy.
83 * @tc.type: FUNC
84 * @tc.require:Issue Number
85 */
86 HWTEST_F(DumpTokenInfoTest, DumpTokenInfoAbnormalTest001, TestSize.Level1)
87 {
88 LOGI(ATM_DOMAIN, ATM_TAG, "DumpTokenInfoAbnormalTest001");
89 AccessTokenIDEx tokenIdEx = {0};
90 ASSERT_EQ(RET_SUCCESS, TestCommon::AllocTestHapToken(g_InfoParms, g_PolicyPrams, tokenIdEx));
91 ASSERT_NE(INVALID_TOKENID, tokenIdEx.tokenIdExStruct.tokenID);
92
93 setuid(g_selfUid);
94 EXPECT_EQ(0, SetSelfTokenID(tokenIdEx.tokenIDEx));
95 setuid(1234); // 1234: UID
96
97 std::string dumpInfo;
98 AtmToolsParamInfo info;
99 info.tokenId = 123;
100 AccessTokenKit::DumpTokenInfo(info, dumpInfo);
101 EXPECT_EQ("", dumpInfo);
102
103 setuid(g_selfUid);
104 EXPECT_EQ(0, SetSelfTokenID(g_selfTokenId));
105 setuid(g_selfUid);
106
107 ASSERT_EQ(RET_SUCCESS, TestCommon::DeleteTestHapToken(tokenIdEx.tokenIdExStruct.tokenID));
108 }
109
110 /**
111 * @tc.name: DumpTokenInfoAbnormalTest002
112 * @tc.desc: Get dump token information with invalid tokenID
113 * @tc.type: FUNC
114 * @tc.require:Issue Number
115 */
116 HWTEST_F(DumpTokenInfoTest, DumpTokenInfoAbnormalTest002, TestSize.Level1)
117 {
118 LOGI(ATM_DOMAIN, ATM_TAG, "DumpTokenInfoAbnormalTest002");
119 SetSelfTokenID(g_selfTokenId);
120 std::string dumpInfo;
121 AtmToolsParamInfo info;
122 info.tokenId = 123;
123 AccessTokenKit::DumpTokenInfo(info, dumpInfo);
124 ASSERT_EQ("invalid tokenId", dumpInfo);
125 }
126
127 /**
128 * @tc.name: DumpTokenInfoFuncTest001
129 * @tc.desc: Get dump token information with valid tokenID
130 * @tc.type: FUNC
131 * @tc.require:Issue Number
132 */
133 HWTEST_F(DumpTokenInfoTest, DumpTokenInfoFuncTest001, TestSize.Level1)
134 {
135 LOGI(ATM_DOMAIN, ATM_TAG, "DumpTokenInfoFuncTest001");
136 AccessTokenIDEx tokenIdEx = {0};
137 ASSERT_EQ(RET_SUCCESS, TestCommon::AllocTestHapToken(g_InfoParms, g_PolicyPrams, tokenIdEx));
138 ASSERT_NE(INVALID_TOKENID, tokenIdEx.tokenIdExStruct.tokenID);
139
140 std::string dumpInfo;
141 AtmToolsParamInfo info1;
142 info1.tokenId = tokenIdEx.tokenIdExStruct.tokenID;
143 AccessTokenKit::DumpTokenInfo(info1, dumpInfo);
144 EXPECT_NE("", dumpInfo);
145
146 AtmToolsParamInfo info2;
147 info2.processName = "hdcd";
148 AccessTokenKit::DumpTokenInfo(info2, dumpInfo);
149 EXPECT_NE("", dumpInfo);
150
151 ASSERT_EQ(RET_SUCCESS, TestCommon::DeleteTestHapToken(tokenIdEx.tokenIdExStruct.tokenID));
152 }
153 } // namespace AccessToken
154 } // namespace Security
155 } // namespace OHOS