You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HexTest.java 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. package org.eclipse.jgit.util;
  12. import static org.eclipse.jgit.util.Hex.decode;
  13. import static org.eclipse.jgit.util.Hex.toHexString;
  14. import static org.junit.Assert.assertEquals;
  15. import org.eclipse.jgit.junit.JGitTestUtil;
  16. import org.eclipse.jgit.lib.Constants;
  17. import org.junit.Test;
  18. public class HexTest {
  19. @Test
  20. public void testEncode() {
  21. assertEquals("68690a", toHexString(b("hi\n")));
  22. assertEquals("0001020d0a0971", toHexString(b("\0\1\2\r\n\tq")));
  23. }
  24. @Test
  25. public void testDecode() {
  26. JGitTestUtil.assertEquals(b("hi\n"), decode("68690a"));
  27. JGitTestUtil.assertEquals(b("\0\1\2\r\n\tq"), decode("0001020d0a0971"));
  28. JGitTestUtil.assertEquals(b("\u000EB"), decode("0E42"));
  29. }
  30. @Test
  31. public void testEncodeMatchesDecode() {
  32. String[] testStrings = { "", "cow", "a", "a secret string",
  33. "\0\1\2\r\n\t" };
  34. for (String e : testStrings) {
  35. JGitTestUtil.assertEquals(b(e), decode(toHexString(b(e))));
  36. }
  37. }
  38. @Test(expected = IllegalArgumentException.class)
  39. public void testIllegal() {
  40. decode("0011test00");
  41. }
  42. @Test(expected = IllegalArgumentException.class)
  43. public void testIllegal2() {
  44. decode("0123456789abcdefgh");
  45. }
  46. @Test(expected = IllegalArgumentException.class)
  47. public void testIllegal3() {
  48. decode("0123456789abcdef-_+*");
  49. }
  50. @Test
  51. public void testLegal() {
  52. decode("0123456789abcdef");
  53. }
  54. @Test
  55. public void testLegal2() {
  56. decode("deadbeef");
  57. }
  58. private static byte[] b(String str) {
  59. return Constants.encode(str);
  60. }
  61. }