1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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 }