• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2008, SnakeYAML
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package biz.source_code.base64Coder;
15 
16 import java.io.UnsupportedEncodingException;
17 import java.nio.charset.StandardCharsets;
18 import junit.framework.TestCase;
19 import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
20 
21 public class Base64CoderTest extends TestCase {
22 
testDecode()23   public void testDecode() throws UnsupportedEncodingException {
24     check("Aladdin:open sesame", "QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
25     check("a", "YQ==");
26     check("aa", "YWE=");
27     check("a=", "YT0=");
28     check("", "");
29   }
30 
testFailure1()31   public void testFailure1() throws UnsupportedEncodingException {
32     try {
33       Base64Coder.decode("YQ=".toCharArray());
34       fail();
35     } catch (Exception e) {
36       assertEquals("Length of Base64 encoded input string is not a multiple of 4.", e.getMessage());
37     }
38   }
39 
testFailure2()40   public void testFailure2() throws UnsupportedEncodingException {
41     checkInvalid("\tWE=");
42     checkInvalid("Y\tE=");
43     checkInvalid("YW\t=");
44     checkInvalid("YWE\t");
45     //
46     checkInvalid("©WE=");
47     checkInvalid("Y©E=");
48     checkInvalid("YW©=");
49     checkInvalid("YWE©");
50   }
51 
checkInvalid(String encoded)52   private void checkInvalid(String encoded) {
53     try {
54       Base64Coder.decode(encoded.toCharArray());
55       fail("Illegal chanracter.");
56     } catch (Exception e) {
57       assertEquals("Illegal character in Base64 encoded data.", e.getMessage());
58     }
59   }
60 
check(String text, String encoded)61   private void check(String text, String encoded) throws UnsupportedEncodingException {
62     char[] s1 = Base64Coder.encode(text.getBytes(StandardCharsets.UTF_8));
63     String t1 = new String(s1);
64     assertEquals(encoded, t1);
65     byte[] s2 = Base64Coder.decode(encoded.toCharArray());
66     String t2 = new String(s2, StandardCharsets.UTF_8);
67     assertEquals(text, t2);
68   }
69 }
70