OSDN Git Service

CLI: Add test option
authorHiroshi Miura <miurahr@linux.com>
Fri, 15 Apr 2016 13:38:48 +0000 (22:38 +0900)
committerHiroshi Miura <miurahr@linux.com>
Fri, 15 Apr 2016 13:39:34 +0000 (22:39 +0900)
Signed-off-by: Hiroshi Miura <miurahr@linux.com>
CHANGELOG.md
dictzip-cli/src/main/java/org/dict/zip/cli/Main.java
dictzip-cli/src/main/resources/org/dict/zip/cli/Bundle.properties
dictzip-lib/src/main/java/org/dict/zip/DictZipFileUtils.java
dictzip-lib/src/main/java/org/dict/zip/DictZipHeader.java
dictzip-lib/src/main/java/org/dict/zip/DictZipInputStream.java
dictzip-lib/src/test/java/org/dict/zip/DictZipFileUtilsTest.java
dictzip-lib/src/test/java/org/dict/zip/DictZipHeaderTest.java

index 863bac3..223c9c1 100644 (file)
@@ -2,6 +2,9 @@
 All notable changes to this project will be documented in this file.
 
 ## [Unreleased]
+### Add
+- Add test option for DictZip CLI.
+
 ### Changed
 - README: there is no test dependency for commons-io.
 
index 1bf85cd..ec3e08c 100644 (file)
@@ -22,6 +22,7 @@
 
 package org.dict.zip.cli;
 
+import org.dict.zip.DictZipFileUtils;
 import org.dict.zip.DictZipHeader.CompressionLevel;
 
 import java.io.File;
