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