forked from oras-project/oras-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArchiveUtilsTest.java
More file actions
245 lines (209 loc) · 10.2 KB
/
Copy pathArchiveUtilsTest.java
File metadata and controls
245 lines (209 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*-
* =LICENSE=
* ORAS Java SDK
* ===
* Copyright (C) 2024 - 2025 ORAS
* ===
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =LICENSEEND=
*/
package land.oras.utils;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Set;
import land.oras.LocalPath;
import land.oras.exception.OrasException;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.CleanupMode;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Execution(ExecutionMode.CONCURRENT)
class ArchiveUtilsTest {
/**
* Logger
*/
private static Logger LOG = LoggerFactory.getLogger(ArchiveUtilsTest.class);
/**
* Archive temporary dir
*/
@TempDir(cleanup = CleanupMode.ON_SUCCESS)
private static Path archiveDir;
@TempDir(cleanup = CleanupMode.ON_SUCCESS)
private static Path targetGzDir;
@TempDir(cleanup = CleanupMode.ON_SUCCESS)
private static Path targetZstdDir;
@BeforeAll
static void beforeAll() throws Exception {
// Create directory structure with few files
Path dir1 = archiveDir.resolve("dir1");
Files.createDirectory(dir1);
Path dir2 = archiveDir.resolve("dir2");
Files.createDirectory(dir2);
Path dir3 = Files.createDirectory(dir2.resolve("dir3"));
// Empty directory
Files.createDirectory(archiveDir.resolve("empty"));
Path file1 = dir1.resolve("file1");
Path file2 = dir2.resolve("file2");
Path file4 = dir3.resolve("file4");
// Write some content to the files
Files.writeString(file1, "file1");
Files.writeString(file2, "file2");
Files.writeString(file4, "file4");
// Create one symlink file3 -> file1
Path file3 = dir1.resolve("file3");
Files.createSymbolicLink(file3, file1);
// Add 777 permission to file2
Files.setPosixFilePermissions(
file2,
Set.of(
PosixFilePermission.OWNER_READ,
PosixFilePermission.OWNER_WRITE,
PosixFilePermission.OWNER_EXECUTE,
PosixFilePermission.GROUP_READ,
PosixFilePermission.GROUP_WRITE,
PosixFilePermission.GROUP_EXECUTE,
PosixFilePermission.OTHERS_READ,
PosixFilePermission.OTHERS_WRITE,
PosixFilePermission.OTHERS_EXECUTE));
}
@Test
void testEnsureSafeEntry() throws Exception {
TarArchiveEntry entry = mock(TarArchiveEntry.class);
doReturn("test").when(entry).getName();
ArchiveUtils.ensureSafeEntry(entry, archiveDir);
}
@Test
void throwOnUnsafeEntries() throws Exception {
TarArchiveEntry entry = mock(TarArchiveEntry.class);
// Simulate a path traversal attack
assertThrows(IOException.class, () -> {
doReturn("/").when(entry).getName();
ArchiveUtils.ensureSafeEntry(entry, archiveDir);
});
assertThrows(IOException.class, () -> {
doReturn("foo/bar/../../../test").when(entry).getName();
ArchiveUtils.ensureSafeEntry(entry, archiveDir);
});
}
@Test
void shouldFailWithUnknownDirectories() {
assertThrows(OrasException.class, () -> {
ArchiveUtils.untar(Path.of("unknown"), Path.of("foo"));
});
assertThrows(OrasException.class, () -> {
ArchiveUtils.uncompressuntar(Path.of("unknown"), SupportedCompression.GZIP.getMediaType());
});
assertThrows(OrasException.class, () -> {
ArchiveUtils.tar(LocalPath.of("foo"));
});
assertThrows(OrasException.class, () -> {
ArchiveUtils.tarcompress(LocalPath.of("foo"), SupportedCompression.ZSTD.getMediaType());
});
}
@Test
void shouldCreateTarGzAndExtractIt() throws Exception {
LocalPath directory = LocalPath.of(archiveDir);
LocalPath archive = ArchiveUtils.tar(LocalPath.of(archiveDir));
LOG.info("Archive created: {}", archive);
Path compressedArchive = ArchiveUtils.tarcompress(LocalPath.of(archiveDir), directory.getMediaType())
.getPath();
assertTrue(Files.exists(compressedArchive), "Archive should exist");
ArchiveUtils.uncompressuntar(compressedArchive, targetGzDir, directory.getMediaType());
// Untar to temporary
Path tmp = ArchiveUtils.untar(archive.getPath());
assertTrue(Files.exists(tmp), "Temp should exist");
assertTrue(Files.exists(tmp.resolve(directory.getPath().getFileName()).resolve("dir1")), "dir1 should exist");
// Ensure all files are extracted
Path extractedDir = targetGzDir.resolve(directory.getPath().getFileName());
assertTrue(Files.exists(extractedDir.resolve("dir1")), "dir1 should exist");
assertTrue(Files.exists(extractedDir.resolve("dir2")), "dir2 should exist");
assertTrue(Files.exists(extractedDir.resolve("dir1").resolve("file1")), "file1 should exist");
assertTrue(Files.exists(extractedDir.resolve("dir2").resolve("file2")), "file2 should exist");
assertTrue(Files.exists(extractedDir.resolve("dir1").resolve("file3")), "file3 should exist");
assertTrue(Files.exists(extractedDir.resolve("dir2").resolve("dir3")), "dir3 should exist");
assertTrue(Files.exists(extractedDir.resolve("dir2").resolve("dir3").resolve("file4")), "file4 should exist");
// Empty directory
assertTrue(Files.exists(extractedDir.resolve("empty")), "empty should exist");
// Assert file content
assertTrue(
Files.readString(extractedDir.resolve("dir1").resolve("file1")).equals("file1"),
"file1 content should match");
assertTrue(
Files.readString(extractedDir.resolve("dir2").resolve("file2")).equals("file2"),
"file2 content should match");
assertTrue(
Files.readString(extractedDir.resolve("dir2").resolve("dir3").resolve("file4"))
.equals("file4"),
"file4 content should match");
// Ensure symlink is extracted
assertTrue(Files.isSymbolicLink(extractedDir.resolve("dir1").resolve("file3")), "file3 should be symlink");
// To temporary
Path temp = ArchiveUtils.uncompressuntar(compressedArchive, directory.getMediaType());
assertTrue(Files.exists(temp), "Temp should exist");
}
@Test
void shouldCreateTarZstdAndExtractIt() throws Exception {
LocalPath directory = LocalPath.of(archiveDir, Const.BLOB_DIR_ZSTD_MEDIA_TYPE);
LocalPath archive = ArchiveUtils.tar(directory);
LOG.info("Archive created: {}", archive);
Path compressedArchive =
ArchiveUtils.compress(archive, directory.getMediaType()).getPath();
assertTrue(Files.exists(compressedArchive), "Archive should exist");
Path uncompressedArchive = ArchiveUtils.uncompress(
Files.newInputStream(compressedArchive), Const.BLOB_DIR_ZSTD_MEDIA_TYPE)
.getPath();
ArchiveUtils.untar(Files.newInputStream(uncompressedArchive), targetZstdDir);
// Untar to temporary
Path tmp = ArchiveUtils.untar(archive.getPath());
assertTrue(Files.exists(tmp), "Temp should exist");
assertTrue(Files.exists(tmp.resolve(directory.getPath().getFileName()).resolve("dir1")), "dir1 should exist");
// Ensure all files are extracted
Path extractedDir = targetZstdDir.resolve(directory.getPath().getFileName());
assertTrue(Files.exists(extractedDir.resolve("dir1")), "dir1 should exist");
assertTrue(Files.exists(extractedDir.resolve("dir2")), "dir2 should exist");
assertTrue(Files.exists(extractedDir.resolve("dir1").resolve("file1")), "file1 should exist");
assertTrue(Files.exists(extractedDir.resolve("dir2").resolve("file2")), "file2 should exist");
assertTrue(Files.exists(extractedDir.resolve("dir1").resolve("file3")), "file3 should exist");
assertTrue(Files.exists(extractedDir.resolve("dir2").resolve("dir3")), "dir3 should exist");
assertTrue(Files.exists(extractedDir.resolve("dir2").resolve("dir3").resolve("file4")), "file4 should exist");
// Empty directory
assertTrue(Files.exists(extractedDir.resolve("empty")), "empty should exist");
// Assert file content
assertTrue(
Files.readString(extractedDir.resolve("dir1").resolve("file1")).equals("file1"),
"file1 content should match");
assertTrue(
Files.readString(extractedDir.resolve("dir2").resolve("file2")).equals("file2"),
"file2 content should match");
assertTrue(
Files.readString(extractedDir.resolve("dir2").resolve("dir3").resolve("file4"))
.equals("file4"),
"file4 content should match");
// Ensure symlink is extracted
assertTrue(Files.isSymbolicLink(extractedDir.resolve("dir1").resolve("file3")), "file3 should be symlink");
// To temporary
Path temp = ArchiveUtils.uncompressuntar(compressedArchive, directory.getMediaType());
assertTrue(Files.exists(temp), "Temp should exist");
}
}