0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-19 19:42:17 +02:00

Fix unit test path issues on windows

This commit is contained in:
Mike Hardy 2019-11-03 19:43:16 -05:00
parent 4b7c26880a
commit 8e95e6885c
2 changed files with 11 additions and 6 deletions

View File

@ -30,6 +30,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Paths;
import java.util.Objects;
import androidx.test.ext.junit.runners.AndroidJUnit4;
@ -40,29 +41,31 @@ public class CompatCopyFileTest {
@Test
public void testCopyFileToStream() throws Exception {
URL resource = Objects.requireNonNull(getClass().getClassLoader()).getResource("path-traversal.zip");
String resourcePath = Paths.get(Objects.requireNonNull(getClass().getClassLoader()).getResource("path-traversal.zip").toURI()).toString();
File copy = File.createTempFile("testCopyFileToStream", ".zip");
copy.deleteOnExit();
FileOutputStream outputStream = new FileOutputStream(copy.getCanonicalPath());
CompatHelper.getCompat().copyFile(resource.getPath(), outputStream);
CompatHelper.getCompat().copyFile(resourcePath, outputStream);
outputStream.close();
Assert.assertEquals(TestUtils.getMD5(resource.getPath()), TestUtils.getMD5(copy.getCanonicalPath()));
Assert.assertEquals(TestUtils.getMD5(resourcePath), TestUtils.getMD5(copy.getCanonicalPath()));
}
@Test
public void testCopyStreamToFile() throws Exception {
URL resource = Objects.requireNonNull(getClass().getClassLoader()).getResource("path-traversal.zip");
String resourcePath = Paths.get(Objects.requireNonNull(getClass().getClassLoader()).getResource("path-traversal.zip").toURI()).toString();
File copy = File.createTempFile("testCopyStreamToFile", ".zip");
copy.deleteOnExit();
CompatHelper.getCompat().copyFile(resource.openStream(), copy.getCanonicalPath());
Assert.assertEquals(TestUtils.getMD5(resource.getPath()), TestUtils.getMD5(copy.getCanonicalPath()));
Assert.assertEquals(TestUtils.getMD5(resourcePath), TestUtils.getMD5(copy.getCanonicalPath()));
}
@Test
public void testCopyErrors() throws Exception {
URL resource = Objects.requireNonNull(getClass().getClassLoader()).getResource("path-traversal.zip");
String resourcePath = Paths.get(Objects.requireNonNull(getClass().getClassLoader()).getResource("path-traversal.zip").toURI()).toString();
File copy = File.createTempFile("testCopyStreamToFile", ".zip");
copy.deleteOnExit();
@ -78,7 +81,7 @@ public class CompatCopyFileTest {
try {
FileOutputStream outputStream = new FileOutputStream(copy.getCanonicalPath());
outputStream.close();
CompatHelper.getCompat().copyFile(resource.getPath(), outputStream);
CompatHelper.getCompat().copyFile(resourcePath, outputStream);
Assert.fail("Should have caught an exception");
} catch (IOException e) {
// this is expected

View File

@ -24,6 +24,7 @@ import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.Objects;
import java.util.zip.ZipEntry;
@ -76,10 +77,11 @@ public class UtilsTest {
@Test
public void testCopyFile() throws Exception {
String resourcePath = Paths.get(Objects.requireNonNull(getClass().getClassLoader()).getResource("path-traversal.zip").toURI()).toString();
URL resource = Objects.requireNonNull(getClass().getClassLoader()).getResource("path-traversal.zip");
File copy = File.createTempFile("testCopyFileToStream", ".zip");
copy.deleteOnExit();
Utils.copyFile(new File(resource.getFile()), copy);
Assert.assertEquals(TestUtils.getMD5(resource.getPath()), TestUtils.getMD5(copy.getCanonicalPath()));
Assert.assertEquals(TestUtils.getMD5(resourcePath), TestUtils.getMD5(copy.getCanonicalPath()));
}
}