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 #include <gtest/gtest.h>
16
17 using namespace testing::ext;
18 namespace OHOS {
19 namespace HiviewDFX {
20 class HidumperPrivacyTest : public testing::Test {
21 public:
22 static void SetUpTestCase(void);
23 static void TearDownTestCase(void);
24 void SetUp();
25 void TearDown();
26 bool IsExistInCmdResult(const std::string &cmd, const std::string &str);
27
28 const int ROOT_UID = 0;
29 const int SYSTEM_UID = 1000;
30 const int SHELL_UID = 2000;
31 const std::string KEY_WORD = "Usage";
32 const std::string CMD = "hidumper -s 10";
33 };
34
SetUpTestCase(void)35 void HidumperPrivacyTest::SetUpTestCase(void)
36 {
37 }
TearDownTestCase(void)38 void HidumperPrivacyTest::TearDownTestCase(void)
39 {
40 }
SetUp(void)41 void HidumperPrivacyTest::SetUp(void)
42 {
43 }
TearDown(void)44 void HidumperPrivacyTest::TearDown(void)
45 {
46 }
47
IsExistInCmdResult(const std::string & cmd,const std::string & str)48 bool HidumperPrivacyTest::IsExistInCmdResult(const std::string &cmd, const std::string &str)
49 {
50 bool ret = false;
51 char* buffer = nullptr;
52 size_t len = 0;
53 FILE *fp = popen(cmd.c_str(), "r");
54 while (getline(&buffer, &len, fp) != -1) {
55 std::string line = buffer;
56 if (line.find(str) != string::npos) {
57 ret = true;
58 break;
59 }
60 }
61 pclose(fp);
62 return ret;
63 }
64
65 /**
66 * @tc.name: HidumperPrivacyTest001
67 * @tc.desc: When uid=root and apl=2,the content of sa can be obtained.
68 * @tc.type: FUNC
69 * @tc.require: issueI5GXTG
70 */
71 HWTEST_F(HidumperPrivacyTest, HidumperPrivacyTest001, TestSize.Level3)
72 {
73 setuid(ROOT_UID);
74 ASSERT_TRUE(IsExistInCmdResult(CMD, KEY_WORD));
75 }
76 } // namespace HiviewDFX
77 } // namespace OHOS
78