✅ Add test for Freenet-specific Base64 implementation
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Sun, 26 May 2024 10:50:35 +0000 (12:50 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Sun, 26 May 2024 10:50:35 +0000 (12:50 +0200)
src/test/java/net/pterodactylus/fcp/FreenetBase64Test.java [new file with mode: 0644]

diff --git a/src/test/java/net/pterodactylus/fcp/FreenetBase64Test.java b/src/test/java/net/pterodactylus/fcp/FreenetBase64Test.java
new file mode 100644 (file)
index 0000000..4033aed
--- /dev/null
@@ -0,0 +1,45 @@
+package net.pterodactylus.fcp;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static java.util.Arrays.asList;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+
+import java.util.List;
+
+import org.junit.Test;
+
+/**
+ * Unit test for {@link FreenetBase64Test}.
+ */
+public class FreenetBase64Test {
+
+       @Test
+       public void testEncodingVectorsFromRfc4648() {
+               getRfc4648TestVectors().forEach(vector -> assertThat(encoder.encode(vector[0].getBytes(UTF_8)), equalTo(vector[1])));
+       }
+
+       @Test
+       public void testDecodingVectorsFromRfc4648() {
+               getRfc4648TestVectors().forEach(vector -> assertThat(encoder.decode(vector[1]), equalTo(vector[0].getBytes(UTF_8))));
+       }
+
+       private static List<String[]> getRfc4648TestVectors() {
+               return asList(
+                       vector("", ""),
+                       vector("f", "Zg=="),
+                       vector("fo", "Zm8="),
+                       vector("foo", "Zm9v"),
+                       vector("foob", "Zm9vYg=="),
+                       vector("fooba", "Zm9vYmE="),
+                       vector("foobar", "Zm9vYmFy")
+               );
+       }
+
+       private static String[] vector(String plain, String encoded) {
+               return new String[] { plain, encoded };
+       }
+
+       private final FreenetBase64 encoder = new FreenetBase64();
+
+}