Skip to content

Commit d4743f1

Browse files
committed
Temp file coverage
Creating temp dirs with varying permissions to trigger problem cases Also added simulated case for being unable to immediately clean-up the temp file
1 parent 6540109 commit d4743f1

2 files changed

Lines changed: 92 additions & 19 deletions

File tree

components/native-loader/src/main/java/datadog/nativeloader/LibFile.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package datadog.nativeloader;
22

33
import java.io.File;
4+
import java.nio.file.Path;
45

56
/**
67
* Represents a resolved library
@@ -54,6 +55,16 @@ public void load() throws LibraryLoadException {
5455
throw new LibraryLoadException(this.libName, t);
5556
}
5657
}
58+
59+
/** Provides a File to the library -- returns null for pre-loaded libraries */
60+
public final File toFile() {
61+
return this.file;
62+
}
63+
64+
/** Provides a Path to the library -- return null for pre-loaded libraries */
65+
public final Path toPath() {
66+
return this.file == null ? null : this.file.toPath();
67+
}
5768

5869
/** Provides the an absolute path to the library -- returns null for pre-loaded libraries */
5970
public final String getAbsolutePath() {

components/native-loader/src/test/java/datadog/nativeloader/NativeLoaderTest.java

Lines changed: 81 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
import java.io.IOException;
1616
import java.net.URL;
1717
import java.net.URLClassLoader;
18+
import java.nio.file.Files;
19+
import java.nio.file.NoSuchFileException;
20+
import java.nio.file.Path;
21+
import java.nio.file.Paths;
22+
import java.nio.file.attribute.PosixFilePermissions;
23+
1824
import org.junit.jupiter.api.Test;
1925

2026
public class NativeLoaderTest {
@@ -208,49 +214,105 @@ public void fromDirBackedClassLoader_with_subResource_and_comp()
208214
public void fromJarBackedClassLoader() throws IOException, LibraryLoadException {
209215
URL[] urls = {new File("test-data/libdummy.jar").toURL()};
210216

211-
URLClassLoader classLoader = new URLClassLoader(urls);
212-
213-
NativeLoader loader = NativeLoader.builder().fromClassLoader(classLoader).build();
214-
try (LibFile lib = loader.resolveDynamic("dummy")) {
215-
// loaded from a jar, so copied to temp file
216-
assertTempFile(lib);
217+
try (URLClassLoader classLoader = new URLClassLoader(urls)) {
218+
NativeLoader loader = NativeLoader.builder().fromClassLoader(classLoader).build();
219+
try (LibFile lib = loader.resolveDynamic("dummy")) {
220+
// loaded from a jar, so copied to temp file
221+
assertTempFile(lib);
222+
}
217223
}
218224
}
219225

220226
@Test
221227
public void fromJarBackedClassLoader_with_tempDir() throws IOException, LibraryLoadException {
222228
URL[] urls = {new File("test-data/libdummy.jar").toURL()};
229+
230+
Path tempDir = Paths.get("temp");
231+
deleteHelper(tempDir);
223232

224-
URLClassLoader classLoader = new URLClassLoader(urls);
233+
try (URLClassLoader classLoader = new URLClassLoader(urls)) {
234+
NativeLoader loader =
235+
NativeLoader.builder().fromClassLoader(classLoader).tempDir(tempDir).build();
236+
try (LibFile lib = loader.resolveDynamic("dummy")) {
237+
// loaded from a jar, so copied to temp file
238+
assertTempFile(lib);
239+
}
240+
} finally {
241+
deleteHelper(tempDir);
242+
}
243+
}
244+
245+
@Test
246+
public void fromJarBackedClassLoader_with_unwritable_tempDir() throws IOException, LibraryLoadException {
247+
URL[] urls = {new File("test-data/libdummy.jar").toURL()};
248+
249+
Path noWriteDir = Paths.get("no-write-temp");
250+
deleteHelper(noWriteDir);
251+
Files.createDirectories(noWriteDir, PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("r-x------")));
225252

226-
NativeLoader loader =
227-
NativeLoader.builder().fromClassLoader(classLoader).tempDir("temp").build();
228-
try (LibFile lib = loader.resolveDynamic("dummy")) {
229-
// loaded from a jar, so copied to temp file
230-
assertTempFile(lib);
253+
try (URLClassLoader classLoader = new URLClassLoader(urls)) {
254+
NativeLoader loader = NativeLoader.builder().fromClassLoader(classLoader).tempDir(noWriteDir).build();
255+
256+
// unable to resolve to a File because tempDir isn't writable
257+
assertThrows(LibraryLoadException.class, () -> loader.resolveDynamic("dummy"));
258+
} finally {
259+
deleteHelper(noWriteDir);
260+
}
261+
}
262+
263+
@Test
264+
public void fromJarBackedClassLoader_with_locked_file() throws IOException, LibraryLoadException {
265+
URL[] urls = {new File("test-data/libdummy.jar").toURL()};
266+
267+
Path tempDir = Paths.get("temp");
268+
deleteHelper(tempDir);
269+
270+
try (URLClassLoader classLoader = new URLClassLoader(urls)) {
271+
NativeLoader loader = NativeLoader.builder().fromClassLoader(classLoader).tempDir(tempDir).build();
272+
try (LibFile lib = loader.resolveDynamic("dummy")) {
273+
// loaded from a jar, so copied to temp file
274+
assertTempFile(lib);
275+
276+
// simulating lock - by blocking ability to delete from parent dir
277+
// forces fallback to deleteOnExit
278+
Files.setPosixFilePermissions(lib.toPath().getParent(), PosixFilePermissions.fromString("r-x------"));
279+
}
280+
} finally {
281+
deleteHelper(tempDir);
231282
}
232283
}
284+
285+
void deleteHelper(Path dir) {
286+
try {
287+
Files.setPosixFilePermissions(dir, PosixFilePermissions.fromString("rwx------"));
288+
Files.delete(dir);
289+
} catch (IOException e) {
290+
}
291+
}
233292

234293
void assertPreloaded(LibFile lib) {
235294
assertTrue(lib.isPreloaded());
236-
assertNull(lib.file);
295+
assertNull(lib.toFile());
296+
assertNull(lib.toPath());
237297
assertNull(lib.getAbsolutePath());
238298
assertFalse(lib.needsCleanup);
239299
}
240300

241301
void assertRegularFile(LibFile lib) {
242302
assertFalse(lib.isPreloaded());
243-
assertNotNull(lib.file);
244-
assertTrue(lib.file.exists());
245-
assertEquals(lib.file.getAbsolutePath(), lib.getAbsolutePath());
303+
assertNotNull(lib.toFile());
304+
assertNotNull(lib.toPath());
305+
assertTrue(lib.toFile().exists());
306+
assertNotNull(lib.getAbsolutePath());
246307
assertFalse(lib.needsCleanup);
247308
}
248309

249310
void assertTempFile(LibFile lib) {
250311
assertFalse(lib.isPreloaded());
251-
assertNotNull(lib.file);
252-
assertTrue(lib.file.exists());
253-
assertEquals(lib.file.getAbsolutePath(), lib.getAbsolutePath());
312+
assertNotNull(lib.toFile());
313+
assertNotNull(lib.toPath());
314+
assertTrue(lib.toFile().exists());
315+
assertNotNull(lib.getAbsolutePath());
254316
assertTrue(lib.needsCleanup);
255317
}
256318
}

0 commit comments

Comments
 (0)