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.

Base64Test.java 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (C) 2010, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.util;
  11. import static org.eclipse.jgit.util.Base64.decode;
  12. import static org.eclipse.jgit.util.Base64.encodeBytes;
  13. import static org.junit.Assert.assertEquals;
  14. import static org.junit.Assert.fail;
  15. import org.eclipse.jgit.junit.JGitTestUtil;
  16. import org.eclipse.jgit.lib.Constants;
  17. import org.junit.Test;
  18. public class Base64Test {
  19. @Test
  20. public void testEncode() {
  21. assertEquals("aGkK", encodeBytes(b("hi\n")));
  22. assertEquals("AAECDQoJcQ==", encodeBytes(b("\0\1\2\r\n\tq")));
  23. }
  24. @Test
  25. public void testDecode() {
  26. JGitTestUtil.assertEquals(b("hi\n"), decode("aGkK"));
  27. JGitTestUtil.assertEquals(b("\0\1\2\r\n\tq"), decode("AAECDQoJcQ=="));
  28. JGitTestUtil.assertEquals(b("\0\1\2\r\n\tq"),
  29. decode("A A E\tC D\rQ o\nJ c Q=="));
  30. JGitTestUtil.assertEquals(b("\u000EB"), decode("DkL="));
  31. }
  32. @Test
  33. public void testDecodeFail_NonBase64Character() {
  34. try {
  35. decode("! a bad base64 string !");
  36. fail("Accepted bad string in decode");
  37. } catch (IllegalArgumentException fail) {
  38. // Expected
  39. }
  40. }
  41. @Test
  42. public void testEncodeMatchesDecode() {
  43. String[] testStrings = { "", //
  44. "cow", //
  45. "a", //
  46. "a secret string", //
  47. "\0\1\2\r\n\t" //
  48. };
  49. for (String e : testStrings)
  50. JGitTestUtil.assertEquals(b(e), decode(encodeBytes(b(e))));
  51. }
  52. private static byte[] b(String str) {
  53. return Constants.encode(str);
  54. }
  55. }