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 
18 #include <iostream>
19 #include <new>
20 #include <selinux/selinux.h>
21 #include <vector>
22 #include "gtest/gtest.h"
23 #include "contexts_trie.h"
24 #include "selinux_error.h"
25 #include "selinux_map.h"
26 #include "selinux_share_mem.h"
27 #include "cstdlib"
28 #include "test_common.h"
29 #include "unistd.h"
30 
31 namespace OHOS {
32 namespace Security {
33 namespace SelinuxUnitTest {
34 using namespace testing::ext;
35 using namespace Selinux;
36 const static std::string PARAMETER_CONTEXTS_FILE = "/system/etc/selinux/targeted/contexts/parameter_contexts";
37 static const char DEFAULT_CONTEXT[] = "u:object_r:default_param:s0";
38 
SetUpTestCase()39 void SelinuxUnitTest::SetUpTestCase() {}
40 
TearDownTestCase()41 void SelinuxUnitTest::TearDownTestCase() {}
42 
SetUp()43 void SelinuxUnitTest::SetUp() {}
44 
TearDown()45 void SelinuxUnitTest::TearDown() {}
46 
GenerateParamHashNode(const std::string & name,ParamHashNode ** groupNode)47 static bool GenerateParamHashNode(const std::string &name, ParamHashNode **groupNode)
48 {
49     auto tmp = static_cast<ParamHashNode*>(calloc(1, sizeof(ParamHashNode)));
50     if (tmp == nullptr) {
51         return false;
52     }
53     tmp->nameLen = name.size();
54     tmp->name = strdup(name.c_str());
55     if (tmp->name == nullptr) {
56         free(tmp);
57         return false;
58     }
59     tmp->childPtr = nullptr;
60     *groupNode = tmp;
61     return true;
62 }
63 
FreeParamHashNode(ParamHashNode * groupNode)64 static void FreeParamHashNode(ParamHashNode *groupNode)
65 {
66     if (groupNode == nullptr) {
67         return;
68     }
69     if (groupNode->name != nullptr) {
70         free(groupNode->name);
71     }
72     free(groupNode);
73 }
74 
75 /**
76  * @tc.name: HashMapCreate001
77  * @tc.desc: Test 'int32_t HashMapCreate(HashTab **handle)' with invalid params.
78  * @tc.type: FUNC
79  * @tc.require:
80  */
81 HWTEST_F(SelinuxUnitTest, HashMapCreate001, TestSize.Level1)
82 {
83     ASSERT_EQ(-1, HashMapCreate(nullptr));
84 }
85 
86 /**
87  * @tc.name: HashMapCreate002
88  * @tc.desc: Test 'int32_t HashMapCreate(HashTab **handle)' with valid params.
89  * @tc.type: FUNC
90  * @tc.require:
91  */
92 HWTEST_F(SelinuxUnitTest, HashMapCreate002, TestSize.Level1)
93 {
94     HashTab *handle = nullptr;
95     EXPECT_EQ(0, HashMapCreate(&handle));
96     HashMapDestroy(handle);
97 }
98 
99 /**
100  * @tc.name: HashMapDestroy001
101  * @tc.desc: Test 'void HashMapDestroy(HashTab *handle)' with handle nullptr.
102  * @tc.type: FUNC
103  * @tc.require:
104  */
105 HWTEST_F(SelinuxUnitTest, HashMapDestroy001, TestSize.Level1)
106 {
107     HashTab *handle = nullptr;
108     HashMapDestroy(handle);
109     ASSERT_EQ(nullptr, handle);
110 }
111 
112 /**
113  * @tc.name: HashMapDestroy002
114  * @tc.desc: Test 'void HashMapDestroy(HashTab *handle)' with handle valid.
115  * @tc.type: FUNC
116  * @tc.require:
117  */
118 HWTEST_F(SelinuxUnitTest, HashMapDestroy002, TestSize.Level1)
119 {
120     HashTab *handle = nullptr;
121     ASSERT_TRUE(HashMapCreate(&handle) == 0);
122     ASSERT_TRUE(handle != nullptr);
123 
124     for (size_t i = 0; i < 100; i++) {
125         ParamHashNode *groupNode = nullptr;
126         ASSERT_TRUE(GenerateParamHashNode(std::to_string(i), &groupNode));
127         ASSERT_EQ(HashMapAdd(handle, &(groupNode->hashNode)), 0);
128     }
129     HashMapDestroy(handle);
130 }
131 
132 /**
133  * @tc.name: HashMapAdd001
134  * @tc.desc: Test 'int32_t HashMapAdd(HashTab *handle, HashNode *node)' with handle nullptr
135  * @tc.type: FUNC
136  * @tc.require:
137  */
138 HWTEST_F(SelinuxUnitTest, HashMapAdd001, TestSize.Level1)
139 {
140     ParamHashNode *groupNode = nullptr;
141     ASSERT_TRUE(GenerateParamHashNode("test", &groupNode));
142     EXPECT_EQ(-1, HashMapAdd(nullptr, &(groupNode->hashNode)));
143     FreeParamHashNode(groupNode);
144 }
145 
146 /**
147  * @tc.name: HashMapAdd002
148  * @tc.desc: Test 'int32_t HashMapAdd(HashTab *handle, HashNode *node)' with node nullptr
149  * @tc.type: FUNC
150  * @tc.require:
151  */
152 HWTEST_F(SelinuxUnitTest, HashMapAdd002, TestSize.Level1)
153 {
154     HashTab *handle = nullptr;
155     ASSERT_EQ(0, HashMapCreate(&handle));
156     ASSERT_EQ(-1, HashMapAdd(handle, nullptr));
157     HashMapDestroy(handle);
158 }
159 
160 /**
161  * @tc.name: HashMapAdd003
162  * @tc.desc: Test 'int32_t HashMapAdd(HashTab *handle, HashNode *node)' with node->next not nullptr
163  * @tc.type: FUNC
164  * @tc.require:
165  */
166 HWTEST_F(SelinuxUnitTest, HashMapAdd003, TestSize.Level1)
167 {
168     HashTab *handle = nullptr;
169     ASSERT_EQ(0, HashMapCreate(&handle));
170     ParamHashNode *groupNode = nullptr;
171     ASSERT_TRUE(GenerateParamHashNode("test", &groupNode));
172     groupNode->hashNode.next = new (std::nothrow) HashNode;
173     EXPECT_EQ(-1, HashMapAdd(handle, &(groupNode->hashNode)));
174     FreeParamHashNode(groupNode);
175     HashMapDestroy(handle);
176 }
177 
178 /**
179  * @tc.name: HashMapAdd004
180  * @tc.desc: Test 'int32_t HashMapAdd(HashTab *handle, HashNode *node)' with key exist
181  * @tc.type: FUNC
182  * @tc.require:
183  */
184 HWTEST_F(SelinuxUnitTest, HashMapAdd004, TestSize.Level1)
185 {
186     HashTab *handle = nullptr;
187     ASSERT_EQ(0, HashMapCreate(&handle));
188     ParamHashNode *groupNode1 = nullptr;
189     ASSERT_TRUE(GenerateParamHashNode("test", &groupNode1));
190     ASSERT_EQ(0, HashMapAdd(handle, &(groupNode1->hashNode)));
191     ParamHashNode *groupNode2 = nullptr;
192     ASSERT_TRUE(GenerateParamHashNode("test", &groupNode2));
193     ASSERT_EQ(-1, HashMapAdd(handle, &(groupNode2->hashNode)));
194     FreeParamHashNode(groupNode2);
195     HashMapDestroy(handle);
196 }
197 
198 /**
199  * @tc.name: InitSharedMem001
200  * @tc.desc: Test 'void *InitSharedMem(const char *fileName, uint32_t spaceSize, bool readOnly)' with invalid params.
201  * @tc.type: FUNC
202  * @tc.require:
203  */
204 HWTEST_F(SelinuxUnitTest, InitSharedMem001, TestSize.Level1)
205 {
206     EXPECT_EQ(nullptr, InitSharedMem("", 0, 0));
207     ASSERT_NE(0, access("/invalid_path", R_OK));
208     EXPECT_EQ(nullptr, InitSharedMem("/invalid_path", 1024 * 80, 1));
209     EXPECT_EQ(nullptr, InitSharedMem("/dev/__parameters__/param_selinux", 0, 0));
210 }
211 
212 /**
213  * @tc.name: ReadSharedMem001
214  * @tc.desc: Test 'char *ReadSharedMem(char *sharedMem, uint32_t length)' with invalid params.
215  * @tc.type: FUNC
216  * @tc.require:
217  */
218 HWTEST_F(SelinuxUnitTest, ReadSharedMem001, TestSize.Level1)
219 {
220     EXPECT_EQ(nullptr, ReadSharedMem(nullptr, 1));
221     EXPECT_EQ(nullptr, ReadSharedMem("test", 0));
222 }
223 
224 /**
225  * @tc.name: ReadSharedMem002
226  * @tc.desc: Test 'char *ReadSharedMem(char *sharedMem, uint32_t length)' with valid params.
227  * @tc.type: FUNC
228  * @tc.require:
229  */
230 HWTEST_F(SelinuxUnitTest, ReadSharedMem002, TestSize.Level1)
231 {
232     EXPECT_EQ("test", ReadSharedMem("test", 4));
233 }
234 } // namespace SelinuxUnitTest
235 } // namespace Security
236 } // namespace OHOS
237