1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests for the {@link SecureContentHandler} class.
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              // expected
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             // expected
113         }
114     }
115 
116 }