001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.filter.compression;
021
022import static org.junit.Assert.assertFalse;
023import static org.junit.Assert.assertTrue;
024
025import java.io.IOException;
026import java.nio.charset.Charset;
027
028import org.apache.mina.core.buffer.IoBuffer;
029import org.junit.Before;
030import org.junit.Test;
031
032/**
033 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
034 */
035public class ZlibTest {
036    private Zlib deflater = null;
037
038    private Zlib inflater = null;
039
040    @Before
041    public void setUp() throws Exception {
042        deflater = new Zlib(Zlib.COMPRESSION_MAX, Zlib.MODE_DEFLATER);
043        inflater = new Zlib(Zlib.COMPRESSION_MAX, Zlib.MODE_INFLATER);
044    }
045
046    @Test
047    public void testCompression() throws Exception {
048        String strInput = "";
049
050        // increase the count to as many as required to generate a long
051        // string for input
052        for (int i = 0; i < 10; i++) {
053            strInput += "The quick brown fox jumps over the lazy dog.  ";
054        }
055        IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
056
057        // increase the count to have the compression and decompression
058        // done using the same instance of Zlib
059        for (int i = 0; i < 5; i++) {
060            IoBuffer byteCompressed = deflater.deflate(byteInput);
061            IoBuffer byteUncompressed = inflater.inflate(byteCompressed);
062            String strOutput = byteUncompressed.getString(Charset.forName("UTF8").newDecoder());
063            assertTrue(strOutput.equals(strInput));
064        }
065    }
066
067    @Test
068    public void testCorruptedData() throws Exception {
069        String strInput = "Hello World";
070        IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
071
072        IoBuffer byteCompressed = deflater.deflate(byteInput);
073        // change the contents to something else. Since this doesn't check
074        // for integrity, it wont throw an exception
075        byteCompressed.put(5, (byte) 0xa);
076        IoBuffer byteUncompressed = inflater.inflate(byteCompressed);
077        String strOutput = byteUncompressed.getString(Charset.forName("UTF8").newDecoder());
078        assertFalse(strOutput.equals(strInput));
079    }
080
081    @Test
082    public void testCorruptedHeader() throws Exception {
083        String strInput = "Hello World";
084        IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
085
086        IoBuffer byteCompressed = deflater.deflate(byteInput);
087        // write a bad value into the zlib header. Make sure that
088        // the decompression fails
089        byteCompressed.put(0, (byte) 0xca);
090        try {
091            inflater.inflate(byteCompressed);
092        } catch (IOException e) {
093            assertTrue(true);
094            return;
095        }
096        assertTrue(false);
097    }
098
099    @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}