- public void clientPutWithDirectDataSendsCorrectCommand()
- throws IOException, ExecutionException, InterruptedException {
- fcpClient.clientPut()
- .from(new ByteArrayInputStream("Hello\n".getBytes()))
- .length(6)
- .uri("KSK@foo.txt")
- .execute();
- connectNode();
- List<String> lines = fcpServer.collectUntil(is("Hello"));
- assertThat(lines, allOf(
- hasHead("ClientPut"),
- hasParameters(1, 2, "UploadFrom=direct", "DataLength=6", "URI=KSK@foo.txt"),
- hasTail("EndMessage", "Hello")
- ));
- }
-
- @Test
- public void clientPutWithDirectDataSucceedsOnCorrectIdentifier()
- throws InterruptedException, ExecutionException, IOException {
- Future<Optional<Key>> key = fcpClient.clientPut()
- .from(new ByteArrayInputStream("Hello\n".getBytes()))
- .length(6)
- .uri("KSK@foo.txt")
- .execute();
- connectNode();
- List<String> lines = fcpServer.collectUntil(is("Hello"));
- String identifier = extractIdentifier(lines);
- fcpServer.writeLine(
- "PutFailed",
- "Identifier=not-the-right-one",
- "EndMessage"
- );
- fcpServer.writeLine(
- "PutSuccessful",
- "URI=KSK@foo.txt",
- "Identifier=" + identifier,
- "EndMessage"
- );
- assertThat(key.get().get().getKey(), is("KSK@foo.txt"));
- }
-
- @Test
- public void clientPutWithDirectDataFailsOnCorrectIdentifier()
- throws InterruptedException, ExecutionException, IOException {
- Future<Optional<Key>> key = fcpClient.clientPut()
- .from(new ByteArrayInputStream("Hello\n".getBytes()))
- .length(6)
- .uri("KSK@foo.txt")
- .execute();
- connectNode();
- List<String> lines = fcpServer.collectUntil(is("Hello"));
- String identifier = extractIdentifier(lines);
- fcpServer.writeLine(
- "PutSuccessful",
- "Identifier=not-the-right-one",
- "URI=KSK@foo.txt",
- "EndMessage"
- );
- fcpServer.writeLine(
- "PutFailed",
- "Identifier=" + identifier,
- "EndMessage"
- );
- assertThat(key.get().isPresent(), is(false));
- }
-
- @Test
- public void clientPutWithRenamedDirectDataSendsCorrectCommand()
- throws InterruptedException, ExecutionException, IOException {
- fcpClient.clientPut()
- .named("otherName.txt")
- .from(new ByteArrayInputStream("Hello\n".getBytes()))
- .length(6)
- .uri("KSK@foo.txt")
- .execute();
- connectNode();
- List<String> lines = fcpServer.collectUntil(is("Hello"));
- assertThat(lines, allOf(
- hasHead("ClientPut"),
- hasParameters(1, 2, "TargetFilename=otherName.txt", "UploadFrom=direct", "DataLength=6", "URI=KSK@foo.txt"),
- hasTail("EndMessage", "Hello")
- ));
- }
-
- @Test
- public void clientPutWithRedirectSendsCorrectCommand()
- throws IOException, ExecutionException, InterruptedException {
- fcpClient.clientPut().redirectTo("KSK@bar.txt").uri("KSK@foo.txt").execute();
- connectNode();
- List<String> lines = fcpServer.collectUntil(is("EndMessage"));
- assertThat(lines,
- matchesFcpMessage("ClientPut", "UploadFrom=redirect", "URI=KSK@foo.txt", "TargetURI=KSK@bar.txt"));
- }
-
- @Test
- public void clientPutWithFileSendsCorrectCommand() throws InterruptedException, ExecutionException, IOException {
- fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt").execute();
- connectNode();
- List<String> lines = fcpServer.collectUntil(is("EndMessage"));
- assertThat(lines,
- matchesFcpMessage("ClientPut", "UploadFrom=disk", "URI=KSK@foo.txt", "Filename=/tmp/data.txt"));
- }
-
- @Test
- public void clientPutWithFileCanCompleteTestDdaSequence()
- throws IOException, ExecutionException, InterruptedException {
- File tempFile = createTempFile();
- fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).uri("KSK@foo.txt").execute();
- connectNode();
- List<String> lines = fcpServer.collectUntil(is("EndMessage"));
- String identifier = extractIdentifier(lines);
- fcpServer.writeLine(
- "ProtocolError",
- "Identifier=" + identifier,
- "Code=25",
- "EndMessage"
- );
- lines = fcpServer.collectUntil(is("EndMessage"));
- assertThat(lines, matchesFcpMessage(
- "TestDDARequest",
- "Directory=" + tempFile.getParent(),
- "WantReadDirectory=true",
- "WantWriteDirectory=false"
- ));
- fcpServer.writeLine(
- "TestDDAReply",
- "Directory=" + tempFile.getParent(),
- "ReadFilename=" + tempFile,
- "EndMessage"
- );
- lines = fcpServer.collectUntil(is("EndMessage"));
- assertThat(lines, matchesFcpMessage(
- "TestDDAResponse",
- "Directory=" + tempFile.getParent(),
- "ReadContent=test-content"
- ));
- fcpServer.writeLine(
- "TestDDAComplete",
- "Directory=" + tempFile.getParent(),
- "ReadDirectoryAllowed=true",
- "EndMessage"
- );
- lines = fcpServer.collectUntil(is("EndMessage"));
- assertThat(lines,
- matchesFcpMessage("ClientPut", "UploadFrom=disk", "URI=KSK@foo.txt",
- "Filename=" + new File(tempFile.getParent(), "test.dat")));
- }
-
- private File createTempFile() throws IOException {
- File tempFile = File.createTempFile("test-dda-", ".dat");
- tempFile.deleteOnExit();
- Files.write("test-content", tempFile, StandardCharsets.UTF_8);
- return tempFile;
- }
-
- @Test
- public void clientPutDoesNotReactToProtocolErrorForDifferentIdentifier()
- throws InterruptedException, ExecutionException, IOException {
- Future<Optional<Key>> key = fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt").execute();
- connectNode();
- List<String> lines = fcpServer.collectUntil(is("EndMessage"));
- String identifier = extractIdentifier(lines);
- fcpServer.writeLine(
- "ProtocolError",
- "Identifier=not-the-right-one",
- "Code=25",
- "EndMessage"
- );
- fcpServer.writeLine(
- "PutSuccessful",
- "Identifier=" + identifier,
- "URI=KSK@foo.txt",
- "EndMessage"
- );
- assertThat(key.get().get().getKey(), is("KSK@foo.txt"));
- }
-
- @Test
- public void clientPutAbortsOnProtocolErrorOtherThan25()
- throws InterruptedException, ExecutionException, IOException {
- Future<Optional<Key>> key = fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt").execute();
- connectNode();
- List<String> lines = fcpServer.collectUntil(is("EndMessage"));
- String identifier = extractIdentifier(lines);
- fcpServer.writeLine(
- "ProtocolError",
- "Identifier=" + identifier,
- "Code=1",
- "EndMessage"
- );
- assertThat(key.get().isPresent(), is(false));
- }
-
- @Test
- public void clientPutDoesNotReplyToWrongTestDdaReply() throws IOException, ExecutionException,
- InterruptedException {
- File tempFile = createTempFile();
- fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).uri("KSK@foo.txt").execute();
- connectNode();
- List<String> lines = fcpServer.collectUntil(is("EndMessage"));
- String identifier = extractIdentifier(lines);
- fcpServer.writeLine(
- "ProtocolError",
- "Identifier=" + identifier,
- "Code=25",
- "EndMessage"
- );
- lines = fcpServer.collectUntil(is("EndMessage"));
- assertThat(lines, matchesFcpMessage(
- "TestDDARequest",
- "Directory=" + tempFile.getParent(),
- "WantReadDirectory=true",
- "WantWriteDirectory=false"
- ));
- fcpServer.writeLine(
- "TestDDAReply",
- "Directory=/some-other-directory",
- "ReadFilename=" + tempFile,
- "EndMessage"
- );
- fcpServer.writeLine(
- "TestDDAReply",
- "Directory=" + tempFile.getParent(),
- "ReadFilename=" + tempFile,
- "EndMessage"
- );
- lines = fcpServer.collectUntil(is("EndMessage"));
- assertThat(lines, matchesFcpMessage(
- "TestDDAResponse",
- "Directory=" + tempFile.getParent(),
- "ReadContent=test-content"
- ));
- }
-
- @Test
- public void clientPutSendsResponseEvenIfFileCanNotBeRead()
- throws IOException, ExecutionException, InterruptedException {
- File tempFile = createTempFile();
- fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).uri("KSK@foo.txt").execute();
- connectNode();
- List<String> lines = fcpServer.collectUntil(is("EndMessage"));
- String identifier = extractIdentifier(lines);
- fcpServer.writeLine(
- "ProtocolError",
- "Identifier=" + identifier,
- "Code=25",
- "EndMessage"
- );
- lines = fcpServer.collectUntil(is("EndMessage"));
- assertThat(lines, matchesFcpMessage(
- "TestDDARequest",
- "Directory=" + tempFile.getParent(),
- "WantReadDirectory=true",
- "WantWriteDirectory=false"
- ));
- fcpServer.writeLine(
- "TestDDAReply",
- "Directory=" + tempFile.getParent(),
- "ReadFilename=" + tempFile + ".foo",
- "EndMessage"
- );
- lines = fcpServer.collectUntil(is("EndMessage"));
- assertThat(lines, matchesFcpMessage(
- "TestDDAResponse",
- "Directory=" + tempFile.getParent(),
- "ReadContent=failed-to-read"
- ));
- }
-
- @Test
- public void clientPutDoesNotResendOriginalClientPutOnTestDDACompleteWithWrongDirectory()
- throws IOException, ExecutionException, InterruptedException {
- File tempFile = createTempFile();
- fcpClient.clientPut().from(new File(tempFile.getParent(), "test.dat")).uri("KSK@foo.txt").execute();
- connectNode();
- List<String> lines = fcpServer.collectUntil(is("EndMessage"));
- String identifier = extractIdentifier(lines);
- fcpServer.writeLine(
- "TestDDAComplete",
- "Directory=/some-other-directory",
- "EndMessage"
- );
- fcpServer.writeLine(
- "ProtocolError",
- "Identifier=" + identifier,
- "Code=25",
- "EndMessage"
- );
- lines = fcpServer.collectUntil(is("EndMessage"));
- assertThat(lines, matchesFcpMessage(
- "TestDDARequest",
- "Directory=" + tempFile.getParent(),
- "WantReadDirectory=true",
- "WantWriteDirectory=false"
- ));
- }
-
- @Test
- public void clientPutSendsNotificationsForGeneratedKeys()
- throws InterruptedException, ExecutionException, IOException {
- List<String> generatedKeys = new CopyOnWriteArrayList<>();
- Future<Optional<Key>> key = fcpClient.clientPut()
- .onKeyGenerated(generatedKeys::add)
- .from(new ByteArrayInputStream("Hello\n".getBytes()))
- .length(6)
- .uri("KSK@foo.txt")
- .execute();
- connectNode();
- List<String> lines = fcpServer.collectUntil(is("Hello"));
- String identifier = extractIdentifier(lines);
- fcpServer.writeLine(
- "URIGenerated",
- "Identifier=" + identifier,
- "URI=KSK@foo.txt",
- "EndMessage"
- );
- fcpServer.writeLine(
- "PutSuccessful",
- "URI=KSK@foo.txt",
- "Identifier=" + identifier,
- "EndMessage"
- );
- assertThat(key.get().get().getKey(), is("KSK@foo.txt"));
- assertThat(generatedKeys, contains("KSK@foo.txt"));
- }
-
- @Test