1 | /**************************************************************** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one * |
---|
3 | * or more contributor license agreements. See the NOTICE file * |
---|
4 | * distributed with this work for additional information * |
---|
5 | * regarding copyright ownership. The ASF licenses this file * |
---|
6 | * to you under the Apache License, Version 2.0 (the * |
---|
7 | * "License"); you may not use this file except in compliance * |
---|
8 | * with the License. You may obtain a copy of the License at * |
---|
9 | * * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 * |
---|
11 | * * |
---|
12 | * Unless required by applicable law or agreed to in writing, * |
---|
13 | * software distributed under the License is distributed on an * |
---|
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
---|
15 | * KIND, either express or implied. See the License for the * |
---|
16 | * specific language governing permissions and limitations * |
---|
17 | * under the License. * |
---|
18 | ****************************************************************/ |
---|
19 | |
---|
20 | package org.apache.james.mime4j.codec; |
---|
21 | |
---|
22 | import org.apache.james.mime4j.ExampleMail; |
---|
23 | |
---|
24 | import java.io.ByteArrayInputStream; |
---|
25 | import java.io.ByteArrayOutputStream; |
---|
26 | import java.io.IOException; |
---|
27 | import java.io.InputStream; |
---|
28 | |
---|
29 | import junit.framework.TestCase; |
---|
30 | |
---|
31 | public class CodecUtilTest extends TestCase { |
---|
32 | |
---|
33 | @Override |
---|
34 | protected void setUp() throws Exception { |
---|
35 | super.setUp(); |
---|
36 | } |
---|
37 | |
---|
38 | @Override |
---|
39 | protected void tearDown() throws Exception { |
---|
40 | super.tearDown(); |
---|
41 | } |
---|
42 | |
---|
43 | public void testCopy() throws Exception { |
---|
44 | byte[] content = ExampleMail.MULTIPART_WITH_BINARY_ATTACHMENTS_BYTES; |
---|
45 | ByteArrayInputStream in = new ByteArrayInputStream(content); |
---|
46 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
---|
47 | CodecUtil.copy(in, out); |
---|
48 | assertEquals(content, out.toByteArray()); |
---|
49 | } |
---|
50 | |
---|
51 | public void testEncodeQuotedPrintableLargeInput() throws Exception { |
---|
52 | StringBuilder sb = new StringBuilder(); |
---|
53 | for (int i = 0; i < 1024 * 5; i++) { |
---|
54 | sb.append((char) ('0' + (i % 10))); |
---|
55 | } |
---|
56 | String expected = sb.toString().replaceAll("(\\d{75})", "$1=\r\n"); |
---|
57 | |
---|
58 | InputStream in = new ByteArrayInputStream(sb.toString().getBytes("US-ASCII")); |
---|
59 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
---|
60 | CodecUtil.encodeQuotedPrintableBinary(in, out); |
---|
61 | String actual = new String(out.toByteArray(), "US-ASCII"); |
---|
62 | assertEquals(expected, actual); |
---|
63 | } |
---|
64 | |
---|
65 | public void testEncodeQuotedPrintableNonAsciiChars() throws Exception { |
---|
66 | String s = "7bit content with euro \u20AC symbol"; |
---|
67 | InputStream in = new ByteArrayInputStream(s.getBytes("iso-8859-15")); |
---|
68 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
---|
69 | CodecUtil.encodeQuotedPrintableBinary(in, out); |
---|
70 | String actual = new String(out.toByteArray(), "US-ASCII"); |
---|
71 | assertEquals("7bit=20content=20with=20euro=20=A4=20symbol", actual); |
---|
72 | } |
---|
73 | |
---|
74 | public void testBase64OutputStream() throws Exception { |
---|
75 | StringBuilder sb = new StringBuilder(2048); |
---|
76 | for (int i = 0; i < 128; i++) { |
---|
77 | sb.append("0123456789ABCDEF"); |
---|
78 | } |
---|
79 | String input = sb.toString(); |
---|
80 | String output = roundtripUsingOutputStream(input); |
---|
81 | assertEquals(input, output); |
---|
82 | } |
---|
83 | |
---|
84 | private String roundtripUsingOutputStream(String input) throws IOException { |
---|
85 | ByteArrayOutputStream out2 = new ByteArrayOutputStream(); |
---|
86 | Base64OutputStream outb64 = new Base64OutputStream(out2, 76); |
---|
87 | CodecUtil.copy(new ByteArrayInputStream(input.getBytes()), outb64); |
---|
88 | outb64.flush(); |
---|
89 | outb64.close(); |
---|
90 | |
---|
91 | InputStream is = new Base64InputStream(new ByteArrayInputStream(out2.toByteArray())); |
---|
92 | ByteArrayOutputStream outRoundtrip = new ByteArrayOutputStream(); |
---|
93 | CodecUtil.copy(is, outRoundtrip); |
---|
94 | String output = new String(outRoundtrip.toByteArray()); |
---|
95 | return output; |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * This test is a proof for MIME4J-67 |
---|
100 | */ |
---|
101 | public void testBase64Encoder() throws Exception { |
---|
102 | StringBuilder sb = new StringBuilder(2048); |
---|
103 | for (int i = 0; i < 128; i++) { |
---|
104 | sb.append("0123456789ABCDEF"); |
---|
105 | } |
---|
106 | String input = sb.toString(); |
---|
107 | String output = roundtripUsingEncoder(input); |
---|
108 | assertEquals(input, output); |
---|
109 | } |
---|
110 | |
---|
111 | private String roundtripUsingEncoder(String input) throws IOException { |
---|
112 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
---|
113 | CodecUtil.encodeBase64(new ByteArrayInputStream(input.getBytes()), out); |
---|
114 | |
---|
115 | InputStream is = new Base64InputStream(new ByteArrayInputStream(out.toByteArray())); |
---|
116 | ByteArrayOutputStream outRoundtrip = new ByteArrayOutputStream(); |
---|
117 | CodecUtil.copy(is, outRoundtrip); |
---|
118 | String output = new String(outRoundtrip.toByteArray()); |
---|
119 | return output; |
---|
120 | } |
---|
121 | |
---|
122 | /* performance test, not a unit test */ |
---|
123 | /* |
---|
124 | public void testPerformance() throws Exception { |
---|
125 | // if (true) return; |
---|
126 | byte[] bytes = new byte[10000]; |
---|
127 | Random r = new Random(432875623874L); |
---|
128 | r.nextBytes(bytes); |
---|
129 | long totalEncoder1 = 0; |
---|
130 | long totalStream1 = 0; |
---|
131 | long totalEncoder2 = 0; |
---|
132 | for (int i = 0; i < 10000; i++) { |
---|
133 | int length = r.nextInt(1000); |
---|
134 | int pos = r.nextInt(9000); |
---|
135 | String input = new String(bytes, pos, length); |
---|
136 | long time1 = System.currentTimeMillis(); |
---|
137 | roundtripUsingEncoder(input); |
---|
138 | long time2 = System.currentTimeMillis(); |
---|
139 | roundtripUsingOutputStream(input); |
---|
140 | long time3 = System.currentTimeMillis(); |
---|
141 | roundtripUsingEncoder(input); |
---|
142 | long time4 = System.currentTimeMillis(); |
---|
143 | |
---|
144 | totalEncoder1 += time2-time1; |
---|
145 | totalStream1 += time3-time2; |
---|
146 | totalEncoder2 += time4-time3; |
---|
147 | } |
---|
148 | |
---|
149 | System.out.println("Encoder 1st: "+totalEncoder1); |
---|
150 | System.out.println("Encoder 2nd: "+totalEncoder2); |
---|
151 | System.out.println("Stream 1st: "+totalStream1); |
---|
152 | } |
---|
153 | */ |
---|
154 | |
---|
155 | private void assertEquals(byte[] expected, byte[] actual) { |
---|
156 | StringBuilder buffer = new StringBuilder(expected.length); |
---|
157 | assertEquals(expected.length, actual.length); |
---|
158 | for (int i = 0; i < actual.length; i++) { |
---|
159 | buffer.append((char)actual[i]); |
---|
160 | assertEquals("Mismatch@" + i, expected[i], actual[i]); |
---|
161 | } |
---|
162 | } |
---|
163 | } |
---|