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 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
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
52
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
59
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
75
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
89
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
113
114
115 IoBuffer byteUncompressed = inflater.inflate(byteCompressed);
116 String strOutput = byteUncompressed.getString(Charset.forName("UTF8").newDecoder());
117 assertTrue(strOutput.equals(strInput));
118 }
119 }
120
121
122 IoBuffer byteUncompressed = inflater.inflate(byteCompressed);
123 String strOutput = byteUncompressed.getString(Charset.forName("UTF8").newDecoder());
124 assertTrue(strOutput.equals(strInput));
125 }
126 }