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