Skip to content

Commit

Permalink
Fix and enable NVRMetadata validator
Browse files Browse the repository at this point in the history
  • Loading branch information
mkoncek committed Oct 4, 2023
1 parent 690ccff commit 0615cab
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Dockerfile.main
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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(), ""));
}
}

Expand Down Expand Up @@ -63,47 +70,80 @@ public String valueOf(RpmFile rpm) {

private static List<Entry> ENTRIES = List.of(new RpmName(), new RpmEpoch(), new RpmVersion(), new RpmRelease());

/*
private class RpmEntry implements RpmJarConsumer {
RpmFile sourceRpm = null;
List<RpmFile> 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<String, RpmEntry> rpms;

public NVRMetadataValidator() {
super(rpm -> {
var release = rpm.getRelease();
this.rpms = new TreeMap<>();
}

@Override
public void validate(Iterable<RpmFile> 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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0615cab

Please sign in to comment.