Skip to content

Commit

Permalink
refs #92 - check for blank lines in fetch file and throw error if the…
Browse files Browse the repository at this point in the history
…re are any
  • Loading branch information
johnscancella committed Jun 27, 2017
1 parent 2a401dd commit 34e841f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/main/java/gov/loc/repository/bagit/reader/FetchReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
public final class FetchReader {
private static final Logger logger = LoggerFactory.getLogger(FetchReader.class);
private static final ResourceBundle messages = ResourceBundle.getBundle("MessageBundle");
private static final String FETCH_LINE_REGEX = ".*[ \t]*(\\d*|-)[ \t]*.*";

private FetchReader(){
//intentionally left empty
Expand Down Expand Up @@ -51,14 +52,19 @@ public static List<FetchItem> readFetch(final Path fetchFile, final Charset enco
long length = 0;
URL url = null;
while(line != null){
parts = line.split("\\s+", 3);
final Path path = TagFileReader.createFileFromManifest(bagRootDir, parts[2]);
length = parts[1].equals("-") ? -1 : Long.decode(parts[1]);
url = new URL(parts[0]);

logger.debug(messages.getString("read_fetch_file_line"), url, length, parts[2], fetchFile);
final FetchItem itemToFetch = new FetchItem(url, length, path);
itemsToFetch.add(itemToFetch);
if(line.matches(FETCH_LINE_REGEX) && !line.matches("\\s*")){
parts = line.split("\\s+", 3);
final Path path = TagFileReader.createFileFromManifest(bagRootDir, parts[2]);
length = parts[1].equals("-") ? -1 : Long.decode(parts[1]);
url = new URL(parts[0]);

logger.debug(messages.getString("read_fetch_file_line"), url, length, parts[2], fetchFile);
final FetchItem itemToFetch = new FetchItem(url, length, path);
itemsToFetch.add(itemToFetch);
}
else{
throw new InvalidBagitFileFormatException(messages.getString("invalid_fetch_file_line_error").replace("{}", line));
}

line = reader.readLine();
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/MessageBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ unparsable_version_error=Version must be in format MAJOR.MINOR but was [{}]!
#for FetchReader.java
reading_fetch_file=Attempting to read [{}].
read_fetch_file_line=Read URL [{}] length [{}] path [{}] from fetch file [{}].
invalid_fetch_file_line_error=The Line [{}] is invalid for fetch.txt. Each line must take the form of <URL> <LENGTH> <FILENAM>.

This comment has been minimized.

Copy link
@rvanheest

rvanheest Jun 27, 2017

Line line?
FILENAM FILENAME?


#for KeyValueReader.java
read_key_value_line=Found key [{}] value [{}] in file [{}] using split regex [{}].
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/gov/loc/repository/bagit/reader/FetchReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ public void testReadFetchWithSizeSpecified() throws Exception{
}
}

@Test(expected=InvalidBagitFileFormatException.class)
public void testReadBlankLinesThrowsException() throws Exception{
Path fetchFile = Paths.get(getClass().getClassLoader().getResource("fetchFiles/fetchWithBlankLines.txt").toURI());
FetchReader.readFetch(fetchFile, StandardCharsets.UTF_8, Paths.get("/foo"));
}

@Test(expected=InvalidBagitFileFormatException.class)
public void testReadWindowsSpecialDirMaliciousFetchThrowsException() throws Exception{
Path fetchFile = Paths.get(getClass().getClassLoader().getResource("maliciousFetchFile/windowsSpecialDirectoryName.txt").toURI());
Expand All @@ -92,4 +98,16 @@ public void testReadFileUrlMaliciousFetchThrowsException() throws Exception{
}
throw new MaliciousPathException("Skipping for windows cause it isn't valid");
}

@Test
public void foo(){
String regex = ".*[ \t]*(\\d*|-)[ \t]*.*";
String test1 = "http://localhost/foo/data/test2.txt - ~/foo/bar/ham.txt";
String test2 = "http://localhost/foo/data/dir1/test3.txt 100057 data/dir1/test3.txt";
String test3 = "http://localhost/foo/data/dir1/test3.txt \t 100057 \t data/dir1/test3.txt";

System.err.println(test1.matches(regex));
System.err.println(test2.matches(regex));
System.err.println(test3.matches(regex));
}
}
7 changes: 7 additions & 0 deletions src/test/resources/fetchFiles/fetchWithBlankLines.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
http://localhost/foo/data/dir1/test3.txt 1 data/dir1/test3.txt
http://localhost/foo/data/dir2/dir3/test5.txt 2 data/dir2/dir3/test5.txt
http://localhost/foo/data/dir2/test4.txt 3 data/dir2/test4.txt
http://localhost/foo/data/test%201.txt 4 data/test 1.txt
http://localhost/foo/data/test2.txt 5 data/test2.txt


0 comments on commit 34e841f

Please sign in to comment.