• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "common_test.h"
16 #include <limits>
17 #include <sys/xattr.h>
18 #include <sys/wait.h>
19 #include "base.h"
20 
21 using namespace testing::ext;
22 
23 namespace Hdc {
SetUpTestCase()24 void HdcMainTest::SetUpTestCase() {}
TearDownTestCase()25 void HdcMainTest::TearDownTestCase() {}
SetUp()26 void HdcMainTest::SetUp()
27 {
28 #ifdef UT_DEBUG
29     Base::SetLogLevel(LOG_ALL);
30 #else
31     Base::SetLogLevel(LOG_OFF);
32 #endif
33 }
TearDown()34 void HdcMainTest::TearDown() {}
35 
TestIsHishellLabel(pid_t pid)36 bool HdcMainTest::TestIsHishellLabel(pid_t pid)
37 {
38     if (pid == 0) {
39         return false;
40     }
41     // static path
42     std::stringstream ss;
43     ss << "/proc/" << pid << "/attr/current";
44     std::string cmd = ss.str();
45     const char *pathBuf = cmd.c_str();
46     const char* attrName = "security.selinux";
47     // get attribute size
48     ssize_t attrSize = getxattr(pathBuf, attrName, nullptr, 0);
49     if (attrSize <= 0 || attrSize >= std::numeric_limits<size_t>::max()) {
50         return false;
51     }
52     char *attrValue = new(std::nothrow) char[attrSize];
53     if (attrValue == nullptr) {
54         return false;
55     }
56     // get attribute value
57     if (getxattr(pathBuf, attrName, attrValue, attrSize) == -1) {
58         delete []attrValue;
59         return false;
60     }
61     string label(attrValue, attrSize - 1);
62     delete []attrValue;
63     return label == "u:r:hishell_hap:s0";
64 }
65 
66 HWTEST_F(HdcMainTest, TestIsHishell_1, TestSize.Level0)
67 {
68     EXPECT_FALSE(TestIsHishellLabel(0));
69 }
70 
71 HWTEST_F(HdcMainTest, TestIsHishell_2, TestSize.Level0)
72 {
73     pid_t pid = fork();
74     ASSERT_GT(pid, 0);
75     if (pid == 0) {
76         pause();
77     } else {
78         // parent process
79         kill(pid, SIGUSR1);
80         EXPECT_TRUE(TestIsHishellLabel(pid));
81     }
82 }
83 
84 HWTEST_F(HdcMainTest, TestIsHishell_3, TestSize.Level0)
85 {
86     pid_t pid = fork();
87     ASSERT_GT(pid, 0);
88     if (pid == 0) {
89         pause();
90     } else {
91         // parent process
92         kill(pid, SIGUSR1);
93         EXPECT_TRUE(TestIsHishellLabel(pid));
94     }
95 }
96 
97 HWTEST_F(HdcMainTest, TestIsHishell_4, TestSize.Level0)
98 {
99     pid_t pid = fork();
100     ASSERT_GT(pid, 0);
101     if (pid == 0) {
102         pause();
103     } else {
104         // parent process
105         kill(pid, SIGUSR1);
106         EXPECT_TRUE(TestIsHishellLabel(pid));
107     }
108 }
109 }