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.commons.io.IOUtils;
22  import org.apache.rat.document.MockDocument;
23  import org.apache.rat.document.impl.FileDocument;
24  import org.apache.rat.test.utils.Resources;
25  import org.junit.Test;
26  
27  import java.io.IOException;
28  import java.io.Reader;
29  import java.util.Arrays;
30  import java.util.List;
31  
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.assertFalse;
34  import static org.junit.Assert.assertTrue;
35  
36  public class BinaryGuesserTest {
37  
38      private static final List<String> BINARY_FILES = Arrays.asList(//
39              "image.png",//
40              "image.pdf",//
41              "image.psd",//
42              "image.gif",//
43              "image.giff",//
44              "image.jpg",//
45              "image.jpeg",//
46              "image.exe",//
47              "Whatever.class",//
48              "data.dat",//
49              "libicuda.so.34",//
50              "my.truststore",//
51              //"foo.Java", //
52              //"manifest.Mf",//
53              "deprecatedtechnology.swf"
54      );
55  
56      @Test
57      public void testMatches() {
58          for (String name : BINARY_FILES) {
59              assertTrue("'" + name + "' should be detected as a binary", BinaryGuesser.isBinary(new MockDocument(name)));
60          }
61  
62      }
63  
64      @Test
65      public void testIsBinary() {
66          for (String name : BINARY_FILES) {
67              assertTrue("'" + name + "' should be detected as a binary", BinaryGuesser.isBinary(name));
68          }
69      }
70  
71      /**
72       * Used to swallow a MalformedInputException and return false
73       * because the encoding of the stream was different from the
74       * platform's default encoding.
75       *
76       * @throws Exception
77       * @see "RAT-81"
78       */
79      @Test
80      public void binaryWithMalformedInputRAT81() throws Exception {
81          FileDocument doc = new FileDocument(Resources.getResourceFile("/binaries/UTF16_with_signature.xml"));
82          Reader r = doc.reader(); // this will fail test if file is not readable
83          try {
84              char[] dummy = new char[100];
85              r.read(dummy);
86              // if we get here, the UTF-16 encoded file didn't throw
87              // any exception, try the UTF-8 encoded one
88              r.close();
89              r = null; // ensure we detect failure to read second file
90              doc = new FileDocument(Resources.getResourceFile("/binaries/UTF8_with_signature.xml"));
91              r = doc.reader();
92              r.read(dummy);
93              // still here?  can't test on this platform
94              System.err.println("Skipping testBinaryWithMalformedInput");
95          } catch (IOException e) {
96              if (r != null) {
97                  IOUtils.closeQuietly(r);
98              } else {
99                  throw e; // could not open the second file
100             }
101             r = null;
102             assertTrue("Expected binary for " + doc.getName(), BinaryGuesser.isBinary(doc));
103         } finally {
104             IOUtils.closeQuietly(r);
105         }
106     }
107 
108     @Test
109     public void realBinaryContent() throws IOException {
110         // This test is not accurate on all platforms
111         final String encoding = System.getProperty("file.encoding");
112         final boolean isBinary = BinaryGuesser.isBinary(new FileDocument(Resources.getResourceFile("/binaries/Image-png.not")));
113         if (encoding.startsWith("ANSI")) {
114             assertTrue(isBinary);
115         } else {
116             if (isBinary) {
117                 System.out.println("BinaryGuesserTest.realBinaryContent() succeeded when using encoding " + encoding);
118             } else {
119                 System.err.println("BinaryGuesserTest.realBinaryContent() failed when using encoding " + encoding);
120             }
121         }
122     }
123 
124     @Test
125     public void textualContent() throws IOException {
126         assertFalse(BinaryGuesser.isBinary(new FileDocument(Resources.getResourceFile("/elements/Text.txt"))));
127     }
128 
129     @Test
130     public void emptyFile() throws IOException {
131         assertFalse(BinaryGuesser.isBinary(new FileDocument(Resources.getResourceFile("/elements/sub/Empty.txt"))));
132     }
133 
134     @Test
135     public void testFileEncodingCanBeSetAndHasFallbackInCaseOfErrors() {
136         System.setProperty(BinaryGuesser.FILE_ENCODING, "shouldThrowAnExceptionBecauseNotFound");
137         assertEquals("UTF-8", BinaryGuesser.getFileEncodingOrUTF8AsFallback().displayName());
138 
139         final String usAscii = "US-ASCII";
140         System.setProperty(BinaryGuesser.FILE_ENCODING, usAscii);
141         assertEquals(usAscii, BinaryGuesser.getFileEncodingOrUTF8AsFallback().displayName());
142     }
143 }