From 0615cabfa9c859e4fd35f5f6e95bd42b21dcd9a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mari=C3=A1n=20Kon=C4=8Dek?= Date: Wed, 4 Oct 2023 14:58:06 +0200 Subject: [PATCH] Fix and enable NVRMetadata validator --- Dockerfile.main | 2 +- .../validators/NVRMetadataValidator.java | 110 ++++++++++++------ ...raproject.javapackages.validator.Validator | 1 + 3 files changed, 77 insertions(+), 36 deletions(-) diff --git a/Dockerfile.main b/Dockerfile.main index f027a433..03244cea 100644 --- a/Dockerfile.main +++ b/Dockerfile.main @@ -3,7 +3,7 @@ RUN microdnf -y install git-core maven-openjdk17 rpm-libs WORKDIR "/usr/local/src" -ENV java_deptools_hash='1b981055d50f287e50ef7f6daa425207442f58dd' +ENV java_deptools_hash='f173289a01eef50e0242eccd73d9a57bdd1f7083' RUN echo "${java_deptools_hash}" RUN git clone "https://github.com/mkoncek/java-deptools-native.git" diff --git a/src/main/java/org/fedoraproject/javapackages/validator/validators/NVRMetadataValidator.java b/src/main/java/org/fedoraproject/javapackages/validator/validators/NVRMetadataValidator.java index 087d8902..e5157c30 100644 --- a/src/main/java/org/fedoraproject/javapackages/validator/validators/NVRMetadataValidator.java +++ b/src/main/java/org/fedoraproject/javapackages/validator/validators/NVRMetadataValidator.java @@ -1,17 +1,24 @@ package org.fedoraproject.javapackages.validator.validators; import java.io.ByteArrayInputStream; +import java.nio.file.Paths; +import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.TreeMap; import java.util.jar.JarInputStream; import org.apache.commons.compress.archivers.cpio.CpioArchiveEntry; import org.fedoraproject.javadeptools.rpm.RpmFile; import org.fedoraproject.javapackages.validator.Common; import org.fedoraproject.javapackages.validator.Decorated; +import org.fedoraproject.javapackages.validator.RpmJarConsumer; import org.fedoraproject.javapackages.validator.TmtTest; +import org.fedoraproject.javapackages.validator.Validator; @TmtTest("/java/nvr-jar-metadata") -public class NVRMetadataValidator extends JarValidator { +public class NVRMetadataValidator extends Validator { private static interface Entry { String name(); String valueOf(RpmFile rpm); @@ -35,7 +42,7 @@ public String name() { } @Override public String valueOf(RpmFile rpm) { - return String.valueOf(rpm.getInfo().getEpoch()); + return String.valueOf(Objects.requireNonNullElse(rpm.getInfo().getEpoch(), "")); } } @@ -63,47 +70,80 @@ public String valueOf(RpmFile rpm) { private static List ENTRIES = List.of(new RpmName(), new RpmEpoch(), new RpmVersion(), new RpmRelease()); - /* + private class RpmEntry implements RpmJarConsumer { + RpmFile sourceRpm = null; + List binaryRpms = new ArrayList<>(); + + @Override + public void acceptJarEntry(RpmFile rpm, CpioArchiveEntry rpmEntry, byte[] content) throws Exception { + try (var is = new JarInputStream(new ByteArrayInputStream(content))) { + var mf = is.getManifest(); + var attrs = mf.getMainAttributes(); + + for (var entry : ENTRIES) { + var srpmValue = entry.valueOf(sourceRpm); + var attrValue = attrs.getValue(entry.name()); + + if (attrValue == null) { + fail("{0}: {1}: Jar manifest attribute {2} is not present", + Decorated.rpm(rpm), + Decorated.custom(Common.getEntryPath(rpmEntry), JarValidator.DECORATION_JAR), + Decorated.struct(entry.name())); + } else if (srpmValue.equals(attrValue)) { + pass("{0}: {1}: Jar manifest attribute {2} with value {3} exactly matches the RPM attribute", + Decorated.rpm(rpm), + Decorated.custom(Common.getEntryPath(rpmEntry), JarValidator.DECORATION_JAR), + Decorated.struct(entry.name()), + Decorated.actual(attrValue)); + } else { + fail("{0}: {1}: Jar manifest attribute {2} with value {3} does not match the RPM attribute value {4}", + Decorated.rpm(rpm), + Decorated.custom(Common.getEntryPath(rpmEntry), JarValidator.DECORATION_JAR), + Decorated.struct(entry.name()), + Decorated.actual(attrValue), + Decorated.expected(srpmValue)); + } + } + } + } + } + + private Map rpms; + public NVRMetadataValidator() { - super(rpm -> { - var release = rpm.getRelease(); + this.rpms = new TreeMap<>(); + } + + @Override + public void validate(Iterable rpms) throws Exception { + for (var rpm : rpms) { + var release = rpm.getInfo().getRelease(); int i = 0; while (release.charAt(i) != '.') { ++i; } - return release.substring(i + 1).startsWith("el"); - }); - } - */ - @Override - public void validateJarEntry(RpmFile rpm, CpioArchiveEntry rpmEntry, byte[] content) throws Exception { - try (var is = new JarInputStream(new ByteArrayInputStream(content))) { - var mf = is.getManifest(); - var attrs = mf.getMainAttributes(); - - for (var entry : ENTRIES) { - var attrValue = attrs.getValue(entry.name()); - - if (attrValue == null) { - fail("{0}: {1}: Jar manifest attribute {2} is not present", - Decorated.rpm(rpm), - Decorated.custom(Common.getEntryPath(rpmEntry), DECORATION_JAR), - Decorated.struct(entry.name())); - } else if (entry.valueOf(rpm).equals(attrValue)) { - pass("{0}: {1}: Jar manifest attribute {2} with value {3} exactly matches the RPM attribute", - Decorated.rpm(rpm), - Decorated.custom(Common.getEntryPath(rpmEntry), DECORATION_JAR), - Decorated.struct(entry.name()), - Decorated.actual(attrValue)); + // We only care about EL packages + if (release.substring(i + 1).startsWith("el")) { + if (rpm.getInfo().isSourcePackage()) { + var filename = Paths.get(rpm.getURL().getPath()).getFileName(); + if (filename == null) { + error("{0}: Could not obtain the path from URL: {1}", + Decorated.rpm(rpm), Decorated.actual(rpm.getURL())); + return; + } + this.rpms.computeIfAbsent(filename.toString(), name -> new RpmEntry()).sourceRpm = rpm; } else { - fail("{0}: {1}: Jar manifest attribute {2} with value {3} does not match the RPM attribute value {4}", - Decorated.rpm(rpm), - Decorated.custom(Common.getEntryPath(rpmEntry), DECORATION_JAR), - Decorated.struct(entry.name()), - Decorated.actual(attrValue), - Decorated.expected(entry.valueOf(rpm))); + this.rpms.computeIfAbsent(rpm.getInfo().getSourceRPM(), name -> new RpmEntry()).binaryRpms.add(rpm); } + } else { + debug("Ignoring {0}", Decorated.rpm(rpm)); + } + } + + for (var entry : this.rpms.values()) { + for (var binary : entry.binaryRpms) { + entry.accept(binary); } } } diff --git a/src/main/resources/META-INF/services/org.fedoraproject.javapackages.validator.Validator b/src/main/resources/META-INF/services/org.fedoraproject.javapackages.validator.Validator index 4c22a89b..626551da 100644 --- a/src/main/resources/META-INF/services/org.fedoraproject.javapackages.validator.Validator +++ b/src/main/resources/META-INF/services/org.fedoraproject.javapackages.validator.Validator @@ -3,3 +3,4 @@ org.fedoraproject.javapackages.validator.validators.BytecodeVersionValidator org.fedoraproject.javapackages.validator.validators.JavadocNoarchValidator org.fedoraproject.javapackages.validator.validators.JavaExclusiveArchValidator org.fedoraproject.javapackages.validator.validators.MavenMetadataValidator +org.fedoraproject.javapackages.validator.validators.NVRMetadataValidator