X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ffcp%2FFcpInterfaceTest.kt;h=e9137b06258adde6f5f9790f96249d62afbab605;hb=31de5826e7dd5d063a9c81152326d80ab87105eb;hp=9e9df1638daabab9e68f8cfac5d979a8dbc01d90;hpb=0747ce52deab6ed6e4c3118959cf76827c2ce495;p=Sone.git diff --git a/src/test/java/net/pterodactylus/sone/fcp/FcpInterfaceTest.kt b/src/test/java/net/pterodactylus/sone/fcp/FcpInterfaceTest.kt index 9e9df16..e9137b0 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/FcpInterfaceTest.kt +++ b/src/test/java/net/pterodactylus/sone/fcp/FcpInterfaceTest.kt @@ -2,10 +2,13 @@ package net.pterodactylus.sone.fcp +import com.google.inject.Guice import freenet.pluginmanager.PluginNotFoundException import freenet.pluginmanager.PluginReplySender import freenet.support.SimpleFieldSet import net.pterodactylus.sone.core.Core +import net.pterodactylus.sone.fcp.FcpInterface.AccessAuthorizer +import net.pterodactylus.sone.fcp.FcpInterface.CommandSupplier import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.ALWAYS import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.NO @@ -13,16 +16,22 @@ import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.WRITING import net.pterodactylus.sone.fcp.event.FcpInterfaceActivatedEvent import net.pterodactylus.sone.fcp.event.FcpInterfaceDeactivatedEvent import net.pterodactylus.sone.fcp.event.FullAccessRequiredChanged +import net.pterodactylus.sone.freenet.fcp.Command.AccessType import net.pterodactylus.sone.freenet.fcp.Command.AccessType.FULL_FCP import net.pterodactylus.sone.freenet.fcp.Command.AccessType.RESTRICTED_FCP -import net.pterodactylus.sone.main.SonePlugin +import net.pterodactylus.sone.freenet.fcp.Command.Response +import net.pterodactylus.sone.test.bindAs import net.pterodactylus.sone.test.capture import net.pterodactylus.sone.test.mock import net.pterodactylus.sone.test.whenever import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.containsInAnyOrder import org.hamcrest.Matchers.equalTo +import org.hamcrest.Matchers.sameInstance import org.junit.Test import org.mockito.ArgumentMatchers +import org.mockito.Mockito.any +import org.mockito.Mockito.anyBoolean import org.mockito.Mockito.verify /** @@ -31,12 +40,35 @@ import org.mockito.Mockito.verify class FcpInterfaceTest { private val core = mock() - private val fcpInterface = FcpInterface(core) + private val workingCommand = mock().apply { + whenever(execute(any())).thenReturn(Response("Working", SimpleFieldSet(true).apply { + putSingle("ReallyWorking", "true") + })) + } + private val brokenCommand = mock().apply { + whenever(execute(any())).thenThrow(RuntimeException::class.java) + } + private val commandSupplier = object : CommandSupplier() { + override fun supplyCommands(core: Core): Map { + return mapOf( + "Working" to workingCommand, + "Broken" to brokenCommand + ) + } + } + private val accessAuthorizer = mock() + private val fcpInterface = FcpInterface(core, commandSupplier, accessAuthorizer) private val pluginReplySender = mock() private val parameters = SimpleFieldSet(true) private val replyParameters = capture() @Test + fun `fcp interface is instantiated as singleton`() { + val injector = Guice.createInjector(core.bindAs(Core::class)) + assertThat(injector.getInstance(FcpInterface::class.java), sameInstance(injector.getInstance(FcpInterface::class.java))) + } + + @Test fun `fcp interface can be activated`() { fcpInterface.fcpInterfaceActivated(FcpInterfaceActivatedEvent()) assertThat(fcpInterface.isActive, equalTo(true)) @@ -85,8 +117,9 @@ class FcpInterfaceTest { } @Test - fun `sending command over restricted fcp connection results in 401 error reply`() { + fun `sending command over non-authorized connection results in 401 error reply`() { fcpInterface.fcpInterfaceActivated(FcpInterfaceActivatedEvent()) + parameters.putSingle("Message", "Working") fcpInterface.handle(pluginReplySender, parameters, null, RESTRICTED_FCP.ordinal) verify(pluginReplySender).send(replyParameters.capture()) assertThat(replyParameters.value["Message"], equalTo("Error")) @@ -94,18 +127,19 @@ class FcpInterfaceTest { } @Test - fun `sending unknown command over full access connection results in 404 error reply`() { + fun `sending unknown command results in 404 error reply`() { fcpInterface.fcpInterfaceActivated(FcpInterfaceActivatedEvent()) - fcpInterface.handle(pluginReplySender, parameters, null, FULL_FCP.ordinal) + fcpInterface.handle(pluginReplySender, parameters, null, RESTRICTED_FCP.ordinal) verify(pluginReplySender).send(replyParameters.capture()) assertThat(replyParameters.value["Message"], equalTo("Error")) assertThat(replyParameters.value["ErrorCode"], equalTo("404")) } @Test - fun `sending version command without identifier results in 400 error code`() { + fun `sending working command without identifier results in 400 error code`() { fcpInterface.fcpInterfaceActivated(FcpInterfaceActivatedEvent()) - parameters.putSingle("Message", "Version") + whenever(accessAuthorizer.authorized(any(), any(), anyBoolean())).thenReturn(true) + parameters.putSingle("Message", "Working") fcpInterface.handle(pluginReplySender, parameters, null, FULL_FCP.ordinal) verify(pluginReplySender).send(replyParameters.capture()) assertThat(replyParameters.value["Message"], equalTo("Error")) @@ -113,9 +147,10 @@ class FcpInterfaceTest { } @Test - fun `sending version command with empty identifier results in 400 error code`() { + fun `sending working command with empty identifier results in 400 error code`() { fcpInterface.fcpInterfaceActivated(FcpInterfaceActivatedEvent()) - parameters.putSingle("Message", "Version") + whenever(accessAuthorizer.authorized(any(), any(), anyBoolean())).thenReturn(true) + parameters.putSingle("Message", "Working") parameters.putSingle("Identifier", "") fcpInterface.handle(pluginReplySender, parameters, null, FULL_FCP.ordinal) verify(pluginReplySender).send(replyParameters.capture()) @@ -124,15 +159,89 @@ class FcpInterfaceTest { } @Test - fun `sending version command with identifier results in version reply`() { + fun `sending working command with identifier results in working reply`() { fcpInterface.fcpInterfaceActivated(FcpInterfaceActivatedEvent()) - parameters.putSingle("Message", "Version") + whenever(accessAuthorizer.authorized(any(), any(), anyBoolean())).thenReturn(true) + parameters.putSingle("Message", "Working") parameters.putSingle("Identifier", "Test") fcpInterface.handle(pluginReplySender, parameters, null, FULL_FCP.ordinal) verify(pluginReplySender).send(replyParameters.capture()) - assertThat(replyParameters.value["Message"], equalTo("Version")) - assertThat(replyParameters.value["Version"], equalTo(SonePlugin.getPluginVersion())) - assertThat(replyParameters.value["ProtocolVersion"], equalTo("1")) + assertThat(replyParameters.value["Message"], equalTo("Working")) + assertThat(replyParameters.value["ReallyWorking"], equalTo("true")) + } + + @Test + fun `sending broken command with identifier results in 500 error reply`() { + fcpInterface.fcpInterfaceActivated(FcpInterfaceActivatedEvent()) + whenever(accessAuthorizer.authorized(any(), any(), anyBoolean())).thenReturn(true) + parameters.putSingle("Message", "Broken") + parameters.putSingle("Identifier", "Test") + fcpInterface.handle(pluginReplySender, parameters, null, FULL_FCP.ordinal) + verify(pluginReplySender).send(replyParameters.capture()) + assertThat(replyParameters.value["Message"], equalTo("Error")) + assertThat(replyParameters.value["ErrorCode"], equalTo("500")) + } + +} + +class CommandSupplierTest { + + private val core = mock() + private val commandSupplier = CommandSupplier() + + @Test + fun `command supplier supplies all commands`() { + val commands = commandSupplier.supplyCommands(core) + assertThat(commands.keys, containsInAnyOrder( + "CreatePost", + "CreateReply", + "DeletePost", + "DeleteReply", + "GetLocalSones", + "GetPost", + "GetPostFeed", + "GetPosts", + "GetSone", + "GetSones", + "LikePost", + "LikeReply", + "LockSone", + "UnlockSone", + "Version" + )) + } + + @Test + fun `command supplier is instantiated as singleton`() { + val injector = Guice.createInjector() + assertThat(injector.getInstance(CommandSupplier::class.java), sameInstance(injector.getInstance(CommandSupplier::class.java))) + } + +} + +class AccessAuthorizerTest { + + private val accessAuthorizer = AccessAuthorizer() + + @Test + fun `access authorizer is instantiated as singleton`() { + val injector = Guice.createInjector() + assertThat(injector.getInstance(AccessAuthorizer::class.java), sameInstance(injector.getInstance(AccessAuthorizer::class.java))) + } + + @Test + fun `access authorizer makes correct decisions`() { + AccessType.values().forEach { accessType -> + FullAccessRequired.values().forEach { fullAccessRequired -> + listOf(false, true).forEach { commandRequiresWriteAccess -> + assertThat("$accessType, $fullAccessRequired, $commandRequiresWriteAccess", accessAuthorizer.authorized(accessType, fullAccessRequired, commandRequiresWriteAccess), equalTo( + accessType != RESTRICTED_FCP || + fullAccessRequired == NO || + (fullAccessRequired == WRITING && !commandRequiresWriteAccess) + )) + } + } + } } }