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.UnsupportedEncodingException; |
---|
23 | |
---|
24 | import junit.framework.TestCase; |
---|
25 | |
---|
26 | public class DecoderUtilTest extends TestCase { |
---|
27 | |
---|
28 | public void testDecodeB() throws UnsupportedEncodingException { |
---|
29 | String s = DecoderUtil.decodeB("VGhpcyBpcyB0aGUgcGxhaW4gd" |
---|
30 | + "GV4dCBtZXNzYWdlIQ==", "ISO8859-1", DecodeMonitor.STRICT); |
---|
31 | assertEquals("This is the plain text message!", s); |
---|
32 | } |
---|
33 | |
---|
34 | public void testDecodeQ() throws UnsupportedEncodingException { |
---|
35 | String s = DecoderUtil.decodeQ("=e1_=e2=09=E3_=E4_", "ISO8859-1", DecodeMonitor.STRICT); |
---|
36 | assertEquals("\u00e1 \u00e2\t\u00e3 \u00e4 ", s); |
---|
37 | } |
---|
38 | |
---|
39 | public void testNonEncodedWordsAreIgnored() { |
---|
40 | assertEquals("", DecoderUtil.decodeEncodedWords("")); |
---|
41 | assertEquals("Yada yada", DecoderUtil.decodeEncodedWords("Yada yada")); |
---|
42 | } |
---|
43 | |
---|
44 | public void testDecodeSomeEncodedWords() { |
---|
45 | assertEquals(" \u00e1\u00e2\u00e3\t\u00e4", |
---|
46 | DecoderUtil.decodeEncodedWords("=?iso-8859-1?Q?_=20=e1=e2=E3=09=E4?=")); |
---|
47 | assertEquals("Word 1 ' \u00e2\u00e3\t\u00e4'. Word 2 ' \u00e2\u00e3\t\u00e4'", |
---|
48 | DecoderUtil.decodeEncodedWords("Word 1 '=?iso-8859-1?Q?_=20=e2=E3=09=E4?=" |
---|
49 | + "'. Word 2 '=?iso-8859-1?q?_=20=e2=E3=09=E4?='")); |
---|
50 | assertEquals("=?iso-8859-YADA?Q?_=20=t1=e2=E3=09=E4?=", |
---|
51 | DecoderUtil.decodeEncodedWords("=?iso-8859-YADA?Q?_=20=t1=e2=E3=09=E4?=")); |
---|
52 | assertEquals("A short text", |
---|
53 | DecoderUtil.decodeEncodedWords("=?US-ASCII?B?QSBzaG9ydCB0ZXh0?=")); |
---|
54 | assertEquals("A short text again!", |
---|
55 | DecoderUtil.decodeEncodedWords("=?US-ASCII?b?QSBzaG9ydCB0ZXh0IGFnYWluIQ==?=")); |
---|
56 | } |
---|
57 | |
---|
58 | public void testDecodeJapaneseEncodedWords() { |
---|
59 | String enc = "=?ISO-2022-JP?B?GyRCTCQbKEobJEI+NRsoShskQkJ6GyhKGyRCOS0bKEo=?= " |
---|
60 | + "=?ISO-2022-JP?B?GyRCOXAbKEobJEIiKBsoShskQiU1GyhKGyRCJSQbKEo=?= " |
---|
61 | + "=?ISO-2022-JP?B?GyRCJUkbKEobJEIlUxsoShskQiU4GyhKGyRCJU0bKEo=?= " |
---|
62 | + "=?ISO-2022-JP?B?GyRCJTkbKEobJEIkThsoShskQjdoGyhKGyRCRGobKEo=?= " |
---|
63 | + "=?ISO-2022-JP?B?GyRCSEcbKEobJEIkRxsoShskQiQ5GyhKGyRCISobKEo=?="; |
---|
64 | |
---|
65 | String dec = DecoderUtil.decodeEncodedWords(enc); |
---|
66 | assertEquals("\u672A\u627F\u8AFE\u5E83\u544A\u203B\u30B5\u30A4\u30C9\u30D3" |
---|
67 | + "\u30B8\u30CD\u30B9\u306E\u6C7A\u5B9A\u7248\u3067\u3059\uFF01", dec); |
---|
68 | } |
---|
69 | |
---|
70 | public void testInvalidEncodedWordsAreIgnored() { |
---|
71 | assertEquals("=?iso8859-1?Q?=", DecoderUtil.decodeEncodedWords("=?iso8859-1?Q?=")); |
---|
72 | assertEquals("=?iso8859-1?b?=", DecoderUtil.decodeEncodedWords("=?iso8859-1?b?=")); |
---|
73 | assertEquals("=?ISO-8859-1?Q?", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?Q?")); |
---|
74 | assertEquals("=?ISO-8859-1?R?abc?=", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?R?abc?=")); |
---|
75 | assertEquals("test =?ISO-8859-1?R?abc?=", DecoderUtil.decodeEncodedWords("test =?ISO-8859-1?R?abc?=")); |
---|
76 | } |
---|
77 | |
---|
78 | public void testEmptyEncodedTextIsIgnored() { |
---|
79 | // encoded-text requires at least one character according to rfc 2047 |
---|
80 | assertEquals("=?ISO-8859-1?Q??=", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?Q??=")); |
---|
81 | assertEquals("=?ISO-8859-1?B??=", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?B??=")); |
---|
82 | } |
---|
83 | |
---|
84 | // see MIME4J-104 |
---|
85 | public void testWhiteSpaceBetweenEncodedWordsGetsRemoved() { |
---|
86 | assertEquals("a", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?Q?a?=")); |
---|
87 | assertEquals("a b", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?Q?a?= b")); |
---|
88 | assertEquals("ab", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=")); |
---|
89 | assertEquals("ab", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=")); |
---|
90 | assertEquals("ab", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?Q?a?=\r\n =?ISO-8859-1?Q?b?=")); |
---|
91 | assertEquals("a b", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?Q?a_b?=")); |
---|
92 | assertEquals("a b", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?Q?a?= =?ISO-8859-2?Q?_b?=")); |
---|
93 | } |
---|
94 | |
---|
95 | // see MIME4J-138 |
---|
96 | public void testEncodedTextMayStartWithAnEqualsSign() { |
---|
97 | assertEquals(" foo", DecoderUtil.decodeEncodedWords("=?utf-8?Q?=20foo?=")); |
---|
98 | assertEquals("Re: How to place a view at the bottom with a 100% width", |
---|
99 | DecoderUtil.decodeEncodedWords("=?utf-8?Q?Re:=20How=20to=20place=20a=20view=20at=20the=20bottom?= " |
---|
100 | + "=?utf-8?Q?=20with=20a=20100%=20width?=")); |
---|
101 | assertEquals("Test \u00fc and more", |
---|
102 | DecoderUtil.decodeEncodedWords("Test =?ISO-8859-1?Q?=FC_?= =?ISO-8859-1?Q?and_more?=")); |
---|
103 | } |
---|
104 | |
---|
105 | // see MIME4J-142 |
---|
106 | public void testEncodedTextMayContainDollarSign() { |
---|
107 | assertEquals("variable ${target.nl}", DecoderUtil.decodeEncodedWords("=?utf-8?Q?variable=20${target.nl}?=")); |
---|
108 | } |
---|
109 | |
---|
110 | public void testNonWhiteSpaceBetweenEncodedWordsIsRetained() { |
---|
111 | assertEquals("a b c", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?Q?a?= b =?ISO-8859-1?Q?c?=")); |
---|
112 | assertEquals("a\rb\nc", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?Q?a?=\rb\n=?ISO-8859-1?Q?c?=")); |
---|
113 | } |
---|
114 | |
---|
115 | public void testTextBeforeAndAfterEncodedWordIsRetained() { |
---|
116 | assertEquals(" a b c ", DecoderUtil.decodeEncodedWords(" =?ISO-8859-1?Q?a?= b =?ISO-8859-1?Q?c?= ")); |
---|
117 | assertEquals("! a b c !", DecoderUtil.decodeEncodedWords("! =?ISO-8859-1?Q?a?= b =?ISO-8859-1?Q?c?= !")); |
---|
118 | } |
---|
119 | |
---|
120 | public void testFunnyInputDoesNotRaiseOutOfMemoryError() { |
---|
121 | // Bug detected on June 7, 2005. Decoding the following string caused OutOfMemoryError. |
---|
122 | assertEquals("=3?!!\\=?\"!g6P\"!Xp:\"!", DecoderUtil.decodeEncodedWords("=3?!!\\=?\"!g6P\"!Xp:\"!")); |
---|
123 | } |
---|
124 | } |
---|