package net.pterodactylus.fcp.quelaton;
+import static net.pterodactylus.fcp.RequestProgressMatcher.isRequestProgress;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.contains;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import net.pterodactylus.fcp.PeerNote;
import net.pterodactylus.fcp.PluginInfo;
import net.pterodactylus.fcp.Priority;
+import net.pterodactylus.fcp.RequestProgress;
import net.pterodactylus.fcp.fake.FakeTcpServer;
import net.pterodactylus.fcp.quelaton.ClientGetCommand.Data;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.junit.After;
import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Ignore;
import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
/**
};
}
- @Test
- public void defaultFcpClientCanGetNodeInformation() throws InterruptedException, ExecutionException, IOException {
- Future<NodeData> nodeData = fcpClient.getNode().execute();
- connectNode();
- List<String> lines = fcpServer.collectUntil(is("EndMessage"));
- String identifier = extractIdentifier(lines);
- assertThat(lines, matchesFcpMessage(
- "GetNode",
- "Identifier=" + identifier,
- "GiveOpennetRef=false",
- "WithPrivate=false",
- "WithVolatile=false"
- ));
- fcpServer.writeLine(
- "NodeData",
- "Identifier=" + identifier,
- "ark.pubURI=SSK@3YEf.../ark",
- "ark.number=78",
- "auth.negTypes=2",
- "version=Fred,0.7,1.0,1466",
- "lastGoodVersion=Fred,0.7,1.0,1466",
- "EndMessage"
- );
- assertThat(nodeData.get(), notNullValue());
- }
-
- @Test
- public void defaultFcpClientCanGetNodeInformationWithOpennetRef()
- throws InterruptedException, ExecutionException, IOException {
- Future<NodeData> nodeData = fcpClient.getNode().opennetRef().execute();
- connectNode();
- List<String> lines = fcpServer.collectUntil(is("EndMessage"));
- String identifier = extractIdentifier(lines);
- assertThat(lines, matchesFcpMessage(
- "GetNode",
- "Identifier=" + identifier,
- "GiveOpennetRef=true",
- "WithPrivate=false",
- "WithVolatile=false"
- ));
- fcpServer.writeLine(
- "NodeData",
- "Identifier=" + identifier,
- "opennet=true",
- "ark.pubURI=SSK@3YEf.../ark",
- "ark.number=78",
- "auth.negTypes=2",
- "version=Fred,0.7,1.0,1466",
- "lastGoodVersion=Fred,0.7,1.0,1466",
- "EndMessage"
- );
- assertThat(nodeData.get().getVersion().toString(), is("Fred,0.7,1.0,1466"));
- }
-
- @Test
- public void defaultFcpClientCanGetNodeInformationWithPrivateData()
- throws InterruptedException, ExecutionException, IOException {
- Future<NodeData> nodeData = fcpClient.getNode().includePrivate().execute();
- connectNode();
- List<String> lines = fcpServer.collectUntil(is("EndMessage"));
- String identifier = extractIdentifier(lines);
- assertThat(lines, matchesFcpMessage(
- "GetNode",
- "Identifier=" + identifier,
- "GiveOpennetRef=false",
- "WithPrivate=true",
- "WithVolatile=false"
- ));
- fcpServer.writeLine(
- "NodeData",
- "Identifier=" + identifier,
- "opennet=false",
- "ark.pubURI=SSK@3YEf.../ark",
- "ark.number=78",
- "auth.negTypes=2",
- "version=Fred,0.7,1.0,1466",
- "lastGoodVersion=Fred,0.7,1.0,1466",
- "ark.privURI=SSK@XdHMiRl",
- "EndMessage"
- );
- assertThat(nodeData.get().getARK().getPrivateURI(), is("SSK@XdHMiRl"));
- }
-
- @Test
- public void defaultFcpClientCanGetNodeInformationWithVolatileData()
- throws InterruptedException, ExecutionException, IOException {
- Future<NodeData> nodeData = fcpClient.getNode().includeVolatile().execute();
- connectNode();
- List<String> lines = fcpServer.collectUntil(is("EndMessage"));
- String identifier = extractIdentifier(lines);
- assertThat(lines, matchesFcpMessage(
- "GetNode",
- "Identifier=" + identifier,
- "GiveOpennetRef=false",
- "WithPrivate=false",
- "WithVolatile=true"
- ));
- fcpServer.writeLine(
- "NodeData",
- "Identifier=" + identifier,
- "opennet=false",
- "ark.pubURI=SSK@3YEf.../ark",
- "ark.number=78",
- "auth.negTypes=2",
- "version=Fred,0.7,1.0,1466",
- "lastGoodVersion=Fred,0.7,1.0,1466",
- "volatile.freeJavaMemory=205706528",
- "EndMessage"
- );
- assertThat(nodeData.get().getVolatile("freeJavaMemory"), is("205706528"));
- }
-
private List<String> lines;
private String identifier;
assertThat(lines, requestMatcher.get());
}
+ private void replyWithProtocolError() throws IOException {
+ fcpServer.writeLine(
+ "ProtocolError",
+ "Identifier=" + identifier,
+ "EndMessage"
+ );
+ }
+
public class ConnectionsAndKeyPairs {
public class Connections {
assertThat(peer.get().get().getIdentity(), is("id1"));
}
+ @Test
+ public void protocolErrorEndsCommand() throws InterruptedException, ExecutionException, IOException {
+ Future<Optional<Peer>> peer = fcpClient.addPeer().fromFile(new File("/tmp/ref.txt")).execute();
+ connectAndAssert(() -> allOf(matchesAddPeer(), hasItem("File=/tmp/ref.txt")));
+ replyWithProtocolError();
+ assertThat(peer.get().isPresent(), is(false));
+ }
+
private NodeRef createNodeRef() {
NodeRef nodeRef = new NodeRef();
nodeRef.setIdentity("id1");
}
- private void replyWithProtocolError() throws IOException {
- fcpServer.writeLine(
- "ProtocolError",
- "Identifier=" + identifier,
- "EndMessage"
- );
- }
-
public class ReloadPlugin {
@Test
verifyPluginInfo(pluginInfo);
}
+ @Test
+ public void protocolErrorIsRecognizedAsFailure()
+ throws InterruptedException, ExecutionException, IOException {
+ Future<Optional<PluginInfo>> pluginInfo = fcpClient.reloadPlugin().plugin(CLASS_NAME).execute();
+ connectAndAssert(() -> matchReloadPluginMessage());
+ replyWithProtocolError();
+ assertThat(pluginInfo.get().isPresent(), is(false));
+ }
+
private Matcher<List<String>> matchReloadPluginMessage() {
return matchesFcpMessage(
"ReloadPlugin",
);
}
- private Matcher<List<String>> matchesDirectClientPut() {
+ private Matcher<List<String>> matchesDirectClientPut(String... additionalLines) {
+ List<String> lines = new ArrayList<>(Arrays.asList("UploadFrom=direct", "DataLength=6", "URI=KSK@foo.txt"));
+ Arrays.asList(additionalLines).forEach(lines::add);
return allOf(
hasHead("ClientPut"),
- hasParameters(1, 2, "UploadFrom=direct", "DataLength=6", "URI=KSK@foo.txt"),
+ hasParameters(1, 2, lines.toArray(new String[lines.size()])),
hasTail("EndMessage", "Hello")
);
}
assertThat(generatedKeys, contains("KSK@foo.txt"));
}
+ @Test
+ public void clientPutSendsNotificationOnProgress() throws InterruptedException, ExecutionException, IOException {
+ List<RequestProgress> requestProgress = new ArrayList<>();
+ Future<Optional<Key>> key = fcpClient.clientPut()
+ .onProgress(requestProgress::add)
+ .from(new ByteArrayInputStream("Hello\n".getBytes()))
+ .length(6)
+ .uri("KSK@foo.txt")
+ .execute();
+ connectNode();
+ readMessage("Hello", () -> matchesDirectClientPut("Verbosity=1"));
+ replyWithSimpleProgress(1, 2, 3, 4, 5, 6, true, 8);
+ replyWithSimpleProgress(11, 12, 13, 14, 15, 16, false, 18);
+ replyWithPutSuccessful(identifier);
+ assertThat(key.get().get().getKey(), is("KSK@foo.txt"));
+ assertThat(requestProgress, contains(
+ isRequestProgress(1, 2, 3, 4, 5, 6, true, 8),
+ isRequestProgress(11, 12, 13, 14, 15, 16, false, 18)
+ ));
+ }
+
+ private void replyWithSimpleProgress(
+ int total, int required, int failed, int fatallyFailed, int succeeded, int lastProgress,
+ boolean finalizedTotal, int minSuccessFetchBlocks) throws IOException {
+ fcpServer.writeLine(
+ "SimpleProgress",
+ "Identifier=" + identifier,
+ "Total=" + total,
+ "Required=" + required,
+ "Failed=" + failed,
+ "FatallyFailed=" + fatallyFailed,
+ "Succeeded=" + succeeded,
+ "LastProgress=" + lastProgress,
+ "FinalizedTotal=" + finalizedTotal,
+ "MinSuccessFetchBlocks=" + minSuccessFetchBlocks,
+ "EndMessage"
+ );
+ }
+
+ }
+
+ public class ClientPutDiskDir {
+
+ private final TemporaryFolder folder = new TemporaryFolder();
+
+ @Before
+ public void setup() throws IOException {
+ folder.create();
+ Files.write("file1\n", folder.newFile("file1.txt"), StandardCharsets.UTF_8);
+ Files.write("file2\n", folder.newFile("file2.txt"), StandardCharsets.UTF_8);
+ File directory = folder.newFolder("dir");
+ Files.write("file3\n", new File(directory, "file3.txt"), StandardCharsets.UTF_8);
+ }
+
+ @After
+ public void removeFolder() {
+ folder.delete();
+ }
+
+ @Test
+ public void commandIsSentCorrectly() throws InterruptedException, ExecutionException, IOException {
+ Future<Optional<Key>> key = fcpClient.clientPutDiskDir().fromDirectory(folder.getRoot()).uri("CHK@").execute();
+ connectAndAssert(this::matchesClientPutDiskDir);
+ fcpServer.writeLine("PutSuccessful", "Identifier=" + identifier, "URI=CHK@abc", "EndMessage");
+ assertThat(key.get().get().getKey(), is("CHK@abc"));
+ }
+
+ @Test
+ public void protocolErrorAbortsCommand() throws InterruptedException, ExecutionException, IOException {
+ Future<Optional<Key>> key = fcpClient.clientPutDiskDir().fromDirectory(folder.getRoot()).uri("CHK@").execute();
+ connectAndAssert(this::matchesClientPutDiskDir);
+ replyWithProtocolError();
+ assertThat(key.get().isPresent(), is(false));
+ }
+
+ private Matcher<List<String>> matchesClientPutDiskDir() {
+ return matchesFcpMessage(
+ "ClientPutDiskDir",
+ "Identifier=" + identifier,
+ "URI=CHK@",
+ "Filename=" + folder.getRoot().getPath()
+ );
+ }
+
}
public class ConfigCommand {
}
+ public class NodeInformation {
+
+ @Test
+ public void defaultFcpClientCanGetNodeInformation() throws InterruptedException, ExecutionException, IOException {
+ Future<NodeData> nodeData = fcpClient.getNode().execute();
+ connectAndAssert(() -> matchesGetNode(false, false, false));
+ replyWithNodeData();
+ assertThat(nodeData.get(), notNullValue());
+ assertThat(nodeData.get().getNodeRef().isOpennet(), is(false));
+ }
+
+ @Test
+ public void defaultFcpClientCanGetNodeInformationWithOpennetRef()
+ throws InterruptedException, ExecutionException, IOException {
+ Future<NodeData> nodeData = fcpClient.getNode().opennetRef().execute();
+ connectAndAssert(() -> matchesGetNode(true, false, false));
+ replyWithNodeData("opennet=true");
+ assertThat(nodeData.get().getVersion().toString(), is("Fred,0.7,1.0,1466"));
+ assertThat(nodeData.get().getNodeRef().isOpennet(), is(true));
+ }
+
+ @Test
+ public void defaultFcpClientCanGetNodeInformationWithPrivateData()
+ throws InterruptedException, ExecutionException, IOException {
+ Future<NodeData> nodeData = fcpClient.getNode().includePrivate().execute();
+ connectAndAssert(() -> matchesGetNode(false, true, false));
+ replyWithNodeData("ark.privURI=SSK@XdHMiRl");
+ assertThat(nodeData.get().getARK().getPrivateURI(), is("SSK@XdHMiRl"));
+ }
+
+ @Test
+ public void defaultFcpClientCanGetNodeInformationWithVolatileData()
+ throws InterruptedException, ExecutionException, IOException {
+ Future<NodeData> nodeData = fcpClient.getNode().includeVolatile().execute();
+ connectAndAssert(() -> matchesGetNode(false, false, true));
+ replyWithNodeData("volatile.freeJavaMemory=205706528");
+ assertThat(nodeData.get().getVolatile("freeJavaMemory"), is("205706528"));
+ }
+
+ private Matcher<List<String>> matchesGetNode(boolean withOpennetRef, boolean withPrivate, boolean withVolatile) {
+ return matchesFcpMessage(
+ "GetNode",
+ "Identifier=" + identifier,
+ "GiveOpennetRef=" + withOpennetRef,
+ "WithPrivate=" + withPrivate,
+ "WithVolatile=" + withVolatile
+ );
+ }
+
+ private void replyWithNodeData(String... additionalLines) throws IOException {
+ fcpServer.writeLine(
+ "NodeData",
+ "Identifier=" + identifier,
+ "ark.pubURI=SSK@3YEf.../ark",
+ "ark.number=78",
+ "auth.negTypes=2",
+ "version=Fred,0.7,1.0,1466",
+ "lastGoodVersion=Fred,0.7,1.0,1466"
+ );
+ fcpServer.writeLine(additionalLines);
+ fcpServer.writeLine("EndMessage");
+ }
+
+ }
+
}