1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package biz.source_code.base64Coder;
18
19 import java.io.UnsupportedEncodingException;
20
21 import junit.framework.TestCase;
22
23 import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
24
25 public class Base64CoderTest extends TestCase {
26
27 public void testDecode() throws UnsupportedEncodingException {
28 check("Aladdin:open sesame", "QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
29 check("a", "YQ==");
30 check("aa", "YWE=");
31 check("a=", "YT0=");
32 check("", "");
33 }
34
35 public void testFailure1() throws UnsupportedEncodingException {
36 try {
37 Base64Coder.decode("YQ=".toCharArray());
38 fail();
39 } catch (Exception e) {
40 assertEquals("Length of Base64 encoded input string is not a multiple of 4.",
41 e.getMessage());
42 }
43 }
44
45 public void testFailure2() throws UnsupportedEncodingException {
46 checkInvalid("\tWE=");
47 checkInvalid("Y\tE=");
48 checkInvalid("YW\t=");
49 checkInvalid("YWE\t");
50
51 checkInvalid("©WE=");
52 checkInvalid("Y©E=");
53 checkInvalid("YW©=");
54 checkInvalid("YWE©");
55 }
56
57 private void checkInvalid(String encoded) {
58 try {
59 Base64Coder.decode(encoded.toCharArray());
60 fail("Illegal chanracter.");
61 } catch (Exception e) {
62 assertEquals("Illegal character in Base64 encoded data.", e.getMessage());
63 }
64 }
65
66 private void check(String text, String encoded) throws UnsupportedEncodingException {
67 char[] s1 = Base64Coder.encode(text.getBytes("UTF-8"));
68 String t1 = new String(s1);
69 assertEquals(encoded, t1);
70 byte[] s2 = Base64Coder.decode(encoded.toCharArray());
71 String t2 = new String(s2, "UTF-8");
72 assertEquals(text, t2);
73 }
74 }