Ticket #15742: v1-0004-PluginDownloadTask-add-tests-covering-behaviour-w.patch

File v1-0004-PluginDownloadTask-add-tests-covering-behaviour-w.patch, 12.8 KB (added by ris, 6 years ago)
  • new file test/data/__files/plugin/corrupted_plugin.jar

    From e2a83420ebc685a3a66a1c15c65fb470baf32e6a Mon Sep 17 00:00:00 2001
    From: Robert Scott <code@humanleg.org.uk>
    Date: Sun, 7 Jan 2018 22:36:28 +0000
    Subject: [PATCH v1 4/4] PluginDownloadTask: add tests covering behaviour when
     updating and possibly overwriting an existing plugin
    
    ---
     test/data/__files/plugin/corrupted_plugin.jar      |   1 +
     test/data/__files/plugin/dummy_plugin.jar          | Bin 0 -> 3043 bytes
     test/data/plugin/corrupted_plugin.MANIFEST.MF      |  12 ++
     .../AbstractDownloadTaskTestParent.java            |  11 +-
     .../downloadtasks/PluginDownloadTaskTest.java      | 157 +++++++++++++++++++++
     5 files changed, 180 insertions(+), 1 deletion(-)
     create mode 100644 test/data/__files/plugin/corrupted_plugin.jar
     create mode 100644 test/data/__files/plugin/dummy_plugin.jar
     create mode 100644 test/data/plugin/corrupted_plugin.MANIFEST.MF
     create mode 100644 test/unit/org/openstreetmap/josm/actions/downloadtasks/PluginDownloadTaskTest.java
    
    diff --git a/test/data/__files/plugin/corrupted_plugin.jar b/test/data/__files/plugin/corrupted_plugin.jar
    new file mode 100644
    index 000000000..8ae23f91f
    - +  
     1This is not a valid jar or zip file.
  • new file test/data/__files/plugin/dummy_plugin.jar

    diff --git a/test/data/__files/plugin/dummy_plugin.jar b/test/data/__files/plugin/dummy_plugin.jar
    new file mode 100644
    index 0000000000000000000000000000000000000000..0a1f4086917323129dc8fda92a29389be0d053ff
    GIT binary patch
    literal 3043
    zcmb7Gc|6ql8z1Km#x@M%r`*Xsj`gEL&S4B@NIAn`7<OjN8VtsfNE@-Obv0s<DMyY%
    zuBDK>(b(iF6-m;mytW)K_B$liM!V1J_4$7OdEW2m^L?Jr^LalQ6c;x?2m}U!cz%(!
    z0)1k9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H<qS*-|NDjMG(Axn;_3liifQnhM8T+jA}HT
    zsFg%5na+nlxA5UCk#w!sE_$XE#u$Y0<&L~fz@L`oz+AhWB0*>PYK_ekeV=qw@K+%F
    zlW9w4RaEhJX1*T3Mi*prBtMta_U%ik%s-|weF)wm^UI0!YO6|qWr|jZdNdaX#lb1U
    zwe_e06b&#a=!FS;|2R#o4;~xPua6#V0NaY<RDthD=qH27cEQ<_x^C>?8Nem_Ag~)j
    z|Ea=kiQR<M1CVvW-5SA`+)c#T2gK*k{&t6COYkP1AGqaP-0<y4*pj?Sa&T*s4ei;E
    zOm2f~t!7c*CR-Eituw@y>~&+8;g5gC_d&onNWU)O8?;4n99*9Z`sVMgBfnJUO{6b?
    s+#=x{x%!4qe;Mn`2WvA{1Mh!|_3874LGb`!_T&Nqv5y=8jP)SU-wCz|b^rhX
  • new file test/data/plugin/corrupted_plugin.MANIFEST.MF

    literal 0
    HcmV?d00001
    
    diff --git a/test/data/plugin/corrupted_plugin.MANIFEST.MF b/test/data/plugin/corrupted_plugin.MANIFEST.MF
    new file mode 100644
    index 000000000..af5e4ee16
    - +  
     1Manifest-Version: 1.0
     2Ant-Version: Apache Ant 1.10.1
     3Created-By: 1.8.0_152-b16 (Oracle Corporation)
     4Plugin-Mainversion: 7654
     5Plugin-Version: 5432
     6Plugin-Class: org.openstreetmap.josm.plugin.corrupted.CorruptedPlugin
     7Plugin-Description: A plugin whose download gets corrupted.
     8Plugin-Date: 2017-06-01T12:34:56.789012Z
     9Author: Example Author <author@example.com>
     10Plugin-Link: https://example.com/corrupted-plugin
     11Plugin-Icon: images/preferences/corrupted.png
     12Plugin-Canloadatruntime: false
  • test/unit/org/openstreetmap/josm/actions/downloadtasks/AbstractDownloadTaskTestParent.java

    diff --git a/test/unit/org/openstreetmap/josm/actions/downloadtasks/AbstractDownloadTaskTestParent.java b/test/unit/org/openstreetmap/josm/actions/downloadtasks/AbstractDownloadTaskTestParent.java
    index b9a51d507..52d28288e 100644
    a b public abstract class AbstractDownloadTaskTestParent {  
    3939    protected abstract String getRemoteFile();
    4040
    4141    /**
     42     * Returns the {@code Content-Type} with which to serve the file referenced
     43     * by {@link getRemoteFile()}
     44     * @return the {@code Content-Type} string for file {@link getRemoteFile()}
     45     */
     46    protected String getRemoteContentType() {
     47        return "text/xml";
     48    };
     49
     50    /**
    4251     * Returns the http URL to remote test file to download.
    4352     * @return the http URL to remote test file to download
    4453     */
    public abstract class AbstractDownloadTaskTestParent {  
    5362        wireMockRule.stubFor(get(urlEqualTo("/" + getRemoteFile()))
    5463                .willReturn(aResponse()
    5564                    .withStatus(200)
    56                     .withHeader("Content-Type", "text/xml")
     65                    .withHeader("Content-Type", getRemoteContentType())
    5766                    .withBodyFile(getRemoteFile())));
    5867    }
    5968}
  • new file test/unit/org/openstreetmap/josm/actions/downloadtasks/PluginDownloadTaskTest.java

    diff --git a/test/unit/org/openstreetmap/josm/actions/downloadtasks/PluginDownloadTaskTest.java b/test/unit/org/openstreetmap/josm/actions/downloadtasks/PluginDownloadTaskTest.java
    new file mode 100644
    index 000000000..432b256d4
    - +  
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.actions.downloadtasks;
     3
     4import static org.junit.Assert.assertArrayEquals;
     5import static org.junit.Assert.assertFalse;
     6import static org.junit.Assert.assertTrue;
     7
     8import java.io.File;
     9import java.io.FileInputStream;
     10import java.io.FileOutputStream;
     11import java.util.concurrent.ExecutionException;
     12import java.util.Collections;
     13
     14import org.openstreetmap.josm.Main;
     15import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
     16import org.openstreetmap.josm.plugins.PluginDownloadTask;
     17import org.openstreetmap.josm.plugins.PluginInformation;
     18import org.openstreetmap.josm.TestUtils;
     19import org.openstreetmap.josm.testutils.JOSMTestRules;
     20
     21import org.junit.Rule;
     22import org.junit.Test;
     23
     24import com.google.common.io.ByteStreams;
     25import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     26
     27/**
     28 * Unit tests for class {@link PluginDownloadTask}.
     29 */
     30public class PluginDownloadTaskTest extends AbstractDownloadTaskTestParent {
     31    protected String pluginPath;
     32
     33    @Rule
     34    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
     35    public JOSMTestRules test = new JOSMTestRules().https().assumeRevision(
     36        "Revision: 8000\n"
     37    ).preferences();
     38
     39    protected String getRemoteContentType() {
     40        return "application/java-archive";
     41    };
     42
     43    protected String getRemoteFile() {
     44        return this.pluginPath;
     45    };
     46
     47    /**
     48     * Test download task when updating a plugin that we already have in our plugins directory
     49     * and the downloaded file is valid
     50     */
     51    @Test
     52    public void testUpdatePluginValid() throws Exception {
     53        this.pluginPath = "plugin/dummy_plugin.jar";
     54        this.mockHttp();
     55
     56        final File srcPluginFile = new File(
     57            new File(TestUtils.getTestDataRoot()), "__files/" + this.pluginPath
     58        );
     59        final File pluginDir = Main.pref.getPluginsDirectory();
     60        final File pluginFile = new File(pluginDir, "dummy_plugin.jar");
     61        final File pluginFileNew = new File(pluginDir, "dummy_plugin.jar.new");
     62
     63        // put existing "plugin file" in place
     64        pluginFile.getParentFile().mkdirs();
     65        final byte[] existingPluginContents = "Existing plugin contents 123".getBytes();
     66        try(final FileOutputStream existingPluginOutputStream = new FileOutputStream(pluginFile)) {
     67            existingPluginOutputStream.write(existingPluginContents);
     68        }
     69
     70        // get PluginInformation from jar file
     71        final PluginInformation pluginInformation = new PluginInformation(srcPluginFile);
     72        // ...and grafting on the downloadlink
     73        pluginInformation.downloadlink = this.getRemoteFileUrl();
     74
     75        final PluginDownloadTask pluginDownloadTask = new PluginDownloadTask(
     76            NullProgressMonitor.INSTANCE,
     77            Collections.singletonList(pluginInformation),
     78            null
     79        );
     80        pluginDownloadTask.run();
     81
     82        // the ".jar.new" file should have been deleted
     83        assertFalse(pluginFileNew.exists());
     84        // the ".jar" file should still exist
     85        assertTrue(pluginFile.exists());
     86        try (
     87            final FileInputStream pluginDirPluginStream = new FileInputStream(pluginFile);
     88            final FileInputStream srcPluginStream = new FileInputStream(srcPluginFile);
     89        ) {
     90            // and its contents should equal those that were served to the task
     91            assertArrayEquals(
     92                ByteStreams.toByteArray(pluginDirPluginStream),
     93                ByteStreams.toByteArray(srcPluginStream)
     94            );
     95        }
     96    }
     97
     98    /**
     99     * Test download task when updating a plugin that we already have in our plugins directory
     100     * and the downloaded file is not a valid jar file.
     101     */
     102    @Test
     103    public void testUpdatePluginCorrupt() throws Exception {
     104        this.pluginPath = "plugin/corrupted_plugin.jar";
     105        this.mockHttp();
     106
     107        final File srcPluginFile = new File(
     108            new File(TestUtils.getTestDataRoot()), "__files/" + this.pluginPath
     109        );
     110        final File pluginDir = Main.pref.getPluginsDirectory();
     111        final File pluginFile = new File(pluginDir, "corrupted_plugin.jar");
     112        final File pluginFileNew = new File(pluginDir, "corrupted_plugin.jar.new");
     113        // have to store this manifest externally as it clearly can't be read from our corrupted plugin
     114        final File pluginManifest = new File(TestUtils.getTestDataRoot(), "plugin/corrupted_plugin.MANIFEST.MF");
     115
     116        pluginFile.getParentFile().mkdirs();
     117        final byte[] existingPluginContents = "Existing plugin contents 123".getBytes();
     118        try(final FileOutputStream existingPluginOutputStream = new FileOutputStream(pluginFile)) {
     119            existingPluginOutputStream.write(existingPluginContents);
     120        }
     121
     122        try (final FileInputStream manifestInputStream = new FileInputStream(pluginManifest)) {
     123            final PluginInformation pluginInformation = new PluginInformation(
     124                manifestInputStream,
     125                "corrupted_plugin",
     126                this.getRemoteFileUrl()
     127            );
     128            final PluginDownloadTask pluginDownloadTask = new PluginDownloadTask(
     129                NullProgressMonitor.INSTANCE,
     130                Collections.singletonList(pluginInformation),
     131                null
     132            );
     133            pluginDownloadTask.run();
     134        }
     135
     136        // the ".jar.new" file should exist, even though invalid
     137        assertTrue(pluginFileNew.exists());
     138        // the ".jar" file should still exist
     139        assertTrue(pluginFile.exists());
     140        try (
     141            final FileInputStream pluginDirPluginNewStream = new FileInputStream(pluginFileNew);
     142            final FileInputStream pluginDirPluginStream = new FileInputStream(pluginFile);
     143            final FileInputStream srcPluginStream = new FileInputStream(srcPluginFile);
     144        ) {
     145            // the ".jar" file's contents should be as before
     146            assertArrayEquals(
     147                existingPluginContents,
     148                ByteStreams.toByteArray(pluginDirPluginStream)
     149            );
     150            // just assert that the "corrupt" jar file made it through in tact
     151            assertArrayEquals(
     152                ByteStreams.toByteArray(pluginDirPluginNewStream),
     153                ByteStreams.toByteArray(srcPluginStream)
     154            );
     155        }
     156    }
     157}