From ecd1accd1f7a19900522135a61b3d2ac8bbaf75a Mon Sep 17 00:00:00 2001 From: David Walluck Date: Sat, 6 Apr 2024 09:56:45 -0400 Subject: [PATCH] Use AssertJ fluent assertions java library (#60) --- deb/pom.xml | 8 -- .../packager/deb/tests/BinaryPackageTest.java | 22 +---- .../deb/tests/ControlFileWriterTest.java | 7 +- .../packager/deb/tests/PackagesTest.java | 94 +++++++------------ pom.xml | 31 ++++++ rpm/pom.xml | 13 --- .../packager/rpm/deps/Dependencies.java | 4 +- .../packager/rpm/deps/RpmDependencyFlags.java | 4 +- .../signature/RpmFileSignatureProcessor.java | 3 +- .../packager/rpm/yum/RepositoryCreator.java | 4 +- .../eclipse/packager/rpm/EmptyWriterTest.java | 2 - .../eclipse/packager/rpm/InputStreamTest.java | 61 ++++++------ .../eclipse/packager/rpm/Issue130Test.java | 15 +-- .../eclipse/packager/rpm/Issue136Test.java | 30 ++---- .../org/eclipse/packager/rpm/Issue24Test.java | 14 +-- .../org/eclipse/packager/rpm/LeadTest.java | 29 +++--- .../eclipse/packager/rpm/PathNameTest.java | 49 ++-------- .../packager/rpm/SetVerifyFlagsTest.java | 21 ++--- .../rpm/StableDependencyOrderTest.java | 24 +++-- .../eclipse/packager/rpm/UnsignedTest.java | 15 ++- .../org/eclipse/packager/rpm/VersionTest.java | 43 +++------ .../packager/rpm/info/RpmInformationTest.java | 16 ++-- .../RpmFileSignatureProcessorTest.java | 94 +++++++++---------- 23 files changed, 242 insertions(+), 361 deletions(-) diff --git a/deb/pom.xml b/deb/pom.xml index 66b3ad2..a282159 100644 --- a/deb/pom.xml +++ b/deb/pom.xml @@ -36,14 +36,6 @@ org.tukaani xz - - - - - org.junit.jupiter - junit-jupiter-engine - test - diff --git a/deb/src/test/java/org/eclipse/packager/deb/tests/BinaryPackageTest.java b/deb/src/test/java/org/eclipse/packager/deb/tests/BinaryPackageTest.java index 84fff62..738b29e 100644 --- a/deb/src/test/java/org/eclipse/packager/deb/tests/BinaryPackageTest.java +++ b/deb/src/test/java/org/eclipse/packager/deb/tests/BinaryPackageTest.java @@ -12,6 +12,8 @@ */ package org.eclipse.packager.deb.tests; +import static org.assertj.core.api.Assertions.assertThat; + import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -22,15 +24,12 @@ import org.eclipse.packager.deb.build.DebianPackageWriter; import org.eclipse.packager.deb.build.EntryInformation; import org.eclipse.packager.deb.control.BinaryPackageControlFile; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import com.google.common.hash.Hashing; - -public class BinaryPackageTest { +class BinaryPackageTest { @SuppressWarnings("deprecation") @Test - public void test1() throws IOException, InterruptedException { + void test1() throws IOException, InterruptedException { final Path file1 = Files.createTempFile("test-1-", ".deb"); final Path file2 = Files.createTempFile("test-2-", ".deb"); @@ -38,22 +37,11 @@ public void test1() throws IOException, InterruptedException { final Supplier timestampProvider = () -> now; createDebFile(file1, timestampProvider); - System.out.println("File: " + file1); - Assertions.assertTrue(Files.exists(file1), "File exists"); Thread.sleep(1_001); // sleep for a second to make sure that a timestamp might be changed createDebFile(file2, timestampProvider); - System.out.println("File: " + file2); - Assertions.assertTrue(Files.exists(file2), "File exists"); - - final byte[] b1 = Files.readAllBytes(file1); - final String h1 = Hashing.md5().hashBytes(b1).toString(); - final byte[] b2 = Files.readAllBytes(file2); - final String h2 = Hashing.md5().hashBytes(b2).toString(); - System.out.println(h1); - System.out.println(h2); - Assertions.assertEquals(h1, h2); + assertThat(file2).hasSameBinaryContentAs(file1); } private void createDebFile(final Path file, final Supplier timestampProvider) throws IOException { diff --git a/deb/src/test/java/org/eclipse/packager/deb/tests/ControlFileWriterTest.java b/deb/src/test/java/org/eclipse/packager/deb/tests/ControlFileWriterTest.java index a619bcf..4ee03b9 100644 --- a/deb/src/test/java/org/eclipse/packager/deb/tests/ControlFileWriterTest.java +++ b/deb/src/test/java/org/eclipse/packager/deb/tests/ControlFileWriterTest.java @@ -12,12 +12,13 @@ */ package org.eclipse.packager.deb.tests; +import static org.assertj.core.api.Assertions.assertThat; + import java.io.StringWriter; import java.util.Optional; import org.eclipse.packager.deb.ControlFileWriter; import org.eclipse.packager.deb.FieldFormatter; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; public class ControlFileWriterTest { @@ -49,9 +50,7 @@ private void testField(final ControlFieldDefinition field, final String value, f try (final StringWriter sw = new StringWriter()) { final ControlFileWriter writer = new ControlFileWriter(sw); writer.writeEntry(field.name, value, Optional.ofNullable(field.formatter)); - final String result = sw.toString(); - System.out.println("Actual: '" + result + "'"); - Assertions.assertEquals(expectedResult, result); + assertThat(sw).hasToString(expectedResult); } } } diff --git a/deb/src/test/java/org/eclipse/packager/deb/tests/PackagesTest.java b/deb/src/test/java/org/eclipse/packager/deb/tests/PackagesTest.java index 73c633c..152695e 100644 --- a/deb/src/test/java/org/eclipse/packager/deb/tests/PackagesTest.java +++ b/deb/src/test/java/org/eclipse/packager/deb/tests/PackagesTest.java @@ -12,11 +12,10 @@ */ package org.eclipse.packager.deb.tests; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.LinkedHashMap; @@ -30,12 +29,9 @@ import org.eclipse.packager.deb.ParserException; import org.junit.jupiter.api.Test; -import com.google.common.io.CharStreams; - -public class PackagesTest { - +class PackagesTest { @Test - public void test1FieldFormatters() throws IOException { + void test1FieldFormatters() throws IOException { testFieldFormatterValue(FieldFormatter.SINGLE, "foo", "foo"); testFieldFormatter(FieldFormatter.SINGLE, "Foo", "bar", "Foo: bar"); @@ -48,7 +44,7 @@ public void test1FieldFormatters() throws IOException { } @Test - public void test1FieldFormattersCornerCases() throws IOException { + void test1FieldFormattersCornerCases() throws IOException { testFieldFormatterValue(FieldFormatter.SINGLE, "foo\nbar", "foobar"); testFieldFormatter(FieldFormatter.SINGLE, "Foo", "bar\nbar", "Foo: barbar"); @@ -68,83 +64,63 @@ public void test1FieldFormattersCornerCases() throws IOException { private void testFieldFormatter(final FieldFormatter formatter, final String key, final String input, final String expected) throws IOException { final StringBuilder sb = new StringBuilder(); formatter.append(key, input, sb); - assertEquals(expected, formatter.format(key, input)); + assertThat(formatter.format(key, input)).isEqualTo(expected); } - private void testFieldFormatterValue(final FieldFormatter formatter, final String input, final String expected) throws IOException { + void testFieldFormatterValue(final FieldFormatter formatter, final String input, final String expected) throws IOException { final StringBuilder sb = new StringBuilder(); formatter.appendValue(input, sb); - - /* - * System.out.println ( "Expected ->" ); - * System.out.println ( expected ); - * System.out.println ( "Actual ->" ); - * System.out.println ( sb.toString () ); - */ - - assertEquals(expected, formatter.formatValue(input)); + assertThat(formatter.formatValue(input)).isEqualTo(expected); } @Test - public void test2() throws IOException, ParserException { - LinkedHashMap control; - try (InputStream is = PackagesTest.class.getResourceAsStream("data/test1")) { - control = ControlFileParser.parse(is); + void test2() throws IOException, ParserException { + try (final InputStream is = PackagesTest.class.getResourceAsStream("data/test1")) { + final Map control = ControlFileParser.parse(is); + assertThat(control).extractingByKey("Description").satisfies(s-> assertThat(Packages.makeDescriptionMd5(s)).isEqualTo("38d96b653196d5ef8c667efe23411a81")); } - - final String md5 = Packages.makeDescriptionMd5(control.get("Description")); - - assertEquals("38d96b653196d5ef8c667efe23411a81", md5); } @Test - public void test3() throws IOException, ParserException { - LinkedHashMap control; - try (InputStream is = PackagesTest.class.getResourceAsStream("data/test2")) { - control = ControlFileParser.parse(is); + void test3() throws IOException, ParserException { + try (final InputStream is = PackagesTest.class.getResourceAsStream("data/test2")) { + final Map control = ControlFileParser.parse(is); + assertThat(control).extractingByKey("Package").isEqualTo("org.eclipse.scada.base.p2-incubation"); + assertThat(control).extractingByKey("Installed-Size").isEqualTo("1100"); + assertThat(control).extractingByKey("Description").isEqualTo("Eclipse SCADA P2 Repository - org.eclipse.scada.base.p2-incubation"); + assertThat(control).extractingByKey("Conffiles").isEqualTo("\n/file1 1234\n/file2 1234"); } - - assertEquals("org.eclipse.scada.base.p2-incubation", control.get("Package")); - assertEquals("1100", control.get("Installed-Size")); - assertEquals("Eclipse SCADA P2 Repository - org.eclipse.scada.base.p2-incubation", control.get("Description")); - assertEquals("\n/file1 1234\n/file2 1234", control.get("Conffiles")); } @Test - public void test4() throws IOException, ParserException { + void test4() throws IOException, ParserException { encodeDecodeTest("data/test1"); encodeDecodeTest("data/test2"); } private void encodeDecodeTest(final String resourceName) throws IOException, ParserException { - LinkedHashMap control; - try (InputStream is = PackagesTest.class.getResourceAsStream(resourceName)) { - control = ControlFileParser.parse(is); - } - final StringBuilder sb = new StringBuilder(); - final Map map = new HashMap<>(); - map.put("Description", FieldFormatter.MULTI); - map.put("Conffiles", FieldFormatter.MULTI); - new ControlFileWriter(sb, map).writeEntries(control); - - String data; - try (InputStream is = PackagesTest.class.getResourceAsStream(resourceName)) { - data = CharStreams.toString(new InputStreamReader(is, StandardCharsets.UTF_8)); - } - System.out.println(sb.toString()); + try (final InputStream is = PackagesTest.class.getResourceAsStream(resourceName)) { + final LinkedHashMap control = ControlFileParser.parse(is); + final Map map = new HashMap<>(); + map.put("Description", FieldFormatter.MULTI); + map.put("Conffiles", FieldFormatter.MULTI); + new ControlFileWriter(sb, map).writeEntries(control); + } - assertEquals(data, sb.toString()); + try (final InputStream is = PackagesTest.class.getResourceAsStream(resourceName)) { + assertThat(is).isNotNull(); + final String data = new String(is.readAllBytes(), StandardCharsets.UTF_8); + assertThat(sb).hasToString(data); + } } @Test - public void testMultiFile1() throws IOException, ParserException { - List> result; - try (InputStream is = PackagesTest.class.getResourceAsStream("data/test3")) { - result = Packages.parseStatusFile(is); + void testMultiFile1() throws IOException, ParserException { + try (final InputStream is = PackagesTest.class.getResourceAsStream("data/test3")) { + final List> result = Packages.parseStatusFile(is); + assertThat(result).hasSize(2); } - - assertEquals(2, result.size()); } } diff --git a/pom.xml b/pom.xml index f4096d0..3d1c603 100644 --- a/pom.xml +++ b/pom.xml @@ -74,6 +74,7 @@ 11 11 + 3.25.3 1.77 1.26.1 1.16.1 @@ -182,6 +183,12 @@ ${logback.version} test + + org.assertj + assertj-core + ${assertj.version} + test + org.junit junit-bom @@ -208,6 +215,30 @@ + + + org.assertj + assertj-core + test + + + org.junit.jupiter + junit-jupiter-engine + test + + + org.junit.jupiter + junit-jupiter-params + test + + + + + ch.qos.logback + logback-classic + test + + diff --git a/rpm/pom.xml b/rpm/pom.xml index 45637de..f1a9909 100644 --- a/rpm/pom.xml +++ b/rpm/pom.xml @@ -44,19 +44,6 @@ true --> - - - - - org.junit.jupiter - junit-jupiter-engine - test - - - org.junit.jupiter - junit-jupiter-params - test - org.testcontainers junit-jupiter diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/deps/Dependencies.java b/rpm/src/main/java/org/eclipse/packager/rpm/deps/Dependencies.java index 74d6e10..d987d7f 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/deps/Dependencies.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/deps/Dependencies.java @@ -16,10 +16,10 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.EnumSet; import java.util.LinkedList; import java.util.List; import java.util.Objects; +import java.util.Set; import org.eclipse.packager.rpm.ReadableHeader; import org.eclipse.packager.rpm.RpmTag; @@ -136,7 +136,7 @@ private static List getDependencies(final ReadableHeader hea for (int i = 0; i < names.length; i++) { final String name = names[i]; final String version = versions[i]; - final EnumSet flagSet = RpmDependencyFlags.parse(flags[i]); + final Set flagSet = RpmDependencyFlags.parse(flags[i]); result.add(new Dependency(name, version, flagSet)); } return result; diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/deps/RpmDependencyFlags.java b/rpm/src/main/java/org/eclipse/packager/rpm/deps/RpmDependencyFlags.java index 6a80a9c..7139d2f 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/deps/RpmDependencyFlags.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/deps/RpmDependencyFlags.java @@ -53,7 +53,7 @@ public static int encode(final Set flags) { return value; } - public static EnumSet parse(final Long flags) { + public static Set parse(final Long flags) { if (flags == null) { return null; } @@ -61,7 +61,7 @@ public static EnumSet parse(final Long flags) { return parse(flags.intValue()); } - public static EnumSet parse(final int flags) { + public static Set parse(final int flags) { final EnumSet result = EnumSet.noneOf(RpmDependencyFlags.class); for (final RpmDependencyFlags f : values()) { diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/signature/RpmFileSignatureProcessor.java b/rpm/src/main/java/org/eclipse/packager/rpm/signature/RpmFileSignatureProcessor.java index fbcac44..729a951 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/signature/RpmFileSignatureProcessor.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/signature/RpmFileSignatureProcessor.java @@ -12,6 +12,7 @@ ********************************************************************************/ package org.eclipse.packager.rpm.signature; +import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; @@ -84,7 +85,7 @@ public static void perform(Path rpm, InputStream privateKeyIn, String passphrase PGPPrivateKey privateKey = getPrivateKey(privateKeyIn, passphrase); // Get the information of the RPM - try (RpmInputStream rpmIn = new RpmInputStream(Files.newInputStream(rpm))) { + try (RpmInputStream rpmIn = new RpmInputStream(new BufferedInputStream(Files.newInputStream(rpm)))) { signatureHeaderStart = rpmIn.getSignatureHeader().getStart(); signatureHeaderLength = rpmIn.getSignatureHeader().getLength(); payloadHeaderStart = rpmIn.getPayloadHeader().getStart(); diff --git a/rpm/src/main/java/org/eclipse/packager/rpm/yum/RepositoryCreator.java b/rpm/src/main/java/org/eclipse/packager/rpm/yum/RepositoryCreator.java index 6f9a8d2..43711d1 100644 --- a/rpm/src/main/java/org/eclipse/packager/rpm/yum/RepositoryCreator.java +++ b/rpm/src/main/java/org/eclipse/packager/rpm/yum/RepositoryCreator.java @@ -17,10 +17,10 @@ import java.io.OutputStream; import java.time.Instant; import java.util.Arrays; -import java.util.EnumSet; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Set; import java.util.TreeSet; import java.util.UUID; import java.util.function.Function; @@ -335,7 +335,7 @@ private void addDependencies(final Element fmt, final String elementName, final final Element ele = addElement(fmt, elementName); for (final Dependency dep : deps) { - final EnumSet flags = RpmDependencyFlags.parse(dep.getFlags()); + final Set flags = RpmDependencyFlags.parse(dep.getFlags()); if (flags.contains(RpmDependencyFlags.RPMLIB)) { continue; } diff --git a/rpm/src/test/java/org/eclipse/packager/rpm/EmptyWriterTest.java b/rpm/src/test/java/org/eclipse/packager/rpm/EmptyWriterTest.java index b442221..df04e97 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/EmptyWriterTest.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/EmptyWriterTest.java @@ -53,7 +53,5 @@ void test3() throws IOException, PGPException { try (final RpmInputStream in = new RpmInputStream(new BufferedInputStream(Files.newInputStream(outFile)))) { Dumper.dumpAll(in); } - - System.out.println(outFile.toAbsolutePath()); } } diff --git a/rpm/src/test/java/org/eclipse/packager/rpm/InputStreamTest.java b/rpm/src/test/java/org/eclipse/packager/rpm/InputStreamTest.java index fcfc4a5..fb80eb1 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/InputStreamTest.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/InputStreamTest.java @@ -13,54 +13,51 @@ package org.eclipse.packager.rpm; +import static org.assertj.core.api.Assertions.assertThat; +import static org.eclipse.packager.rpm.RpmTag.ARCH; +import static org.eclipse.packager.rpm.RpmTag.DIRNAMES; +import static org.eclipse.packager.rpm.RpmTag.LICENSE; +import static org.eclipse.packager.rpm.RpmTag.NAME; +import static org.eclipse.packager.rpm.RpmTag.OS; +import static org.eclipse.packager.rpm.RpmTag.PAYLOAD_CODING; +import static org.eclipse.packager.rpm.RpmTag.PAYLOAD_FORMAT; +import static org.eclipse.packager.rpm.RpmTag.RELEASE; +import static org.eclipse.packager.rpm.RpmTag.VERSION; + import java.io.BufferedInputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; - +import java.util.List; import org.eclipse.packager.rpm.app.Dumper; +import org.eclipse.packager.rpm.parse.InputHeader; import org.eclipse.packager.rpm.parse.RpmInputStream; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -public class InputStreamTest { +class InputStreamTest { + private static final List EXPECTED_DIRNAMES = List.of("/etc/", "/etc/eclipsescada/", "/etc/profile.d/", "/usr/bin/", "/usr/", "/usr/share/", "/usr/share/eclipsescada/", "/usr/share/eclipsescada/sql/", "/var/log/", "/var/run/"); @Test - public void test1() throws IOException { + void test1() throws IOException { try (final RpmInputStream in = new RpmInputStream(new BufferedInputStream(Files.newInputStream(Path.of("src/test/resources/data/org.eclipse.scada-0.2.1-1.noarch.rpm"))))) { Dumper.dumpAll(in); - - Assertions.assertEquals(280, in.getPayloadHeader().getStart()); - Assertions.assertEquals(3501, in.getPayloadHeader().getLength()); - - Assertions.assertEquals("cpio", in.getPayloadHeader().getTag(RpmTag.PAYLOAD_FORMAT)); - Assertions.assertEquals("lzma", in.getPayloadHeader().getTag(RpmTag.PAYLOAD_CODING)); - - Assertions.assertEquals("org.eclipse.scada", in.getPayloadHeader().getTag(RpmTag.NAME)); - Assertions.assertEquals("0.2.1", in.getPayloadHeader().getTag(RpmTag.VERSION)); - Assertions.assertEquals("1", in.getPayloadHeader().getTag(RpmTag.RELEASE)); - - Assertions.assertEquals("noarch", in.getPayloadHeader().getTag(RpmTag.ARCH)); - Assertions.assertEquals("linux", in.getPayloadHeader().getTag(RpmTag.OS)); - Assertions.assertEquals("EPL", in.getPayloadHeader().getTag(RpmTag.LICENSE)); - - Assertions.assertArrayEquals(new String[] { // - "/etc/", // - "/etc/eclipsescada/", // - "/etc/profile.d/", // - "/usr/bin/", // - "/usr/", // - "/usr/share/", // - "/usr/share/eclipsescada/", // - "/usr/share/eclipsescada/sql/", // - "/var/log/", // - "/var/run/", // - }, (String[]) in.getPayloadHeader().getTag(RpmTag.DIRNAMES)); + final InputHeader header = in.getPayloadHeader(); + assertThat(header).extracting("start").isEqualTo(280L); + assertThat(header).extracting("length").isEqualTo(3501L); + assertThat(header.getTag(PAYLOAD_FORMAT)).isEqualTo("cpio"); + assertThat(header.getTag(PAYLOAD_CODING)).isEqualTo("lzma"); + assertThat(header.getTag(NAME)).isEqualTo("org.eclipse.scada"); + assertThat(header.getTag(VERSION)).isEqualTo("0.2.1"); + assertThat(header.getTag(RELEASE)).isEqualTo( "1"); + assertThat(header.getTag(ARCH)).isEqualTo( "noarch"); + assertThat(header.getTag(OS)).isEqualTo("linux"); + assertThat(header.getTag(LICENSE)).isEqualTo("EPL"); + assertThat(List.of((String[]) header.getTag(DIRNAMES))).containsExactlyElementsOf(EXPECTED_DIRNAMES); } } @Test - public void test2() throws IOException { + void test2() throws IOException { try (final RpmInputStream in = new RpmInputStream(new BufferedInputStream(Files.newInputStream(Path.of("src/test/resources/data/org.eclipse.scada-centos6-0.2.1-1.noarch.rpm"))))) { Dumper.dumpAll(in); } diff --git a/rpm/src/test/java/org/eclipse/packager/rpm/Issue130Test.java b/rpm/src/test/java/org/eclipse/packager/rpm/Issue130Test.java index 1fbe7b4..2da1ad2 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/Issue130Test.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/Issue130Test.java @@ -13,25 +13,23 @@ package org.eclipse.packager.rpm; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; import java.io.BufferedInputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Arrays; import java.util.List; import org.eclipse.packager.rpm.app.Dumper; import org.eclipse.packager.rpm.build.RpmBuilder; import org.eclipse.packager.rpm.build.RpmBuilder.PackageInformation; -import org.eclipse.packager.rpm.parse.InputHeader; import org.eclipse.packager.rpm.parse.RpmInputStream; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; class Issue130Test { - private static final List PREFIXES = Arrays.asList("/opt", "/var/log"); + private static final List PREFIXES = List.of("/opt", "/var/log"); @TempDir private Path outBase; @@ -42,21 +40,14 @@ void test() throws IOException { try (final RpmBuilder builder = new RpmBuilder("prefixes-test", "1.0.0", "1", "noarch", outBase)) { final PackageInformation pinfo = builder.getInformation(); - pinfo.setPrefixes(PREFIXES); - outFile = builder.getTargetFile(); - builder.build(); } try (final RpmInputStream in = new RpmInputStream(new BufferedInputStream(Files.newInputStream(outFile)))) { Dumper.dumpAll(in); - - final InputHeader header = in.getPayloadHeader(); - final List prefixes = Arrays.asList(new RpmTagValue(header.getTag(RpmTag.PREFIXES)).asStringArray().orElse(null)); - - assertEquals(PREFIXES, prefixes); + assertThat(List.of((String[]) in.getPayloadHeader().getTag(RpmTag.PREFIXES))).containsExactlyElementsOf(PREFIXES); } } diff --git a/rpm/src/test/java/org/eclipse/packager/rpm/Issue136Test.java b/rpm/src/test/java/org/eclipse/packager/rpm/Issue136Test.java index 024324a..8a9ed07 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/Issue136Test.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/Issue136Test.java @@ -13,8 +13,7 @@ package org.eclipse.packager.rpm; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; import java.io.BufferedInputStream; import java.io.IOException; @@ -24,8 +23,6 @@ import org.eclipse.packager.rpm.app.Dumper; import org.eclipse.packager.rpm.build.RpmBuilder; -import org.eclipse.packager.rpm.info.RpmInformations; -import org.eclipse.packager.rpm.parse.InputHeader; import org.eclipse.packager.rpm.parse.RpmInputStream; import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.params.ParameterizedTest; @@ -45,26 +42,15 @@ class Issue136Test { "0123456789012345678901234567890123456789012345678901234567890123456", }) void test(final String originalName) throws IOException { - Path outFile; - - try (RpmBuilder builder = new RpmBuilder(originalName, "1.0.0", "1", "noarch", outBase)) { - outFile = builder.getTargetFile(); - + try (final RpmBuilder builder = new RpmBuilder(originalName, "1.0.0", "1", "noarch", outBase)) { + final Path outFile = builder.getTargetFile(); builder.build(); - } - - try (final RpmInputStream in = new RpmInputStream(new BufferedInputStream(Files.newInputStream(outFile)))) { - Dumper.dumpAll(in); - - final RpmLead lead = in.getLead(); - final InputHeader header = in.getPayloadHeader(); - final String name = RpmInformations.asString(header.getTag(RpmTag.NAME)); - - assertEquals(originalName.length(), name.length()); - - byte[] leadNameBytes = lead.getName().getBytes(StandardCharsets.UTF_8); - assertTrue(leadNameBytes.length < 66, () -> "Expected lead name bytes to be less than 66, was " + leadNameBytes.length); + try (final RpmInputStream in = new RpmInputStream(new BufferedInputStream(Files.newInputStream(outFile)))) { + Dumper.dumpAll(in); + assertThat((String) in.getPayloadHeader().getTag(RpmTag.NAME)).hasSameSizeAs(originalName); + assertThat(in.getLead().getName().getBytes(StandardCharsets.UTF_8)).hasSizeLessThan(66); + } } } } diff --git a/rpm/src/test/java/org/eclipse/packager/rpm/Issue24Test.java b/rpm/src/test/java/org/eclipse/packager/rpm/Issue24Test.java index cf43ea1..52115d6 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/Issue24Test.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/Issue24Test.java @@ -13,7 +13,7 @@ package org.eclipse.packager.rpm; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; import java.io.IOException; import java.nio.file.Path; @@ -39,13 +39,11 @@ void test() throws IOException { try (final RpmBuilder builder = new RpmBuilder(name, new RpmVersion(version, release), architecture, outBase, options)) { final Path outFile = builder.getTargetFile(); - builder.build(); - final String expectedRpmFileName = name + "-" + version + "-" + release + "." + architecture + ".rpm"; final String rpmFileName = options.getFileNameProvider().getRpmFileName(builder.getName(), builder.getVersion(), builder.getArchitecture()); - assertEquals(expectedRpmFileName, rpmFileName); - assertEquals(expectedRpmFileName, outFile.getFileName().toString()); + assertThat(rpmFileName).isEqualTo(expectedRpmFileName); + assertThat(outFile.getFileName()).hasToString(expectedRpmFileName); } final BuilderOptions options2 = new BuilderOptions(); @@ -53,13 +51,11 @@ void test() throws IOException { try (final RpmBuilder builder = new RpmBuilder(name, new RpmVersion(version, release), architecture, outBase, options2)) { final Path outFile = builder.getTargetFile(); - builder.build(); - final String expectedRpmFileName = name + "-" + version + "-" + release + "-" + architecture + ".rpm"; final String rpmFileName = options2.getFileNameProvider().getRpmFileName(builder.getName(), builder.getVersion(), builder.getArchitecture()); - assertEquals(expectedRpmFileName, rpmFileName); - assertEquals(expectedRpmFileName, outFile.getFileName().toString()); + assertThat(rpmFileName).isEqualTo(expectedRpmFileName); + assertThat(outFile.getFileName()).hasToString(expectedRpmFileName); } } } diff --git a/rpm/src/test/java/org/eclipse/packager/rpm/LeadTest.java b/rpm/src/test/java/org/eclipse/packager/rpm/LeadTest.java index 90b6106..d091036 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/LeadTest.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/LeadTest.java @@ -13,18 +13,21 @@ package org.eclipse.packager.rpm; -import static java.util.Optional.of; +import static org.assertj.core.api.Assertions.assertThat; +import static org.eclipse.packager.rpm.Architecture.ARM; +import static org.eclipse.packager.rpm.Architecture.NOARCH; +import static org.eclipse.packager.rpm.OperatingSystem.AIX; +import static org.eclipse.packager.rpm.OperatingSystem.UNKNOWN; import java.util.Optional; import org.eclipse.packager.rpm.build.LeadBuilder; import org.eclipse.packager.rpm.header.Header; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -public class LeadTest { +class LeadTest { @Test - public void testArch1() { + void testArch1() { testArch(Architecture.INTEL, "i386"); testArch(Architecture.INTEL, "INTEL"); testArch(Architecture.INTEL, "X86_64"); @@ -32,18 +35,14 @@ public void testArch1() { private void testArch(final Architecture expected, final String provided) { final Optional arch = Architecture.fromAlias(provided); - if (expected == null) { - Assertions.assertFalse(arch.isPresent()); - } else { - Assertions.assertEquals(expected, arch.orElse(null)); - } + assertThat(arch).hasValue(expected); } /** * Test the mappers for arch and os. */ @Test - public void testMapper1() { + void testMapper1() { final LeadBuilder lead = new LeadBuilder(); final Header header = new Header<>(); @@ -52,12 +51,12 @@ public void testMapper1() { lead.fillFlagsFromHeader(header); - Assertions.assertEquals(Architecture.NOARCH.getValue(), lead.getArchitecture()); - Assertions.assertEquals(OperatingSystem.UNKNOWN.getValue(), lead.getOperatingSystem()); + assertThat(Architecture.fromValue(lead.getArchitecture())).hasValue(NOARCH); + assertThat(OperatingSystem.fromValue(lead.getOperatingSystem())).hasValue(UNKNOWN); - lead.fillFlagsFromHeader(header, s -> of(Architecture.ARM), s -> of(OperatingSystem.AIX)); + lead.fillFlagsFromHeader(header, s -> Optional.of(ARM), s -> Optional.of(AIX)); - Assertions.assertEquals(Architecture.ARM.getValue(), lead.getArchitecture()); - Assertions.assertEquals(OperatingSystem.AIX.getValue(), lead.getOperatingSystem()); + assertThat(Architecture.fromValue(lead.getArchitecture())).hasValue(ARM); + assertThat(OperatingSystem.fromValue(lead.getOperatingSystem())).hasValue(AIX); } } diff --git a/rpm/src/test/java/org/eclipse/packager/rpm/PathNameTest.java b/rpm/src/test/java/org/eclipse/packager/rpm/PathNameTest.java index f97289c..ef92abd 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/PathNameTest.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/PathNameTest.java @@ -13,48 +13,17 @@ package org.eclipse.packager.rpm; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; -public class PathNameTest { - @Test - public void test1() { - assertPath("/", "", ""); - } - - @Test - public void test2() { - assertPath("/foo", "", "foo"); - } - - @Test - public void test3() { - assertPath("/foo/bar", "foo", "bar"); - } - - @Test - public void test4() { - assertPath("/foo/bar/baz", "foo/bar", "baz"); - } +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; - @Test - public void test4a() { - assertPath("/foo//bar/baz", "foo/bar", "baz"); - } - - @Test - public void test4b() { - assertPath("/foo//bar/baz/", "foo/bar", "baz"); - } - - @Test - public void test4c() { - assertPath("foo//bar/baz/", "foo/bar", "baz"); - } - - private void assertPath(final String pathName, final String dirname, final String basename) { +public class PathNameTest { + @ParameterizedTest + @CsvSource(value = {"/,'',''", "/foo,'',foo", "/foo/bar,foo,bar", "/foo/bar/baz,foo/bar,baz", "/foo//bar/baz,foo/bar,baz", "/foo//bar/baz/,foo/bar,baz"}) + void assertPath(final String pathName, final String dirname, final String basename) { final PathName result = PathName.parse(pathName); - Assertions.assertEquals(dirname, result.getDirname()); - Assertions.assertEquals(basename, result.getBasename()); + assertThat(result).extracting("dirname").isEqualTo(dirname); + assertThat(result).extracting("basename").isEqualTo(basename); } } diff --git a/rpm/src/test/java/org/eclipse/packager/rpm/SetVerifyFlagsTest.java b/rpm/src/test/java/org/eclipse/packager/rpm/SetVerifyFlagsTest.java index 3ce81a5..b5df3b1 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/SetVerifyFlagsTest.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/SetVerifyFlagsTest.java @@ -13,7 +13,7 @@ package org.eclipse.packager.rpm; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.assertj.core.api.Assertions.assertThat; import java.io.BufferedInputStream; import java.io.IOException; @@ -22,6 +22,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.HashSet; +import java.util.List; import java.util.Set; import org.eclipse.packager.rpm.app.Dumper; @@ -70,7 +71,7 @@ void writeRpmWithVerifyFlags() throws IOException { final Set verifyFlags = new HashSet<>( Arrays.asList(VerifyFlags.USER, VerifyFlags.GROUP)); ret.setVerifyFlags(verifyFlags); - LOGGER.info("file info for {}: {}", targetName, ret); + LOGGER.debug("file info for {}: {}", targetName, ret); return ret; } throw new IllegalArgumentException("unexpected target name: " + targetName); @@ -81,27 +82,23 @@ void writeRpmWithVerifyFlags() throws IOException { final FileInformation ret = new FileInformation(); final Set fileFlags = new HashSet<>(Collections.singletonList(FileFlags.README)); ret.setFileFlags(fileFlags); - LOGGER.info("file info for {}: {}", targetName, ret); + LOGGER.debug("file info for {}: {}", targetName, ret); return ret; } throw new IllegalArgumentException("unexpected target name: " + targetName); }); outFile = builder.getTargetFile(); builder.build(); - LOGGER.info("Written: {}", outFile); + LOGGER.debug("Written: {}", outFile); } try (final RpmInputStream in = new RpmInputStream(new BufferedInputStream(Files.newInputStream(outFile)))) { Dumper.dumpAll(in); final InputHeader header = in.getPayloadHeader(); - final String[] dirNames = (String[]) header.getTag(RpmTag.DIRNAMES); - assertArrayEquals(new String[] { DIRNAME }, dirNames); - final String[] baseNames = (String[]) header.getTag(RpmTag.BASENAMES); - assertArrayEquals(new String[] { NAME_myconf, NAME_myreadme }, baseNames); - final Integer[] fileFlags = (Integer[]) header.getTag(RpmTag.FILE_FLAGS); - assertArrayEquals(new Integer[] { 17, 256 }, fileFlags); // 17: CONFIGURATION|NOREPLACE, 256: README - final Integer[] fileVerifyFlags = (Integer[]) header.getTag(RpmTag.FILE_VERIFYFLAGS); - assertArrayEquals(new Integer[] { 24, -1 }, fileVerifyFlags); // 24: USER|GROUP, -1: + assertThat(List.of((String[]) header.getTag(RpmTag.DIRNAMES))).containsExactly(DIRNAME); + assertThat(List.of((String[]) header.getTag(RpmTag.BASENAMES))).containsExactly(NAME_myconf, NAME_myreadme); + assertThat(List.of((Integer[]) header.getTag(RpmTag.FILE_FLAGS))).containsExactly(17, 256); + assertThat((Integer[]) header.getTag(RpmTag.FILE_VERIFYFLAGS)).containsExactly(24, -1); } } } diff --git a/rpm/src/test/java/org/eclipse/packager/rpm/StableDependencyOrderTest.java b/rpm/src/test/java/org/eclipse/packager/rpm/StableDependencyOrderTest.java index 6783296..c90b750 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/StableDependencyOrderTest.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/StableDependencyOrderTest.java @@ -13,22 +13,27 @@ package org.eclipse.packager.rpm; -import java.io.IOException; +import static org.assertj.core.api.Assertions.assertThat; +import static org.eclipse.packager.rpm.RpmTag.REQUIRE_FLAGS; +import static org.eclipse.packager.rpm.RpmTag.REQUIRE_NAME; +import static org.eclipse.packager.rpm.RpmTag.REQUIRE_VERSION; + +import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; import org.eclipse.packager.rpm.deps.Dependencies; import org.eclipse.packager.rpm.deps.Dependency; import org.eclipse.packager.rpm.deps.RpmDependencyFlags; import org.eclipse.packager.rpm.header.Header; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -public class StableDependencyOrderTest { +class StableDependencyOrderTest { @Test - public void testStableRequirementSort() throws IOException { - + void testStableRequirementSort() { final Header header1 = new Header<>(); final Header header2 = new Header<>(); @@ -46,9 +51,12 @@ public void testStableRequirementSort() throws IOException { Collections.reverse(requirementsReverse); Dependencies.putRequirements(header2, requirementsReverse); - Assertions.assertArrayEquals((String[]) header1.get(RpmTag.REQUIRE_NAME), (String[]) header2.get(RpmTag.REQUIRE_NAME)); - Assertions.assertArrayEquals((String[]) header1.get(RpmTag.REQUIRE_VERSION), (String[]) header2.get(RpmTag.REQUIRE_VERSION)); - Assertions.assertArrayEquals((int[]) header1.get(RpmTag.REQUIRE_FLAGS), (int[]) header2.get(RpmTag.REQUIRE_FLAGS)); + assertThat(header2.get(REQUIRE_NAME)).isEqualTo(header1.get(REQUIRE_NAME)); + assertThat(header2.get(REQUIRE_VERSION)).isEqualTo(header1.get(REQUIRE_VERSION)); + assertThat(getRequireFlags(header2)).isEqualTo(getRequireFlags(header1)); } + private static List> getRequireFlags(Header header) { + return Arrays.stream((int[]) header.get(REQUIRE_FLAGS)).mapToObj(RpmDependencyFlags::parse).collect(Collectors.toUnmodifiableList()); + } } diff --git a/rpm/src/test/java/org/eclipse/packager/rpm/UnsignedTest.java b/rpm/src/test/java/org/eclipse/packager/rpm/UnsignedTest.java index 8d564de..daae3d3 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/UnsignedTest.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/UnsignedTest.java @@ -13,6 +13,8 @@ package org.eclipse.packager.rpm; +import static org.assertj.core.api.Assertions.assertThat; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; @@ -20,22 +22,17 @@ import java.io.DataOutputStream; import java.io.IOException; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -public class UnsignedTest { +class UnsignedTest { @Test - public void test1() throws IOException { + void test1() throws IOException { final long value = 0xFFFFFFFFL; final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final DataOutput dos = new DataOutputStream(bos); - dos.writeInt((int) value); - - Assertions.assertEquals(4, bos.size()); - + assertThat(bos.size()).isEqualTo(4); final DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bos.toByteArray())); - - Assertions.assertEquals(value, dis.readInt() & 0xFFFFFFFFL); + assertThat(dis.readInt() & 0xFFFFFFFFL).isEqualTo(value); } } diff --git a/rpm/src/test/java/org/eclipse/packager/rpm/VersionTest.java b/rpm/src/test/java/org/eclipse/packager/rpm/VersionTest.java index 7fa291a..d76af35 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/VersionTest.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/VersionTest.java @@ -13,41 +13,20 @@ package org.eclipse.packager.rpm; -import java.util.Optional; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class VersionTest { - @Test - public void test1() { - testVersion("1.2.3", null, "1.2.3", null); - } +import static org.assertj.core.api.Assertions.assertThat; - @Test - public void test2() { - testVersion("0:1.2.3", 0, "1.2.3", null); - } +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; - @Test - public void test3() { - testVersion("0:1.2.3-1", 0, "1.2.3", "1"); - } - - @Test - public void test4() { - testVersion("1.2.3-1", null, "1.2.3", "1"); - } - - @Test - public void test5() { - testVersion("1.2.3-123-456", null, "1.2.3", "123-456"); - } +import java.util.Optional; - private void testVersion(final String version, final Integer expectedEpoch, final String expectedVersion, final String expectedRelease) { +class VersionTest { + @ParameterizedTest + @CsvSource(value = {"1.2.3,,1.2.3,", "0:1.2.3,0,1.2.3,", "0:1.2.3-1,0,1.2.3,1", "1.2.3-1,,1.2.3,1", "1.2.3-123-456,,1.2.3,123-456"}) + void testVersion(final String version, final Integer expectedEpoch, final String expectedVersion, final String expectedRelease) { final RpmVersion v = RpmVersion.valueOf(version); - Assertions.assertEquals(Optional.ofNullable(expectedEpoch), v.getEpoch(), "Epoch"); - Assertions.assertEquals(expectedVersion, v.getVersion(), "Version"); - Assertions.assertEquals(Optional.ofNullable(expectedRelease), v.getRelease(), "Release"); + assertThat(v.getEpoch()).isEqualTo(Optional.ofNullable(expectedEpoch)); + assertThat(v.getVersion()).isEqualTo(expectedVersion); + assertThat(v.getRelease()).isEqualTo(Optional.ofNullable(expectedRelease)); } } diff --git a/rpm/src/test/java/org/eclipse/packager/rpm/info/RpmInformationTest.java b/rpm/src/test/java/org/eclipse/packager/rpm/info/RpmInformationTest.java index 2682d1c..4bfe134 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/info/RpmInformationTest.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/info/RpmInformationTest.java @@ -13,29 +13,29 @@ package org.eclipse.packager.rpm.info; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; import org.junit.jupiter.api.Test; -public class RpmInformationTest { +class RpmInformationTest { @Test - public void testVersionEquals() { + void testVersionEquals() { RpmInformation.Version version1 = new RpmInformation.Version("1.0.0", "1", null); RpmInformation.Version version2 = new RpmInformation.Version("1.0.0", "1", null); - assertEquals(version1, version2); + assertThat(version2).isEqualTo(version1); } @Test - public void testChangelogEquals() { + void testChangelogEquals() { RpmInformation.Changelog changelog1 = new RpmInformation.Changelog(0L, "", ""); RpmInformation.Changelog changelog2 = new RpmInformation.Changelog(0L, "", ""); - assertEquals(changelog1, changelog2); + assertThat(changelog2).isEqualTo(changelog1); } @Test - public void testDependencyEquals() { + void testDependencyEquals() { RpmInformation.Dependency dependency1 = new RpmInformation.Dependency("", "", 0L); RpmInformation.Dependency dependency2 = new RpmInformation.Dependency("", "", 0L); - assertEquals(dependency1, dependency2); + assertThat(dependency2).isEqualTo(dependency1); } } diff --git a/rpm/src/test/java/org/eclipse/packager/rpm/signature/RpmFileSignatureProcessorTest.java b/rpm/src/test/java/org/eclipse/packager/rpm/signature/RpmFileSignatureProcessorTest.java index 59bb034..18b68c8 100644 --- a/rpm/src/test/java/org/eclipse/packager/rpm/signature/RpmFileSignatureProcessorTest.java +++ b/rpm/src/test/java/org/eclipse/packager/rpm/signature/RpmFileSignatureProcessorTest.java @@ -14,25 +14,20 @@ package org.eclipse.packager.rpm.signature; import static java.nio.file.StandardOpenOption.CREATE_NEW; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import static org.assertj.core.api.Assertions.assertThat; import static org.testcontainers.containers.Container.ExecResult; import static org.testcontainers.images.builder.Transferable.DEFAULT_FILE_MODE; +import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; -import java.util.List; -import java.util.stream.Collectors; import org.bouncycastle.openpgp.PGPException; import org.eclipse.packager.rpm.HashAlgorithm; import org.eclipse.packager.rpm.RpmSignatureTag; -import org.eclipse.packager.rpm.Rpms; import org.eclipse.packager.rpm.parse.InputHeader; import org.eclipse.packager.rpm.parse.RpmInputStream; import org.junit.jupiter.api.BeforeAll; @@ -45,90 +40,85 @@ import org.testcontainers.utility.DockerImageName; @Testcontainers -public class RpmFileSignatureProcessorTest { +class RpmFileSignatureProcessorTest { + private static final String PASSPHRASE = "testkey"; // Do not change + private static final Path RPM = Path.of("src/test/resources/data/org.eclipse.scada-0.2.1-1.noarch.rpm"); private static final Path PRIVATE_KEY = Path.of("src/test/resources/key/private_key.txt"); private static final Path PUBLIC_KEY = Path.of("src/test/resources/key/public_key.txt"); + private static final String IMAGE_NAME = "registry.access.redhat.com/ubi9/ubi-minimal:latest"; + private static final String COMMAND = "sleep infinity"; - private static Path signedRpm; + static Path signedRpm; @TempDir static Path resultDirectory; @BeforeAll static void testSigningExistingRpm() throws IOException, PGPException { - // Read files - final String passPhrase = "testkey"; // Do not change - - if (!Files.exists(RPM) || !Files.exists(PRIVATE_KEY)) { - fail("Input files rpm or private_key does not exist"); - } + assertThat(RPM).exists(); + assertThat(PRIVATE_KEY).exists(); // Init the signed RPM signedRpm = resultDirectory.resolve("org.eclipse.scada-0.2.1-1.noarch.rpm"); - try (final OutputStream resultOut = Files.newOutputStream(signedRpm, CREATE_NEW); - final InputStream privateKeyStream = Files.newInputStream(PRIVATE_KEY)) { + try (final OutputStream resultOut = Files.newOutputStream(signedRpm, CREATE_NEW); final InputStream privateKeyStream = Files.newInputStream(PRIVATE_KEY)) { // Sign the RPM - RpmFileSignatureProcessor.perform(RPM, privateKeyStream, passPhrase, resultOut, HashAlgorithm.SHA256); + RpmFileSignatureProcessor.perform(RPM, privateKeyStream, PASSPHRASE, resultOut, HashAlgorithm.SHA256); // Read the signed rpm file - try (RpmInputStream initialRpm = new RpmInputStream(Files.newInputStream(RPM)); RpmInputStream rpmSigned = new RpmInputStream(Files.newInputStream(signedRpm))) { - InputHeader initialHeader = initialRpm.getSignatureHeader(); - InputHeader signedHeader = rpmSigned.getSignatureHeader(); - // Get information of the signed rpm file - int signedSize = (int) signedHeader.getEntry(RpmSignatureTag.SIZE).get().getValue(); - int signedPayloadSize = (int) signedHeader.getEntry(RpmSignatureTag.PAYLOAD_SIZE).get().getValue(); - String signedSha1 = signedHeader.getEntry(RpmSignatureTag.SHA1HEADER).get().getValue().toString(); - String signedMd5 = Rpms.dumpValue(signedHeader.getEntry(RpmSignatureTag.MD5).get().getValue()); - String pgpSignature = Rpms.dumpValue(signedHeader.getEntry(RpmSignatureTag.PGP).get().getValue()); + try (final RpmInputStream initialRpm = new RpmInputStream(new BufferedInputStream(Files.newInputStream(RPM))); final RpmInputStream rpmSigned = new RpmInputStream(new BufferedInputStream(Files.newInputStream(signedRpm)))) { // Get information of the initial rpm file - int initialSize = (int) initialHeader.getEntry(RpmSignatureTag.SIZE).get().getValue(); - int initialPayloadSize = (int) initialHeader.getEntry(RpmSignatureTag.PAYLOAD_SIZE).get().getValue(); - String initialSha1 = initialHeader.getEntry(RpmSignatureTag.SHA1HEADER).get().getValue().toString(); - String initialMd5 = Rpms.dumpValue(initialHeader.getEntry(RpmSignatureTag.MD5).get().getValue()); + final InputHeader initialHeader = initialRpm.getSignatureHeader(); + final int initialSize = (int) initialHeader.getEntry(RpmSignatureTag.SIZE).get().getValue(); + final int initialPayloadSize = (int) initialHeader.getEntry(RpmSignatureTag.PAYLOAD_SIZE).get().getValue(); + final String initialSha1 = (String) initialHeader.getEntry(RpmSignatureTag.SHA1HEADER).get().getValue(); + final byte[] initialMd5 = (byte[]) initialHeader.getEntry(RpmSignatureTag.MD5).get().getValue(); + + // Get information of the signed rpm file + final InputHeader signedHeader = rpmSigned.getSignatureHeader(); + final int signedSize = (int) signedHeader.getEntry(RpmSignatureTag.SIZE).get().getValue(); + final int signedPayloadSize = (int) signedHeader.getEntry(RpmSignatureTag.PAYLOAD_SIZE).get().getValue(); + final String signedSha1 = (String) signedHeader.getEntry(RpmSignatureTag.SHA1HEADER).get().getValue(); + final byte[] signedMd5 = (byte[]) signedHeader.getEntry(RpmSignatureTag.MD5).get().getValue(); + final byte[] pgpSignature = (byte[]) signedHeader.getEntry(RpmSignatureTag.PGP).get().getValue(); // Compare information values of initial rpm and signed rpm - assertEquals(initialSize, signedSize); - assertEquals(initialPayloadSize, signedPayloadSize); - assertEquals(initialSha1, signedSha1); - assertEquals(initialMd5, signedMd5); + assertThat(signedSize).isEqualTo(initialSize); + assertThat(signedPayloadSize).isEqualTo(initialPayloadSize); + assertThat(signedSha1).isEqualTo(initialSha1); + assertThat(signedMd5).isEqualTo(initialMd5); // Verify if signature is present - assertNotNull(pgpSignature); + assertThat(pgpSignature).isNotNull(); } } } @Test void verifyRpmSignature() throws Exception { - // check if the output from the previous test is found - if (!Files.exists(PUBLIC_KEY) || !Files.exists(signedRpm)) { - fail("Input files signedRpm or publicKey does not exist"); - } + assertThat(PUBLIC_KEY).exists(); + assertThat(signedRpm).exists(); // extract the plain file name - Path publicKeyName = PUBLIC_KEY.getFileName(); - Path rpmFileName = signedRpm.getFileName(); + final Path publicKeyName = PUBLIC_KEY.getFileName(); + final Path rpmFileName = signedRpm.getFileName(); - try (final GenericContainer container = new GenericContainer<>(DockerImageName.parse("registry.access.redhat.com/ubi9/ubi-minimal:latest"))) { + try (final GenericContainer container = new GenericContainer<>(DockerImageName.parse(IMAGE_NAME))) { container.setCommand(COMMAND); container.withCopyToContainer(Transferable.of(Files.readAllBytes(PUBLIC_KEY), DEFAULT_FILE_MODE), "/" + publicKeyName); container.withCopyToContainer(Transferable.of(Files.readAllBytes(signedRpm), DEFAULT_FILE_MODE), "/" + rpmFileName); container.start(); - ExecResult importResult = container.execInContainer("rpm", "--import", "/" + publicKeyName); - assertEquals(0, importResult.getExitCode()); - ExecResult checksigResult = container.execInContainer("rpm", "--verbose", "--checksig", "/" + rpmFileName); - assertEquals(0, checksigResult.getExitCode()); - String stdout = checksigResult.getStdout(); - List lines = stdout.lines().collect(Collectors.toList()); - // ensure that we find a valid signature for our key - assertTrue(lines.contains(" V4 RSA/SHA256 Signature, key ID 679f5723: OK")); - System.out.println(stdout); + final ExecResult importResult = container.execInContainer("rpm", "--import", "/" + publicKeyName); + assertThat(importResult.getExitCode()).isZero(); + final ExecResult checksigResult = container.execInContainer("rpm", "--verbose", "--checksig", "/" + rpmFileName); + assertThat(checksigResult.getExitCode()).isZero(); + final String stdout = checksigResult.getStdout(); + assertThat(stdout).containsIgnoringWhitespaces("V4 RSA/SHA256 Signature, key ID 679f5723: OK"); } } }