+++ /dev/null
-/*
- * Sone - SoneUri.java - Copyright © 2013–2020 David Roden
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.core;
-
-import static java.util.logging.Logger.getLogger;
-
-import java.net.MalformedURLException;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-import freenet.keys.FreenetURI;
-
-/**
- * Helper class that creates {@link FreenetURI}s for Sone to insert to and
- * request from.
- */
-public class SoneUri {
-
- /** The logger. */
- private static final Logger logger = getLogger(SoneUri.class.getName());
-
- /**
- * Generate a Sone URI from the given URI.
- *
- * @param uri
- * The URI to derive the Sone URI from
- * @return The derived URI
- */
- public static FreenetURI create(String uri) {
- try {
- return new FreenetURI(uri).setDocName("Sone").setMetaString(new String[0]);
- } catch (MalformedURLException mue1) {
- /* this should never happen. */
- logger.log(Level.WARNING, String.format("Could not create Sone URI from URI: %s", uri), mue1);
- return null;
- }
- }
-
-}
--- /dev/null
+package net.pterodactylus.sone.core
+
+import freenet.keys.FreenetURI
+import net.pterodactylus.sone.data.Sone
+import net.pterodactylus.sone.freenet.wot.OwnIdentity
+
+/**
+ * Injectable helper class that can create request and insert URIs for [Sones][Sone].
+ */
+class SoneUriCreator {
+
+ fun getRequestUri(sone: Sone): FreenetURI = sone.identity.requestUri
+ .let(::FreenetURI)
+ .sonify(sone.latestEdition)
+
+ fun getInsertUri(sone: Sone): FreenetURI? = (sone.identity as? OwnIdentity)?.insertUri
+ ?.let(::FreenetURI)
+ ?.sonify(sone.latestEdition)
+
+}
+
+private fun FreenetURI.sonify(edition: Long): FreenetURI =
+ setKeyType("USK")
+ .setDocName("Sone")
+ .setMetaString(emptyArray())
+ .setSuggestedEdition(edition)
--- /dev/null
+package net.pterodactylus.sone.core
+
+import net.pterodactylus.sone.data.impl.IdOnlySone
+import net.pterodactylus.sone.freenet.wot.DefaultIdentity
+import net.pterodactylus.sone.freenet.wot.DefaultOwnIdentity
+import net.pterodactylus.sone.test.createInsertUri
+import net.pterodactylus.sone.test.createRequestUri
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.emptyArray
+import org.hamcrest.Matchers.equalTo
+import org.hamcrest.Matchers.nullValue
+import kotlin.test.Test
+
+/**
+ * Unit test for [SoneUriCreator].
+ */
+class SoneUriCreatorTest {
+
+ private val soneUriCreator = SoneUriCreator()
+
+ private val requestUri = soneUriCreator.getRequestUri(sone)
+ private val insertUri = soneUriCreator.getInsertUri(sone)
+
+ @Test
+ fun `generated request URI is a USK`() {
+ assertThat(requestUri.keyType, equalTo("USK"))
+ }
+
+ @Test
+ fun `generated request URI has correct doc name`() {
+ assertThat(requestUri.docName, equalTo("Sone"))
+ }
+
+ @Test
+ fun `generated request URI has no meta strings`() {
+ assertThat(requestUri.allMetaStrings, emptyArray())
+ }
+
+ @Test
+ fun `generated request URI has correct edition`() {
+ assertThat(requestUri.suggestedEdition, equalTo(123L))
+ }
+
+ @Test
+ fun `insert URI is null if sone’s identity is not an own identity`() {
+ val remoteSone = object : IdOnlySone("id") {
+ override fun getIdentity() = DefaultIdentity("id", "name", createRequestUri.toString())
+ }
+ assertThat(soneUriCreator.getInsertUri(remoteSone), nullValue())
+ }
+
+ @Test
+ fun `generated insert URI is a USK`() {
+ assertThat(insertUri!!.keyType, equalTo("USK"))
+ }
+
+ @Test
+ fun `generated insert URI has correct doc name`() {
+ assertThat(insertUri!!.docName, equalTo("Sone"))
+ }
+
+ @Test
+ fun `generated insert URI has no meta strings`() {
+ assertThat(insertUri!!.allMetaStrings, emptyArray())
+ }
+
+ @Test
+ fun `generated insert URI has correct edition`() {
+ assertThat(insertUri!!.suggestedEdition, equalTo(123L))
+ }
+
+}
+
+private val sone = object : IdOnlySone("id") {
+ override fun getIdentity() =
+ DefaultOwnIdentity("id", "name", createRequestUri.toString(), createInsertUri.toString())
+
+ override fun getLatestEdition() = 123L
+}
+++ /dev/null
-package net.pterodactylus.sone.core;
-
-import static freenet.keys.InsertableClientSSK.createRandom;
-import static net.pterodactylus.sone.core.SoneUri.create;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.nullValue;
-
-import freenet.crypt.DummyRandomSource;
-import freenet.keys.FreenetURI;
-
-import org.junit.Test;
-
-/**
- * Unit test for {@link SoneUri}.
- */
-public class SoneUriTest {
-
- @Test
- public void callConstructorForIncreasedTestCoverage() {
- new SoneUri();
- }
-
- @Test
- public void returnedUriHasCorrectDocNameAndMetaStrings() {
- FreenetURI uri = createRandom(new DummyRandomSource(), "test-0").getURI().uskForSSK();
- assertThat(create(uri.toString()).getDocName(), is("Sone"));
- assertThat(create(uri.toString()).getAllMetaStrings(), is(new String[0]));
- }
-
- @Test
- public void malformedUriReturnsNull() {
- assertThat(create("not a key"), nullValue());
- }
-
-}
val localSone1 = createLocalSone()
val localSone2 = createLocalSone()
+val createRequestUri: FreenetURI get() = InsertableClientSSK.createRandom(DummyRandomSource(), "").uri
val createInsertUri: FreenetURI get() = InsertableClientSSK.createRandom(DummyRandomSource(), "").insertURI
fun createId() = InsertableClientSSK.createRandom(DummyRandomSource(), "").uri.routingKey.asFreenetBase64