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