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