From: David ‘Bombe’ Roden Date: Wed, 7 Aug 2019 19:03:17 +0000 (+0200) Subject: 🎨 Replace Key with Kotlin version X-Git-Tag: v81^2~129 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=bde9f3b65b851c006a8d80c41501f016ef8cfea3;p=Sone.git 🎨 Replace Key with Kotlin version --- diff --git a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java index b3b32b3..28c8e22 100644 --- a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java +++ b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java @@ -21,7 +21,6 @@ import static freenet.keys.USK.create; import static java.lang.String.format; import static java.util.logging.Level.WARNING; import static java.util.logging.Logger.getLogger; -import static net.pterodactylus.sone.freenet.Key.routingKey; import java.io.IOException; import java.net.MalformedURLException; @@ -75,6 +74,7 @@ import freenet.support.api.Bucket; import freenet.support.api.RandomAccessBucket; import freenet.support.io.ArrayBucket; import freenet.support.io.ResumeFailedException; +import net.pterodactylus.sone.freenet.*; /** * Contains all necessary functionality for interacting with the Freenet node. @@ -255,7 +255,7 @@ public class FreenetInterface { public void registerActiveUsk(FreenetURI requestUri, USKCallback uskCallback) { try { - soneUskCallbacks.put(routingKey(requestUri), uskCallback); + soneUskCallbacks.put(FreenetURIsKt.getRoutingKeyString(requestUri), uskCallback); node.clientCore.uskManager.subscribe(create(requestUri), uskCallback, true, requestClient); } catch (MalformedURLException mue1) { @@ -267,7 +267,7 @@ public class FreenetInterface { public void registerPassiveUsk(FreenetURI requestUri, USKCallback uskCallback) { try { - soneUskCallbacks.put(routingKey(requestUri), uskCallback); + soneUskCallbacks.put(FreenetURIsKt.getRoutingKeyString(requestUri), uskCallback); node.clientCore .uskManager .subscribe(create(requestUri), uskCallback, false, requestClient); diff --git a/src/main/java/net/pterodactylus/sone/freenet/Key.java b/src/main/java/net/pterodactylus/sone/freenet/Key.java deleted file mode 100644 index 6811642..0000000 --- a/src/main/java/net/pterodactylus/sone/freenet/Key.java +++ /dev/null @@ -1,65 +0,0 @@ -package net.pterodactylus.sone.freenet; - -import static freenet.support.Base64.encode; -import static java.lang.String.format; - -import freenet.keys.FreenetURI; - -import com.google.common.annotations.VisibleForTesting; - -/** - * Encapsulates the parts of a {@link FreenetURI} that do not change while - * being converted from SSK to USK and/or back. - */ -public class Key { - - private final byte[] routingKey; - private final byte[] cryptoKey; - private final byte[] extra; - - private Key(byte[] routingKey, byte[] cryptoKey, byte[] extra) { - this.routingKey = routingKey; - this.cryptoKey = cryptoKey; - this.extra = extra; - } - - @VisibleForTesting - public String getRoutingKey() { - return encode(routingKey); - } - - @VisibleForTesting - public String getCryptoKey() { - return encode(cryptoKey); - } - - @VisibleForTesting - public String getExtra() { - return encode(extra); - } - - public FreenetURI toUsk(String docName, long edition, String... paths) { - return new FreenetURI("USK", docName, paths, routingKey, cryptoKey, - extra, edition); - } - - public FreenetURI toSsk(String docName, String... paths) { - return new FreenetURI("SSK", docName, paths, routingKey, cryptoKey, - extra); - } - - public FreenetURI toSsk(String docName, long edition, String... paths) { - return new FreenetURI("SSK", format("%s-%d", docName, edition), paths, - routingKey, cryptoKey, extra, edition); - } - - public static Key from(FreenetURI freenetURI) { - return new Key(freenetURI.getRoutingKey(), freenetURI.getCryptoKey(), - freenetURI.getExtra()); - } - - public static String routingKey(FreenetURI freenetURI) { - return from(freenetURI).getRoutingKey(); - } - -} diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/FreenetURIs.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/FreenetURIs.kt new file mode 100644 index 0000000..3a6d43c --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/FreenetURIs.kt @@ -0,0 +1,6 @@ +package net.pterodactylus.sone.freenet + +import freenet.keys.* +import freenet.support.Base64.* + +val FreenetURI.routingKeyString: String get() = encode(routingKey) diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/FreenetURIsTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/FreenetURIsTest.kt new file mode 100644 index 0000000..ca1fc6e --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/FreenetURIsTest.kt @@ -0,0 +1,24 @@ +package net.pterodactylus.sone.freenet + +import freenet.keys.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import org.junit.* + +/** + * Unit test for [Key]. + */ +class FreenetURIsTest { + + private val uri = FreenetURI("SSK@$routingKey,$cryptoKey,$extra/some-site-12/foo/bar.html") + + @Test + fun routingKeyIsExtractCorrectly() { + assertThat(uri.routingKeyString, equalTo(routingKey)) + } + +} + +private const val routingKey = "NfUYvxDwU9vqb2mh-qdT~DYJ6U0XNbxMGGoLe0aCHJs" +private const val cryptoKey = "Miglsgix0VR56ZiPl4NgjnUd~UdrnHqIvXJ3KKHmxmI" +private const val extra = "AQACAAE" diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/KeyTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/KeyTest.kt deleted file mode 100644 index 6e7fda1..0000000 --- a/src/test/kotlin/net/pterodactylus/sone/freenet/KeyTest.kt +++ /dev/null @@ -1,51 +0,0 @@ -package net.pterodactylus.sone.freenet - -import freenet.keys.* -import net.pterodactylus.sone.freenet.Key.* -import org.hamcrest.MatcherAssert.* -import org.hamcrest.Matchers.* -import org.junit.* - -/** - * Unit test for [Key]. - */ -class KeyTest { - - private val uri = FreenetURI("SSK@$routingKey,$cryptoKey,$extra/some-site-12/foo/bar.html") - private val key: Key = from(uri) - - @Test - fun keyCanBeCreatedFromFreenetUri() { - assertThat(key.routingKey, equalTo(routingKey)) - assertThat(key.cryptoKey, equalTo(cryptoKey)) - assertThat(key.extra, equalTo(extra)) - } - - @Test - fun keyCanBeConvertedToUsk() { - val uskUri = key.toUsk("other-site", 15, "some", "path.html") - assertThat(uskUri.toString(), equalTo("USK@$routingKey,$cryptoKey,$extra/other-site/15/some/path.html")) - } - - @Test - fun keyCanBeConvertedToSskWithoutEdition() { - val uskUri = key.toSsk("other-site", "some", "path.html") - assertThat(uskUri.toString(), equalTo("SSK@$routingKey,$cryptoKey,$extra/other-site/some/path.html")) - } - - @Test - fun keyCanBeConvertedToSskWithEdition() { - val uskUri = key.toSsk("other-site", 15, "some", "path.html") - assertThat(uskUri.toString(), equalTo("SSK@$routingKey,$cryptoKey,$extra/other-site-15/some/path.html")) - } - - @Test - fun routingKeyIsExtractCorrectly() { - assertThat(routingKey(uri), equalTo(routingKey)) - } - -} - -private const val routingKey = "NfUYvxDwU9vqb2mh-qdT~DYJ6U0XNbxMGGoLe0aCHJs" -private const val cryptoKey = "Miglsgix0VR56ZiPl4NgjnUd~UdrnHqIvXJ3KKHmxmI" -private const val extra = "AQACAAE"