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("UTF8").newDecoder());
63 assertTrue(strOutput.equals(strInput));
64 }
65 }
66
67 @Test
68 public void testCorruptedData() throws Exception {
69 String strInput = "Hello World";
70 IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
71
72 IoBuffer byteCompressed = deflater.deflate(byteInput);
73
74
75 byteCompressed.put(5, (byte) 0xa);
76 IoBuffer byteUncompressed = inflater.inflate(byteCompressed);
77 String strOutput = byteUncompressed.getString(Charset.forName("UTF8").newDecoder());
78 assertFalse(strOutput.equals(strInput));
79 }
80
81 @Test
82 public void testCorruptedHeader() throws Exception {
83 String strInput = "Hello World";
84 IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
85
86 IoBuffer byteCompressed = deflater.deflate(byteInput);
87
88
89 byteCompressed.put(0, (byte) 0xca);
90 try {
91 inflater.inflate(byteCompressed);
92 } catch (IOException e) {
93 assertTrue(true);
94 return;
95 }
96 assertTrue(false);
97 }
98
99 @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
112
113
114 IoBuffer byteUncompressed = inflater.inflate(byteCompressed);
115 String strOutput = byteUncompressed.getString(Charset.forName("UTF8").newDecoder());
116 assertTrue(strOutput.equals(strInput));
117 }
118 }
119
120
121 IoBuffer byteUncompressed = inflater.inflate(byteCompressed);
122 String strOutput = byteUncompressed.getString(Charset.forName("UTF8").newDecoder());
123 assertTrue(strOutput.equals(strInput));
124 }
125 }