[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [metrics-lib/release] Added test and cure for corrupted history file.



commit 3e37f25eb203a24c4b7b9d807a28b1a98a4e323e
Author: iwakeh <iwakeh@xxxxxxxxxxxxxx>
Date:   Tue Dec 13 20:14:44 2016 +0100

    Added test and cure for corrupted history file.
    In that case a warning is logged and parsing continued.
    
    Warning makes sense, as it could be due to problems with the
    file system, which an operator can do something about.
---
 .../descriptor/impl/DescriptorReaderImpl.java        | 20 ++++++++++----------
 .../descriptor/impl/DescriptorReaderImplTest.java    | 18 +++++++++++++++++-
 2 files changed, 27 insertions(+), 11 deletions(-)

diff --git a/src/main/java/org/torproject/descriptor/impl/DescriptorReaderImpl.java b/src/main/java/org/torproject/descriptor/impl/DescriptorReaderImpl.java
index cf2939a..6a3cd64 100644
--- a/src/main/java/org/torproject/descriptor/impl/DescriptorReaderImpl.java
+++ b/src/main/java/org/torproject/descriptor/impl/DescriptorReaderImpl.java
@@ -232,20 +232,20 @@ public class DescriptorReaderImpl implements DescriptorReader {
       try {
         lines = Files.readAllLines(historyFile.toPath(),
             StandardCharsets.UTF_8);
+        for (String line : lines) {
+          if (!line.contains(" ")) {
+            log.warn("Unexpected line structure in old history: {}", line);
+            continue;
+          }
+          long lastModifiedMillis = Long.parseLong(line.substring(0,
+              line.indexOf(" ")));
+          String absolutePath = line.substring(line.indexOf(" ") + 1);
+          this.excludedFilesBefore.put(absolutePath, lastModifiedMillis);
+        }
       } catch (IOException | NumberFormatException e) {
         log.warn("Trouble reading given history file {}.", historyFile, e);
         return;
       }
-      for (String line : lines) {
-        if (!line.contains(" ")) {
-          log.warn("Unexpected line structure in old history: {}", line);
-          continue;
-        }
-        long lastModifiedMillis = Long.parseLong(line.substring(0,
-            line.indexOf(" ")));
-        String absolutePath = line.substring(line.indexOf(" ") + 1);
-        this.excludedFilesBefore.put(absolutePath, lastModifiedMillis);
-      }
     }
 
     private void writeNewHistory(File historyFile) {
diff --git a/src/test/java/org/torproject/descriptor/impl/DescriptorReaderImplTest.java b/src/test/java/org/torproject/descriptor/impl/DescriptorReaderImplTest.java
index d5b1572..c01984d 100644
--- a/src/test/java/org/torproject/descriptor/impl/DescriptorReaderImplTest.java
+++ b/src/test/java/org/torproject/descriptor/impl/DescriptorReaderImplTest.java
@@ -62,12 +62,15 @@ public class DescriptorReaderImplTest {
         parseHistoryContents.getBytes(StandardCharsets.UTF_8));
   }
 
-  private void readAllDescriptors() {
+  private int readAllDescriptors() {
     Iterator<DescriptorFile> descriptorFiles =
         this.descriptorReader.readDescriptors();
+    int count = 0;
     while (descriptorFiles.hasNext()) {
+      count++;
       descriptorFiles.next();
     }
+    return count;
   }
 
   private void assertExcludedFilesParsedFilesAndHistoryFileLines(
@@ -147,5 +150,18 @@ public class DescriptorReaderImplTest {
     this.readAllDescriptors();
     this.assertExcludedFilesParsedFilesAndHistoryFileLines(0, 0, 1);
   }
+
+  @Test
+  public void testSetHistoryCorruptFile() throws IOException {
+    File corruptHistoryFile = this.temporaryFolder.newFile("corruptHistory");
+    Files.write(corruptHistoryFile.toPath(),"1293145200000  \n   ".getBytes());
+    this.descriptorReader.setHistoryFile(corruptHistoryFile);
+    this.descriptorReader.addDirectory(this.inputDirectory);
+    int count = this.readAllDescriptors();
+    assertEquals("Two files should have been parsed.", 2, count);
+    descriptorReader.saveHistoryFile(this.historyFile);
+    this.assertExcludedFilesParsedFilesAndHistoryFileLines(0, 2, 2);
+  }
+
 }
 



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits