Testes: versões, lista de plugins, status e servidor HTTP
This commit is contained in:
1 parent
f80fca8eea
commit
4f5641c308
4 files changed
+277
No files matched your search
@@ -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<RemotePluginEntry> 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<RemotePluginEntry> parsed = PluginListFormat.parse(text);
|
||||
assertEquals(1, parsed.size());
|
||||
assertEquals("bom", parsed.get(0).id());
|
||||
}
|
||||
|
||||
@Test
|
||||
void byteOrderMarkIsIgnored() {
|
||||
List<RemotePluginEntry> parsed = PluginListFormat.parse(
|
||||
"\uFEFFchat\tChat\t1\ta\td\tchat.jar\t" + HASH + "\n");
|
||||
assertEquals(1, parsed.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tabsInsideFieldsDoNotBreakColumns() {
|
||||
List<RemotePluginEntry> list = List.of(
|
||||
new RemotePluginEntry("chat", "Chat", "1", "a", "uma\tdescrição", "chat.jar", HASH));
|
||||
List<RemotePluginEntry> 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));
|
||||
}
|
||||
}
|
||||
@@ -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<RemotePluginEntry> 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<Path> 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"));
|
||||
}
|
||||
}
|
||||
@@ -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<InstalledPluginRef> list = List.of(installed("pitiupi.chat.jar", null, "1.0"));
|
||||
assertTrue(PluginStatus.findInstalled(list, "chat").isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesByArquivoId() {
|
||||
List<InstalledPluginRef> 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<InstalledPluginRef> 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<InstalledPluginRef> 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<InstalledPluginRef> inst = Optional.of(installed("chat.jar", null, null));
|
||||
assertEquals(PluginStatus.State.UNKNOWN_VERSION, PluginStatus.of(remote("chat", "1.0"), inst));
|
||||
}
|
||||
}
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user