1 /*
2 * Copyright (c) 2025-2025 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 "gtest/gtest.h"
17 #include "gtest/hwext/gtest-multithread.h"
18
19 #include "nativetoken_kit.h"
20 #include "suspend_manager_base_client.h"
21 #include "token_setproc.h"
22
23 namespace OHOS {
24 namespace ResourceSchedule {
25 using namespace std;
26 using namespace testing::ext;
27 using namespace testing::mt;
28 class SuspendManagerBaseClientTest : public testing::Test {
29 public:
30 static void SetUpTestCase(void);
31 static void TearDownTestCase(void);
32 void SetUp();
33 void TearDown();
34 void MockProcess(int32_t uid);
35 };
36
37
SetUpTestCase(void)38 void SuspendManagerBaseClientTest::SetUpTestCase(void) {}
39
TearDownTestCase()40 void SuspendManagerBaseClientTest::TearDownTestCase() {}
41
SetUp()42 void SuspendManagerBaseClientTest::SetUp() {}
43
TearDown()44 void SuspendManagerBaseClientTest::TearDown() {}
45
MockProcess(int32_t uid)46 void SuspendManagerBaseClientTest::MockProcess(int32_t uid)
47 {
48 static const char *perms[] = {
49 "ohos.permission.GET_SUSPEND_STATE"
50 };
51 uint64_t tokenId;
52 NativeTokenInfoParams infoInstance = {
53 .dcapsNum = 0,
54 .permsNum = 1,
55 .aclsNum = 0,
56 .dcaps = nullptr,
57 .perms = perms,
58 .acls = nullptr,
59 .processName = "samgr",
60 .aplStr = "system_core",
61 };
62 tokenId = GetAccessTokenId(&infoInstance);
63 SetSelfTokenID(tokenId);
64 setuid(uid);
65 }
66
67 /**
68 * @tc.name: GetSuspendStateByUid
69 * @tc.desc: Get application suspend state by uid test.
70 * @tc.type: FUNC
71 * @tc.require: #IC49DE
72 * @tc.author: wujinghui
73 */
74 HWTEST_F(SuspendManagerBaseClientTest, GetSuspendStateByUid, Function | MediumTest | Level0)
75 {
76 int32_t uid = 7777;
77 MockProcess(uid);
78 int32_t appUid = 20020021;
79 bool isFrozen;
80 SuspendManagerBaseClient::GetInstance().GetSuspendStateByUid(appUid, isFrozen);
81 if (SuspendManagerBaseClient::GetInstance().GetSuspendManagerProxy()) {
82 EXPECT_NE(SuspendManagerBaseClient::GetInstance().suspendManagerBaseProxy_, nullptr);
83 } else {
84 EXPECT_EQ(SuspendManagerBaseClient::GetInstance().suspendManagerBaseProxy_, nullptr);
85 }
86 }
87
88 /**
89 * @tc.name: GetSuspendStateByPid
90 * @tc.desc: Get application suspend state by pid test.
91 * @tc.type: FUNC
92 * @tc.require: #IC49DE
93 * @tc.author: wujinghui
94 */
95 HWTEST_F(SuspendManagerBaseClientTest, GetSuspendStateByPid, Function | MediumTest | Level0)
96 {
97 int32_t uid = 7777;
98 MockProcess(uid);
99 int32_t appPid = 6666;
100 bool isFrozen;
101 SuspendManagerBaseClient::GetInstance().GetSuspendStateByPid(appPid, isFrozen);
102 if (SuspendManagerBaseClient::GetInstance().GetSuspendManagerProxy()) {
103 EXPECT_NE(SuspendManagerBaseClient::GetInstance().suspendManagerBaseProxy_, nullptr);
104 } else {
105 EXPECT_EQ(SuspendManagerBaseClient::GetInstance().suspendManagerBaseProxy_, nullptr);
106 }
107 }
108 } // namespace ResourceSchedule
109 } // namespace OHOS