1 /*
2  * Copyright (c) 2022-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 <gtest/gtest.h>
17 
18 #define private public
19 #include "app_mgr_service.h"
20 #undef private
21 #include "hilog_tag_wrapper.h"
22 #include "mock_native_token.h"
23 #include "mock_sa_call.h"
24 #include "app_mgr_service_dump_error_code.h"
25 
26 using namespace testing;
27 using namespace testing::ext;
28 
29 namespace OHOS {
30 namespace AppExecFwk {
31 namespace {
32 const int32_t USER_ID = 100;
33 } // namespace
34 
35 class AppMgrServiceDumpTest : public testing::Test {
36 public:
37     static void SetUpTestCase();
38     static void TearDownTestCase();
39     void SetUp() override;
40     void TearDown() override;
41 };
42 
SetUpTestCase(void)43 void AppMgrServiceDumpTest::SetUpTestCase(void)
44 {
45     MockNativeToken::SetNativeToken();
46 }
47 
TearDownTestCase(void)48 void AppMgrServiceDumpTest::TearDownTestCase(void)
49 {}
50 
SetUp()51 void AppMgrServiceDumpTest::SetUp()
52 {}
53 
TearDown()54 void AppMgrServiceDumpTest::TearDown()
55 {}
56 
57 /**
58  * @tc.name: AppMgrServiceDump_GetProcessRunningInfosByUserId_0100
59  * @tc.desc: GetProcessRunningInfosByUserId
60  * @tc.type: FUNC
61  * @tc.require: SR000GH1GO
62  */
63 HWTEST_F(AppMgrServiceDumpTest, AppMgrServiceDump_GetProcessRunningInfosByUserId_0100, TestSize.Level1)
64 {
65     TAG_LOGI(AAFwkTag::TEST, "AppMgrServiceDump_GetProcessRunningInfosByUserId_0100 start");
66 
67     AAFwk::IsMockSaCall::IsMockSaCallWithPermission();
68     auto appMgrServiceInner = std::make_shared<AppMgrServiceInner>();
69     EXPECT_NE(appMgrServiceInner, nullptr);
70 
71     std::vector<RunningProcessInfo> info;
72     auto result = appMgrServiceInner->GetProcessRunningInfosByUserId(info, USER_ID);
73     EXPECT_EQ(result, ERR_OK);
74 
75     TAG_LOGI(AAFwkTag::TEST, "AppMgrServiceDump_GetProcessRunningInfosByUserId_0100 end");
76 }
77 
78 /**
79  * @tc.name: AppMgrServiceDump_GetProcessRunningInfosByUserId_0200
80  * @tc.desc: GetProcessRunningInfosByUserId
81  * @tc.type: FUNC
82  * @tc.require: SR000GH1GO
83  */
84 HWTEST_F(AppMgrServiceDumpTest, AppMgrServiceDump_GetProcessRunningInfosByUserId_0200, TestSize.Level1)
85 {
86     TAG_LOGI(AAFwkTag::TEST, "AppMgrServiceDump_GetProcessRunningInfosByUserId_0200 start");
87 
88     AAFwk::IsMockSaCall::IsMockSaCallWithPermission();
89     auto appMgrService = std::make_shared<AppMgrService>();
90     EXPECT_NE(appMgrService, nullptr);
91 
92     appMgrService->taskHandler_ = AAFwk::TaskHandlerWrap::CreateQueueHandler(Constants::APP_MGR_SERVICE_NAME);
93     appMgrService->eventHandler_ = std::make_shared<AMSEventHandler>(
94         appMgrService->taskHandler_, appMgrService->appMgrServiceInner_);
95 
96     std::vector<RunningProcessInfo> info;
97     auto result = appMgrService->GetProcessRunningInfosByUserId(info, USER_ID);
98     EXPECT_EQ(result, ERR_OK);
99 
100     TAG_LOGI(AAFwkTag::TEST, "AppMgrServiceDump_GetProcessRunningInfosByUserId_0200 end");
101 }
102 
103 /*
104  * @tc.number    : AppMgrServiceDump_0100
105  * @tc.name      : AppMgrService dump
106  * @tc.desc      : 1.Test dump interface
107  */
108 HWTEST_F(AppMgrServiceDumpTest, AppMgrServiceDump_0100, TestSize.Level1)
109 {
110     TAG_LOGI(AAFwkTag::TEST, "AppMgrServiceDump_0100 start");
111 
112     auto appMgrService = std::make_shared<AppMgrService>();
113     EXPECT_NE(appMgrService, nullptr);
114     appMgrService->taskHandler_ = AAFwk::TaskHandlerWrap::CreateQueueHandler(Constants::APP_MGR_SERVICE_NAME);
115     EXPECT_NE(appMgrService->taskHandler_, nullptr);
116     appMgrService->eventHandler_ = std::make_shared<AMSEventHandler>(
117         appMgrService->taskHandler_, appMgrService->appMgrServiceInner_);
118 
119     constexpr int fd(1);
120     std::vector<std::u16string> args;
121     auto arg = Str8ToStr16("-h");
122     args.emplace_back(arg);
123     auto result = appMgrService->Dump(fd, args);
124     EXPECT_EQ(result, ERR_OK);
125 
126     TAG_LOGI(AAFwkTag::TEST, "AppMgrServiceDump_0100 end");
127 }
128 
129 /*
130  * @tc.number    : AppMgrServiceDump_0200
131  * @tc.name      : AppMgrService dump
132  * @tc.desc      : 1.Test dump interface
133  */
134 HWTEST_F(AppMgrServiceDumpTest, AppMgrServiceDump_0200, TestSize.Level1)
135 {
136     TAG_LOGI(AAFwkTag::TEST, "AppMgrServiceDump_0200 start");
137 
138     auto appMgrService = std::make_shared<AppMgrService>();
139     EXPECT_NE(appMgrService, nullptr);
140 
141     constexpr int fd(0);
142     std::vector<std::u16string> args;
143     auto arg = Str8ToStr16("-h");
144     args.emplace_back(arg);
145     auto result = appMgrService->Dump(fd, args);
146     EXPECT_EQ(result, ERR_APPEXECFWK_HIDUMP_ERROR);
147 
148     TAG_LOGI(AAFwkTag::TEST, "AppMgrServiceDump_0200 end");
149 }
150 
151 /*
152  * @tc.number    : AppMgrServiceDump_0300
153  * @tc.name      : AppMgrService dump
154  * @tc.desc      : 1.Test dump interface
155  */
156 HWTEST_F(AppMgrServiceDumpTest, AppMgrServiceDump_0300, TestSize.Level1)
157 {
158     TAG_LOGI(AAFwkTag::TEST, "AppMgrServiceDump_0300 start");
159 
160     auto appMgrService = std::make_shared<AppMgrService>();
161     EXPECT_NE(appMgrService, nullptr);
162     appMgrService->taskHandler_ = AAFwk::TaskHandlerWrap::CreateQueueHandler(Constants::APP_MGR_SERVICE_NAME);
163     EXPECT_NE(appMgrService->taskHandler_, nullptr);
164     appMgrService->eventHandler_ = std::make_shared<AMSEventHandler>(
165         appMgrService->taskHandler_, appMgrService->appMgrServiceInner_);
166 
167     constexpr int fd(1);
168     std::vector<std::u16string> args;
169     auto result = appMgrService->Dump(fd, args);
170     EXPECT_EQ(result, ERR_OK);
171 
172     TAG_LOGI(AAFwkTag::TEST, "AppMgrServiceDump_0300 end");
173 }
174 
175 /*
176  * @tc.number    : AppMgrServiceDump_0400
177  * @tc.name      : AppMgrService dump
178  * @tc.desc      : 1.Test dump interface
179  */
180 HWTEST_F(AppMgrServiceDumpTest, AppMgrServiceDump_0400, TestSize.Level1)
181 {
182     TAG_LOGI(AAFwkTag::TEST, "AppMgrServiceDump_0400 start");
183 
184     auto appMgrService = std::make_shared<AppMgrService>();
185     EXPECT_NE(appMgrService, nullptr);
186     appMgrService->taskHandler_ = AAFwk::TaskHandlerWrap::CreateQueueHandler(Constants::APP_MGR_SERVICE_NAME);
187     EXPECT_NE(appMgrService->taskHandler_, nullptr);
188     appMgrService->eventHandler_ = std::make_shared<AMSEventHandler>(
189         appMgrService->taskHandler_, appMgrService->appMgrServiceInner_);
190 
191     constexpr int fd(1);
192     std::vector<std::u16string> args;
193     auto arg = Str8ToStr16("-i");
194     args.emplace_back(arg);
195     auto result = appMgrService->Dump(fd, args);
196     EXPECT_EQ(result, DumpErrorCode::ERR_UNKNOWN_OPTION_ERROR);
197 
198     TAG_LOGI(AAFwkTag::TEST, "AppMgrServiceDump_0400 end");
199 }
200 }  // namespace AppExecFwk
201 }  // namespace OHOS
202