Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Base85Test.java 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (C) 2021 Thomas Wolf <thomas.wolf@paranor.ch> 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.junit.Assert.assertArrayEquals;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertNotNull;
  14. import static org.junit.Assert.assertThrows;
  15. import static org.junit.Assert.assertTrue;
  16. import java.nio.charset.StandardCharsets;
  17. import org.junit.Test;
  18. /**
  19. * Tests for {@link Base85}.
  20. */
  21. public class Base85Test {
  22. private static final String VALID_CHARS = "0123456789"
  23. + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  24. + "!#$%&()*+-;<=>?@^_`{|}~";
  25. @Test
  26. public void testChars() {
  27. for (int i = 0; i < 256; i++) {
  28. byte[] testData = { '1', '2', '3', '4', (byte) i };
  29. if (VALID_CHARS.indexOf(i) >= 0) {
  30. byte[] decoded = Base85.decode(testData, 4);
  31. assertNotNull(decoded);
  32. } else {
  33. assertThrows(IllegalArgumentException.class,
  34. () -> Base85.decode(testData, 4));
  35. }
  36. }
  37. }
  38. private void roundtrip(byte[] data, int expectedLength) {
  39. byte[] encoded = Base85.encode(data);
  40. assertEquals(expectedLength, encoded.length);
  41. assertArrayEquals(data, Base85.decode(encoded, data.length));
  42. }
  43. private void roundtrip(String data, int expectedLength) {
  44. roundtrip(data.getBytes(StandardCharsets.US_ASCII), expectedLength);
  45. }
  46. @Test
  47. public void testPadding() {
  48. roundtrip("", 0);
  49. roundtrip("a", 5);
  50. roundtrip("ab", 5);
  51. roundtrip("abc", 5);
  52. roundtrip("abcd", 5);
  53. roundtrip("abcde", 10);
  54. roundtrip("abcdef", 10);
  55. roundtrip("abcdefg", 10);
  56. roundtrip("abcdefgh", 10);
  57. roundtrip("abcdefghi", 15);
  58. }
  59. @Test
  60. public void testBinary() {
  61. roundtrip(new byte[] { 1 }, 5);
  62. roundtrip(new byte[] { 1, 2 }, 5);
  63. roundtrip(new byte[] { 1, 2, 3 }, 5);
  64. roundtrip(new byte[] { 1, 2, 3, 4 }, 5);
  65. roundtrip(new byte[] { 1, 2, 3, 4, 5 }, 10);
  66. roundtrip(new byte[] { 1, 2, 3, 4, 5, 0, 0, 0 }, 10);
  67. roundtrip(new byte[] { 1, 2, 3, 4, 0, 0, 0, 5 }, 10);
  68. }
  69. @Test
  70. public void testOverflow() {
  71. IllegalArgumentException e = assertThrows(
  72. IllegalArgumentException.class,
  73. () -> Base85.decode(new byte[] { '~', '~', '~', '~', '~' }, 4));
  74. assertTrue(e.getMessage().contains("overflow"));
  75. }
  76. }