[6785] | 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 java.io.ByteArrayInputStream; |
---|
| 23 | import java.io.ByteArrayOutputStream; |
---|
| 24 | import java.io.InputStream; |
---|
| 25 | import java.nio.charset.Charset; |
---|
| 26 | import java.util.Arrays; |
---|
| 27 | |
---|
| 28 | import junit.framework.TestCase; |
---|
| 29 | |
---|
| 30 | import org.apache.commons.io.IOUtils; |
---|
| 31 | import org.apache.james.mime4j.util.CharsetUtil; |
---|
| 32 | |
---|
| 33 | public class QuotedPrintableEncodeTest extends TestCase { |
---|
| 34 | |
---|
| 35 | @Override |
---|
| 36 | protected void setUp() throws Exception { |
---|
| 37 | super.setUp(); |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | @Override |
---|
| 41 | protected void tearDown() throws Exception { |
---|
| 42 | super.tearDown(); |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | public void testEscapedSoftBreak() throws Exception { |
---|
| 46 | byte[] content = new byte[500]; |
---|
| 47 | Arrays.fill(content, (byte)0x20); |
---|
| 48 | byte[] expected = new byte[1557]; |
---|
| 49 | int index = 0; |
---|
| 50 | for (int l=0;l<20;l++) { |
---|
| 51 | for (int i=0;i<25;i++) { |
---|
| 52 | expected[index++] = '='; |
---|
| 53 | expected[index++] = '2'; |
---|
| 54 | expected[index++] = '0'; |
---|
| 55 | } |
---|
| 56 | if (l<19) { |
---|
| 57 | expected[index++] = '='; |
---|
| 58 | expected[index++] = '\r'; |
---|
| 59 | expected[index++] = '\n'; |
---|
| 60 | } |
---|
| 61 | } |
---|
| 62 | check(content, expected); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | public void testPlainAsciiSoftBreak() throws Exception { |
---|
| 66 | byte[] content = new byte[500]; |
---|
| 67 | Arrays.fill(content, (byte)0x29); |
---|
| 68 | byte[] expected = new byte[518]; |
---|
| 69 | Arrays.fill(expected, (byte)0x29); |
---|
| 70 | expected[75] = '='; |
---|
| 71 | expected[76] = '\r'; |
---|
| 72 | expected[77] = '\n'; |
---|
| 73 | expected[153] = '='; |
---|
| 74 | expected[154] = '\r'; |
---|
| 75 | expected[155] = '\n'; |
---|
| 76 | expected[231] = '='; |
---|
| 77 | expected[232] = '\r'; |
---|
| 78 | expected[233] = '\n'; |
---|
| 79 | expected[309] = '='; |
---|
| 80 | expected[310] = '\r'; |
---|
| 81 | expected[311] = '\n'; |
---|
| 82 | expected[387] = '='; |
---|
| 83 | expected[388] = '\r'; |
---|
| 84 | expected[389] = '\n'; |
---|
| 85 | expected[465] = '='; |
---|
| 86 | expected[466] = '\r'; |
---|
| 87 | expected[467] = '\n'; |
---|
| 88 | check(content, expected); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | public void testPlainASCII() throws Exception { |
---|
| 92 | checkRoundtrip("Thisisaverysimplemessage.Thisisaverysimplemessage.Thisisaverysimplemessage." + |
---|
| 93 | "Thisisaverysimplemessage.Thisisaverysimplemessage.Thisisaverysimplemessage." + |
---|
| 94 | "Thisisaverysimplemessage.Thisisaverysimplemessage.Thisisaverysimplemessage." + |
---|
| 95 | "Thisisaverysimplemessage.Thisisaverysimplemessage.Thisisaverysimplemessage."); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | public void testEncodeSpace() throws Exception { |
---|
| 99 | checkRoundtrip(" "); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | public void testLetterEncoding() throws Exception { |
---|
| 103 | for (byte b=0;b<Byte.MAX_VALUE;b++) { |
---|
| 104 | byte[] content = {b}; |
---|
| 105 | checkRoundtrip(content); |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | private void checkRoundtrip(String content) throws Exception { |
---|
| 110 | checkRoundtrip(content, CharsetUtil.US_ASCII); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | private void checkRoundtrip(String content, Charset charset) throws Exception { |
---|
| 114 | checkRoundtrip(charset.encode(content).array()); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | private void checkRoundtrip(byte[] content) throws Exception { |
---|
| 118 | InputStream in = new ByteArrayInputStream(content); |
---|
| 119 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
---|
| 120 | CodecUtil.encodeQuotedPrintableBinary(in, out); |
---|
| 121 | // read back through decoder |
---|
| 122 | in = new QuotedPrintableInputStream(new ByteArrayInputStream(out.toByteArray())); |
---|
| 123 | out = new ByteArrayOutputStream(); |
---|
| 124 | IOUtils.copy(in, out); |
---|
| 125 | assertEquals(content, out.toByteArray()); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | private void check(byte[] content, byte[] expected) throws Exception { |
---|
| 129 | ByteArrayInputStream in = new ByteArrayInputStream(content); |
---|
| 130 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
---|
| 131 | CodecUtil.encodeQuotedPrintableBinary(in, out); |
---|
| 132 | assertEquals(expected, out.toByteArray()); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | private void assertEquals(byte[] expected, byte[] actual) { |
---|
| 136 | assertEquals(expected.length, actual.length); |
---|
| 137 | for (int i = 0; i < actual.length; i++) { |
---|
| 138 | assertEquals("Mismatch@" + i, expected[i], actual[i]); |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | } |
---|