1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.trinidad.util;
20
21 import java.io.IOException;
22 import java.io.StringWriter;
23 import java.io.BufferedWriter;
24 import java.util.zip.GZIPOutputStream;
25
26 import junit.framework.Test;
27 import junit.framework.TestSuite;
28
29 import org.apache.myfaces.trinidad.util.Base64OutputStream;
30 import org.apache.myfaces.trinidadbuild.test.FacesTestCase;
31
32 /**
33 * Unit tests for Base64OutputStream.
34 *
35 */
36 public class Base64OutputStreamTest extends FacesTestCase
37 {
38 /**
39 * Creates a new Base64OutputStreamTest.
40 *
41 * @param testName the unit test name
42 */
43 public Base64OutputStreamTest(String testName)
44 {
45 super(testName);
46 }
47
48 @Override
49 protected void setUp() throws Exception
50 {
51 super.setUp();
52 }
53
54 @Override
55 protected void tearDown() throws Exception
56 {
57 super.tearDown();
58 }
59
60 public static Test suite()
61 {
62 return new TestSuite(Base64OutputStreamTest.class);
63 }
64
65 /**
66 * Tests decoding of stream that contains no trailing padding characters.
67 */
68 public void testNoPaddingChar() throws IOException
69 {
70
71 String str = "abcdefghijklmnopqrstuvwxBase64 Encoding is a popular way to convert the 8bit and the binary to the 7bit for network trans using Socket, and a security method to handle text or file, often used in Authentical Login and Mail Attachment, also stored in text file or database. Most SMTP server will handle the login UserName and Password in this way. 1";
72 String str_encoded = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4QmFzZTY0IEVuY29kaW5nIGlzIGEgcG9wdWxhciB3YXkgdG8gY29udmVydCB0aGUgOGJpdCBhbmQgdGhlIGJpbmFyeSB0byB0aGUgN2JpdCBmb3IgbmV0d29yayB0cmFucyB1c2luZyBTb2NrZXQsIGFuZCBhIHNlY3VyaXR5IG1ldGhvZCB0byBoYW5kbGUgdGV4dCBvciBmaWxlLCBvZnRlbiB1c2VkIGluIEF1dGhlbnRpY2FsIExvZ2luIGFuZCBNYWlsIEF0dGFjaG1lbnQsIGFsc28gc3RvcmVkIGluIHRleHQgZmlsZSBvciBkYXRhYmFzZS4gTW9zdCBTTVRQIHNlcnZlciB3aWxsIGhhbmRsZSB0aGUgbG9naW4gVXNlck5hbWUgYW5kIFBhc3N3b3JkIGluIHRoaXMgd2F5LiAx";
73
74 _testWriteChar(str, str_encoded);
75
76 _testWriteArray(str, str_encoded);
77 }
78
79 /**
80 * Tests decoding of stream that contains exactly one trailing padding character.
81 */
82 public void testOnePaddingChar() throws IOException
83 {
84 String str = "Base64 Encoding is a popular way to convert the 8bit and the binary to the 7bit for network trans using Socket, and a security method to handle text or file, often used in Authentical Login and Mail Attachment, also stored in text file or database. Most SMTP server will handle the login UserName and Password in this way.9";
85 String str_encoded = "QmFzZTY0IEVuY29kaW5nIGlzIGEgcG9wdWxhciB3YXkgdG8gY29udmVydCB0aGUgOGJpdCBhbmQgdGhlIGJpbmFyeSB0byB0aGUgN2JpdCBmb3IgbmV0d29yayB0cmFucyB1c2luZyBTb2NrZXQsIGFuZCBhIHNlY3VyaXR5IG1ldGhvZCB0byBoYW5kbGUgdGV4dCBvciBmaWxlLCBvZnRlbiB1c2VkIGluIEF1dGhlbnRpY2FsIExvZ2luIGFuZCBNYWlsIEF0dGFjaG1lbnQsIGFsc28gc3RvcmVkIGluIHRleHQgZmlsZSBvciBkYXRhYmFzZS4gTW9zdCBTTVRQIHNlcnZlciB3aWxsIGhhbmRsZSB0aGUgbG9naW4gVXNlck5hbWUgYW5kIFBhc3N3b3JkIGluIHRoaXMgd2F5Ljk=";
86
87 _testWriteChar(str, str_encoded);
88
89 _testWriteArray(str, str_encoded);
90
91 }
92
93 /**
94 * Tests decoding of stream that contains exactly two trailing padding characters.
95 */
96 public void testTwoPaddingChars() throws IOException
97 {
98 String str = "Base64 Encoding is a popular way to convert the 8bit and the binary to the 7bit for network trans using Socket, and a security method to handle text or file, often used in Authentical Login and Mail Attachment, also stored in text file or database. Most SMTP server will handle the login UserName and Password in this way.";
99 String str_encoded = "QmFzZTY0IEVuY29kaW5nIGlzIGEgcG9wdWxhciB3YXkgdG8gY29udmVydCB0aGUgOGJpdCBhbmQgdGhlIGJpbmFyeSB0byB0aGUgN2JpdCBmb3IgbmV0d29yayB0cmFucyB1c2luZyBTb2NrZXQsIGFuZCBhIHNlY3VyaXR5IG1ldGhvZCB0byBoYW5kbGUgdGV4dCBvciBmaWxlLCBvZnRlbiB1c2VkIGluIEF1dGhlbnRpY2FsIExvZ2luIGFuZCBNYWlsIEF0dGFjaG1lbnQsIGFsc28gc3RvcmVkIGluIHRleHQgZmlsZSBvciBkYXRhYmFzZS4gTW9zdCBTTVRQIHNlcnZlciB3aWxsIGhhbmRsZSB0aGUgbG9naW4gVXNlck5hbWUgYW5kIFBhc3N3b3JkIGluIHRoaXMgd2F5Lg==";
100
101 _testWriteChar(str, str_encoded);
102
103 _testWriteArray(str, str_encoded);
104
105 }
106
107 /**
108 * Tests decoding of stream by writing out to stream in different intervals to test whether
109 * whehther leftover data is persisted across calls to write
110 */
111 public void testMultipleWrites() throws IOException
112 {
113 String str = "Base64 Encoding is a popular way to convert the 8bit and the binary to the 7bit for network trans using Socket, and a security method to handle text or file, often used in Authentical Login and Mail Attachment, also stored in text file or database. Most SMTP server will handle the login UserName and Password in this way.";
114 String str_encoded = "QmFzZTY0IEVuY29kaW5nIGlzIGEgcG9wdWxhciB3YXkgdG8gY29udmVydCB0aGUgOGJpdCBhbmQgdGhlIGJpbmFyeSB0byB0aGUgN2JpdCBmb3IgbmV0d29yayB0cmFucyB1c2luZyBTb2NrZXQsIGFuZCBhIHNlY3VyaXR5IG1ldGhvZCB0byBoYW5kbGUgdGV4dCBvciBmaWxlLCBvZnRlbiB1c2VkIGluIEF1dGhlbnRpY2FsIExvZ2luIGFuZCBNYWlsIEF0dGFjaG1lbnQsIGFsc28gc3RvcmVkIGluIHRleHQgZmlsZSBvciBkYXRhYmFzZS4gTW9zdCBTTVRQIHNlcnZlciB3aWxsIGhhbmRsZSB0aGUgbG9naW4gVXNlck5hbWUgYW5kIFBhc3N3b3JkIGluIHRoaXMgd2F5Lg==";
115
116
117 StringWriter strwriter = new StringWriter();
118 BufferedWriter buffwriter = new BufferedWriter(strwriter);
119 Base64OutputStream b64_out = new Base64OutputStream(buffwriter);
120
121 byte[] b = str.getBytes();
122
123
124
125 b64_out.write(b, 0, 33);
126 b64_out.write(b, 33, 1);
127 b64_out.write(b, 34, 1);
128 b64_out.write(b, 35, 2);
129 b64_out.write(b, 37, 3);
130 b64_out.write(b, 40, 3);
131 b64_out.write(b, 43, 3);
132 b64_out.write(b, 46, 5);
133 b64_out.write(b, 51, 4);
134 b64_out.write(b, 55, 5);
135 b64_out.write(b, 60, 6);
136 b64_out.write(b, 66, 10);
137 b64_out.write(b, 76, 51);
138 b64_out.write(b, 127, 150);
139 b64_out.write(b, 277, 22);
140 b64_out.write(b, 299, 21);
141 b64_out.write(b, 320, 2);
142
143
144 b64_out.close();
145
146
147 assertEquals(strwriter.toString(), str_encoded);
148 }
149
150
151 /**
152 * Testing writing binary data whose byte values range from -128 to 128.
153 * We create such data by GZIP-ing the string before writing out to the Base64OutputStream.
154 *
155 */
156 public void testWriteBinaryData() throws IOException
157 {
158 String str = "Base64 Encoding is a popular way to convert the 8bit and the binary to the 7bit for network trans using Socket, and a security method to handle text or file, often used in Authentical Login and Mail Attachment, also stored in text file or database. Most SMTP server will handle the login UserName and Password in this way.";
159 String str_encoded = "H4sIAAAAAAAAAD2QQW7DMAwEv7IPCHIK2l4ToLe6CJD2AbREx0JkMpDouvl9KQXokVxylssTVX454F2CxiRXpArCXe9rpoKNHjBFUPnhYrCZ8TYmA0nsxZiESh9p1WuTJi0Qtk3LDVZIKtbauBcNN7ZdXyVUDmtJ9sDCNmtshNmVzDD+NThjSpl30MlYnMARSXBc3UYsBcr40Kt3Gm2glHE0ozAvrrpFropqWp5bndhwDRvJaPTIewxaDZfh6+zHFI+HLeX8f4XHyd3h29VPWrhbnalWT/bEzv4qf9D+DzA2UNlCAQAA";
160
161 byte[] bytes = str.getBytes();
162
163 StringWriter strwriter = new StringWriter();
164 BufferedWriter buffwriter = new BufferedWriter(strwriter);
165 Base64OutputStream b64_out = new Base64OutputStream(buffwriter);
166
167 GZIPOutputStream zip = new GZIPOutputStream(b64_out);
168
169 zip.write(bytes, 0, bytes.length);
170 zip.finish();
171 buffwriter.flush();
172 b64_out.close();
173
174 assertEquals(str_encoded, strwriter.toString());
175
176 }
177
178
179
180
181 /**
182 *
183 * Writes each individual char from str to a Base64OutputStream and compares
184 * the resulting output stream contents with the expected encoded string.
185 *
186 * @param str the decoded string
187 * @param str_encoded the encoded string
188 *
189 **/
190
191 private void _testWriteChar(String str, String str_encoded) throws IOException
192 {
193
194 StringWriter strwriter = new StringWriter();
195 BufferedWriter buffwriter = new BufferedWriter(strwriter);
196 Base64OutputStream b64_out = new Base64OutputStream(buffwriter);
197
198
199 for (int i = 0; i<str.length(); i++)
200 {
201 b64_out.write(str.charAt(i));
202 }
203
204 b64_out.close();
205
206
207 assertEquals(strwriter.toString(), str_encoded);
208 }
209
210 /**
211 *
212 * Writes str (after converting to an array of bytes) to a Base64OutputStream
213 * and compares the resulting output stream contents with the expected encoded
214 * string.
215 *
216 * @param str the decoded string
217 * @param str_encoded the encoded string
218 *
219 **/
220 private void _testWriteArray(String str, String str_encoded) throws IOException
221 {
222
223 StringWriter strwriter = new StringWriter();
224 BufferedWriter buffwriter = new BufferedWriter(strwriter);
225 Base64OutputStream b64_out = new Base64OutputStream(buffwriter);
226
227
228 byte[] b = str.getBytes();
229
230
231 b64_out.write(b, 0, b.length);
232
233 b64_out.close();
234
235
236
237
238
239 assertEquals(strwriter.toString(), str_encoded);
240 }
241 }