handle installing OTA files separately from generic .zip files

It is valid to include .zip files in a repo, but only OTA ZIP files
should be installed into the OTA dir.
This commit is contained in:
Hans-Christoph Steiner
2020-10-19 16:10:45 +02:00
parent 5a0092d42e
commit 4bb158ef77
5 changed files with 158 additions and 7 deletions

View File

@@ -0,0 +1,76 @@
package org.fdroid.fdroid.data;
import android.content.ContextWrapper;
import android.os.Environment;
import android.util.Log;
import android.webkit.MimeTypeMap;
import androidx.test.core.app.ApplicationProvider;
import org.apache.commons.io.FileUtils;
import org.fdroid.fdroid.installer.ApkCache;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.Shadows;
import org.robolectric.shadows.ShadowLog;
import org.robolectric.shadows.ShadowMimeTypeMap;
import java.io.File;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@RunWith(RobolectricTestRunner.class)
public class ApkTest {
public static final String TAG = "ApkTest";
private static ContextWrapper context;
@Before
public final void setUp() {
context = ApplicationProvider.getApplicationContext();
ShadowMimeTypeMap mimeTypeMap = Shadows.shadowOf(MimeTypeMap.getSingleton());
mimeTypeMap.addExtensionMimeTypMapping("apk", "application/vnd.android.package-archive");
mimeTypeMap.addExtensionMimeTypMapping("obf", "application/octet-stream");
mimeTypeMap.addExtensionMimeTypMapping("zip", "application/zip");
ShadowLog.stream = System.out;
}
@Test(expected = IllegalStateException.class)
public void testGetMediaInstallPathWithApk() {
Apk apk = new Apk();
apk.apkName = "test.apk";
apk.repoAddress = "https://example.com/fdroid/repo";
assertTrue(apk.isApk());
apk.getMediaInstallPath(context);
}
@Test
public void testGetMediaInstallPathWithOta() throws IOException {
Apk apk = new Apk();
apk.apkName = "org.fdroid.fdroid.privileged.ota_2110.zip";
apk.repoAddress = "https://example.com/fdroid/repo";
assertFalse(apk.isApk());
copyResourceFileToCache(apk);
File path = apk.getMediaInstallPath(context);
assertEquals(new File(context.getApplicationInfo().dataDir + "/ota"), path);
}
@Test
public void testGetMediaInstallPathWithObfZip() throws IOException {
Apk apk = new Apk();
apk.apkName = "Norway_bouvet_europe_2.obf.zip";
apk.repoAddress = "https://example.com/fdroid/repo";
assertFalse(apk.isApk());
copyResourceFileToCache(apk);
File path = apk.getMediaInstallPath(context);
assertEquals(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), path);
}
private void copyResourceFileToCache(Apk apk) throws IOException {
FileUtils.copyInputStreamToFile(getClass().getClassLoader().getResource(apk.apkName).openStream(),
ApkCache.getApkDownloadPath(context, apk.getCanonicalUrl()));
}
}

View File

@@ -0,0 +1,58 @@
package org.fdroid.fdroid.installer;
import android.content.ContextWrapper;
import android.util.Log;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadows.ShadowLog;
import java.io.File;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(RobolectricTestRunner.class)
public class ApkCacheTest {
private static final String TAG = "ApkCacheTest";
private ContextWrapper context;
private File cacheDir;
@Before
public final void setUp() {
context = ApplicationProvider.getApplicationContext();
cacheDir = ApkCache.getApkCacheDir(context);
ShadowLog.stream = System.out;
}
@Test
public void testGetApkCacheDir() {
Log.i(TAG, "path: " + cacheDir);
assertTrue("Must be full path", cacheDir.isAbsolute());
assertTrue("Must be a directory", cacheDir.isDirectory());
assertTrue("Must be writable", cacheDir.canWrite());
}
@Test
public void testGetApkDownloadPath() {
assertEquals("Should be in folder based on repo hostname",
new File(cacheDir, "f-droid.org--1/org.fdroid.fdroid_1008000.apk"),
ApkCache.getApkDownloadPath(context,
"https://f-droid.org/repo/org.fdroid.fdroid_1008000.apk"));
assertEquals("Should be in folder based on repo hostname with port number",
new File(cacheDir, "192.168.234.12-8888/sun.bob.leela_2.apk"),
ApkCache.getApkDownloadPath(context,
"http://192.168.234.12:8888/fdroid/repo/sun.bob.leela_2.apk"));
assertEquals("Should work for OTA files also",
new File(cacheDir, "f-droid.org--1/org.fdroid.fdroid.privileged.ota_2110.zip"),
ApkCache.getApkDownloadPath(context,
"http://f-droid.org/fdroid/repo/org.fdroid.fdroid.privileged.ota_2110.zip"));
assertEquals("Should work for ZIP files also",
new File(cacheDir, "example.com--1/Norway_bouvet_europe_2.obf.zip"),
ApkCache.getApkDownloadPath(context,
"https://example.com/fdroid/repo/Norway_bouvet_europe_2.obf.zip"));
}
}