55import org .junit .Test ;
66import static org .junit .Assert .assertTrue ;
77import static org .junit .Assert .assertEquals ;
8- import static org .junit .Assert .assertArrayEquals ;
98
109public class Base64Test {
1110
1211 private Base64 java8Base64 ;
12+ private Base64 commonsCodecBase64 ;
1313 private byte [] helloWorldBytes ;
1414 private byte [] helloWorldTwoLinesBytes ;
1515 private byte [] helloWorldTwoLinesAndNewLineBytes ;
1616 private byte [] helloWorldDifferentCharsBytes ;
17+ private byte [] bytes ;
1718
1819 @ Before
1920 public void setUp () throws UnsupportedEncodingException {
@@ -23,12 +24,43 @@ public void setUp() throws UnsupportedEncodingException {
2324 helloWorldDifferentCharsBytes = ("`1234567890-=~!@#$%^&*()_+ёЁ\" №;:?qwertyuiop[]asdfghjkl;'zxcvbnm,./QWERTYUIOP"
2425 + "{}|ASDFGHJKL:ZXCVBNM<>?йфяцычувскамепинртгоьшлбщдюзж.хэъ\\ ЙФЯЦЫЧУВСКАМЕПИНРТГОЬШЛБЩДЮЗЖ,ХЭЪ/\r \t \f \' "
2526 + "\b \n " ).getBytes ("UTF-8" );
27+ bytes = new byte []{48 , -126 , 2 , 118 , 2 , 1 , 0 , 48 , 13 , 6 , 9 , 42 , -122 , 72 , -122 , -9 , 13 , 1 , 1 , 1 , 5 , 0 , 4 , -126 ,
28+ 2 , 96 , 48 , -126 , 2 , 92 , 2 , 1 , 0 , 2 , -127 , -127 , 0 , -61 , -48 , -28 , 16 , -116 , -58 , 85 , 42 , -39 , 54 , 50 , -119 ,
29+ 18 , 40 , 17 , 75 , 51 , -24 , 113 , -109 , 38 , 17 , -18 , 106 , -60 , -74 , -97 , 29 , 82 , 123 , -128 , -88 , -34 , 92 , 112 ,
30+ -57 , 43 , -101 , 85 , -47 , 99 , -16 , 11 , -95 , 28 , -46 , 82 , -104 , -101 , -29 , -106 , -106 , -45 , -80 , 99 , -93 , 45 ,
31+ -102 , 107 , 31 , 32 , -60 , 13 , -46 , 102 , 127 , 81 , 94 , -98 , -56 , 117 , 50 , 21 , 39 , 5 , -98 , 26 , -18 , -30 , -21 ,
32+ 102 , -78 , -77 , 20 , 113 , -55 , 117 , -87 , -105 , -10 , -100 , 90 , -92 , 31 , 61 , -68 , -73 , -121 , -108 , 42 , 45 , -10 ,
33+ 21 , 87 , 118 , -74 , 71 , -100 , -37 , 96 , -24 , 87 , 102 , 68 , -95 , -1 , -14 , 6 , -20 , -14 , 32 , -14 , 33 , -84 , -123 ,
34+ -65 , 54 , 3 , 2 , 3 , 1 , 0 , 1 , 2 , -127 , -128 , 62 , 115 , -45 , 41 , 76 , 28 , -67 , 113 , 11 , 17 , -12 , 16 , 47 , -112 , 67 ,
35+ -29 , -66 , 76 , 118 , 92 , -66 , 25 , -99 , -10 , -61 , -126 , -109 , 64 , -32 , -37 , -82 , -17 , 44 , -20 , 66 , -77 , -29 ,
36+ 62 , -119 , -94 , 92 , -61 , 100 , -110 , 32 , 5 , 28 , 126 , -69 , -55 , 92 , 112 , 2 , 88 , 17 , -113 , 43 , -82 , 66 , 88 , 13 ,
37+ 53 , 58 , 74 , -65 , 36 , 45 , 93 , -63 , -15 , 125 , -7 , -44 , -45 , -51 , -76 , 86 , 97 , 54 , -36 , -49 , -117 , -18 , 56 , 54 ,
38+ 78 , 80 , 119 , -6 , -75 , 39 , 16 , 57 , -125 , -68 , 42 , 50 , -114 , 92 , 6 , 13 , 30 , -91 , 53 , -66 , -19 , -20 , 88 , 32 ,
39+ -38 , 36 , 126 , -119 , -86 , 47 , -46 , 37 , 115 , -49 , -23 , 125 , -61 , 75 , 37 , 70 , 92 , -122 , -79 , 2 , 65 , 0 , -11 ,
40+ -105 , 91 , 105 , -73 , 54 , 97 , 96 , -87 , -16 , -15 , -73 , 15 , 31 , -80 , -96 , -74 , -53 , -54 , 53 , -17 , -9 , 39 , 62 ,
41+ 58 , 51 , 68 , 107 , 86 , 111 , -62 , -48 , -125 , 117 , 66 , 111 , -55 , 27 , 56 , 81 , -50 , 96 , -47 , -102 , -50 , -83 , -52 ,
42+ -17 , -20 , 3 , -42 , -94 , 11 , 23 , 104 , 127 , 29 , -25 , 32 , 43 , -41 , -112 , -83 , -99 , 2 , 65 , 0 , -52 , 29 , 122 , 9 ,
43+ 49 , -14 , -118 , 110 , -79 , 107 , 76 , -88 , 4 , -49 , 40 , 32 , 59 , 88 , 45 , -71 , 62 , 78 , 93 , -121 , -123 , 123 , 3 , 4 ,
44+ 111 , -112 , 27 , 12 , -115 , -123 , 125 , 39 , 54 , 96 , -2 , -46 , 30 , 40 , -4 , -119 , 13 , -121 , 118 , -23 , 1 , -83 , -76 ,
45+ -26 , -117 , -86 , -79 , -121 , 113 , -26 , 33 , 30 , 124 , 35 , -16 , 31 , 2 , 65 , 0 , -47 , -113 , 111 , -81 , 75 , 104 , -103 ,
46+ -69 , 20 , 7 , -57 , 25 , -65 , 75 , -7 , 57 , -118 , 1 , 102 , -16 , -109 , 108 , -64 , 13 , -73 , 55 , -37 , -32 , 3 , -121 ,
47+ -90 , 34 , -86 , -87 , -70 , 33 , 12 , -25 , -81 , 45 , 14 , -1 , 74 , -101 , -32 , 84 , 41 , -107 , 104 , 60 , -10 , 62 , -101 ,
48+ 92 , 68 , 12 , -124 , 5 , -98 , 76 , 10 , -53 , 39 , 121 , 2 , 64 , 7 , 106 , 102 , -67 , -96 , -57 , -20 , 9 , -101 , 126 , -121 ,
49+ 121 , 111 , 59 , 75 , 124 , -24 , 75 , 10 , -42 , 57 , 18 , 69 , -55 , -97 , -86 , -39 , 112 , 54 , -47 , 104 , 122 , 43 , 70 , 23 ,
50+ 70 , -18 , 109 , -43 , -76 , 50 , -114 , 80 , -90 , 118 , 12 , 94 , -32 , -106 , 68 , 6 , 87 , 125 , -23 , -124 , -85 , -92 , 18 ,
51+ -75 , 79 , 83 , 57 , 71 , 7 , 2 , 64 , 73 , -64 , -3 , 78 , -90 , -122 , -64 , -99 , -29 , -71 , 75 , 21 , -74 , -24 , -43 , -37 ,
52+ 116 , -89 , 31 , -115 , -30 , 50 , 8 , 23 , 79 , -71 , -68 , -39 , 36 , -23 , 60 , 102 , -90 , -42 , 19 , -33 , -102 , -85 , -74 ,
53+ 103 , 73 , -30 , 120 , -15 , 104 , -9 , 110 , -24 , -127 , 14 , -57 , -44 , 67 , 9 , 80 , 120 , 42 , 94 , 107 , -81 , -109 , 101 ,
54+ -1 , 91 };
55+
2656 java8Base64 = new Java8Base64 ();
57+ commonsCodecBase64 = new CommonsCodecBase64 ();
2758 }
2859
2960 @ Test
3061 public void allImplementationsAreAvailable () {
3162 assertTrue (Java8Base64 .isAvailable ());
63+ assertTrue (CommonsCodecBase64 .isAvailable ());
3264 }
3365
3466 @ Test
@@ -40,12 +72,34 @@ public void testEncode() {
4072 + "bXWFzZGZnaGprbDsnenhjdmJubSwuL1FXRVJUWVVJT1B7fXxBU0RGR0hKS0w6WlhDVkJOTTw+P9C50YTRj9GG0YvRh9GD0LLRgdC"
4173 + "60LDQvNC10L/QuNC90YDRgtCz0L7RjNGI0LvQsdGJ0LTRjtC30LYu0YXRjdGKXNCZ0KTQr9Cm0KvQp9Cj0JLQodCa0JDQnNCV0J/"
4274 + "QmNCd0KDQotCT0J7QrNCo0JvQkdCp0JTQrtCX0JYs0KXQrdCqLw0JDCcICg==" ;
75+ final String str = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAMPQ5BCMxlUq2TYy"
76+ + "iRIoEUsz6HGTJhHuasS2nx1Se4Co3lxwxyubVdFj8AuhHNJSmJvjlpbTsGOjLZpr"
77+ + "HyDEDdJmf1Fensh1MhUnBZ4a7uLrZrKzFHHJdamX9pxapB89vLeHlCot9hVXdrZH"
78+ + "nNtg6FdmRKH/8gbs8iDyIayFvzYDAgMBAAECgYA+c9MpTBy9cQsR9BAvkEPjvkx2"
79+ + "XL4ZnfbDgpNA4Nuu7yzsQrPjPomiXMNkkiAFHH67yVxwAlgRjyuuQlgNNTpKvyQt"
80+ + "XcHxffnU0820VmE23M+L7jg2TlB3+rUnEDmDvCoyjlwGDR6lNb7t7Fgg2iR+iaov"
81+ + "0iVzz+l9w0slRlyGsQJBAPWXW2m3NmFgqfDxtw8fsKC2y8o17/cnPjozRGtWb8LQ"
82+ + "g3VCb8kbOFHOYNGazq3M7+wD1qILF2h/HecgK9eQrZ0CQQDMHXoJMfKKbrFrTKgE"
83+ + "zyggO1gtuT5OXYeFewMEb5AbDI2FfSc2YP7SHij8iQ2HdukBrbTmi6qxh3HmIR58"
84+ + "I/AfAkEA0Y9vr0tombsUB8cZv0v5OYoBZvCTbMANtzfb4AOHpiKqqbohDOevLQ7/"
85+ + "SpvgVCmVaDz2PptcRAyEBZ5MCssneQJAB2pmvaDH7Ambfod5bztLfOhLCtY5EkXJ"
86+ + "n6rZcDbRaHorRhdG7m3VtDKOUKZ2DF7glkQGV33phKukErVPUzlHBwJAScD9TqaG"
87+ + "wJ3juUsVtujV23SnH43iMggXT7m82STpPGam1hPfmqu2Z0niePFo927ogQ7H1EMJ"
88+ + "UHgqXmuvk2X/Ww==" ;
4389
4490 assertEquals (helloWorldEncoded , java8Base64 .internalEncode (helloWorldBytes ));
4591 assertEquals (helloWorldTwoLinesEncoded , java8Base64 .internalEncode (helloWorldTwoLinesBytes ));
4692 assertEquals (helloWorldTwoLinesAndNewLineEncoded ,
4793 java8Base64 .internalEncode (helloWorldTwoLinesAndNewLineBytes ));
4894 assertEquals (helloWorldDifferentCharsEncoded , java8Base64 .internalEncode (helloWorldDifferentCharsBytes ));
95+ assertEquals (str , java8Base64 .internalEncode (bytes ));
96+
97+ assertEquals (helloWorldEncoded , commonsCodecBase64 .internalEncode (helloWorldBytes ));
98+ assertEquals (helloWorldTwoLinesEncoded , commonsCodecBase64 .internalEncode (helloWorldTwoLinesBytes ));
99+ assertEquals (helloWorldTwoLinesAndNewLineEncoded ,
100+ commonsCodecBase64 .internalEncode (helloWorldTwoLinesAndNewLineBytes ));
101+ assertEquals (helloWorldDifferentCharsEncoded , commonsCodecBase64 .internalEncode (helloWorldDifferentCharsBytes ));
102+ assertEquals (str , commonsCodecBase64 .internalEncode (bytes ));
49103 }
50104
51105 @ Test
@@ -57,31 +111,36 @@ public void testEncodeUrlWithoutPadding() {
57111 + "bXWFzZGZnaGprbDsnenhjdmJubSwuL1FXRVJUWVVJT1B7fXxBU0RGR0hKS0w6WlhDVkJOTTw-P9C50YTRj9GG0YvRh9GD0LLRgdC"
58112 + "60LDQvNC10L_QuNC90YDRgtCz0L7RjNGI0LvQsdGJ0LTRjtC30LYu0YXRjdGKXNCZ0KTQr9Cm0KvQp9Cj0JLQodCa0JDQnNCV0J_"
59113 + "QmNCd0KDQotCT0J7QrNCo0JvQkdCp0JTQrtCX0JYs0KXQrdCqLw0JDCcICg" ;
114+ final String str = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAMPQ5BCMxlUq2TYy"
115+ + "iRIoEUsz6HGTJhHuasS2nx1Se4Co3lxwxyubVdFj8AuhHNJSmJvjlpbTsGOjLZpr"
116+ + "HyDEDdJmf1Fensh1MhUnBZ4a7uLrZrKzFHHJdamX9pxapB89vLeHlCot9hVXdrZH"
117+ + "nNtg6FdmRKH_8gbs8iDyIayFvzYDAgMBAAECgYA-c9MpTBy9cQsR9BAvkEPjvkx2"
118+ + "XL4ZnfbDgpNA4Nuu7yzsQrPjPomiXMNkkiAFHH67yVxwAlgRjyuuQlgNNTpKvyQt"
119+ + "XcHxffnU0820VmE23M-L7jg2TlB3-rUnEDmDvCoyjlwGDR6lNb7t7Fgg2iR-iaov"
120+ + "0iVzz-l9w0slRlyGsQJBAPWXW2m3NmFgqfDxtw8fsKC2y8o17_cnPjozRGtWb8LQ"
121+ + "g3VCb8kbOFHOYNGazq3M7-wD1qILF2h_HecgK9eQrZ0CQQDMHXoJMfKKbrFrTKgE"
122+ + "zyggO1gtuT5OXYeFewMEb5AbDI2FfSc2YP7SHij8iQ2HdukBrbTmi6qxh3HmIR58"
123+ + "I_AfAkEA0Y9vr0tombsUB8cZv0v5OYoBZvCTbMANtzfb4AOHpiKqqbohDOevLQ7_"
124+ + "SpvgVCmVaDz2PptcRAyEBZ5MCssneQJAB2pmvaDH7Ambfod5bztLfOhLCtY5EkXJ"
125+ + "n6rZcDbRaHorRhdG7m3VtDKOUKZ2DF7glkQGV33phKukErVPUzlHBwJAScD9TqaG"
126+ + "wJ3juUsVtujV23SnH43iMggXT7m82STpPGam1hPfmqu2Z0niePFo927ogQ7H1EMJ"
127+ + "UHgqXmuvk2X_Ww" ;
60128
61129 assertEquals (helloWorldEncoded , java8Base64 .internalEncodeUrlWithoutPadding (helloWorldBytes ));
62130 assertEquals (helloWorldTwoLinesEncoded , java8Base64 .internalEncodeUrlWithoutPadding (helloWorldTwoLinesBytes ));
63131 assertEquals (helloWorldTwoLinesAndNewLineEncoded ,
64132 java8Base64 .internalEncodeUrlWithoutPadding (helloWorldTwoLinesAndNewLineBytes ));
65133 assertEquals (helloWorldDifferentCharsEncoded ,
66134 java8Base64 .internalEncodeUrlWithoutPadding (helloWorldDifferentCharsBytes ));
67- }
68-
69- @ Test
70- public void testDecodeMime () {
71- final String helloWorldEncoded = "SGVsbG8gV29ybGQ=" ;
72- final String helloWorldTwoLinesEncoded = "SGVsbG8gV29ybGQNCk5ldyBMaW5lMg==" ;
73- final String helloWorldTwoLinesAndNewLineEncoded = "SGVsbG8gV29ybGQNClNlY29uZCBMaW5lDQo=" ;
74- final String helloWorldDifferentCharsEncoded = "YDEyMzQ1Njc4OTAtPX4hQCMkJV4mKigpXyvRkdCBIuKEljs6P3F3ZXJ0eXVpb3B"
75- + "bXWFzZGZnaGprbDsnenhjdmJubSwuL1FXRVJUWVVJT1B7fXxBU0RGR0hKS0w6WlhDVkJOTTw+P9C50YTRj9GG0YvRh9GD0LLRgdC"
76- + "60LDQvNC10L/QuNC90YDRgtCz0L7RjNGI0LvQsdGJ0LTRjtC30LYu0YXRjdGKXNCZ0KTQr9Cm0KvQp9Cj0JLQodCa0JDQnNCV0J/"
77- + "QmNCd0KDQotCT0J7QrNCo0JvQkdCp0JTQrtCX0JYs0KXQrdCqLw0JDCcICg==" ;
135+ assertEquals (str , java8Base64 .internalEncodeUrlWithoutPadding (bytes ));
78136
79- assertArrayEquals (helloWorldBytes , java8Base64 .internalDecodeMime (helloWorldEncoded ));
80- assertArrayEquals (helloWorldTwoLinesBytes , java8Base64 .internalDecodeMime (helloWorldTwoLinesEncoded ));
81- assertArrayEquals (helloWorldTwoLinesAndNewLineBytes ,
82- java8Base64 .internalDecodeMime (helloWorldTwoLinesAndNewLineEncoded ));
83- assertArrayEquals (helloWorldDifferentCharsBytes ,
84- java8Base64 .internalDecodeMime (helloWorldDifferentCharsEncoded ));
137+ assertEquals (helloWorldEncoded , commonsCodecBase64 .internalEncodeUrlWithoutPadding (helloWorldBytes ));
138+ assertEquals (helloWorldTwoLinesEncoded ,
139+ commonsCodecBase64 .internalEncodeUrlWithoutPadding (helloWorldTwoLinesBytes ));
140+ assertEquals (helloWorldTwoLinesAndNewLineEncoded ,
141+ commonsCodecBase64 .internalEncodeUrlWithoutPadding (helloWorldTwoLinesAndNewLineBytes ));
142+ assertEquals (helloWorldDifferentCharsEncoded ,
143+ commonsCodecBase64 .internalEncodeUrlWithoutPadding (helloWorldDifferentCharsBytes ));
144+ assertEquals (str , commonsCodecBase64 .internalEncodeUrlWithoutPadding (bytes ));
85145 }
86-
87146}
0 commit comments