View Javadoc
1   /*
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
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
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.
16   */
17  
18  package org.apache.commons.codec.net;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.nio.charset.StandardCharsets;
27  import java.nio.charset.UnsupportedCharsetException;
28  import java.util.stream.Stream;
29  
30  import org.apache.commons.codec.CharEncoding;
31  import org.apache.commons.codec.CodecPolicy;
32  import org.apache.commons.codec.DecoderException;
33  import org.apache.commons.codec.EncoderException;
34  import org.junit.jupiter.api.Named;
35  import org.junit.jupiter.api.Test;
36  import org.junit.jupiter.params.ParameterizedTest;
37  import org.junit.jupiter.params.provider.Arguments;
38  import org.junit.jupiter.params.provider.EnumSource;
39  import org.junit.jupiter.params.provider.MethodSource;
40  
41  /**
42   * Quoted-printable codec test cases
43   */
44  class BCodecTest {
45      private static final String[] BASE64_IMPOSSIBLE_CASES = {
46              // Require the RFC 1522 "encoded-word" header
47              "=?ASCII?B?ZE==?=",
48              "=?ASCII?B?ZmC=?=",
49              "=?ASCII?B?Zm9vYE==?=",
50              "=?ASCII?B?Zm9vYmC=?=",
51              "=?ASCII?B?AB==?="
52      };
53  
54      static final int[] SWISS_GERMAN_STUFF_UNICODE =
55          { 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4 };
56  
57      static final int[] RUSSIAN_STUFF_UNICODE =
58          { 0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438, 0x432, 0x435, 0x442 };
59  
60      static Stream<String> base64ImpossibleSamples() {
61          return Stream.of(BASE64_IMPOSSIBLE_CASES);
62      }
63  
64      static Stream<Arguments> embeddedQuestionMarks() {
65          return Stream.of("=?UTF-8?B?QUJD?REVG?=", "=?UTF-8?B?QQ==??=", "=?UTF-8?B???=")
66                  .flatMap(encoded -> Stream.of(
67                          Arguments.of(Named.of("default", new BCodec()), encoded),
68                          Arguments.of(Named.of("lenient", new BCodec(StandardCharsets.UTF_8, CodecPolicy.LENIENT)), encoded),
69                          Arguments.of(Named.of("strict", new BCodec(StandardCharsets.UTF_8, CodecPolicy.STRICT)), encoded)));
70      }
71  
72      private String constructString(final int[] unicodeChars) {
73          final StringBuilder buffer = new StringBuilder();
74          if (unicodeChars != null) {
75              for (final int unicodeChar : unicodeChars) {
76                  buffer.append((char) unicodeChar);
77              }
78          }
79          return buffer.toString();
80      }
81  
82      @ParameterizedTest(name = "{0}")
83      @MethodSource("base64ImpossibleSamples")
84      void testBase64ImpossibleSamplesDefault(final String encoded) throws DecoderException {
85          final BCodec codec = new BCodec();
86          assertFalse(codec.isStrictDecoding());
87          codec.decode(encoded);
88      }
89  
90      @ParameterizedTest(name = "{0}")
91      @MethodSource("base64ImpossibleSamples")
92      void testBase64ImpossibleSamplesLenient(final String encoded) throws DecoderException {
93          final BCodec codec = new BCodec(StandardCharsets.UTF_8, CodecPolicy.LENIENT);
94          assertFalse(codec.isStrictDecoding());
95          codec.decode(encoded);
96      }
97  
98      @ParameterizedTest(name = "{0}")
99      @MethodSource("base64ImpossibleSamples")
100     void testBase64ImpossibleSamplesStrict(final String encoded) {
101         final BCodec codec = new BCodec(StandardCharsets.UTF_8, CodecPolicy.STRICT);
102         assertTrue(codec.isStrictDecoding());
103         assertThrows(DecoderException.class, () -> codec.decode(encoded));
104     }
105 
106     @Test
107     void testBasicEncodeDecode() throws Exception {
108         final BCodec bcodec = new BCodec();
109         final String plain = "Hello there";
110         final String encoded = bcodec.encode(plain);
111         assertEquals("=?UTF-8?B?SGVsbG8gdGhlcmU=?=", encoded, "Basic B encoding test");
112         assertEquals(plain, bcodec.decode(encoded), "Basic B decoding test");
113     }
114 
115     @ParameterizedTest(name = "{0}: {1}")
116     @MethodSource("embeddedQuestionMarks")
117     void testDecodeEmbeddedQuestionMark(final BCodec codec, final String encoded) {
118         assertThrows(DecoderException.class, () -> codec.decode(encoded));
119     }
120 
121     @ParameterizedTest
122     @EnumSource(CodecPolicy.class)
123     void testDecodeEncodedQuestionMark(final CodecPolicy policy) throws DecoderException {
124         final BCodec codec = new BCodec(StandardCharsets.UTF_8, policy);
125         assertEquals("ABC?DEF", codec.decode("=?UTF-8?B?QUJDP0RFRg==?="));
126     }
127 
128     @Test
129     void testDecodeObjects() throws Exception {
130         final BCodec bcodec = new BCodec();
131         final String decoded = "=?UTF-8?B?d2hhdCBub3Q=?=";
132         final String plain = (String) bcodec.decode((Object) decoded);
133         assertEquals("what not", plain, "Basic B decoding test");
134         final Object result = bcodec.decode((Object) null);
135         assertNull(result, "Decoding a null Object should return null");
136 
137         assertThrows(DecoderException.class, () -> bcodec.decode(Double.valueOf(3.0d)));
138     }
139 
140     @Test
141     void testDecodeSeparatorNotFoundThrowsDecoderException() {
142         assertThrows(DecoderException.class, () -> new BCodec().decode("=?="));
143     }
144 
145     @Test
146     void testDecodeStringWithNull() throws Exception {
147         final BCodec bcodec = new BCodec();
148         final String test = null;
149         final String result = bcodec.decode(test);
150         assertNull(result, "Result should be null");
151     }
152 
153     @Test
154     void testEncodeDecodeNull() throws Exception {
155         final BCodec bcodec = new BCodec();
156         assertNull(bcodec.encode((String) null), "Null string B encoding test");
157         assertNull(bcodec.decode((String) null), "Null string B decoding test");
158     }
159 
160     @Test
161     void testEncodeObjects() throws Exception {
162         final BCodec bcodec = new BCodec();
163         final String plain = "what not";
164         final String encoded = (String) bcodec.encode((Object) plain);
165 
166         assertEquals("=?UTF-8?B?d2hhdCBub3Q=?=", encoded, "Basic B encoding test");
167         final Object result = bcodec.encode((Object) null);
168 
169         assertNull(result, "Encoding a null Object should return null");
170 
171         assertThrows(EncoderException.class, () -> bcodec.encode(Double.valueOf(3.0d)),
172             "Trying to url encode a Double object should cause an exception.");
173     }
174 
175     @Test
176     void testEncodeStringWithNull() throws Exception {
177         final BCodec bcodec = new BCodec();
178         final String test = null;
179         final String result = bcodec.encode(test, "charset");
180         assertNull(result, "Result should be null");
181     }
182 
183     @Test
184     void testInvalidEncoding() {
185         assertThrows(UnsupportedCharsetException.class, () -> new BCodec("NONSENSE"));
186     }
187 
188     @Test
189     void testNullInput() throws Exception {
190         final BCodec bcodec = new BCodec();
191         assertNull(bcodec.doDecoding(null));
192         assertNull(bcodec.doEncoding(null));
193     }
194 
195     @Test
196     void testUTF8RoundTrip() throws Exception {
197 
198         final String ru_msg = constructString(RUSSIAN_STUFF_UNICODE);
199         final String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE);
200 
201         final BCodec bcodec = new BCodec(CharEncoding.UTF_8);
202 
203         assertEquals("=?UTF-8?B?0JLRgdC10Lxf0L/RgNC40LLQtdGC?=", bcodec.encode(ru_msg));
204         assertEquals("=?UTF-8?B?R3LDvGV6aV96w6Rtw6Q=?=", bcodec.encode(ch_msg));
205 
206         assertEquals(ru_msg, bcodec.decode(bcodec.encode(ru_msg)));
207         assertEquals(ch_msg, bcodec.decode(bcodec.encode(ch_msg)));
208     }
209 
210 }