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   */
20  package org.apache.mina.filter.compression;
21  
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.io.IOException;
26  import java.nio.charset.Charset;
27  
28  import org.apache.mina.core.buffer.IoBuffer;
29  import org.junit.Before;
30  import org.junit.Test;
31  
32  /**
33   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
34   */
35  public class ZlibTest {
36      private Zlib deflater = null;
37  
38      private Zlib inflater = null;
39  
40      @Before
41      public void setUp() throws Exception {
42          deflater = new Zlib(Zlib.COMPRESSION_MAX, Zlib.MODE_DEFLATER);
43          inflater = new Zlib(Zlib.COMPRESSION_MAX, Zlib.MODE_INFLATER);
44      }
45  
46      @Test
47      public void testCompression() throws Exception {
48          String strInput = "";
49  
50          // increase the count to as many as required to generate a long
51          // string for input
52          for (int i = 0; i < 10; i++) {
53              strInput += "The quick brown fox jumps over the lazy dog.  ";
54          }
55          IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
56  
57          // increase the count to have the compression and decompression
58          // done using the same instance of Zlib
59          for (int i = 0; i < 5; i++) {
60              IoBuffer byteCompressed = deflater.deflate(byteInput);
61              IoBuffer byteUncompressed = inflater.inflate(byteCompressed);
62              String strOutput = byteUncompressed.getString(Charset.forName("UTF8").newDecoder());
63              assertTrue(strOutput.equals(strInput));
64          }
65      }
66  
67      @Test
68      public void testCorruptedData() throws Exception {
69          String strInput = "Hello World";
70          IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
71  
72          IoBuffer byteCompressed = deflater.deflate(byteInput);
73          // change the contents to something else. Since this doesn't check
74          // for integrity, it wont throw an exception
75          byteCompressed.put(5, (byte) 0xa);
76          IoBuffer byteUncompressed = inflater.inflate(byteCompressed);
77          String strOutput = byteUncompressed.getString(Charset.forName("UTF8").newDecoder());
78          assertFalse(strOutput.equals(strInput));
79      }
80  
81      @Test
82      public void testCorruptedHeader() throws Exception {
83          String strInput = "Hello World";
84          IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
85  
86          IoBuffer byteCompressed = deflater.deflate(byteInput);
87          // write a bad value into the zlib header. Make sure that
88          // the decompression fails
89          byteCompressed.put(0, (byte) 0xca);
90          try {
91              inflater.inflate(byteCompressed);
92          } catch (IOException e) {
93              assertTrue(true);
94              return;
95          }
96          assertTrue(false);
97      }
98  
99      @Test
100     public void testFragments() throws Exception {
101         String strInput = "";
102         for (int i = 0; i < 10; i++) {
103             strInput += "The quick brown fox jumps over the lazy dog.  ";
104         }
105         IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
106         IoBuffer byteCompressed = null;
107 
108         for (int i = 0; i < 5; i++) {
109             byteCompressed = deflater.deflate(byteInput);
110             if (i == 0) {
111                 // decompress the first compressed output since it contains
112                 // the zlib header, which will not be generated for further
113                 // compressions done with the same instance
114                 IoBuffer byteUncompressed = inflater.inflate(byteCompressed);
115                 String strOutput = byteUncompressed.getString(Charset.forName("UTF8").newDecoder());
116                 assertTrue(strOutput.equals(strInput));
117             }
118         }
119         // check if the last compressed data block can be decompressed
120         // successfully.
121         IoBuffer byteUncompressed = inflater.inflate(byteCompressed);
122         String strOutput = byteUncompressed.getString(Charset.forName("UTF8").newDecoder());
123         assertTrue(strOutput.equals(strInput));
124     }
125 }