2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
20
package org.apache.xmlgraphics.util.io;
23
import java.io.IOException;
24
import java.io.InputStream;
25
import java.io.OutputStream;
26
import java.io.PipedInputStream;
27
import java.io.PipedOutputStream;
30
import org.junit.Test;
32
import static org.junit.Assert.fail;
35
* This test validates that the Base64 encoder/decoders work properly.
39
public class Base64TestCase {
41
private void innerBase64Test(String action, URL in, URL ref) throws Exception {
42
InputStream inIS = in.openStream();
44
if (action.equals("ROUND")) {
46
} else if (!action.equals("ENCODE") && !action.equals("DECODE")) {
47
fail("Bad action string");
50
InputStream refIS = ref.openStream();
52
if (action.equals("ENCODE") || action.equals("ROUND")) {
53
// We need to encode the incomming data
54
PipedOutputStream pos = new PipedOutputStream();
55
OutputStream os = new Base64EncodeStream(pos);
57
// Copy the input to the Base64 Encoder (in a seperate thread).
58
Thread t = new StreamCopier(inIS, os);
60
// Read that from the piped output stream.
61
inIS = new PipedInputStream(pos);
65
if (action.equals("DECODE") || action.equals("ROUND")) {
66
inIS = new Base64DecodeStream(inIS);
70
int mismatch = compareStreams(inIS, refIS, action.equals("ENCODE"));
77
private void innerBase64Test(String action, String in, String ref) throws Exception {
78
final String baseURL = "file:test/resources/org/apache/xmlgraphics/util/io/";
79
innerBase64Test(action, new URL(baseURL + in), new URL(baseURL + ref));
82
private void innerBase64Test(String in) throws Exception {
83
innerBase64Test("ROUND", in, in);
86
private void testBase64Group(String name) throws Exception {
87
innerBase64Test("ENCODE", name, name + ".64");
88
innerBase64Test("DECODE", name + ".64", name);
89
innerBase64Test(name);
93
* This method will only throw exceptions if some aspect
94
* of the test's internal operation fails.
97
public void testBase64() throws Exception {
98
System.out.println(new File(".").getCanonicalPath());
99
testBase64Group("zeroByte");
100
testBase64Group("oneByte");
101
testBase64Group("twoByte");
102
testBase64Group("threeByte");
103
testBase64Group("fourByte");
104
testBase64Group("tenByte");
105
testBase64Group("small");
106
testBase64Group("medium");
107
innerBase64Test("DECODE", "medium.pc.64", "medium");
108
innerBase64Test("large");
112
* Returns true if the contents of <tt>is1</tt> match the
113
* contents of <tt>is2</tt>
115
public static int compareStreams(InputStream is1, InputStream is2,
117
byte[] data1 = new byte[100];
118
byte[] data2 = new byte[100];
125
int len1 = is1.read(data1, off1, data1.length - off1);
126
int len2 = is2.read(data2, off2, data2.length - off2);
146
break; // Both done...
148
// Only is1 is done...
152
// check if the rest of is2 is whitespace...
153
for (int i2 = 0; i2 < len2; i2++) {
154
if ((data2[i2] != '\n') && (data2[i2] != '\r') && (data2[i2] != ' ')) {
163
// Only is2 is done...
168
// Check if rest of is1 is whitespace...
169
for (int i1 = 0; i1 < len1; i1++) {
170
if ((data1[i1] != '\n') && (data1[i1] != '\r') && (data1[i1] != ' ')) {
180
while ((i1 < len1) && (i2 < len2)) {
182
if ((data1[i1] == '\n') || (data1[i1] == '\r') || (data1[i1] == ' ')) {
186
if ((data2[i2] == '\n') || (data2[i2] == '\r') || (data2[i2] == ' ')) {
191
if (data1[i1] != data2[i2]) {
200
System.arraycopy(data1, i1, data1, 0, len1 - i1);
203
System.arraycopy(data2, i2, data2, 0, len2 - i2);
209
} catch (IOException ioe) {
210
ioe.printStackTrace();
218
static class StreamCopier extends Thread {
222
public StreamCopier(InputStream src,
230
byte[] data = new byte[1000];
232
int len = src.read(data, 0, data.length);
237
dst.write(data, 0, len);
239
} catch (IOException ioe) {
244
} catch (IOException ioe) {