1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
51
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
58
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
75
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
90
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
114
115
116 IoBuffer byteUncompressed = inflater.inflate(byteCompressed);
117 String strOutput = byteUncompressed.getString(Charset.forName(
118 "UTF8").newDecoder());
119 assertTrue(strOutput.equals(strInput));
120 }
121 }
122
123
124 IoBuffer byteUncompressed = inflater.inflate(byteCompressed);
125 String strOutput = byteUncompressed.getString(Charset.forName("UTF8")
126 .newDecoder());
127 assertTrue(strOutput.equals(strInput));
128 }
129 }