Skip to content

Commit

Permalink
fix broken OPF_060/061 check for duplicate ZIP entries // fixes #728
Browse files Browse the repository at this point in the history
refs #265
  • Loading branch information
tofi86 committed Dec 24, 2016
1 parent 5f5ef7b commit 05e96f4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
9 changes: 8 additions & 1 deletion src/main/java/com/adobe/epubcheck/ocf/OCFChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,11 @@ else if (validationVersion == EPUBVersion.VERSION_3)
//
try
{
// report duplicate entries
Set<String> entriesSet = new HashSet<String>();
Set<String> normalizedEntriesSet = new HashSet<String>();
for (final String entry : ocf.getFileEntries())
// run duplicate check from the LinkedList which may contain duplicates
for (final String entry : ocf.getEntries())
{
if (!entriesSet.add(entry.toLowerCase(Locale.ENGLISH)))
{
Expand All @@ -319,7 +321,11 @@ else if (!normalizedEntriesSet.add(Normalizer.normalize(entry, Form.NFC)))
{
report.message(MessageId.OPF_061, EPUBLocation.create(ocf.getPackagePath()), entry);
}
}

// check all file entries without duplicates
for (final String entry : ocf.getFileEntries())
{
ocf.reportMetadata(entry, report);

// if the entry is not in the whitelist (META-INF/* + mimetype)
Expand All @@ -345,6 +351,7 @@ public boolean apply(OPFHandler opfHandler)
OCFFilenameChecker.checkCompatiblyEscaped(entry, report, validationVersion);
}

// check all directory entries without duplicates
for (String directory : ocf.getDirectoryEntries())
{
boolean hasContents = false;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/adobe/epubcheck/ocf/OCFPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ public abstract List<String> getEntries()
throws IOException;

/**
* @return a set of relative file names of files in this container
* @return a set of relative file names of files in this container (cleaned from duplicates)
* @throws IOException
*/
public abstract Set<String> getFileEntries()
throws IOException;

/**
* @return a set of relative directory entries in this container
* @return a set of relative directory entries in this container (cleaned from duplicates)
* @throws IOException
*/
public abstract Set<String> getDirectoryEntries()
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/com/adobe/epubcheck/ocf/OCFZipPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.security.MessageDigest;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -150,16 +149,14 @@ public Set<String> getFileEntries() throws
public Set<String> getDirectoryEntries() throws
IOException
{
HashSet<String> entryNames = new HashSet<String>();
for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements(); )
synchronized (zip)
{
ZipEntry entry = entries.nextElement();
if (entry.isDirectory())
if (allEntries == null)
{
entryNames.add(entry.getName());
listEntries();
}
return Collections.unmodifiableSet(dirEntries);
}
return entryNames;
}

public void reportMetadata(String fileName, Report report)
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/adobe/epubcheck/api/Epub30CheckTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,14 @@ public void testFilenameContainsSpacesIssue239()
public void testDuplicateZipEntriesIssue265()
{
// duplicate entries should raise an error
Collections.addAll(expectedErrors, MessageId.OPF_060);
testValidateDocument("invalid/issue265.epub");
}

@Test
public void testDuplicateZipEntriesIssue265b()
{
Collections.addAll(expectedWarnings, MessageId.OPF_003, MessageId.PKG_012, MessageId.OPF_061,
Collections.addAll(expectedWarnings, MessageId.OPF_061, MessageId.OPF_003, MessageId.PKG_012,
MessageId.PKG_012);
// non-unique entry names (after NFC normalization) should raise a warning
testValidateDocument("invalid/issue265b.epub");
Expand Down

0 comments on commit 05e96f4

Please sign in to comment.