1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
52
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
73
74
75
76
77
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();
83 try {
84 char[] dummy = new char[100];
85 r.read(dummy);
86
87
88 r.close();
89 r = null;
90 doc = new FileDocument(Resources.getResourceFile("/binaries/UTF8_with_signature.xml"));
91 r = doc.reader();
92 r.read(dummy);
93
94 System.err.println("Skipping testBinaryWithMalformedInput");
95 } catch (IOException e) {
96 if (r != null) {
97 IOUtils.closeQuietly(r);
98 } else {
99 throw e;
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
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 }