View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   */
19  package org.apache.rat.document.impl.guesser;
20  
21  import org.apache.rat.document.MockDocument;
22  import org.apache.rat.document.impl.FileDocument;
23  import org.junit.Test;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.io.Reader;
28  
29  import static org.junit.Assert.assertFalse;
30  import static org.junit.Assert.assertTrue;
31  
32  public class BinaryGuesserTest {
33  
34      @Test
35      public void testMatches() {
36          assertTrue(BinaryGuesser.isBinary(new MockDocument("image.png")));
37          assertTrue(BinaryGuesser.isBinary(new MockDocument("image.pdf")));
38          assertTrue(BinaryGuesser.isBinary(new MockDocument("image.gif")));
39          assertTrue(BinaryGuesser.isBinary(new MockDocument("image.giff")));
40          assertTrue(BinaryGuesser.isBinary(new MockDocument("image.tif")));
41          assertTrue(BinaryGuesser.isBinary(new MockDocument("image.tiff")));
42          assertTrue(BinaryGuesser.isBinary(new MockDocument("image.jpg")));
43          assertTrue(BinaryGuesser.isBinary(new MockDocument("image.jpeg")));
44          assertTrue(BinaryGuesser.isBinary(new MockDocument("image.exe")));
45          assertTrue(BinaryGuesser.isBinary(new MockDocument("Whatever.class")));
46          assertTrue(BinaryGuesser.isBinary(new MockDocument("data.dat")));
47          assertTrue(BinaryGuesser.isBinary(new MockDocument("libicudata.so.34.")));
48      }
49  
50      public void testIsBinary() {
51          assertTrue(BinaryGuesser.isBinary("image.png"));
52          assertTrue(BinaryGuesser.isBinary("image.pdf"));
53          assertTrue(BinaryGuesser.isBinary("image.gif"));
54          assertTrue(BinaryGuesser.isBinary("image.giff"));
55          assertTrue(BinaryGuesser.isBinary("image.tif"));
56          assertTrue(BinaryGuesser.isBinary("image.tiff"));
57          assertTrue(BinaryGuesser.isBinary("image.jpg"));
58          assertTrue(BinaryGuesser.isBinary("image.jpeg"));
59          assertTrue(BinaryGuesser.isBinary("image.exe"));
60          assertTrue(BinaryGuesser.isBinary("Whatever.class"));
61          assertTrue(BinaryGuesser.isBinary("data.dat"));
62          assertTrue(BinaryGuesser.isBinary("libicudata.so.34."));
63      }
64  
65      /**
66       * Used to swallow a MalformedInputException and return false
67       * because the encoding of the stream was different from the
68       * platform's default encoding.
69       *
70       * @see "RAT-81"
71       */
72      @Test
73      public void binaryWithMalformedInputRAT81() throws Throwable {
74          FileDocument doc = new FileDocument(new File("src/test/resources/binaries/UTF16_with_signature.xml"));
75          Reader r = null;
76          try {
77              char[] dummy = new char[100];
78              r = doc.reader();
79              r.read(dummy);
80              // if we get here, the UTF-16 encoded file didn't throw
81              // any exception, try the UTF-8 encoded one
82              r.close();
83              doc = new FileDocument(new File("src/test/resources/binaries/UTF8_with_signature.xml"));
84              r = doc.reader();
85              r.read(dummy);
86              // still here?  can't test on this platform
87              System.err.println("Skipping testBinaryWithMalformedInput");
88          } catch (IOException e) {
89              if (r!= null) {
90                  r.close();
91              }
92              r = null;
93              assertTrue(BinaryGuesser.isBinary(doc));
94          } finally {
95              if (r != null) {
96                  r.close();
97              }
98          }
99      }
100 
101     @Test
102     public void realBinaryContent() {
103         // This test is not accurate on all platforms
104         if (System.getProperty("file.encoding").startsWith("ANSI")) {
105             assertTrue(BinaryGuesser.isBinary(new FileDocument(new File("src/test/resources/binaries/Image-png.not"))));
106         }
107     }
108 
109     @Test
110     public void textualContent() {
111         assertFalse(BinaryGuesser.isBinary(new FileDocument(new File("src/test/resources/elements/Text.txt"))));
112     }
113 
114     @Test
115     public void emptyFile() {
116         assertFalse(BinaryGuesser.isBinary(new FileDocument(new File("src/test/resources/elements/sub/Empty.txt"))));
117     }
118 }