View Javadoc
1   /*
2    * Copyright (C) 2010, Google Inc.
3    * Copyright (C) 2020 Michael Dardis and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.util;
13  
14  import static org.eclipse.jgit.util.Hex.decode;
15  import static org.eclipse.jgit.util.Hex.toHexString;
16  import static org.junit.Assert.assertEquals;
17  
18  import org.eclipse.jgit.junit.JGitTestUtil;
19  import org.eclipse.jgit.lib.Constants;
20  import org.junit.Test;
21  
22  public class HexTest {
23  	@Test
24  	public void testEncode() {
25  		assertEquals("68690a", toHexString(b("hi\n")));
26  		assertEquals("0001020d0a0971", toHexString(b("\0\1\2\r\n\tq")));
27  	}
28  
29  	@Test
30  	public void testDecode() {
31  		JGitTestUtil.assertEquals(b("hi\n"), decode("68690a"));
32  		JGitTestUtil.assertEquals(b("\0\1\2\r\n\tq"), decode("0001020d0a0971"));
33  		JGitTestUtil.assertEquals(b("\u000EB"), decode("0E42"));
34  	}
35  
36  	@Test
37  	public void testEncodeMatchesDecode() {
38  		String[] testStrings = { "", "cow", "a", "a secret string",
39  				"\0\1\2\r\n\t" };
40  		for (String e : testStrings) {
41  			JGitTestUtil.assertEquals(b(e), decode(toHexString(b(e))));
42  		}
43  	}
44  
45  	@Test(expected = IllegalArgumentException.class)
46  	public void testIllegal() {
47  		decode("0011test00");
48  	}
49  
50  	@Test(expected = IllegalArgumentException.class)
51  	public void testIllegal2() {
52  		decode("0123456789abcdefgh");
53  	}
54  
55  	@Test(expected = IllegalArgumentException.class)
56  	public void testIllegal3() {
57  		decode("0123456789abcdef-_+*");
58  	}
59  
60  	@Test
61  	public void testLegal() {
62  		decode("0123456789abcdef");
63  	}
64  
65  	@Test
66  	public void testLegal2() {
67  		decode("deadbeef");
68  	}
69  
70  	private static byte[] b(String str) {
71  		return Constants.encode(str);
72  	}
73  
74  }