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 namespace OHOS {
25 namespace Security {
26 namespace SelinuxUnitTest {
27 using namespace testing::ext;
28 using namespace Selinux;
29 
SetUpTestCase()30 void SelinuxUnitTest::SetUpTestCase() {}
31 
TearDownTestCase()32 void SelinuxUnitTest::TearDownTestCase() {}
33 
SetUp()34 void SelinuxUnitTest::SetUp() {}
35 
TearDown()36 void SelinuxUnitTest::TearDown() {}
37 
38 /**
39  * @tc.name: SelinuxHilog001
40  * @tc.desc: Test 'int SelinuxHilog(int logLevel, const char *fmt, ...)' with g_logLevel info.
41  * @tc.type: FUNC
42  * @tc.require:
43  */
44 HWTEST_F(SelinuxUnitTest, SelinuxHilog001, TestSize.Level1)
45 {
46     SetSelinuxHilogLevel(SELINUX_HILOG_INFO);
47     EXPECT_EQ(0, SelinuxHilog(SELINUX_HILOG_INFO, "test"));
48     EXPECT_EQ(0, SelinuxHilog(SELINUX_HILOG_WARN, "test"));
49     EXPECT_EQ(0, SelinuxHilog(SELINUX_HILOG_ERROR, "test"));
50     EXPECT_EQ(0, SelinuxHilog(SELINUX_HILOG_AVC, "test"));
51 }
52 
53 /**
54  * @tc.name: SelinuxHilog002
55  * @tc.desc: Test 'int SelinuxHilog(int logLevel, const char *fmt, ...)' with g_logLevel warn.
56  * @tc.type: FUNC
57  * @tc.require:
58  */
59 HWTEST_F(SelinuxUnitTest, SelinuxHilog002, TestSize.Level1)
60 {
61     SetSelinuxHilogLevel(SELINUX_HILOG_WARN);
62     EXPECT_EQ(-1, SelinuxHilog(SELINUX_HILOG_INFO, "test"));
63     EXPECT_EQ(0, SelinuxHilog(SELINUX_HILOG_WARN, "test"));
64     EXPECT_EQ(0, SelinuxHilog(SELINUX_HILOG_ERROR, "test"));
65     EXPECT_EQ(0, SelinuxHilog(SELINUX_HILOG_AVC, "test"));
66 }
67 
68 /**
69  * @tc.name: SelinuxHilog003
70  * @tc.desc: Test 'int SelinuxHilog(int logLevel, const char *fmt, ...)' with g_logLevel error.
71  * @tc.type: FUNC
72  * @tc.require:
73  */
74 HWTEST_F(SelinuxUnitTest, SelinuxHilog003, TestSize.Level1)
75 {
76     SetSelinuxHilogLevel(SELINUX_HILOG_ERROR);
77     EXPECT_EQ(-1, SelinuxHilog(SELINUX_HILOG_INFO, "test"));
78     EXPECT_EQ(-1, SelinuxHilog(SELINUX_HILOG_WARN, "test"));
79     EXPECT_EQ(0, SelinuxHilog(SELINUX_HILOG_ERROR, "test"));
80     EXPECT_EQ(0, SelinuxHilog(SELINUX_HILOG_AVC, "test"));
81 }
82 
83 /**
84  * @tc.name: SelinuxHilog004
85  * @tc.desc: Test 'int SelinuxHilog(int logLevel, const char *fmt, ...)' with logLevel invalid.
86  * @tc.type: FUNC
87  * @tc.require:
88  */
89 HWTEST_F(SelinuxUnitTest, SelinuxHilog004, TestSize.Level1)
90 {
91     SetSelinuxHilogLevel(SELINUX_HILOG_ERROR);
92     EXPECT_EQ(-1, SelinuxHilog(SELINUX_HILOG_AVC + 1, "test"));
93 }
94 
95 /**
96  * @tc.name: SelinuxKmsg001
97  * @tc.desc: Test 'int SelinuxKmsg(int logLevel, const char *fmt, ...)' with g_logLevel info.
98  * @tc.type: FUNC
99  * @tc.require:
100  */
101 HWTEST_F(SelinuxUnitTest, SelinuxKmsg001, TestSize.Level1)
102 {
103     SetSelinuxKmsgLevel(SELINUX_KINFO);
104     EXPECT_EQ(0, SelinuxKmsg(SELINUX_KINFO, "test"));
105     EXPECT_EQ(0, SelinuxKmsg(SELINUX_KWARN, "test"));
106     EXPECT_EQ(0, SelinuxKmsg(SELINUX_KERROR, "test"));
107     EXPECT_EQ(0, SelinuxKmsg(SELINUX_KAVC, "test"));
108 }
109 
110 /**
111  * @tc.name: SelinuxKmsg002
112  * @tc.desc: Test 'int SelinuxKmsg(int logLevel, const char *fmt, ...)' with g_logLevel warn.
113  * @tc.type: FUNC
114  * @tc.require:
115  */
116 HWTEST_F(SelinuxUnitTest, SelinuxKmsg002, TestSize.Level1)
117 {
118     SetSelinuxKmsgLevel(SELINUX_KWARN);
119     EXPECT_EQ(-1, SelinuxKmsg(SELINUX_KINFO, "test"));
120     EXPECT_EQ(0, SelinuxKmsg(SELINUX_KWARN, "test"));
121     EXPECT_EQ(0, SelinuxKmsg(SELINUX_KERROR, "test"));
122     EXPECT_EQ(0, SelinuxKmsg(SELINUX_KAVC, "test"));
123 }
124 
125 /**
126  * @tc.name: SelinuxKmsg003
127  * @tc.desc: Test 'int SelinuxKmsg(int logLevel, const char *fmt, ...)' with g_logLevel error.
128  * @tc.type: FUNC
129  * @tc.require:
130  */
131 HWTEST_F(SelinuxUnitTest, SelinuxKmsg003, TestSize.Level1)
132 {
133     SetSelinuxKmsgLevel(SELINUX_KERROR);
134     EXPECT_EQ(-1, SelinuxKmsg(SELINUX_KINFO, "test"));
135     EXPECT_EQ(-1, SelinuxKmsg(SELINUX_KWARN, "test"));
136     EXPECT_EQ(0, SelinuxKmsg(SELINUX_KERROR, "test"));
137     EXPECT_EQ(0, SelinuxKmsg(SELINUX_KAVC, "test"));
138 }
139 
140 /**
141  * @tc.name: SelinuxKmsg004
142  * @tc.desc: Test 'int SelinuxKmsg(int logLevel, const char *fmt, ...)' with logLevel invalid.
143  * @tc.type: FUNC
144  * @tc.require:
145  */
146 HWTEST_F(SelinuxUnitTest, SelinuxKmsg004, TestSize.Level1)
147 {
148     SetSelinuxKmsgLevel(SELINUX_KERROR);
149     EXPECT_EQ(-1, SelinuxKmsg(SELINUX_KAVC + 1, "test"));
150 }
151 } // namespace SelinuxUnitTest
152 } // namespace Security
153 } // namespace OHOS
154