• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "unit_test.h"
17 #include <thread>
18 #include <selinux/selinux.h>
19 #include "selinux_error.h"
20 #include "selinux_klog.h"
21 #include "selinux_log.h"
22 #include "test_common.h"
23 
24 using namespace testing::ext;
25 using namespace OHOS::Security::SelinuxUnitTest;
26 using namespace Selinux;
27 
SetUpTestCase()28 void SelinuxUnitTest::SetUpTestCase() {}
29 
TearDownTestCase()30 void SelinuxUnitTest::TearDownTestCase() {}
31 
SetUp()32 void SelinuxUnitTest::SetUp() {}
33 
TearDown()34 void SelinuxUnitTest::TearDown() {}
35 
36 /**
37  * @tc.name: MlsTest001
38  * @tc.desc: test mls.
39  * @tc.type: FUNC
40  * @tc.require:
41  */
42 HWTEST_F(SelinuxUnitTest, MlsTest001, TestSize.Level1)
43 {
44     pid_t pid = fork();
45     ASSERT_TRUE(pid >= 0);
46     const char *contextC10 = "u:r:normal_hap:s0:c10";
47     if (pid == 0) {
48         const char *contextS0 = "u:r:normal_hap:s0";
49         const char *contextC1023 = "u:r:normal_hap:s0:c1023";
50         const char *contextC1025 = "u:r:normal_hap:s0:c1025";
51         char *test = nullptr;
52         int ret = security_check_context(contextS0);
53         EXPECT_EQ(SELINUX_SUCC, ret);
54 
55         ret = security_check_context(contextC10);
56         EXPECT_EQ(SELINUX_SUCC, ret);
57 
58         ret = security_check_context(contextC1023);
59         EXPECT_EQ(SELINUX_SUCC, ret);
60 
61         ret = security_check_context(contextC1025);
62         EXPECT_NE(SELINUX_SUCC, ret);
63 
64         ret = setcon(contextC10);
65         ASSERT_EQ(SELINUX_SUCC, ret);
66         getcon(&test);
67         EXPECT_EQ(0, strcmp(test, contextC10));
68         usleep(20000); // 20000 : sleep 20ms
69         freecon(test);
70         exit(0);
71     } else {
72         usleep(10000); // 10000 : sleep 10ms
73         char *test1 = nullptr;
74         getpidcon(pid, &test1);
75         EXPECT_EQ(0, strcmp(test1, contextC10));
76         freecon(test1);
77     }
78 }
79 
80 /**
81  * @tc.name: SelinuxHilog001
82  * @tc.desc: Test 'int SelinuxHilog(int logLevel, const char *fmt, ...)' with g_logLevel info.
83  * @tc.type: FUNC
84  * @tc.require:
85  */
86 HWTEST_F(SelinuxUnitTest, SelinuxHilog001, TestSize.Level1)
87 {
88     SetSelinuxHilogLevel(SELINUX_HILOG_INFO);
89     EXPECT_EQ(0, SelinuxHilog(SELINUX_HILOG_INFO, "test"));
90     EXPECT_EQ(0, SelinuxHilog(SELINUX_HILOG_WARN, "test"));
91     EXPECT_EQ(0, SelinuxHilog(SELINUX_HILOG_ERROR, "test"));
92     EXPECT_EQ(0, SelinuxHilog(SELINUX_HILOG_AVC, "test"));
93 }
94 
95 /**
96  * @tc.name: SelinuxHilog002
97  * @tc.desc: Test 'int SelinuxHilog(int logLevel, const char *fmt, ...)' with g_logLevel warn.
98  * @tc.type: FUNC
99  * @tc.require:
100  */
101 HWTEST_F(SelinuxUnitTest, SelinuxHilog002, TestSize.Level1)
102 {
103     SetSelinuxHilogLevel(SELINUX_HILOG_WARN);
104     EXPECT_EQ(-1, SelinuxHilog(SELINUX_HILOG_INFO, "test"));
105     EXPECT_EQ(0, SelinuxHilog(SELINUX_HILOG_WARN, "test"));
106     EXPECT_EQ(0, SelinuxHilog(SELINUX_HILOG_ERROR, "test"));
107     EXPECT_EQ(0, SelinuxHilog(SELINUX_HILOG_AVC, "test"));
108 }
109 
110 /**
111  * @tc.name: SelinuxHilog003
112  * @tc.desc: Test 'int SelinuxHilog(int logLevel, const char *fmt, ...)' with g_logLevel error.
113  * @tc.type: FUNC
114  * @tc.require:
115  */
116 HWTEST_F(SelinuxUnitTest, SelinuxHilog003, TestSize.Level1)
117 {
118     SetSelinuxHilogLevel(SELINUX_HILOG_ERROR);
119     EXPECT_EQ(-1, SelinuxHilog(SELINUX_HILOG_INFO, "test"));
120     EXPECT_EQ(-1, SelinuxHilog(SELINUX_HILOG_WARN, "test"));
121     EXPECT_EQ(0, SelinuxHilog(SELINUX_HILOG_ERROR, "test"));
122     EXPECT_EQ(0, SelinuxHilog(SELINUX_HILOG_AVC, "test"));
123 }
124 
125 /**
126  * @tc.name: SelinuxHilog004
127  * @tc.desc: Test 'int SelinuxHilog(int logLevel, const char *fmt, ...)' with logLevel invalid.
128  * @tc.type: FUNC
129  * @tc.require:
130  */
131 HWTEST_F(SelinuxUnitTest, SelinuxHilog004, TestSize.Level1)
132 {
133     SetSelinuxHilogLevel(SELINUX_HILOG_ERROR);
134     EXPECT_EQ(-1, SelinuxHilog(SELINUX_HILOG_AVC + 1, "test"));
135 }
136 
137 /**
138  * @tc.name: SelinuxKmsg001
139  * @tc.desc: Test 'int SelinuxKmsg(int logLevel, const char *fmt, ...)' with g_logLevel info.
140  * @tc.type: FUNC
141  * @tc.require:
142  */
143 HWTEST_F(SelinuxUnitTest, SelinuxKmsg001, TestSize.Level1)
144 {
145     SetSelinuxKmsgLevel(SELINUX_KINFO);
146     EXPECT_EQ(0, SelinuxKmsg(SELINUX_KINFO, "test"));
147     EXPECT_EQ(0, SelinuxKmsg(SELINUX_KWARN, "test"));
148     EXPECT_EQ(0, SelinuxKmsg(SELINUX_KERROR, "test"));
149     EXPECT_EQ(0, SelinuxKmsg(SELINUX_KAVC, "test"));
150 }
151 
152 /**
153  * @tc.name: SelinuxKmsg002
154  * @tc.desc: Test 'int SelinuxKmsg(int logLevel, const char *fmt, ...)' with g_logLevel warn.
155  * @tc.type: FUNC
156  * @tc.require:
157  */
158 HWTEST_F(SelinuxUnitTest, SelinuxKmsg002, TestSize.Level1)
159 {
160     SetSelinuxKmsgLevel(SELINUX_KWARN);
161     EXPECT_EQ(-1, SelinuxKmsg(SELINUX_KINFO, "test"));
162     EXPECT_EQ(0, SelinuxKmsg(SELINUX_KWARN, "test"));
163     EXPECT_EQ(0, SelinuxKmsg(SELINUX_KERROR, "test"));
164     EXPECT_EQ(0, SelinuxKmsg(SELINUX_KAVC, "test"));
165 }
166 
167 /**
168  * @tc.name: SelinuxKmsg003
169  * @tc.desc: Test 'int SelinuxKmsg(int logLevel, const char *fmt, ...)' with g_logLevel error.
170  * @tc.type: FUNC
171  * @tc.require:
172  */
173 HWTEST_F(SelinuxUnitTest, SelinuxKmsg003, TestSize.Level1)
174 {
175     SetSelinuxKmsgLevel(SELINUX_KERROR);
176     EXPECT_EQ(-1, SelinuxKmsg(SELINUX_KINFO, "test"));
177     EXPECT_EQ(-1, SelinuxKmsg(SELINUX_KWARN, "test"));
178     EXPECT_EQ(0, SelinuxKmsg(SELINUX_KERROR, "test"));
179     EXPECT_EQ(0, SelinuxKmsg(SELINUX_KAVC, "test"));
180 }
181 
182 /**
183  * @tc.name: SelinuxKmsg004
184  * @tc.desc: Test 'int SelinuxKmsg(int logLevel, const char *fmt, ...)' with logLevel invalid.
185  * @tc.type: FUNC
186  * @tc.require:
187  */
188 HWTEST_F(SelinuxUnitTest, SelinuxKmsg004, TestSize.Level1)
189 {
190     SetSelinuxKmsgLevel(SELINUX_KERROR);
191     EXPECT_EQ(-1, SelinuxKmsg(SELINUX_KAVC + 1, "test"));
192 }
193