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    *      http://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  
29  /**
30   * RFC 1522 compliant codec test cases
31   */
32  public class RFC1522CodecTest {
33  
34      static class RFC1522TestCodec extends RFC1522Codec {
35  
36          RFC1522TestCodec() {
37              super(StandardCharsets.UTF_8);
38          }
39  
40          @Override
41          protected byte[] doDecoding(final byte[] bytes) {
42              return bytes;
43          }
44  
45          @Override
46          protected byte[] doEncoding(final byte[] bytes) {
47              return bytes;
48          }
49  
50          @Override
51          protected String getEncoding() {
52              return "T";
53          }
54  
55      }
56  
57      private void assertExpectedDecoderException(final String s) {
58          assertThrows(DecoderException.class, () -> new RFC1522TestCodec().decodeText(s));
59      }
60  
61      @Test
62      public void testDecodeInvalid() throws Exception {
63          assertExpectedDecoderException("whatever");
64          assertExpectedDecoderException("=?");
65          assertExpectedDecoderException("?=");
66          assertExpectedDecoderException("==");
67          assertExpectedDecoderException("=??=");
68          assertExpectedDecoderException("=?stuff?=");
69          assertExpectedDecoderException("=?UTF-8??=");
70          assertExpectedDecoderException("=?UTF-8?stuff?=");
71          assertExpectedDecoderException("=?UTF-8?T?stuff");
72          assertExpectedDecoderException("=??T?stuff?=");
73          assertExpectedDecoderException("=?UTF-8??stuff?=");
74          assertExpectedDecoderException("=?UTF-8?W?stuff?=");
75      }
76  
77      @Test
78      public void testNullInput() throws Exception {
79          final RFC1522TestCodec testCodec = new RFC1522TestCodec();
80          assertNull(testCodec.decodeText(null));
81          assertNull(testCodec.encodeText(null, CharEncoding.UTF_8));
82      }
83  
84  }