aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/yaml/snakeyaml/types/BinaryTagTest.java
blob: 803685f917e5ede9fa6fea99cd9899ff8bb63b29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
 * Copyright (c) 2008, SnakeYAML
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package org.yaml.snakeyaml.types;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @see <a href="http://yaml.org/type/binary.html">binary</a>
 */
public class BinaryTagTest extends AbstractTest {

  String line1 = "R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5";
  String line2 = "OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+";
  String line3 = "+f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC";
  String line4 = "AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=";
  String content = line1 + line2 + line3 + line4;

  public void testBinary() {
    byte[] binary = (byte[]) getMapValue("canonical: !!binary " + content, "canonical");
    assertEquals((byte) 'G', binary[0]);
    assertEquals((byte) 'I', binary[1]);
    assertEquals((byte) 'F', binary[2]);
    assertEquals((byte) '8', binary[3]);
    assertEquals((byte) '9', binary[4]);
  }

  public void testBinary2() {
    byte[] binary = (byte[]) load("!!binary \"MQ==\"");
    assertEquals(1, binary.length);
    assertEquals((byte) '1', binary[0]);
  }

  public void testBinaryTag() {
    byte[] binary =
        (byte[]) getMapValue("canonical: !<tag:yaml.org,2002:binary> " + content, "canonical");
    assertEquals((byte) 'G', binary[0]);
    assertEquals((byte) 'I', binary[1]);
    assertEquals((byte) 'F', binary[2]);
    assertEquals((byte) '8', binary[3]);
    assertEquals((byte) '9', binary[4]);
  }

  public void testBinaryOut() throws IOException {
    byte[] data = "GIF89\tbi\u0003\u0000nary\n\u001Fimage\n".getBytes(StandardCharsets.ISO_8859_1);
    Map<String, String> map = new HashMap<String, String>();
    String value = new String(data, StandardCharsets.ISO_8859_1);
    map.put("canonical", value);
    String output = dump(map);
    assertEquals("canonical: !!binary |-\n  R0lGODkJYmkDAG5hcnkKH2ltYWdlCg==\n", output);
  }

  public void testByteArray() {
    byte[] data = {8, 14, 15, 10, 126, 32, 65, 65, 65};
    String output = dump(data);
    assertEquals("!!binary |-\n  CA4PCn4gQUFB\n", output);
    byte[] parsed = (byte[]) load(output);
    assertEquals(data.length, parsed.length);
    for (int i = 0; i < data.length; i++) {
      assertEquals(data[i], parsed[i]);
    }
  }
}