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