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.assertNull;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import java.nio.charset.StandardCharsets;
24  
25  import org.apache.commons.codec.CharEncoding;
26  import org.apache.commons.codec.DecoderException;
27  import org.junit.jupiter.api.Test;
28  import org.junit.jupiter.params.ParameterizedTest;
29  import org.junit.jupiter.params.provider.ValueSource;
30  
31  /**
32   * RFC 1522 compliant codec test cases
33   */
34  class RFC1522CodecTest {
35  
36      static class RFC1522TestCodec extends RFC1522Codec {
37  
38          RFC1522TestCodec() {
39              super(StandardCharsets.UTF_8);
40          }
41  
42          @Override
43          protected byte[] doDecoding(final byte[] bytes) {
44              return bytes;
45          }
46  
47          @Override
48          protected byte[] doEncoding(final byte[] bytes) {
49              return bytes;
50          }
51  
52          @Override
53          protected String getEncoding() {
54              return "T";
55          }
56      }
57  
58      static void assertExpectedDecoderException(final String text) {
59          assertThrows(DecoderException.class, () -> new RFC1522TestCodec().decodeText(text));
60      }
61  
62      @ParameterizedTest
63      // @formatter:off
64      @ValueSource(strings = {
65          "whatever",
66          "=?",
67          "?=",
68          "==",
69          "=?=",
70          "=??=",
71          "=?stuff?=",
72          "=?UTF-8??=",
73          "=?UTF-8?stuff?=",
74          "=?UTF-8?T?stuff",
75          "=??T?stuff?=",
76          "=?UTF-8??stuff?=",
77          "=?UTF-8?W?stuff?=",
78          "=?UTF-8?T?stuff?more?=",
79          "=?UTF-8?T?stuff??=",
80          "=?UTF-8?T???="
81      })
82      // @formatter:on
83      void testDecodeInvalid(final String text) throws Exception {
84          assertExpectedDecoderException(text);
85      }
86  
87      @Test
88      void testNullInput() throws Exception {
89          final RFC1522TestCodec testCodec = new RFC1522TestCodec();
90          assertNull(testCodec.decodeText(null));
91          assertNull(testCodec.encodeText(null, CharEncoding.UTF_8));
92      }
93  }