From 4f5641c3088a3d68144e61e83fd127f6a7206ee9 Mon Sep 17 00:00:00 2001 From: tonyhcj Date: Thu, 5 Nov 2026 18:21:11 -0300 Subject: [PATCH] =?UTF-8?q?Testes:=20vers=C3=B5es,=20lista=20de=20plugins,?= =?UTF-8?q?=20status=20e=20servidor=20HTTP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugins/remote/PluginListFormatTest.java | 63 +++++++++ .../plugins/remote/PluginServerTest.java | 121 ++++++++++++++++++ .../plugins/remote/PluginStatusTest.java | 61 +++++++++ .../plugins/remote/VersionCompareTest.java | 32 +++++ 4 files changed, 277 insertions(+) create mode 100644 pitiupi/test/pitiupi/plugins/remote/PluginListFormatTest.java create mode 100644 pitiupi/test/pitiupi/plugins/remote/PluginServerTest.java create mode 100644 pitiupi/test/pitiupi/plugins/remote/PluginStatusTest.java create mode 100644 pitiupi/test/pitiupi/plugins/remote/VersionCompareTest.java diff --git a/pitiupi/test/pitiupi/plugins/remote/PluginListFormatTest.java b/pitiupi/test/pitiupi/plugins/remote/PluginListFormatTest.java new file mode 100644 index 0000000..b8aa72f --- /dev/null +++ b/pitiupi/test/pitiupi/plugins/remote/PluginListFormatTest.java @@ -0,0 +1,63 @@ +package pitiupi.plugins.remote; + +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class PluginListFormatTest { + + private static final String HASH = "a".repeat(64); + + @Test + void formatThenParseGivesTheSameList() { + List list = List.of( + new RemotePluginEntry("chat", "Chat", "1.2.0", "Fulano", "Chat de sala", "chat.jar", HASH), + new RemotePluginEntry("ping", "Ping", "0.1", "", "", "pitiupi.ping.jar", HASH)); + assertEquals(list, PluginListFormat.parse(PluginListFormat.format(list))); + } + + @Test + void badLinesAreDroppedButGoodOnesStay() { + String text = String.join("\n", + "# comentário", + "", + "bom\tBom\t1\ta\td\tbom.jar\t" + HASH, + "curto\tCurto\t1\ta\td\tcurto.jar", // menos de 7 colunas + "hashruim\tX\t1\ta\td\tx.jar\t1234", // hash curto + "fuga\tX\t1\ta\td\t../evil.jar\t" + HASH, // nome com .. + "barra\tX\t1\ta\td\tpasta/x.jar\t" + HASH, // nome com / + "naojar\tX\t1\ta\td\tx.txt\t" + HASH); // não termina em .jar + List parsed = PluginListFormat.parse(text); + assertEquals(1, parsed.size()); + assertEquals("bom", parsed.get(0).id()); + } + + @Test + void byteOrderMarkIsIgnored() { + List parsed = PluginListFormat.parse( + "\uFEFFchat\tChat\t1\ta\td\tchat.jar\t" + HASH + "\n"); + assertEquals(1, parsed.size()); + } + + @Test + void tabsInsideFieldsDoNotBreakColumns() { + List list = List.of( + new RemotePluginEntry("chat", "Chat", "1", "a", "uma\tdescrição", "chat.jar", HASH)); + List back = PluginListFormat.parse(PluginListFormat.format(list)); + assertEquals(1, back.size()); + assertEquals("uma descrição", back.get(0).description()); + } + + @Test + void jarNameValidation() { + assertTrue(PluginListFormat.isSafeJarName("pitiupi.chat.jar")); + assertFalse(PluginListFormat.isSafeJarName("../x.jar")); + assertFalse(PluginListFormat.isSafeJarName("a/b.jar")); + assertFalse(PluginListFormat.isSafeJarName("x.txt")); + assertFalse(PluginListFormat.isSafeJarName(null)); + } +} diff --git a/pitiupi/test/pitiupi/plugins/remote/PluginServerTest.java b/pitiupi/test/pitiupi/plugins/remote/PluginServerTest.java new file mode 100644 index 0000000..9d7e8ab --- /dev/null +++ b/pitiupi/test/pitiupi/plugins/remote/PluginServerTest.java @@ -0,0 +1,121 @@ +package pitiupi.plugins.remote; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import pitiupi.errors.ErrorCode; +import pitiupi.errors.PitiupiException; +import pitiupi.plugins.store.PluginMetadataWriter; + +import java.io.OutputStream; +import java.net.InetAddress; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.Properties; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class PluginServerTest { + + @TempDir + Path temp; + + private PluginServer server; + private URI base; + private Path chatJar; + private final PluginRepositoryClient client = new PluginRepositoryClient(); + + @BeforeEach + void setUp() throws Exception { + Path shared = temp.resolve("shared"); + Files.createDirectories(shared); + + chatJar = shared.resolve("chat.jar"); + Files.writeString(chatJar, "conteudo-do-jar"); + Properties meta = new Properties(); + meta.setProperty("arquivoId", "chat"); + meta.setProperty("displayName", "Chat"); + meta.setProperty("version", "1.2.0"); + meta.setProperty("author", "Fulano"); + meta.setProperty("description", "Chat de teste"); + try (OutputStream out = Files.newOutputStream(PluginMetadataWriter.metaPathFor(chatJar))) { + meta.store(out, null); + } + Files.writeString(shared.resolve("extra.jar"), "outro"); // sem .plugin.meta + Files.writeString(shared.resolve("notas.txt"), "nao e jar"); // não deve aparecer + + server = new PluginServer(shared); + server.start(InetAddress.getByAddress(new byte[]{127, 0, 0, 1}), 0); + base = URI.create("http://127.0.0.1:" + server.getPort() + "/"); + } + + @AfterEach + void tearDown() { + server.stop(); + } + + private int status(String path, String method) throws Exception { + HttpRequest req = HttpRequest.newBuilder(URI.create(base + path)) + .method(method, HttpRequest.BodyPublishers.noBody()).build(); + return HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.discarding()).statusCode(); + } + + @Test + void listShowsOnlyJarsAndReadsMetadata() throws Exception { + List list = client.fetchList(base, PluginListFormat.FILE_NAME); + assertEquals(2, list.size()); + + RemotePluginEntry chat = list.get(0); + assertEquals("chat", chat.id()); + assertEquals("Chat", chat.name()); + assertEquals("1.2.0", chat.version()); + assertEquals("Fulano", chat.author()); + assertEquals(Sha256.of(chatJar), chat.sha256()); + + RemotePluginEntry extra = list.get(1); + assertEquals("extra", extra.id()); + assertEquals("0", extra.version()); + } + + @Test + void downloadDeliversTheSameBytes() throws Exception { + RemotePluginEntry chat = client.fetchList(base, PluginListFormat.FILE_NAME).get(0); + Path downloaded = client.download(base, chat, temp.resolve("cache")); + assertEquals("conteudo-do-jar", Files.readString(downloaded)); + } + + @Test + void wrongHashIsRejectedAndNothingIsLeftBehind() throws Exception { + RemotePluginEntry bad = new RemotePluginEntry( + "chat", "Chat", "1", "", "", "chat.jar", "0".repeat(64)); + Path cache = temp.resolve("cache"); + + PitiupiException ex = assertThrows(PitiupiException.class, () -> client.download(base, bad, cache)); + assertEquals(ErrorCode.PLUGIN_HASH_MISMATCH, ex.getCode()); + try (Stream s = Files.list(cache)) { + assertEquals(0, s.count()); + } + } + + @Test + void onlyListedJarsAreServed() throws Exception { + assertEquals(200, status("chat.jar", "GET")); + assertEquals(404, status("nao-existe.jar", "GET")); + assertEquals(404, status("notas.txt", "GET")); + assertEquals(404, status("..%2Fconfig.properties", "GET")); + assertEquals(404, status("..%2F..%2Fetc%2Fpasswd", "GET")); + } + + @Test + void onlyGetIsAllowed() throws Exception { + assertEquals(405, status("chat.jar", "POST")); + } +} diff --git a/pitiupi/test/pitiupi/plugins/remote/PluginStatusTest.java b/pitiupi/test/pitiupi/plugins/remote/PluginStatusTest.java new file mode 100644 index 0000000..f435e10 --- /dev/null +++ b/pitiupi/test/pitiupi/plugins/remote/PluginStatusTest.java @@ -0,0 +1,61 @@ +package pitiupi.plugins.remote; + +import org.junit.jupiter.api.Test; +import pitiupi.plugins.store.InstalledPluginRef; + +import java.nio.file.Path; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class PluginStatusTest { + + private static RemotePluginEntry remote(String id, String version) { + return new RemotePluginEntry(id, id, version, "", "", id + ".jar", "a".repeat(64)); + } + + private static InstalledPluginRef installed(String file, String arquivoId, String version) { + return new InstalledPluginRef(Path.of("/x/" + file), file, + Optional.ofNullable(arquivoId), Optional.ofNullable(version), + Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()); + } + + @Test + void matchesByFileNameWithoutPrefix() { + List list = List.of(installed("pitiupi.chat.jar", null, "1.0")); + assertTrue(PluginStatus.findInstalled(list, "chat").isPresent()); + } + + @Test + void matchesByArquivoId() { + List list = List.of(installed("chat.jar", "chat", "1.0")); + assertTrue(PluginStatus.findInstalled(list, "chat").isPresent()); + } + + @Test + void notInstalled() { + assertEquals(PluginStatus.State.NOT_INSTALLED, + PluginStatus.of(remote("chat", "1.0"), Optional.empty())); + } + + @Test + void updateAvailableWhenRemoteIsNewer() { + Optional inst = Optional.of(installed("chat.jar", "chat", "1.9.0")); + assertEquals(PluginStatus.State.UPDATE_AVAILABLE, PluginStatus.of(remote("chat", "1.10.0"), inst)); + } + + @Test + void upToDateWhenSameOrOlder() { + Optional inst = Optional.of(installed("chat.jar", "chat", "2.0")); + assertEquals(PluginStatus.State.UP_TO_DATE, PluginStatus.of(remote("chat", "2.0"), inst)); + assertEquals(PluginStatus.State.UP_TO_DATE, PluginStatus.of(remote("chat", "1.0"), inst)); + } + + @Test + void unknownVersionWhenThereIsNoMetadata() { + Optional inst = Optional.of(installed("chat.jar", null, null)); + assertEquals(PluginStatus.State.UNKNOWN_VERSION, PluginStatus.of(remote("chat", "1.0"), inst)); + } +} diff --git a/pitiupi/test/pitiupi/plugins/remote/VersionCompareTest.java b/pitiupi/test/pitiupi/plugins/remote/VersionCompareTest.java new file mode 100644 index 0000000..60fd839 --- /dev/null +++ b/pitiupi/test/pitiupi/plugins/remote/VersionCompareTest.java @@ -0,0 +1,32 @@ +package pitiupi.plugins.remote; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class VersionCompareTest { + + @Test + void comparesNumbersNotText() { + assertTrue(VersionCompare.compare("1.10.0", "1.9.0") > 0); + assertTrue(VersionCompare.compare("1.9.0", "1.10.0") < 0); + assertTrue(VersionCompare.compare("2.0", "1.99") > 0); + } + + @Test + void missingPartsCountAsZero() { + assertEquals(0, VersionCompare.compare("1.0", "1.0.0")); + assertTrue(VersionCompare.compare("1.0.1", "1.0") > 0); + } + + @Test + void equalVersionsAreEqual() { + assertEquals(0, VersionCompare.compare("3.2.1", "3.2.1")); + } + + @Test + void suffixIsIgnored() { + assertEquals(0, VersionCompare.compare("1.0.0-beta", "1.0.0")); + } +}