summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java/libcore/util/HexEncodingTest.java
blob: f5cfb3e7f43b3fc63523986b469673a924f42de9 (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
77
78
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * 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 libcore.util;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import junit.framework.TestCase;
import static libcore.util.HexEncoding.decode;
import static libcore.util.HexEncoding.encode;

public class HexEncodingTest extends TestCase {
  public void testEncode() {
    final byte[] avocados = "avocados".getBytes(StandardCharsets.UTF_8);

    assertArraysEqual("61766F6361646F73".toCharArray(), encode(avocados));
    assertArraysEqual(avocados, decode(encode(avocados), false));
    // Make sure we can handle lower case hex encodings as well.
    assertArraysEqual(avocados, decode("61766f6361646f73".toCharArray(), false));
  }

  public void testDecode_allow4Bit() {
    assertArraysEqual(new byte[] { 6 }, decode("6".toCharArray(), true));
    assertArraysEqual(new byte[] { 6, 'v' }, decode("676".toCharArray(), true));
  }

  public void testDecode_disallow4Bit() {
    try {
      decode("676".toCharArray(), false);
      fail();
    } catch (IllegalArgumentException expected) {
    }
  }

  public void testDecode_invalid() {
    try {
      decode("DEADBARD".toCharArray(), false);
      fail();
    } catch (IllegalArgumentException expected) {
    }

    // This demonstrates a difference in behaviour from apache commons : apache
    // commons uses Character.isDigit and would successfully decode a string with
    // arabic and devanagari characters.
    try {
      decode("६१٧٥٥F6361646F73".toCharArray(), false);
      fail();
    } catch (IllegalArgumentException expected) {
    }

    try {
      decode("#%6361646F73".toCharArray(), false);
      fail();
    } catch (IllegalArgumentException expected) {
    }
  }

  private static void assertArraysEqual(char[] lhs, char[] rhs) {
    assertEquals(new String(lhs), new String(rhs));
  }

  private static void assertArraysEqual(byte[] lhs, byte[] rhs) {
    assertEquals(Arrays.toString(lhs), Arrays.toString(rhs));
  }
}