1 /*
2 * Copyright (c) 2023-2024 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 "dlp_zip_test.h"
17 #include <cstdio>
18 #include <cstring>
19 #include <fcntl.h>
20 #include <iostream>
21 #include <fstream>
22 #include <thread>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #define private public
26 #include "dlp_file.h"
27 #include "dlp_file_manager.h"
28 #undef private
29 #include "dlp_permission.h"
30 #include "dlp_permission_log.h"
31 #include "dlp_zip.h"
32 #include "c_mock_common.h"
33
34 using namespace testing::ext;
35 using namespace OHOS::Security::DlpPermission;
36 using namespace std;
37
38 namespace {
39 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_DLP_PERMISSION, "DlpFileTest"};
40
41 static const std::string DLP_TEST_DIR = "/data/dlpTest/";
42
initDlpFileCiper(DlpFile & testFile)43 void initDlpFileCiper(DlpFile &testFile)
44 {
45 uint8_t keyData[16] = {};
46 struct DlpBlob key = {
47 .data = keyData,
48 .size = 16
49 };
50
51 uint8_t ivData[16] = {};
52 struct DlpCipherParam param;
53 param.iv.data = ivData;
54 param.iv.size = IV_SIZE;
55 struct DlpUsageSpec spec = {
56 .mode = DLP_MODE_CTR,
57 .algParam = ¶m
58 };
59
60 uint8_t hmacKeyData[32] = {};
61 struct DlpBlob hmacKey = {
62 .data = hmacKeyData,
63 .size = 32
64 };
65
66 testFile.SetCipher(key, spec, hmacKey);
67 }
68 }
69
SetUpTestCase()70 void DlpZipTest::SetUpTestCase()
71 {
72 struct stat fstat;
73 if (stat(DLP_TEST_DIR.c_str(), &fstat) != 0) {
74 if (errno == ENOENT) {
75 int32_t ret = mkdir(DLP_TEST_DIR.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
76 if (ret < 0) {
77 DLP_LOG_ERROR(LABEL, "mkdir mount point failed errno %{public}d", errno);
78 return;
79 }
80 } else {
81 DLP_LOG_ERROR(LABEL, "get mount point failed errno %{public}d", errno);
82 return;
83 }
84 }
85 }
86
TearDownTestCase()87 void DlpZipTest::TearDownTestCase()
88 {
89 rmdir(DLP_TEST_DIR.c_str());
90 }
91
SetUp()92 void DlpZipTest::SetUp() {}
93
TearDown()94 void DlpZipTest::TearDown() {}
95
96 /**
97 * @tc.name: AddBuffToZip001
98 * @tc.desc: test AddBuffToZip
99 * @tc.type: FUNC
100 * @tc.require:
101 */
102 HWTEST_F(DlpZipTest, AddBuffToZip, TestSize.Level0)
103 {
104 DLP_LOG_INFO(LABEL, "AddBuffToZip");
105 std::string buf("123");
106 std::string inZip("dlp_general_info");
107 std::string zipFile("test_zip");
108
109 int32_t res = AddBuffToZip(buf.c_str(), buf.size(), inZip.c_str(), zipFile.c_str());
110 ASSERT_EQ(res, -1);
111
112 int32_t fd = open(zipFile.c_str(), O_RDWR | O_CREAT, 0666);
113 ASSERT_NE(fd, -1);
114
115 res = AddBuffToZip(buf.c_str(), buf.size(), inZip.c_str(), zipFile.c_str());
116 ASSERT_EQ(res, 0);
117
118 res = AddBuffToZip(buf.c_str(), buf.size(), inZip.c_str(), zipFile.c_str());
119 ASSERT_EQ(res, 0);
120
121 unlink(zipFile.c_str());
122 }
123
124 /**
125 * @tc.name: AddFileContextToZip001
126 * @tc.desc: test AddFileContextToZip
127 * @tc.type: FUNC
128 * @tc.require:
129 */
130 HWTEST_F(DlpZipTest, AddFileContextToZip, TestSize.Level0)
131 {
132 DLP_LOG_INFO(LABEL, "AddFileContextToZip");
133 std::string inZip("dlp_general_info");
134 std::string zipFile("test_zip");
135
136 int32_t res = AddFileContextToZip(-1, inZip.c_str(), zipFile.c_str());
137 ASSERT_EQ(res, -1);
138
139 int32_t fd = open(zipFile.c_str(), O_RDWR | O_CREAT, 0666);
140 ASSERT_NE(fd, -1);
141
142 int32_t fd2 = open(inZip.c_str(), O_RDWR | O_CREAT, 0666);
143 ASSERT_NE(fd, -1);
144
145 res = AddFileContextToZip(fd2, inZip.c_str(), zipFile.c_str());
146 ASSERT_EQ(res, 0);
147
148 res = AddFileContextToZip(fd2, inZip.c_str(), zipFile.c_str());
149 ASSERT_EQ(res, 0);
150
151 unlink(inZip.c_str());
152 unlink(zipFile.c_str());
153 }
154
155
156 /**
157 * @tc.name: AddFileContextToZip001
158 * @tc.desc: test AddFileContextToZip
159 * @tc.type: FUNC
160 * @tc.require:
161 */
162 HWTEST_F(DlpZipTest, IsZipFile, TestSize.Level0)
163 {
164 DLP_LOG_INFO(LABEL, "IsZipFile");
165 std::string inZip1("dlp_general_info1");
166 std::string inZip2("dlp_general_info");
167 std::string zipFile("test_zip");
168
169 int32_t res = IsZipFile(-1);
170 ASSERT_EQ(res, false);
171
172 int32_t fd = open(zipFile.c_str(), O_WRONLY | O_CREAT, 0666);
173 res = IsZipFile(-1);
174 ASSERT_EQ(res, false);
175
176 int32_t fd2 = open(zipFile.c_str(), O_RDWR | O_CREAT, 0666);
177 res = IsZipFile(fd2);
178 ASSERT_EQ(res, false);
179
180 int32_t fd3 = open(inZip1.c_str(), O_RDWR | O_CREAT, 0666);
181 res = AddFileContextToZip(fd3, inZip1.c_str(), zipFile.c_str());
182 res = IsZipFile(fd2);
183 ASSERT_EQ(res, false);
184
185 int32_t fd4 = open(inZip2.c_str(), O_RDWR | O_CREAT, 0666);
186 res = AddFileContextToZip(fd4, inZip2.c_str(), zipFile.c_str());
187 res = IsZipFile(fd2);
188 ASSERT_EQ(res, true);
189
190 close(fd);
191 close(fd2);
192 close(fd3);
193 close(fd4);
194
195 unlink(inZip1.c_str());
196 unlink(inZip2.c_str());
197 unlink(zipFile.c_str());
198 }