@@ -60,6 +61,20 @@ public final class Main {
                     commandLine.options.setKeep(true);
                     dict = new DictData(fName, null);
                     dict.printHeader();
+                } else if (commandLine.options.isTest()) {
+                    boolean result = false;
+                    try {
+                        result = DictZipFileUtils.checkDictZipInputStream(fName);
+                    } catch (IOException e) {
+                        System.err.println(e.getMessage());
+                        System.exit(2);
+                    }
+                    if (result) {
+                        System.exit(0);
+                    } else {
+                        System.err.println(messages.getString("main.test.error"));
+                        System.exit(1);
+                    }
                 } else if (commandLine.options.isDecompress()) {
                     String extractFile = DictZipUtils.uncompressedFileName(fName);
                     long start = commandLine.options.getStart();
index 86a3b51..55cd753 100644 (file)
@@ -78,3 +78,5 @@ main.io.error=File I/O error.
 main.delete.error=File delete failed.
 
 dictzip.header.title=type\tcrc     \tdate    \ttime  \tchunks\tsize\tcompr.\tuncompr.\tratio\tname
+
+main.test.error=File integrity error.
\ No newline at end of file
index 1f9106d..bd6bd36 100644 (file)
@@ -6,7 +6,9 @@ import java.util.Arrays;
 /**
  * Created by Hiroshi Miura on 16/04/09.
  */
-class DictZipFileUtils {
+public class DictZipFileUtils {
+
+
     /**
      * Reads unsigned byte.
      *
@@ -172,6 +174,41 @@ class DictZipFileUtils {
         return result;
     }
 
+    /**
+     * Check gzip member stream w/ CRC and length in trailer.
+     * @throws IOException when CRC error or total length error.
+     */
+    public static boolean checkDictZipInputStream(String fileName) throws IOException {
+        boolean result;
+        try (DictZipInputStream dzin = new DictZipInputStream(new RandomAccessInputStream(fileName, "r"))) {
+            result = checkDictZipInputStream(dzin);
+            dzin.close();
+        }
+        return result;
+    }
+
+    /**
+     * Check gzip member stream w/ CRC and length in trailer.
+     * @throws IOException when CRC error or total length error.
+     */
+    public static boolean checkDictZipInputStream(DictZipInputStream in) throws IOException {
+        final int BUF_LEN = 65536;
+        byte[] tmpBuf = new byte[BUF_LEN];
+        in.seek(0);
+        long readLen = 0;
+        while (readLen < in.getLength()) {
+            int len = in.read(tmpBuf, 0, BUF_LEN);
+            if (len < 0) {
+                break;
+            }
+            readLen += len;
+        }
+        if (readLen != in.getLength()) {
+            return false;
+        }
+        return true;
+    }
+
     private DictZipFileUtils() {
     }
 }
index b19bf65..4410d9d 100644 (file)
@@ -515,6 +515,14 @@ public class DictZipHeader {
     }
 
     /**
+     * Get member length.
+     * @return gzip member length
+     */
+    public long getMemberLength() {
+        return (long) offsets[chunkCount - 1] + chunks[chunkCount - 1] + 8 + 2;
+    }
+
+    /**
      * Get header length.
      * @return header length
      */
index 0cda50f..2729f38 100644 (file)
@@ -82,6 +82,7 @@ public class DictZipInputStream extends InflaterInputStream {
     public DictZipInputStream(final RandomAccessInputStream in, final int size) throws IOException {
         super(in, new Inflater(true), size);
         header = readHeader();
+        readTrailer();
     }
 
     /**
@@ -263,36 +264,10 @@ public class DictZipInputStream extends InflaterInputStream {
     }
 
     /**
-     * Check gzip member trailer; CRC and length.
-     * @throws IOException when CRC error or total length error.
-     */
-    public void checkTrailer() throws IOException {
-        InputStream in = this.in;
-        int num = inf.getRemaining();
-        if (num > 0) {
-            in = new SequenceInputStream(
-                    new ByteArrayInputStream(buf, len - num, num), in);
-        }
-        long val = crc.getValue();
-        long crcValue = DictZipFileUtils.readUInt(in);
-        if (crcValue != val) {
-            throw new IOException(MessageFormat
-                    .format("Incorrect CRC: Computed CRC = %8x / From input %8x", val, crcValue));
-        }
-        long total = inf.getTotalOut();
-        long trailerTotal = DictZipFileUtils.readUInt(in);
-        if (trailerTotal != total) {
-            throw new IOException(MessageFormat
-                    .format("False number of uncompressed bytes: Computed size =%d / From input %d",
-                            total, trailerTotal));
-        }
-    }
-
-    /**
      * Reads GZIP member trailer.
      * @throws java.io.IOException If file I/O error
      */
-    public void readTrailer() throws IOException {
+    void readTrailer() throws IOException {
         if (in instanceof RandomAccessInputStream) {
             RandomAccessInputStream rain = (RandomAccessInputStream) in;
             compLength = rain.getLength();
index 5878e2a..d3ec639 100644 (file)
@@ -3,6 +3,7 @@ package org.dict.zip;
 import org.testng.annotations.Test;
 
 import java.io.File;
+import java.io.IOException;
 
 import static org.testng.Assert.*;
 
@@ -41,4 +42,37 @@ public class DictZipFileUtilsTest {
         File secondFile = new File(this.getClass().getResource("/test_util2.txt").getFile());
         assertFalse(DictZipFileUtils.isFileBinaryEquals(firstFile, secondFile, 0, 64));
     }
+
+    @Test
+    public void testCheckDictZipInputStream_string() throws Exception {
+        System.out.println("checkDictZipInputStream_string");
+        String targetFile = this.getClass().getResource("/test.dict.dz").getFile();
+        assertTrue(DictZipFileUtils.checkDictZipInputStream(targetFile));
+    }
+
+    @Test
+    public void testCheckDictZipInputStream_stringNoExist() throws Exception {
+        System.out.println("checkDictZipInputStream_string");
+        String targetFile = "false.dict.dz";
+        boolean result;
+        try {
+            DictZipFileUtils.checkDictZipInputStream(targetFile);
+            result = false;
+        } catch (IOException e) {
+            // expected.
+            result = true;
+        }
+        assertTrue(result);
+    }
+
+    @Test
+    public void testCheckDictZipInputStream() throws Exception {
+        System.out.println("checkDictZipInputStream");
+        String targetFile = this.getClass().getResource("/test.dict.dz").getFile();
+        try (DictZipInputStream dzin = new DictZipInputStream(new
+                RandomAccessInputStream(targetFile, "r"))) {
+            assertTrue(DictZipFileUtils.checkDictZipInputStream(dzin));
+            dzin.close();
+        }
+    }
 }
\ No newline at end of file
index 1a75b07..2fa4590 100644 (file)
@@ -202,6 +202,17 @@ public class DictZipHeaderTest {
     }
 
     /**
+     * Test of getMemberLength method.
+     */
+    @Test
+    public void testGetMemberLength() throws Exception {
+        System.out.println("getMemberLength");
+        String dataFile = this.getClass().getResource("/test.dict.dz").getFile();
+        DictZipHeader result = DictZipHeader.readHeader(dataFile);
+        assertEquals(result.getMemberLength(), 136856);
+    }
+
+    /**
      * Test of writeHeader method.
      *
      * @throws Exception if file I/O error occurred.