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.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
29 import org.apache.commons.codec.CharEncoding;
30 import org.apache.commons.codec.CodecPolicy;
31 import org.apache.commons.codec.DecoderException;
32 import org.apache.commons.codec.EncoderException;
33 import org.junit.jupiter.api.Test;
34
35
36
37
38 public class BCodecTest {
39 private static final String[] BASE64_IMPOSSIBLE_CASES = {
40
41 "=?ASCII?B?ZE==?=",
42 "=?ASCII?B?ZmC=?=",
43 "=?ASCII?B?Zm9vYE==?=",
44 "=?ASCII?B?Zm9vYmC=?=",
45 "=?ASCII?B?AB==?="
46 };
47
48 static final int[] SWISS_GERMAN_STUFF_UNICODE =
49 { 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4 };
50
51 static final int[] RUSSIAN_STUFF_UNICODE =
52 { 0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438, 0x432, 0x435, 0x442 };
53
54 private String constructString(final int[] unicodeChars) {
55 final StringBuilder buffer = new StringBuilder();
56 if (unicodeChars != null) {
57 for (final int unicodeChar : unicodeChars) {
58 buffer.append((char) unicodeChar);
59 }
60 }
61 return buffer.toString();
62 }
63
64 @Test
65 public void testBase64ImpossibleSamplesDefault() throws DecoderException {
66 final BCodec codec = new BCodec();
67
68 assertFalse(codec.isStrictDecoding());
69 for (final String s : BASE64_IMPOSSIBLE_CASES) {
70 codec.decode(s);
71 }
72 }
73
74 @Test
75 public void testBase64ImpossibleSamplesLenient() throws DecoderException {
76 final BCodec codec = new BCodec(StandardCharsets.UTF_8, CodecPolicy.LENIENT);
77
78 assertFalse(codec.isStrictDecoding());
79 for (final String s : BASE64_IMPOSSIBLE_CASES) {
80 codec.decode(s);
81 }
82 }
83
84 @Test
85 public void testBase64ImpossibleSamplesStrict() {
86 final BCodec codec = new BCodec(StandardCharsets.UTF_8, CodecPolicy.STRICT);
87 assertTrue(codec.isStrictDecoding());
88 for (final String s : BASE64_IMPOSSIBLE_CASES) {
89 assertThrows(DecoderException.class, () -> codec.decode(s));
90 }
91 }
92
93 @Test
94 public void testBasicEncodeDecode() throws Exception {
95 final BCodec bcodec = new BCodec();
96 final String plain = "Hello there";
97 final String encoded = bcodec.encode(plain);
98 assertEquals("=?UTF-8?B?SGVsbG8gdGhlcmU=?=", encoded, "Basic B encoding test");
99 assertEquals(plain, bcodec.decode(encoded), "Basic B decoding test");
100 }
101
102 @Test
103 public void testDecodeObjects() throws Exception {
104 final BCodec bcodec = new BCodec();
105 final String decoded = "=?UTF-8?B?d2hhdCBub3Q=?=";
106 final String plain = (String) bcodec.decode((Object) decoded);
107 assertEquals("what not", plain, "Basic B decoding test");
108 final Object result = bcodec.decode((Object) null);
109 assertNull(result, "Decoding a null Object should return null");
110
111 assertThrows(DecoderException.class, () -> bcodec.decode(Double.valueOf(3.0d)));
112 }
113
114 @Test
115 public void testDecodeStringWithNull() throws Exception {
116 final BCodec bcodec = new BCodec();
117 final String test = null;
118 final String result = bcodec.decode(test);
119 assertNull(result, "Result should be null");
120 }
121
122 @Test
123 public void testEncodeDecodeNull() throws Exception {
124 final BCodec bcodec = new BCodec();
125 assertNull(bcodec.encode((String) null), "Null string B encoding test");
126 assertNull(bcodec.decode((String) null), "Null string B decoding test");
127 }
128
129 @Test
130 public void testEncodeObjects() throws Exception {
131 final BCodec bcodec = new BCodec();
132 final String plain = "what not";
133 final String encoded = (String) bcodec.encode((Object) plain);
134
135 assertEquals("=?UTF-8?B?d2hhdCBub3Q=?=", encoded, "Basic B encoding test");
136 final Object result = bcodec.encode((Object) null);
137
138 assertNull(result, "Encoding a null Object should return null");
139
140 assertThrows(EncoderException.class, () -> bcodec.encode(Double.valueOf(3.0d)),
141 "Trying to url encode a Double object should cause an exception.");
142 }
143
144 @Test
145 public void testEncodeStringWithNull() throws Exception {
146 final BCodec bcodec = new BCodec();
147 final String test = null;
148 final String result = bcodec.encode(test, "charset");
149 assertNull(result, "Result should be null");
150 }
151
152 @Test
153 public void testInvalidEncoding() {
154 assertThrows(UnsupportedCharsetException.class, () -> new BCodec("NONSENSE"));
155 }
156
157 @Test
158 public void testNullInput() throws Exception {
159 final BCodec bcodec = new BCodec();
160 assertNull(bcodec.doDecoding(null));
161 assertNull(bcodec.doEncoding(null));
162 }
163
164 @Test
165 public void testUTF8RoundTrip() throws Exception {
166
167 final String ru_msg = constructString(RUSSIAN_STUFF_UNICODE);
168 final String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE);
169
170 final BCodec bcodec = new BCodec(CharEncoding.UTF_8);
171
172 assertEquals("=?UTF-8?B?0JLRgdC10Lxf0L/RgNC40LLQtdGC?=", bcodec.encode(ru_msg));
173 assertEquals("=?UTF-8?B?R3LDvGV6aV96w6Rtw6Q=?=", bcodec.encode(ch_msg));
174
175 assertEquals(ru_msg, bcodec.decode(bcodec.encode(ru_msg)));
176 assertEquals(ch_msg, bcodec.decode(bcodec.encode(ch_msg)));
177 }
178
179 }