Skip to content

Commit

Permalink
Merge pull request #112 from openpreserve/test/rules
Browse files Browse the repository at this point in the history
TEST: Unit tests for profile rules
  • Loading branch information
carlwilson authored Nov 30, 2023
2 parents 4890808 + 4fd9779 commit ebb6695
Show file tree
Hide file tree
Showing 23 changed files with 1,189 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class TestFiles {
final static String ENCRYPTED_TEST_ROOT = PKG_TEST_ROOT + "encrypted/";
final static String DSIG_TEST_ROOT = PKG_TEST_ROOT + "dsigs/";
final static String INVALID_PKG_ROOT = PKG_TEST_ROOT + "invalid/";
final static String RULES_ROOT = PKG_TEST_ROOT + "rules/";
final static String XML_TEST_ROOT = TEST_ROOT + "xml/";
final static String FILE_TEST_ROOT = TEST_ROOT + "files/";
public final static URL EMPTY_ODS = ClassLoader.getSystemResource(ZIP_TEST_ROOT + "empty.ods");
Expand Down Expand Up @@ -58,4 +59,10 @@ public class TestFiles {
public final static URL DSIG_VALID = ClassLoader.getSystemResource(DSIG_TEST_ROOT + "dsigs-valid.ods");
public final static URL DSIG_BADNAME = ClassLoader.getSystemResource(DSIG_TEST_ROOT + "bad_dsig_name.ods");
public final static URL MANIFEST_NOT_WF = ClassLoader.getSystemResource(INVALID_PKG_ROOT + "manifest_not_wf.ods");
public final static URL ODF4_BAD_EXT = ClassLoader.getSystemResource(RULES_ROOT + "bad_ext_odf4.ext");
public final static URL ODF4_BAD_MIME = ClassLoader.getSystemResource(RULES_ROOT + "template_sheet_ext_odf4.ods");
public final static URL SCHEMATRON_CHECKER_XML = ClassLoader.getSystemResource(RULES_ROOT + "schematron_checker.xml");
public final static URL SCHEMATRON_CHECKER_ODS = ClassLoader.getSystemResource(RULES_ROOT + "schematron_checker.ods");
public final static URL MACRO_XML = ClassLoader.getSystemResource(RULES_ROOT + "macro.xml");
public final static URL MACRO_PACKAGE = ClassLoader.getSystemResource(RULES_ROOT + "macro.ods");
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,34 @@ public void testFromInputStream() throws ParserConfigurationException, SAXExcept
assertNull("Media type for non existing entry should be null",
manifest.getEntryMediaType("Thumbnails/thumbnail.pngx"));
}

@Test
public void testEncryptedDocument() throws ParserConfigurationException, SAXException, IOException {
InputStream is = ClassLoader.getSystemResourceAsStream("org/openpreservation/odf/fmt/xml/manifest-encrypted.xml");
Manifest manifest = ManifestImpl.from(is);
assertEquals("Manifest version should be 1.3", "1.3", manifest.getVersion());
assertEquals("The file should contain 7 file entries", 7, manifest.getEntryCount());
assertEquals("The file should contain 4 XML file entries", 4,
manifest.getEntriesByMediaType("text/xml").size());
assertEquals("application/vnd.oasis.opendocument.spreadsheet", manifest.getRootMediaType());
assertEquals("Root version should be 1.3", "1.3", manifest.getRootVersion());
manifest.getEntriesByMediaType("text/xml").forEach(entry -> {
assertTrue("Entry should be encrypted", entry.isEncrypted());
});
}

@Test
public void testNotEncryptedDocument() throws ParserConfigurationException, SAXException, IOException {
InputStream is = ClassLoader.getSystemResourceAsStream("org/openpreservation/odf/fmt/xml/manifest.xml");
Manifest manifest = ManifestImpl.from(is);
assertEquals("Manifest version should be 1.3", "1.3", manifest.getVersion());
assertEquals("The file should contain 8 file entries", 8, manifest.getEntryCount());
assertEquals("The file should contain 4 XML file entries", 4,
manifest.getEntriesByMediaType("text/xml").size());
assertEquals("application/vnd.oasis.opendocument.spreadsheet", manifest.getRootMediaType());
assertEquals("Root version should be 1.3", "1.3", manifest.getRootVersion());
manifest.getEntriesByMediaType("text/xml").forEach(entry -> {
assertFalse("Entry should NOT be encrypted", entry.isEncrypted());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import nl.jqno.equalsverifier.EqualsVerifier;

public class ValidationReportTest {

@Test
public void testEqualsContract() {
EqualsVerifier.forClass(ValidationReport.class).verify();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.openpreservation.odf.validation.rules;

import org.junit.Test;

import nl.jqno.equalsverifier.EqualsVerifier;

public class AbstractRuleTest {
@Test
public void testEqualsContract() {
EqualsVerifier.forClass(AbstractRule.class).verify();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.openpreservation.odf.validation.rules;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.nio.file.Paths;
import java.util.List;

import org.junit.Test;
import org.openpreservation.messages.MessageLog;
import org.openpreservation.odf.fmt.TestFiles;
import org.openpreservation.odf.pkg.OdfPackage;
import org.openpreservation.odf.pkg.OdfPackages;
import org.openpreservation.odf.pkg.PackageParser;
import org.openpreservation.odf.validation.Rule;
import org.openpreservation.odf.xml.OdfXmlDocument;

import com.helger.commons.io.resource.URLResource;
import com.helger.schematron.pure.SchematronResourcePure;
import com.helger.schematron.svrl.AbstractSVRLMessage;
import com.helger.schematron.svrl.SVRLHelper;
import com.helger.schematron.svrl.jaxb.SchematronOutputType;

public class ContentTest {
@Test
public void testGetInstance() {
final SchematronResourcePure schematron = ((SchematronRule) Rules.odf7()).schematron;
assertTrue(schematron.isValidSchematron());
}

@Test
public void testCheckNullXmlDoc() {
Rule rule = Rules.odf7();
OdfXmlDocument nullDoc = null;
assertThrows("UnsupportedOperationException expected",
UnsupportedOperationException.class,
() -> {
rule.check(nullDoc);
});
}

@Test
public void testCheckNullPackage() {
Rule rule = Rules.odf7();
OdfPackage nullPkg = null;
assertThrows("NullPointerException expected",
NullPointerException.class,
() -> {
rule.check(nullPkg);
});
}

@Test
public void testSchematronContentFail() throws Exception {
final SchematronResourcePure schematron = ((SchematronRule) Rules.odf7()).schematron;
final SchematronOutputType schResult = schematron.applySchematronValidationToSVRL(new URLResource(ClassLoader.getSystemResource("org/openpreservation/odf/fmt/xml/content.xml")));
assertNotNull(schResult);
List<AbstractSVRLMessage> results = SVRLHelper.getAllFailedAssertionsAndSuccessfulReports(schResult);
assertEquals(1, results.size());
}

@Test
public void testSchematronContentPass() throws Exception {
final SchematronResourcePure schematron = ((SchematronRule) Rules.odf7()).schematron;
final SchematronOutputType schResult = schematron.applySchematronValidationToSVRL(new URLResource(TestFiles.SCHEMATRON_CHECKER_XML));
assertNotNull(schResult);
List<AbstractSVRLMessage> results = SVRLHelper.getAllFailedAssertionsAndSuccessfulReports(schResult);
assertEquals(0, results.size());
}

@Test
public void testPackageContentFail() throws Exception {
final Rule odf7 = Rules.odf7();
PackageParser parser = OdfPackages.getPackageParser();
OdfPackage pkg = parser.parsePackage(Paths.get(new File(TestFiles.EMPTY_ODS.toURI()).getAbsolutePath()));
MessageLog messages = odf7.check(pkg);
assertNotNull(messages);
assertEquals(1, messages.getErrors().size());
assertEquals(1, messages.getMessages().values().stream().filter(m -> m.stream().filter(e -> e.getId().equals("ODF_7")).count() > 0).count());
}

@Test
public void testPackageContentPass() throws Exception {
final Rule odf7 = Rules.odf7();
PackageParser parser = OdfPackages.getPackageParser();
OdfPackage pkg = parser.parsePackage(Paths.get(new File(TestFiles.SCHEMATRON_CHECKER_ODS.toURI()).getAbsolutePath()));
MessageLog messages = odf7.check(pkg);
assertNotNull(messages);
assertEquals(0, messages.getErrors().size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.openpreservation.odf.validation.rules;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Paths;

import org.junit.Test;
import org.openpreservation.messages.MessageLog;
import org.openpreservation.odf.fmt.TestFiles;
import org.openpreservation.odf.pkg.OdfPackage;
import org.openpreservation.odf.pkg.OdfPackages;
import org.openpreservation.odf.pkg.PackageParser;
import org.openpreservation.odf.validation.Rule;
import org.openpreservation.odf.xml.OdfXmlDocument;

import nl.jqno.equalsverifier.EqualsVerifier;

public class DigitalSignaturesRuleTest {
@Test
public void testEqualsContract() {
EqualsVerifier.forClass(DigitalSignaturesRule.class).verify();
}

@Test
public void testGetInstance() {
Rule rule = DigitalSignaturesRule.getInstance();
assertNotNull("Returned Rule should not be null", rule);
}

@Test
public void testCheckNullXmlDoc() {
Rule rule = DigitalSignaturesRule.getInstance();
OdfXmlDocument nullDoc = null;
assertThrows("UnsupportedOperationException expected",
UnsupportedOperationException.class,
() -> {
rule.check(nullDoc);
});
}

@Test
public void testCheckNullPackage() {
Rule rule = DigitalSignaturesRule.getInstance();
OdfPackage nullPkg = null;
assertThrows("NullPointerException expected",
NullPointerException.class,
() -> {
rule.check(nullPkg);
});
}

@Test
public void testCheckValidPackage() throws IOException, URISyntaxException {
PackageParser parser = OdfPackages.getPackageParser();
OdfPackage pkg = parser.parsePackage(Paths.get(new File(TestFiles.EMPTY_ODS.toURI()).getAbsolutePath()));
Rule rule = DigitalSignaturesRule.getInstance();
MessageLog results = rule.check(pkg);
assertFalse("Valid Package should not return errors", results.hasErrors());
}

@Test
public void testCheckNotZipPackage() throws IOException, URISyntaxException {
PackageParser parser = OdfPackages.getPackageParser();
OdfPackage pkg = parser.parsePackage(Paths.get(new File(TestFiles.EMPTY_FODS.toURI()).getAbsolutePath()));
Rule rule = DigitalSignaturesRule.getInstance();
MessageLog results = rule.check(pkg);
assertFalse("Document XML should NOT return errors", results.hasErrors());
}

@Test
public void testCheckNotWellFormedPackage() throws IOException, URISyntaxException {
PackageParser parser = OdfPackages.getPackageParser();
OdfPackage pkg = parser.parsePackage(Paths.get(new File(TestFiles.BADLY_FORMED_PKG.toURI()).getAbsolutePath()));
Rule rule = Rules.odf9();
MessageLog results = rule.check(pkg);
assertFalse("Badly formed package does not contain digital signatures.", results.hasErrors());
}

@Test
public void testCheckInvalidPackage() throws IOException, URISyntaxException {
PackageParser parser = OdfPackages.getPackageParser();
OdfPackage pkg = parser.parsePackage(Paths.get(new File(TestFiles.MIME_EXTRA_ODS.toURI()).getAbsolutePath()));
Rule rule = DigitalSignaturesRule.getInstance();
MessageLog results = rule.check(pkg);
assertFalse("Invalid extra headers for mimetype is OK.", results.hasErrors());
}

@Test
public void testCheckValidDsigPackage() throws IOException, URISyntaxException {
PackageParser parser = OdfPackages.getPackageParser();
OdfPackage pkg = parser.parsePackage(Paths.get(new File(TestFiles.DSIG_VALID.toURI()).getAbsolutePath()));
Rule rule = DigitalSignaturesRule.getInstance();
MessageLog results = rule.check(pkg);
assertTrue("File contains valid digital signatures.", results.hasErrors());
assertEquals(1, results.getMessages().values().stream().filter(m -> m.stream().filter(e -> e.getId().equals("ODF_9")).count() > 0).count());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.openpreservation.odf.validation.rules;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Paths;

import org.junit.Test;
import org.openpreservation.messages.MessageLog;
import org.openpreservation.odf.fmt.TestFiles;
import org.openpreservation.odf.pkg.OdfPackage;
import org.openpreservation.odf.pkg.OdfPackages;
import org.openpreservation.odf.pkg.PackageParser;
import org.openpreservation.odf.validation.Rule;
import org.openpreservation.odf.xml.OdfXmlDocument;

import nl.jqno.equalsverifier.EqualsVerifier;

public class EncryptionRuleTest {
@Test
public void testEqualsContract() {
EqualsVerifier.forClass(EncryptionRule.class).verify();
}

@Test
public void testGetInstance() {
Rule rule = EncryptionRule.getInstance();
assertNotNull("Returned Rule should not be null", rule);
}

@Test
public void testCheckNullXmlDoc() {
Rule rule = EncryptionRule.getInstance();
OdfXmlDocument nullDoc = null;
assertThrows("UnsupportedOperationException expected",
UnsupportedOperationException.class,
() -> {
rule.check(nullDoc);
});
}

@Test
public void testCheckNullPackage() {
Rule rule = EncryptionRule.getInstance();
OdfPackage nullPkg = null;
assertThrows("NullPointerException expected",
NullPointerException.class,
() -> {
rule.check(nullPkg);
});
}

@Test
public void testCheckValidPackage() throws IOException, URISyntaxException {
PackageParser parser = OdfPackages.getPackageParser();
OdfPackage pkg = parser.parsePackage(Paths.get(new File(TestFiles.EMPTY_ODS.toURI()).getAbsolutePath()));
Rule rule = EncryptionRule.getInstance();
MessageLog results = rule.check(pkg);
assertFalse("Valid Package should not return errors", results.hasErrors());
}

@Test
public void testCheckNotZipPackage() throws IOException, URISyntaxException {
PackageParser parser = OdfPackages.getPackageParser();
OdfPackage pkg = parser.parsePackage(Paths.get(new File(TestFiles.EMPTY_FODS.toURI()).getAbsolutePath()));
Rule rule = EncryptionRule.getInstance();
MessageLog results = rule.check(pkg);
assertFalse("Document XML should NOT return errors", results.hasErrors());
}

@Test
public void testCheckNotWellFormedPackage() throws IOException, URISyntaxException {
PackageParser parser = OdfPackages.getPackageParser();
OdfPackage pkg = parser.parsePackage(Paths.get(new File(TestFiles.BADLY_FORMED_PKG.toURI()).getAbsolutePath()));
Rule rule = Rules.odf1();
MessageLog results = rule.check(pkg);
assertFalse("Badly formed package does not contain digital signatures.", results.hasErrors());
}

@Test
public void testCheckInvalidPackage() throws IOException, URISyntaxException {
PackageParser parser = OdfPackages.getPackageParser();
OdfPackage pkg = parser.parsePackage(Paths.get(new File(TestFiles.MIME_EXTRA_ODS.toURI()).getAbsolutePath()));
Rule rule = EncryptionRule.getInstance();
MessageLog results = rule.check(pkg);
assertFalse("Invalid extra headers for mimetype is OK.", results.hasErrors());
}

@Test
public void testCheckValidEncryptedPackage() throws IOException, URISyntaxException {
PackageParser parser = OdfPackages.getPackageParser();
OdfPackage pkg = parser.parsePackage(Paths.get(new File(TestFiles.ENCRYPTED_PASSWORDS.toURI()).getAbsolutePath()));
Rule rule = EncryptionRule.getInstance();
MessageLog results = rule.check(pkg);
assertTrue("File contains valid digital signatures.", results.hasErrors());
assertEquals(5, results.getMessages().values().stream().filter(m -> m.stream().filter(e -> e.getId().equals("ODF_1")).count() > 0).count());
}
}
Loading

0 comments on commit ebb6695

Please sign in to comment.