From 239f08e37721401290f43596e8a7e9aa83aae980 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 28 Sep 2014 13:56:36 +0200 Subject: [PATCH] =?utf8?q?Don=E2=80=99t=20set=20the=20request=20URI=20anym?= =?utf8?q?ore,=20either.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit It can also be calculcated from the identity’s key, together with the latest edition of the Sone. --- .../java/net/pterodactylus/sone/core/Core.java | 1 - .../sone/core/SoneDownloaderImpl.java | 16 -- .../java/net/pterodactylus/sone/data/Sone.java | 9 -- .../java/net/pterodactylus/sone/data/SoneImpl.java | 29 ++-- .../pterodactylus/sone/data/impl/IdOnlySone.java | 5 - .../sone/core/SoneDownloaderTest.java | 173 +++++++++++---------- .../core/sone-parser-with-invalid-request-uri.xml | 7 - .../sone/core/sone-parser-with-request-uri.xml | 7 - 8 files changed, 100 insertions(+), 147 deletions(-) delete mode 100644 src/test/resources/net/pterodactylus/sone/core/sone-parser-with-invalid-request-uri.xml delete mode 100644 src/test/resources/net/pterodactylus/sone/core/sone-parser-with-request-uri.xml diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index ef05eba..26f9def 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -736,7 +736,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } boolean newSone = !existingSone.isPresent(); Sone sone = !newSone ? existingSone.get() : database.newSoneBuilder().from(identity).build(); - sone.setRequestUri(SoneUri.create(identity.getRequestUri())); sone.setLatestEdition(latestEdition); if (newSone) { synchronized (knownSones) { diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java index 28d4521..d4ed927 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java @@ -236,11 +236,6 @@ public class SoneDownloaderImpl extends AbstractService implements SoneDownloade Sone parsedSone = parseSone(originalSone, soneInputStream); if (parsedSone != null) { parsedSone.setLatestEdition(requestUri.getEdition()); - if (requestUri.getKeyType().equals("USK")) { - parsedSone.setRequestUri(requestUri.setMetaString(new String[0])); - } else { - parsedSone.setRequestUri(requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0])); - } } return parsedSone; } catch (Exception e1) { @@ -340,17 +335,6 @@ public class SoneDownloaderImpl extends AbstractService implements SoneDownloade sone.setClient(new Client(clientName, clientVersion)); } - String soneRequestUri = soneXml.getValue("request-uri", null); - if (soneRequestUri != null) { - try { - sone.setRequestUri(new FreenetURI(soneRequestUri)); - } catch (MalformedURLException mue1) { - /* TODO - mark Sone as bad. */ - logger.log(Level.WARNING, String.format("Downloaded Sone %s has invalid request URI: %s", sone, soneRequestUri), mue1); - return null; - } - } - SimpleXML profileXml = soneXml.getNode("profile"); if (profileXml == null) { /* TODO - mark Sone as bad. */ diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java index 3b36f46..b23e48e 100644 --- a/src/main/java/net/pterodactylus/sone/data/Sone.java +++ b/src/main/java/net/pterodactylus/sone/data/Sone.java @@ -228,15 +228,6 @@ public interface Sone extends Identified, Fingerprintable, Comparable { FreenetURI getRequestUri(); /** - * Sets the request URI of this Sone. - * - * @param requestUri - * The request URI of this Sone - * @return This Sone (for method chaining) - */ - Sone setRequestUri(FreenetURI requestUri); - - /** * Returns the insert URI of this Sone. * * @return The insert URI of this Sone diff --git a/src/main/java/net/pterodactylus/sone/data/SoneImpl.java b/src/main/java/net/pterodactylus/sone/data/SoneImpl.java index 08c104f..86c5935 100644 --- a/src/main/java/net/pterodactylus/sone/data/SoneImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/SoneImpl.java @@ -167,26 +167,17 @@ public class SoneImpl implements Sone { * @return The request URI of this Sone */ public FreenetURI getRequestUri() { - return (requestUri != null) ? requestUri.setSuggestedEdition(latestEdition) : null; - } - - /** - * Sets the request URI of this Sone. - * - * @param requestUri - * The request URI of this Sone - * @return This Sone (for method chaining) - */ - public Sone setRequestUri(FreenetURI requestUri) { - if (this.requestUri == null) { - this.requestUri = requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]); - return this; - } - if (!this.requestUri.equalsKeypair(requestUri)) { - logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", requestUri, this.requestUri)); - return this; + try { + return new FreenetURI(getIdentity().getRequestUri()) + .setKeyType("USK") + .setDocName("Sone") + .setMetaString(new String[0]) + .setSuggestedEdition(latestEdition); + } catch (MalformedURLException e) { + throw new IllegalStateException( + format("Identity %s's request URI is incorrect.", + getIdentity()), e); } - return this; } /** diff --git a/src/main/java/net/pterodactylus/sone/data/impl/IdOnlySone.java b/src/main/java/net/pterodactylus/sone/data/impl/IdOnlySone.java index 1cc1801..fa7aace 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/IdOnlySone.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/IdOnlySone.java @@ -53,11 +53,6 @@ public class IdOnlySone implements Sone { } @Override - public Sone setRequestUri(FreenetURI requestUri) { - return null; - } - - @Override public FreenetURI getInsertUri() { return null; } diff --git a/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java b/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java index 28879bf..127837a 100644 --- a/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java +++ b/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java @@ -1,6 +1,7 @@ package net.pterodactylus.sone.core; import static com.google.common.base.Optional.of; +import static freenet.keys.InsertableClientSSK.createRandom; import static java.lang.System.currentTimeMillis; import static java.util.UUID.randomUUID; import static java.util.concurrent.TimeUnit.DAYS; @@ -56,7 +57,10 @@ import net.pterodactylus.sone.freenet.wot.Identity; import freenet.client.ClientMetadata; import freenet.client.FetchResult; import freenet.client.async.USKCallback; +import freenet.crypt.DummyRandomSource; +import freenet.keys.ClientSSK; import freenet.keys.FreenetURI; +import freenet.keys.InsertableClientSSK; import freenet.support.api.Bucket; import com.google.common.base.Optional; @@ -79,7 +83,7 @@ public class SoneDownloaderTest { private final Core core = mock(Core.class); private final FreenetInterface freenetInterface = mock(FreenetInterface.class); private final SoneDownloaderImpl soneDownloader = new SoneDownloaderImpl(core, freenetInterface); - private final FreenetURI requestUri = mock(FreenetURI.class); + private FreenetURI requestUri = mock(FreenetURI.class); private Sone sone = mock(Sone.class); private final PostBuilder postBuilder = mock(PostBuilder.class); private final List createdPosts = new ArrayList(); @@ -100,10 +104,19 @@ public class SoneDownloaderTest { public void setupSone() { Sone sone = SoneDownloaderTest.this.sone; Identity identity = mock(Identity.class); + InsertableClientSSK clientSSK = createRandom(new DummyRandomSource(), "WoT"); + when(identity.getRequestUri()).thenReturn(clientSSK.getURI().toString()); when(identity.getId()).thenReturn("identity"); when(sone.getId()).thenReturn("identity"); when(sone.getIdentity()).thenReturn(identity); - when(sone.getRequestUri()).thenReturn(requestUri); + requestUri = clientSSK.getURI().setKeyType("USK").setDocName("Sone"); + when(sone.getRequestUri()).thenAnswer(new Answer() { + @Override + public FreenetURI answer(InvocationOnMock invocation) + throws Throwable { + return requestUri; + } + }); when(sone.getTime()).thenReturn(currentTimeMillis() - DAYS.toMillis(1)); } @@ -112,15 +125,6 @@ public class SoneDownloaderTest { } @Before - public void setupRequestUri() { - when(requestUri.setKeyType(anyString())).thenReturn(requestUri); - when(requestUri.sskForUSK()).thenReturn(requestUri); - when(requestUri.setDocName(anyString())).thenReturn(requestUri); - when(requestUri.setMetaString(any(String[].class))).thenReturn(requestUri); - when(requestUri.getKeyType()).thenReturn("USK"); - } - - @Before public void setupSoneBuilder() { when(core.soneBuilder()).thenAnswer(new Answer() { @Override @@ -196,30 +200,42 @@ public class SoneDownloaderTest { return postReplyBuilder; } }); - when(postReplyBuilder.from(anyString())).thenAnswer(new Answer() { - @Override - public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable { - Sone sone = when(mock(Sone.class).getId()).thenReturn((String) invocation.getArguments()[0]).getMock(); - when(postReply.getSone()).thenReturn(sone); - return postReplyBuilder; - } - }); - when(postReplyBuilder.to(anyString())).thenAnswer(new Answer() { - @Override - public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable { - when(postReply.getPostId()).thenReturn((String) invocation.getArguments()[0]); - Post post = when(mock(Post.class).getId()).thenReturn((String) invocation.getArguments()[0]).getMock(); - when(postReply.getPost()).thenReturn(of(post)); - return postReplyBuilder; - } - }); - when(postReplyBuilder.withTime(anyLong())).thenAnswer(new Answer() { - @Override - public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable { - when(postReply.getTime()).thenReturn((Long) invocation.getArguments()[0]); - return postReplyBuilder; - } - }); + when(postReplyBuilder.from(anyString())).thenAnswer( + new Answer() { + @Override + public PostReplyBuilder answer( + InvocationOnMock invocation) throws Throwable { + Sone sone = when(mock(Sone.class).getId()).thenReturn( + (String) invocation.getArguments()[0]) + .getMock(); + when(postReply.getSone()).thenReturn(sone); + return postReplyBuilder; + } + }); + when(postReplyBuilder.to(anyString())).thenAnswer( + new Answer() { + @Override + public PostReplyBuilder answer( + InvocationOnMock invocation) throws Throwable { + when(postReply.getPostId()).thenReturn( + (String) invocation.getArguments()[0]); + Post post = when(mock(Post.class).getId()).thenReturn( + (String) invocation.getArguments()[0]) + .getMock(); + when(postReply.getPost()).thenReturn(of(post)); + return postReplyBuilder; + } + }); + when(postReplyBuilder.withTime(anyLong())).thenAnswer( + new Answer() { + @Override + public PostReplyBuilder answer( + InvocationOnMock invocation) throws Throwable { + when(postReply.getTime()).thenReturn( + (Long) invocation.getArguments()[0]); + return postReplyBuilder; + } + }); when(postReplyBuilder.withText(anyString())).thenAnswer(new Answer() { @Override public PostReplyBuilder answer(InvocationOnMock invocation) throws Throwable { @@ -345,7 +361,8 @@ public class SoneDownloaderTest { public void setupAlbums() { when(core.getAlbum(anyString())).thenAnswer(new Answer() { @Override - public Album answer(InvocationOnMock invocation) throws Throwable { + public Album answer(InvocationOnMock invocation) + throws Throwable { return albums.get(invocation.getArguments()[0]); } }); @@ -536,13 +553,15 @@ public class SoneDownloaderTest { @Test public void parsingASoneSucceedsWithoutPayload() throws SoneException { InputStream inputStream = getClass().getResourceAsStream("sone-parser-no-payload.xml"); - assertThat(soneDownloader.parseSone(sone, inputStream).getTime(), is(1407197508000L)); + assertThat(soneDownloader.parseSone(sone, inputStream).getTime(), is( + 1407197508000L)); } @Test public void parsingASoneSucceedsWithoutProtocolVersion() throws SoneException { InputStream inputStream = getClass().getResourceAsStream("sone-parser-missing-protocol-version.xml"); - assertThat(soneDownloader.parseSone(sone, inputStream), not(nullValue())); + assertThat(soneDownloader.parseSone(sone, inputStream), not( + nullValue())); } @Test @@ -564,18 +583,6 @@ public class SoneDownloaderTest { } @Test - public void parsingASoneSucceedsWithRequestUri() throws SoneException, MalformedURLException { - InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-request-uri.xml"); - assertThat(soneDownloader.parseSone(sone, inputStream).getRequestUri().toString(), is("USK@w~RyTGmv12Lg9oO91q1Untupi7my9qczT1RheGkEkVE,E8ElVfUgukSCPHxIEJp-gHMiR80wpM7sID3Jo5O7w1s,AQACAAE/Sone/0")); - } - - @Test - public void parsingASoneFailsWithInvalidRequestUri() throws SoneException, MalformedURLException { - InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-invalid-request-uri.xml"); - assertThat(soneDownloader.parseSone(sone, inputStream), nullValue()); - } - - @Test public void parsingASoneSucceedsWithProfile() throws SoneException, MalformedURLException { InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-profile.xml"); final Profile profile = soneDownloader.parseSone(sone, inputStream).getProfile(); @@ -637,7 +644,8 @@ public class SoneDownloaderTest { assertThat(posts.get(0).getSone().getId(), is(sone.getId())); assertThat(posts.get(0).getId(), is("post-id")); assertThat(posts.get(0).getTime(), is(1407197508000L)); - assertThat(posts.get(0).getRecipientId(), is(of("1234567890123456789012345678901234567890123"))); + assertThat(posts.get(0).getRecipientId(), is(of( + "1234567890123456789012345678901234567890123"))); assertThat(posts.get(0).getText(), is("text")); } @@ -700,31 +708,36 @@ public class SoneDownloaderTest { @Test public void parsingASoneSucceedsWithoutLikedPostIds() throws SoneException, MalformedURLException { InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-liked-post-ids.xml"); - assertThat(soneDownloader.parseSone(sone, inputStream), not(nullValue())); + assertThat(soneDownloader.parseSone(sone, inputStream), not( + nullValue())); } @Test public void parsingASoneSucceedsWithLikedPostIds() throws SoneException, MalformedURLException { InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-liked-post-ids.xml"); - assertThat(soneDownloader.parseSone(sone, inputStream).getLikedPostIds(), is((Set) ImmutableSet.of("liked-post-id"))); + assertThat(soneDownloader.parseSone(sone, inputStream).getLikedPostIds(), is( + (Set) ImmutableSet.of("liked-post-id"))); } @Test public void parsingASoneSucceedsWithoutLikedPostReplyIds() throws SoneException, MalformedURLException { InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-liked-post-reply-ids.xml"); - assertThat(soneDownloader.parseSone(sone, inputStream), not(nullValue())); + assertThat(soneDownloader.parseSone(sone, inputStream), not( + nullValue())); } @Test public void parsingASoneSucceedsWithLikedPostReplyIds() throws SoneException, MalformedURLException { InputStream inputStream = getClass().getResourceAsStream("sone-parser-with-liked-post-reply-ids.xml"); - assertThat(soneDownloader.parseSone(sone, inputStream).getLikedReplyIds(), is((Set) ImmutableSet.of("liked-post-reply-id"))); + assertThat(soneDownloader.parseSone(sone, inputStream).getLikedReplyIds(), is( + (Set) ImmutableSet.of("liked-post-reply-id"))); } @Test public void parsingASoneSucceedsWithoutAlbums() throws SoneException, MalformedURLException { InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-albums.xml"); - assertThat(soneDownloader.parseSone(sone, inputStream), not(nullValue())); + assertThat(soneDownloader.parseSone(sone, inputStream), not( + nullValue())); } @Test @@ -766,7 +779,8 @@ public class SoneDownloaderTest { @Test public void parsingASoneSucceedsWithoutImages() throws SoneException, MalformedURLException { InputStream inputStream = getClass().getResourceAsStream("sone-parser-without-images.xml"); - assertThat(soneDownloader.parseSone(sone, inputStream), not(nullValue())); + assertThat(soneDownloader.parseSone(sone, inputStream), not( + nullValue())); } @Test @@ -844,9 +858,11 @@ public class SoneDownloaderTest { @Test public void notBeingAbleToFetchAnUnknownSoneDoesNotUpdateCore() { + FreenetURI finalRequestUri = requestUri.sskForUSK() + .setMetaString(new String[] { "sone.xml" }); setupSoneAsUnknown(); soneDownloader.fetchSoneAction(sone).run(); - verify(freenetInterface).fetchUri(requestUri); + verify(freenetInterface).fetchUri(finalRequestUri); verifyThatSoneStatusWasChangedToDownloadingAndBackTo(unknown); verify(core, never()).updateSone(any(Sone.class)); } @@ -860,20 +876,24 @@ public class SoneDownloaderTest { @Test public void notBeingAbleToFetchAKnownSoneDoesNotUpdateCore() { + FreenetURI finalRequestUri = requestUri.sskForUSK() + .setMetaString(new String[] { "sone.xml" }); soneDownloader.fetchSoneAction(sone).run(); - verify(freenetInterface).fetchUri(requestUri); + verify(freenetInterface).fetchUri(finalRequestUri); verifyThatSoneStatusWasChangedToDownloadingAndBackTo(idle); verify(core, never()).updateSone(any(Sone.class)); } @Test(expected = NullPointerException.class) public void exceptionWhileFetchingAnUnknownSoneDoesNotUpdateCore() { + FreenetURI finalRequestUri = requestUri.sskForUSK() + .setMetaString(new String[] { "sone.xml" }); setupSoneAsUnknown(); - when(freenetInterface.fetchUri(requestUri)).thenThrow(NullPointerException.class); + when(freenetInterface.fetchUri(finalRequestUri)).thenThrow(NullPointerException.class); try { soneDownloader.fetchSoneAction(sone).run(); } finally { - verify(freenetInterface).fetchUri(requestUri); + verify(freenetInterface).fetchUri(finalRequestUri); verifyThatSoneStatusWasChangedToDownloadingAndBackTo(unknown); verify(core, never()).updateSone(any(Sone.class)); } @@ -881,11 +901,13 @@ public class SoneDownloaderTest { @Test(expected = NullPointerException.class) public void exceptionWhileFetchingAKnownSoneDoesNotUpdateCore() { - when(freenetInterface.fetchUri(requestUri)).thenThrow(NullPointerException.class); + FreenetURI finalRequestUri = requestUri.sskForUSK() + .setMetaString(new String[] { "sone.xml" }); + when(freenetInterface.fetchUri(finalRequestUri)).thenThrow( NullPointerException.class); try { soneDownloader.fetchSoneAction(sone).run(); } finally { - verify(freenetInterface).fetchUri(requestUri); + verify(freenetInterface).fetchUri(finalRequestUri); verifyThatSoneStatusWasChangedToDownloadingAndBackTo(idle); verify(core, never()).updateSone(any(Sone.class)); } @@ -893,8 +915,11 @@ public class SoneDownloaderTest { @Test public void successfulFetchingOfSoneWithUskRequestUriUpdatesTheCoreWithASone() throws IOException { - final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-no-payload.xml")); - when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult); + FreenetURI finalRequestUri = requestUri.sskForUSK() + .setMetaString(new String[] { "sone.xml" }); + final Fetched fetchResult = createFetchResult(finalRequestUri, + getClass().getResourceAsStream("sone-parser-no-payload.xml")); + when(freenetInterface.fetchUri(finalRequestUri)).thenReturn(fetchResult); soneDownloader.fetchSoneAction(sone).run(); verifyThatParsedSoneHasTheSameIdAsTheOriginalSone(); } @@ -906,24 +931,6 @@ public class SoneDownloaderTest { } @Test - public void successfulFetchingOfSoneWithSskRequestUriUpdatesTheCoreWithASone() throws IOException { - when(requestUri.getKeyType()).thenReturn("SSK"); - final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-no-payload.xml")); - when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult); - soneDownloader.fetchSoneAction(sone).run(); - verifyThatParsedSoneHasTheSameIdAsTheOriginalSone(); - } - - @Test - public void successfulFetchingAnUnknownSoneWithSskRequestUriUpdatesTheCoreWithASone() throws IOException { - when(requestUri.getKeyType()).thenReturn("SSK"); - final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-with-zero-time.xml")); - when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult); - soneDownloader.fetchSoneAction(sone).run(); - verifyThatParsedSoneHasTheSameIdAsTheOriginalSone(); - } - - @Test public void fetchingSoneWithInvalidXmlWillNotUpdateTheCore() throws IOException { final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-not-xml.xml")); when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult); diff --git a/src/test/resources/net/pterodactylus/sone/core/sone-parser-with-invalid-request-uri.xml b/src/test/resources/net/pterodactylus/sone/core/sone-parser-with-invalid-request-uri.xml deleted file mode 100644 index 5bcb0cc..0000000 --- a/src/test/resources/net/pterodactylus/sone/core/sone-parser-with-invalid-request-uri.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - 0 - - invalid-request-uri - - diff --git a/src/test/resources/net/pterodactylus/sone/core/sone-parser-with-request-uri.xml b/src/test/resources/net/pterodactylus/sone/core/sone-parser-with-request-uri.xml deleted file mode 100644 index 5271c34..0000000 --- a/src/test/resources/net/pterodactylus/sone/core/sone-parser-with-request-uri.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - 0 - - USK@w~RyTGmv12Lg9oO91q1Untupi7my9qczT1RheGkEkVE,E8ElVfUgukSCPHxIEJp-gHMiR80wpM7sID3Jo5O7w1s,AQACAAE/Sone/0 - - -- 2.7.4