Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#359 - Verify repomd.xml #370

Merged
merged 1 commit into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 87 additions & 4 deletions src/test/java/com/artipie/rpm/hm/StorageHasRepoMd.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,23 @@
*/
package com.artipie.rpm.hm;

import com.artipie.asto.Content;
import com.artipie.asto.Key;
import com.artipie.asto.Storage;
import org.llorllale.cactoos.matchers.MatcherEnvelope;
import com.artipie.asto.ext.ContentDigest;
import com.artipie.asto.ext.PublisherAs;
import com.artipie.rpm.Digest;
import com.artipie.rpm.RepoConfig;
import com.artipie.rpm.meta.XmlPackage;
import com.jcabi.xml.XMLDocument;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import org.cactoos.text.FormattedText;
import org.hamcrest.Matcher;
import org.hamcrest.core.AllOf;
import org.llorllale.cactoos.matchers.MatcherOf;

/**
* Matcher for checking rempomd.xml file presence and information in the storage.
Expand All @@ -35,13 +50,81 @@
* filelists and other metadata files. After implementing this, enable tests
* in StorageHasRepoMdTest.
*/
public final class StorageHasRepoMd extends MatcherEnvelope<Storage> {
public final class StorageHasRepoMd extends AllOf<Storage> {

/**
* Repodata key.
*/
private static final Key BASE = new Key.From("repodata");

/**
* Repomd rey.
*/
private static final Key.From REPOMD = new Key.From(StorageHasRepoMd.BASE, "repomd.xml");

/**
* Ctor.
* @param config Rmp repo config
*/
public StorageHasRepoMd() {
super(stg -> false, desc -> { }, (stg, mis) -> { }
public StorageHasRepoMd(final RepoConfig config) {
super(matchers(config));
}

/**
* List of matchers.
* @param config Rmp repo config
* @return Matchers list
*/
private static List<Matcher<? super Storage>> matchers(final RepoConfig config) {
final List<Matcher<? super Storage>> res = new ArrayList<>(4);
res.add(
new MatcherOf<Storage>(
storage -> storage.exists(StorageHasRepoMd.REPOMD).join(),
new FormattedText("Repomd is present")
)
);
new XmlPackage.Stream(config.filelists()).get().forEach(
pckg -> res.add(
new MatcherOf<Storage>(
storage -> hasRecord(storage, pckg, config.digest()),
new FormattedText("Repomd has record for %s xml", pckg.name())
)
)
);
return res;
}

/**
* Has repomd record for xml metadata package?
* @param storage Storage
* @param pckg Metadata package
* @param digest Digest algorithm
* @return True if record is present
*/
private static boolean hasRecord(final Storage storage, final XmlPackage pckg,
final Digest digest) {
final Optional<Content> repomd = storage.list(StorageHasRepoMd.BASE).join().stream()
.filter(item -> item.string().contains(pckg.filename())).findFirst()
.map(item -> storage.value(new Key.From(item)).join());
boolean res = false;
if (repomd.isPresent()) {
final String checksum = new ContentDigest(
repomd.get(),
digest::messageDigest
).hex().toCompletableFuture().join();
res = !new XMLDocument(
new PublisherAs(storage.value(StorageHasRepoMd.REPOMD).join())
.asciiString().toCompletableFuture().join()
).nodes(
String.format(
//@checkstyle LineLengthCheck (1 line)
"/*[name()='repomd']/*[name()='data' and @type='%s']/*[name()='checksum' and @type='%s' and text()='%s']",
pckg.name().toLowerCase(Locale.US),
digest.type(),
checksum
)
).isEmpty();
}
return res;
}
}
29 changes: 18 additions & 11 deletions src/test/java/com/artipie/rpm/hm/StorageHasRepoMdTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import com.artipie.asto.Storage;
import com.artipie.asto.memory.InMemoryStorage;
import com.artipie.asto.test.TestResource;
import com.artipie.rpm.Digest;
import com.artipie.rpm.RepoConfig;
import com.artipie.rpm.StandardNamingPolicy;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.llorllale.cactoos.matchers.Assertion;
Expand All @@ -40,22 +43,26 @@
public class StorageHasRepoMdTest {

@Test
@Disabled
public void matchPositive() throws Exception {
public void matchPositive() {
final Storage storage = new InMemoryStorage();
new TestResource(
"src/test/resources-binary/repodata/StorageHasRepoMdTest/repomd.xml"
).saveTo(storage, new Key.From("repomd.xml"));
"repodata/StorageHasRepoMdTest/repomd.xml"
).saveTo(storage, new Key.From("repodata/repomd.xml"));
new TestResource(
"repodata/StorageHasRepoMdTest/primary.xml.gz"
).saveTo(storage, new Key.From("repodata/primary.xml.gz"));
new TestResource(
"src/test/resources-binary/repodata/StorageHasRepoMdTest/primary.xml.gz"
).saveTo(storage, new Key.From("primary.xml.gz"));
"repodata/StorageHasRepoMdTest/other.xml.gz"
).saveTo(storage, new Key.From("repodata/other.xml.gz"));
new TestResource(
"src/test/resources-binary/repodata/StorageHasRepoMdTest/other.xml.gz"
).saveTo(storage, new Key.From("other.xml.gz"));
"repodata/StorageHasRepoMdTest/filelists.xml.gz"
).saveTo(storage, new Key.From("repodata/filelists.xml.gz"));
new Assertion<>(
"The matcher gives positive result for a valid repommd.xml configuration",
"The matcher gives positive result for a valid repomd.xml configuration",
storage,
new StorageHasRepoMd()
new StorageHasRepoMd(
new RepoConfig.Simple(Digest.SHA256, StandardNamingPolicy.PLAIN, true)
)
).affirm();
}

Expand All @@ -64,7 +71,7 @@ public void matchPositive() throws Exception {
public void doNotMatchesWhenRepomdAbsent() throws Exception {
new Assertion<>(
"The matcher gives a negative result when storage does not have repomd.xml",
new StorageHasRepoMd(),
new StorageHasRepoMd(new RepoConfig.Simple()),
new Mismatches<>(
new InMemoryStorage(),
"repomd.xml file expected",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ SOFTWARE.
<repomd xmlns="http://linux.duke.edu/metadata/repo" xmlns:rpm="http://linux.duke.edu/metadata/rpm">
<revision>1585928825</revision>
<data type="primary">
<checksum type="sha256">4539c03d91a99d1bb01a3b151dc6dee0129bc12708863b9d20bb3c97bba39453</checksum>
<checksum type="sha256">3b37791217014b834f63bc33fa0575b5673ab61a36089d81b02e98ab2a554a02</checksum>
<open-checksum type="sha256">7e19eabdf41d1707ea13ed3c9ec639751fb3fb4999c8f9504fe8ddca4f0ac656</open-checksum>
<location href="repodata/primary.xml.gz"/>
<timestamp>1585928825</timestamp>
<size>1742</size>
<open-size>6142</open-size>
</data>
<data type="filelists">
<checksum type="sha256">aa433fe4c2cea220546a2566c245154f3e3561acb3a20e6cc187fef17f47c899</checksum>
<checksum type="sha256">afc1db81d813536bb709c7f1080b76e8482b0ae85e17b2f6fc94247cf8403c52</checksum>
<open-checksum type="sha256">15c8ff0a236172a2af68a65b3c3ff43f434b011738797d1135bba2cbb703d847</open-checksum>
<location href="repodata/filelists.xml.gz"/>
<timestamp>1585928825</timestamp>
<size>875</size>
<open-size>2933</open-size>
</data>
<data type="other">
<checksum type="sha256">433b0a32fa2af504204d9052426c96e9f9b45d00dbb369701e04931b48b35421</checksum>
<checksum type="sha256">f4cf156be36b128523e156aa734521b87db7f8af646c8fe2a0cdf931edcbb0ba</checksum>
<open-checksum type="sha256">b79de20735fa800069392e5971a1c2c19b69d59095d6af3883ff3cd8c1437091</open-checksum>
<location href="repodata/other.xml.gz"/>
<timestamp>1585928825</timestamp>
Expand Down