Search in sources :

Example 1 with LocalHTTPD

use of org.fdroid.fdroid.nearby.LocalHTTPD in project fdroidclient by f-droid.

the class SwapRepoEmulatorTest method testSwap.

/**
 * @see org.fdroid.fdroid.nearby.WifiStateChangeService.WifiInfoThread#run()
 */
@Ignore
@Test
public void testSwap() throws IOException, LocalRepoKeyStore.InitException, IndexUpdater.UpdateException, InterruptedException {
    Looper.prepare();
    LocalHTTPD localHttpd = null;
    try {
        Log.i(TAG, "REPO: " + FDroidApp.repo);
        final Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
        Preferences.setupForTests(context);
        FDroidApp.initWifiSettings();
        assertNull(FDroidApp.repo.address);
        final CountDownLatch latch = new CountDownLatch(1);
        new Thread() {

            @Override
            public void run() {
                while (FDroidApp.repo.address == null) {
                    try {
                        Log.i(TAG, "Waiting for IP address... " + FDroidApp.repo.address);
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                    // ignored
                    }
                }
                latch.countDown();
            }
        }.start();
        latch.await(10, TimeUnit.MINUTES);
        assertNotNull(FDroidApp.repo.address);
        LocalRepoService.runProcess(context, new String[] { context.getPackageName() });
        Log.i(TAG, "REPO: " + FDroidApp.repo);
        File indexJarFile = LocalRepoManager.get(context).getIndexJar();
        assertTrue(indexJarFile.isFile());
        localHttpd = new LocalHTTPD(context, null, FDroidApp.port, LocalRepoManager.get(context).getWebRoot(), false);
        localHttpd.start();
        // give the server some tine to start.
        Thread.sleep(100);
        assertTrue(localHttpd.isAlive());
        LocalRepoKeyStore localRepoKeyStore = LocalRepoKeyStore.get(context);
        Certificate localCert = localRepoKeyStore.getCertificate();
        String signingCert = Hasher.hex(localCert);
        assertFalse(TextUtils.isEmpty(signingCert));
        assertFalse(TextUtils.isEmpty(Utils.calcFingerprint(localCert)));
        Repo repoToDelete = RepoProvider.Helper.findByAddress(context, FDroidApp.repo.address);
        while (repoToDelete != null) {
            Log.d(TAG, "Removing old test swap repo matching this one: " + repoToDelete.address);
            RepoProvider.Helper.remove(context, repoToDelete.getId());
            repoToDelete = RepoProvider.Helper.findByAddress(context, FDroidApp.repo.address);
        }
        ContentValues values = new ContentValues(4);
        values.put(Schema.RepoTable.Cols.SIGNING_CERT, signingCert);
        values.put(Schema.RepoTable.Cols.ADDRESS, FDroidApp.repo.address);
        values.put(Schema.RepoTable.Cols.NAME, FDroidApp.repo.name);
        values.put(Schema.RepoTable.Cols.IS_SWAP, true);
        final String lastEtag = UUID.randomUUID().toString();
        values.put(Schema.RepoTable.Cols.LAST_ETAG, lastEtag);
        RepoProvider.Helper.insert(context, values);
        Repo repo = RepoProvider.Helper.findByAddress(context, FDroidApp.repo.address);
        assertTrue(repo.isSwap);
        assertNotEquals(-1, repo.getId());
        assertTrue(repo.name.startsWith(FDroidApp.repo.name));
        assertEquals(lastEtag, repo.lastetag);
        assertNull(repo.lastUpdated);
        assertTrue(isPortInUse(FDroidApp.ipAddressString, FDroidApp.port));
        Thread.sleep(100);
        IndexUpdater updater = new IndexUpdater(context, repo);
        updater.update();
        assertTrue(updater.hasChanged());
        repo = RepoProvider.Helper.findByAddress(context, FDroidApp.repo.address);
        final Date lastUpdated = repo.lastUpdated;
        assertTrue("repo lastUpdated should be updated", new Date(2019, 5, 13).compareTo(repo.lastUpdated) > 0);
        App app = AppProvider.Helper.findSpecificApp(context.getContentResolver(), context.getPackageName(), repo.getId());
        assertEquals(context.getPackageName(), app.packageName);
        List<Apk> apks = ApkProvider.Helper.findByRepo(context, repo, Schema.ApkTable.Cols.ALL);
        assertEquals(1, apks.size());
        for (Apk apk : apks) {
            Log.i(TAG, "Apk: " + apk);
            assertEquals(context.getPackageName(), apk.packageName);
            assertEquals(BuildConfig.VERSION_NAME, apk.versionName);
            assertEquals(BuildConfig.VERSION_CODE, apk.versionCode);
            assertEquals(app.repoId, apk.repoId);
        }
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> resolveInfoList = context.getPackageManager().queryIntentActivities(mainIntent, 0);
        HashSet<String> packageNames = new HashSet<>();
        for (ResolveInfo resolveInfo : resolveInfoList) {
            if (!isSystemPackage(resolveInfo)) {
                Log.i(TAG, "resolveInfo: " + resolveInfo);
                packageNames.add(resolveInfo.activityInfo.packageName);
            }
        }
        LocalRepoService.runProcess(context, packageNames.toArray(new String[0]));
        updater = new IndexUpdater(context, repo);
        updater.update();
        assertTrue(updater.hasChanged());
        assertTrue("repo lastUpdated should be updated", lastUpdated.compareTo(repo.lastUpdated) < 0);
        for (String packageName : packageNames) {
            assertNotNull(ApkProvider.Helper.findByPackageName(context, packageName));
        }
    } finally {
        if (localHttpd != null) {
            localHttpd.stop();
        }
    }
    if (localHttpd != null) {
        assertFalse(localHttpd.isAlive());
    }
}
Also used : Context(android.content.Context) ContentValues(android.content.ContentValues) FDroidApp(org.fdroid.fdroid.FDroidApp) App(org.fdroid.fdroid.data.App) LocalHTTPD(org.fdroid.fdroid.nearby.LocalHTTPD) LocalRepoKeyStore(org.fdroid.fdroid.nearby.LocalRepoKeyStore) Intent(android.content.Intent) CountDownLatch(java.util.concurrent.CountDownLatch) Date(java.util.Date) ResolveInfo(android.content.pm.ResolveInfo) Repo(org.fdroid.fdroid.data.Repo) File(java.io.File) Apk(org.fdroid.fdroid.data.Apk) IndexUpdater(org.fdroid.fdroid.IndexUpdater) Certificate(java.security.cert.Certificate) HashSet(java.util.HashSet) Ignore(org.junit.Ignore) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 2 with LocalHTTPD

