1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.tika.sax;
18
19 import java.io.IOException;
20
21 import org.apache.tika.io.CountingInputStream;
22 import org.apache.tika.io.NullInputStream;
23 import org.xml.sax.SAXException;
24 import org.xml.sax.helpers.DefaultHandler;
25
26 import junit.framework.TestCase;
27
28
29
30
31 public class SecureContentHandlerTest extends TestCase {
32
33 private static final int MANY_BYTES = 2000000;
34
35 private CountingInputStream stream;
36
37 private SecureContentHandler handler;
38
39 protected void setUp() {
40 stream = new CountingInputStream(new NullInputStream(MANY_BYTES));
41 handler = new SecureContentHandler(new DefaultHandler(), stream);
42 }
43
44 public void testZeroCharactersPerByte() throws IOException {
45 try {
46 char[] ch = new char[] { 'x' };
47 for (int i = 0; i < MANY_BYTES; i++) {
48 stream.read();
49 }
50 handler.characters(ch, 0, 1);
51 } catch (SAXException e) {
52 fail("Unexpected SAXException");
53 }
54 }
55
56 public void testOneCharacterPerByte() throws IOException {
57 try {
58 char[] ch = new char[1];
59 for (int i = 0; i < MANY_BYTES; i++) {
60 stream.read();
61 handler.characters(ch, 0, ch.length);
62 }
63 } catch (SAXException e) {
64 fail("Unexpected SAXException");
65 }
66 }
67
68 public void testTenCharactersPerByte() throws IOException {
69 try {
70 char[] ch = new char[10];
71 for (int i = 0; i < MANY_BYTES; i++) {
72 stream.read();
73 handler.characters(ch, 0, ch.length);
74 }
75 } catch (SAXException e) {
76 fail("Unexpected SAXException");
77 }
78 }
79
80 public void testManyCharactersPerByte() throws IOException {
81 try {
82 char[] ch = new char[1000];
83 for (int i = 0; i < MANY_BYTES; i++) {
84 stream.read();
85 handler.characters(ch, 0, ch.length);
86 }
87 fail("Expected SAXException not thrown");
88 } catch (SAXException e) {
89
90 }
91 }
92
93 public void testSomeCharactersWithoutInput() throws IOException {
94 try {
95 char[] ch = new char[100];
96 for (int i = 0; i < 100; i++) {
97 handler.characters(ch, 0, ch.length);
98 }
99 } catch (SAXException e) {
100 fail("Unexpected SAXException");
101 }
102 }
103
104 public void testManyCharactersWithoutInput() throws IOException {
105 try {
106 char[] ch = new char[100];
107 for (int i = 0; i < 20000; i++) {
108 handler.characters(ch, 0, ch.length);
109 }
110 fail("Expected SAXException not thrown");
111 } catch (SAXException e) {
112
113 }
114 }
115
116 }