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(
63                      "UTF8").newDecoder());
64              assertTrue(strOutput.equals(strInput));
65          }
66      }
67  
68      @Test
69      public void testCorruptedData() throws Exception {
70          String strInput = "Hello World";
71          IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
72  
73          IoBuffer byteCompressed = deflater.deflate(byteInput);
74          // change the contents to something else. Since this doesn't check
75          // for integrity, it wont throw an exception
76          byteCompressed.put(5, (byte) 0xa);
77          IoBuffer byteUncompressed = inflater.inflate(byteCompressed);
78          String strOutput = byteUncompressed.getString(Charset.forName("UTF8")
79                  .newDecoder());
80          assertFalse(strOutput.equals(strInput));
81      }
82  
83      @Test
84      public void testCorruptedHeader() throws Exception {
85          String strInput = "Hello World";
86          IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
87  
88          IoBuffer byteCompressed = deflater.deflate(byteInput);
89          // write a bad value into the zlib header. Make sure that
90          // the decompression fails
91          byteCompressed.put(0, (byte) 0xca);
92          try {
93              inflater.inflate(byteCompressed);
94          } catch (IOException e) {
95              assertTrue(true);
96              return;
97          }
98          assertTrue(false);
99      }
100 
101     @Test
102     public void testFragments() throws Exception {
103         String strInput = "";
104         for (int i = 0; i < 10; i++) {
105             strInput += "The quick brown fox jumps over the lazy dog.  ";
106         }
107         IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
108         IoBuffer byteCompressed = null;
109 
110         for (int i = 0; i < 5; i++) {
111             byteCompressed = deflater.deflate(byteInput);
112             if (i == 0) {
113                 // decompress the first compressed output since it contains
114                 // the zlib header, which will not be generated for further
115                 // compressions done with the same instance
116                 IoBuffer byteUncompressed = inflater.inflate(byteCompressed);
117                 String strOutput = byteUncompressed.getString(Charset.forName(
118                         "UTF8").newDecoder());
119                 assertTrue(strOutput.equals(strInput));
120             }
121         }
122         // check if the last compressed data block can be decompressed
123         // successfully.
124         IoBuffer byteUncompressed = inflater.inflate(byteCompressed);
125         String strOutput = byteUncompressed.getString(Charset.forName("UTF8")
126                 .newDecoder());
127         assertTrue(strOutput.equals(strInput));
128     }
129 }