use of org.fdroid.fdroid.nearby.LocalHTTPD in project fdroidclient by f-droid.

the class SwapRepoTest method testSwap.

/**
 * @see WifiStateChangeService.WifiInfoThread#run()
 */
@Test
public void testSwap() throws IOException, LocalRepoKeyStore.InitException, IndexUpdater.UpdateException, InterruptedException {
    PackageManager packageManager = context.getPackageManager();
    ApplicationInfo appInfo = new ApplicationInfo();
    appInfo.flags = 0;
    appInfo.packageName = context.getPackageName();
    appInfo.minSdkVersion = 10;
    appInfo.targetSdkVersion = 23;
    appInfo.sourceDir = getClass().getClassLoader().getResource("F-Droid.apk").getPath();
    appInfo.publicSourceDir = getClass().getClassLoader().getResource("F-Droid.apk").getPath();
    System.out.println("appInfo.sourceDir " + appInfo.sourceDir);
    appInfo.name = "F-Droid";
    PackageInfo packageInfo = new PackageInfo();
    packageInfo.packageName = appInfo.packageName;
    packageInfo.applicationInfo = appInfo;
    packageInfo.versionCode = 1002001;
    packageInfo.versionName = "1.2-fake";
    shadowOf(packageManager).addPackage(packageInfo);
    try {
        FDroidApp.initWifiSettings();
        FDroidApp.ipAddressString = "127.0.0.1";
        FDroidApp.subnetInfo = new SubnetUtils("127.0.0.0/8").getInfo();
        FDroidApp.repo.name = "test";
        FDroidApp.repo.address = "http://" + FDroidApp.ipAddressString + ":" + FDroidApp.port + "/fdroid/repo";
        LocalRepoService.runProcess(context, new String[] { context.getPackageName() });
        File indexJarFile = LocalRepoManager.get(context).getIndexJar();
        System.out.println("indexJarFile:" + indexJarFile);
        assertTrue(indexJarFile.isFile());
        localHttpd = new LocalHTTPD(context, FDroidApp.ipAddressString, FDroidApp.port, LocalRepoManager.get(context).getWebRoot(), false);
        localHttpd.start();
        // give the server some tine to start.
        Thread.sleep(100);
        assertTrue(localHttpd.isAlive());
        LocalRepoKeyStore localRepoKeyStore = LocalRepoKeyStore.get(context);
        Certificate localCert = localRepoKeyStore.getCertificate();
        String signingCert = Hasher.hex(localCert);
        assertFalse(TextUtils.isEmpty(signingCert));
        assertFalse(TextUtils.isEmpty(Utils.calcFingerprint(localCert)));
        Repo repo = MultiIndexUpdaterTest.createRepo(FDroidApp.repo.name, FDroidApp.repo.address, context, signingCert);
        IndexUpdater updater = new IndexUpdater(context, repo);
        updater.update();
        assertTrue(updater.hasChanged());
        updater.processDownloadedFile(indexJarFile);
        boolean foundRepo = false;
        for (Repo repoFromDb : RepoProvider.Helper.all(context)) {
            if (TextUtils.equals(repo.address, repoFromDb.address)) {
                foundRepo = true;
                repo = repoFromDb;
            }
        }
        assertTrue(foundRepo);
        assertNotEquals(-1, repo.getId());
        List<Apk> apks = ApkProvider.Helper.findByRepo(context, repo, Schema.ApkTable.Cols.ALL);
        assertEquals(1, apks.size());
        for (Apk apk : apks) {
            System.out.println(apk);
        }
        // MultiIndexUpdaterTest.assertApksExist(apks, context.getPackageName(), new int[]{BuildConfig.VERSION_CODE});
        Thread.sleep(10000);
    } finally {
        if (localHttpd != null) {
            localHttpd.stop();
        }
    }
}
Also used : SubnetUtils(org.apache.commons.net.util.SubnetUtils) LocalHTTPD(org.fdroid.fdroid.nearby.LocalHTTPD) PackageInfo(android.content.pm.PackageInfo) ApplicationInfo(android.content.pm.ApplicationInfo) LocalRepoKeyStore(org.fdroid.fdroid.nearby.LocalRepoKeyStore) PackageManager(android.content.pm.PackageManager) Repo(org.fdroid.fdroid.data.Repo) File(java.io.File) Apk(org.fdroid.fdroid.data.Apk) IndexUpdater(org.fdroid.fdroid.IndexUpdater) Certificate(java.security.cert.Certificate) Test(org.junit.Test)

Aggregations

File (java.io.File)2 Certificate (java.security.cert.Certificate)2 IndexUpdater (org.fdroid.fdroid.IndexUpdater)2 Apk (org.fdroid.fdroid.data.Apk)2 Repo (org.fdroid.fdroid.data.Repo)2 LocalHTTPD (org.fdroid.fdroid.nearby.LocalHTTPD)2 LocalRepoKeyStore (org.fdroid.fdroid.nearby.LocalRepoKeyStore)2 Test (org.junit.Test)2 ContentValues (android.content.ContentValues)1 Context (android.content.Context)1 Intent (android.content.Intent)1 ApplicationInfo (android.content.pm.ApplicationInfo)1 PackageInfo (android.content.pm.PackageInfo)1 PackageManager (android.content.pm.PackageManager)1 ResolveInfo (android.content.pm.ResolveInfo)1 LargeTest (androidx.test.filters.LargeTest)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 SubnetUtils (org.apache.commons.net.util.SubnetUtils)1