001/*
002 * The contents of this file are subject to the license and copyright detailed
003 * in the LICENSE and NOTICE files at the root of the source tree.
004 */
005package org.duraspace.bagit;
006
007
008import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE;
009import static org.assertj.core.api.Assertions.assertThat;
010import static org.junit.Assert.fail;
011
012import java.io.File;
013import java.io.IOException;
014import java.net.URISyntaxException;
015import java.net.URL;
016import java.nio.file.Files;
017import java.nio.file.Path;
018import java.nio.file.Paths;
019import java.security.MessageDigest;
020import java.time.LocalDate;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
024import java.util.Objects;
025
026import gov.loc.repository.bagit.domain.Bag;
027import gov.loc.repository.bagit.exceptions.CorruptChecksumException;
028import gov.loc.repository.bagit.exceptions.FileNotInPayloadDirectoryException;
029import gov.loc.repository.bagit.exceptions.InvalidBagitFileFormatException;
030import gov.loc.repository.bagit.exceptions.MaliciousPathException;
031import gov.loc.repository.bagit.exceptions.MissingBagitFileException;
032import gov.loc.repository.bagit.exceptions.MissingPayloadDirectoryException;
033import gov.loc.repository.bagit.exceptions.MissingPayloadManifestException;
034import gov.loc.repository.bagit.exceptions.UnparsableVersionException;
035import gov.loc.repository.bagit.exceptions.UnsupportedAlgorithmException;
036import gov.loc.repository.bagit.exceptions.VerificationException;
037import gov.loc.repository.bagit.reader.BagReader;
038import gov.loc.repository.bagit.verify.BagVerifier;
039import org.apache.commons.compress.utils.Sets;
040import org.apache.commons.io.FileUtils;
041import org.assertj.core.util.Maps;
042import org.junit.After;
043import org.junit.Before;
044import org.junit.Test;
045
046/**
047 * Test basic bag writing functionality to make sure we are writing compliant bags
048 *
049 * @author mikejritter
050 * @since 2020-03-05
051 */
052public class BagWriterTest {
053
054    // set up expected bag, data file, and tag files
055    private final String bagName = "bag-writer-test";
056    private final String filename = "hello-writer";
057    private final String extraTagName = "extra-tag.txt";
058
059    private Path bag;
060    private BagProfile profile;
061
062    @Before
063    public void setup() throws URISyntaxException, IOException {
064        final URL sampleUrl = this.getClass().getClassLoader().getResource("sample");
065        final Path sample = Paths.get(Objects.requireNonNull(sampleUrl).toURI());
066        bag = sample.resolve(bagName);
067
068        profile = new BagProfile(BagProfile.BuiltIn.BEYOND_THE_REPOSITORY);
069    }
070
071    @After
072    public void teardown() {
073        if (bag != null) {
074            FileUtils.deleteQuietly(bag.toFile());
075        }
076    }
077
078    @Test
079    public void write() throws IOException {
080        // The message digests to use
081        final BagItDigest sha1 = BagItDigest.SHA1;
082        final BagItDigest sha256 = BagItDigest.SHA256;
083        final BagItDigest sha512 = BagItDigest.SHA512;
084        final MessageDigest sha1MD = sha1.messageDigest();
085        final MessageDigest sha256MD = sha256.messageDigest();
086        final MessageDigest sha512MD = sha512.messageDigest();
087
088        // Create a writer with 3 manifest algorithms
089        Files.createDirectories(bag);
090        final BagWriter writer = new BagWriter(bag.toFile(), Sets.newHashSet(sha1.bagitName(), sha256.bagitName(),
091                                                                             sha512.bagitName()));
092
093        // Setup the data file
094        final Path data = bag.resolve("data");
095        final Path file = Files.createFile(data.resolve(filename));
096        final Map<File, String> sha1Sums = Maps.newHashMap(file.toFile(), HexEncoder.toString(sha1MD.digest()));
097        final Map<File, String> sha256Sums  = Maps.newHashMap(file.toFile(), HexEncoder.toString(sha256MD.digest()));
098        final Map<File, String> sha512Sums = Maps.newHashMap(file.toFile(), HexEncoder.toString(sha512MD.digest()));
099
100        writer.addTags(extraTagName, Maps.newHashMap("test-key", "test-value"));
101        final Map<String, String> bagInfoFields = new HashMap<>();
102        bagInfoFields.put(BagConfig.SOURCE_ORGANIZATION_KEY, "bagit-support");
103        bagInfoFields.put(BagConfig.BAGGING_DATE_KEY, ISO_LOCAL_DATE.format(LocalDate.now()));
104        bagInfoFields.put(BagConfig.BAG_SIZE_KEY, "0 bytes");
105        bagInfoFields.put(BagConfig.PAYLOAD_OXUM_KEY, "1.0");
106        writer.addTags(BagConfig.BAG_INFO_KEY, bagInfoFields);
107        writer.registerChecksums(sha1.bagitName(), sha1Sums);
108        writer.registerChecksums(sha256.bagitName(), sha256Sums);
109        writer.registerChecksums(sha512.bagitName(), sha512Sums);
110
111        writer.write();
112
113        final Path bagit = bag.resolve("bagit.txt");
114        final Path extra = bag.resolve(extraTagName);
115        final Path bagInfo = bag.resolve(BagConfig.BAG_INFO_KEY);
116        final Path sha1Manifest = bag.resolve("manifest-" + sha1.bagitName() + ".txt");
117        final Path sha1Tagmanifest = bag.resolve("tagmanifest-" + sha1.bagitName() + ".txt");
118        final Path sha256Manifest = bag.resolve("manifest-" + sha256.bagitName() + ".txt");
119        final Path sha256Tagmanifest = bag.resolve("tagmanifest-" + sha256.bagitName() + ".txt");
120        final Path sha512Manifest = bag.resolve("manifest-" + sha512.bagitName() + ".txt");
121        final Path sha512Tagmanifest = bag.resolve("tagmanifest-" + sha512.bagitName() + ".txt");
122
123        // Assert that all tag files (bagit.txt, bag-info.txt, etc) exist
124        assertThat(bagit).exists();
125        assertThat(extra).exists();
126        assertThat(bagInfo).exists();
127        assertThat(sha1Manifest).exists();
128        assertThat(sha1Tagmanifest).exists();
129        assertThat(sha256Manifest).exists();
130        assertThat(sha256Tagmanifest).exists();
131        assertThat(sha512Manifest).exists();
132        assertThat(sha512Tagmanifest).exists();
133
134        // Assert that bagit.txt contains expected lines
135        final List<String> bagitLines = Files.readAllLines(bagit);
136        assertThat(bagitLines).contains("BagIt-Version: 0.97", "Tag-File-Character-Encoding: UTF-8");
137
138        // Assert that bag-info.txt contains... the bare necessities
139        final List<String> bagInfoLines = Files.readAllLines(bagInfo);
140        assertThat(bagInfoLines).contains(BagConfig.SOURCE_ORGANIZATION_KEY + ": bagit-support");
141
142        // Assert that extra-tag.txt exists
143        final List<String> extraLines = Files.readAllLines(extra);
144        assertThat(extraLines).contains("test-key: test-value");
145
146        // Finally, pass BagProfile validation and BagIt validation
147        final BagReader reader = new BagReader();
148        final BagVerifier verifier = new BagVerifier();
149        try {
150            final Bag readBag = reader.read(bag);
151            profile.validateBag(readBag);
152            verifier.isValid(readBag, false);
153        } catch (UnparsableVersionException | MaliciousPathException | UnsupportedAlgorithmException |
154            InvalidBagitFileFormatException e) {
155            fail("Unable to read bag:\n" + e.getMessage());
156        } catch (VerificationException | MissingPayloadDirectoryException | MissingPayloadManifestException |
157            FileNotInPayloadDirectoryException | CorruptChecksumException | MissingBagitFileException |
158            InterruptedException e) {
159            fail("Unable to verify bag:\n" + e.getMessage());
160        }
161    }
162}