2 Commity af3cee6a1e ... c281f40846

Autor SHA1 Správa Dátum
  desdes c281f40846 Fix testcase 3 rokov pred
  desdes 011343aa55 Fix message client output 3 rokov pred

+ 16 - 18
src/main/java/dslab/client/MessageClient.java

@@ -1,26 +1,24 @@
 package dslab.client;
 
+import dslab.ComponentFactory;
+import dslab.enums.Outcome;
+import dslab.util.*;
+
+import javax.crypto.*;
 import java.io.*;
 import java.net.Socket;
-import java.net.UnknownHostException;
-import java.nio.charset.StandardCharsets;
 import java.nio.file.Path;
 import java.nio.file.Paths;
-import java.rmi.NotBoundException;
-import java.rmi.RemoteException;
 import java.security.InvalidAlgorithmParameterException;
 import java.security.InvalidKeyException;
 import java.security.NoSuchAlgorithmException;
-import java.util.*;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Scanner;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import dslab.ComponentFactory;
-import dslab.enums.Outcome;
-import dslab.util.*;
-
-import javax.crypto.*;
-
 public class MessageClient implements IMessageClient, Runnable {
 
     // configuration keys that have to be present
@@ -131,9 +129,9 @@ public class MessageClient implements IMessageClient, Runnable {
                 dmapWriter.flush();
 
                 // present the raw data to the user
-                System.out.printf("%n%s%n", id);
+                out.printf("%n%s%n", id);
                 while (!(line = dmapReader.readLine()).startsWith("ok"))
-                    System.out.println(line);
+                    out.println(line);
             }
         } catch (IOException | DMAPProtocolException exception) {
             System.err.printf("@%s: there was an error querying your inbox%n", componentId);
@@ -149,7 +147,7 @@ public class MessageClient implements IMessageClient, Runnable {
             dmapWriter.flush();
 
             // check result
-            System.out.println(dmapReader.readLine());
+            out.println(dmapReader.readLine());
         } catch (IOException exception) {
             System.err.printf("@%s: there was an error deleting mail %s%n", componentId, id);
         }
@@ -176,14 +174,14 @@ public class MessageClient implements IMessageClient, Runnable {
                 message.fill(line);
             } while (!(line = dmapReader.readLine()).startsWith("ok"));
 
-            System.out.println(message);
+            out.println(message);
             if (message.hash == null) {
                 System.err.printf("error hash is not set for this message%n", componentId);
                 return;
             }
 
             boolean success = CryptoUtil.verifyHash(message, hmac);
-            System.out.printf(
+            out.printf(
                 "%s message integrity %ssuccessfully verified%n",
                 success ? "ok" : "error",
                 success ? "" : "un-"
@@ -224,9 +222,9 @@ public class MessageClient implements IMessageClient, Runnable {
 
         // notify user of what has transpired
         if (outcome != Outcome.Success)
-            System.err.printf("error %s%n", outcome.getMessage());
+            out.printf("error %s%n", outcome.getMessage());
         else
-            System.out.printf("ok%n");
+            out.printf("ok%n");
     }
 
     @Override

+ 21 - 11
src/test/java/dslab/client/MessageClientStartupTest.java

@@ -26,15 +26,6 @@ public class MessageClientStartupTest {
 
     @Before
     public void setUp() throws Exception {
-        Config clientConfig = new Config(clientId);
-
-        int port = clientConfig.getInt("mailbox.port");
-        dmapServer = new SimpleTcpServer(port);
-
-        serverThread = new Thread(dmapServer);
-        serverThread.start();
-
-        Sockets.waitForSocket("localhost", port, Constants.COMPONENT_STARTUP_WAIT);
     }
 
     @After
@@ -45,11 +36,23 @@ public class MessageClientStartupTest {
 
     @Test(timeout = 15000)
     public void startClient_shouldConnectToMailboxServerAndSendStartsecure() throws Exception {
-        final CountDownLatch connected = new CountDownLatch(1);
+        final CountDownLatch connected = new CountDownLatch(2);
+
+        Config clientConfig = new Config(clientId);
+
+        int port = clientConfig.getInt("mailbox.port");
+        dmapServer = new SimpleTcpServer(port);
 
-        // setup mock server
         dmapServer.setSocketAcceptor(socket -> {
+            if (connected.getCount() == 2) {
+                connected.countDown();
+                System.out.println("got in there once");
+                return;
+            }
+
             try (JunitSocketClient client = new JunitSocketClient(socket)) {
+                System.out.println("got in there second time");
+
                 client.send("ok DMAP2.0");
                 err.checkThat("expected first command from client to be startsecure", client.read(), is("startsecure"));
 
@@ -60,6 +63,13 @@ public class MessageClientStartupTest {
             }
         });
 
+        serverThread = new Thread(dmapServer);
+        serverThread.start();
+
+        Sockets.waitForSocket("localhost", port, Constants.COMPONENT_STARTUP_WAIT);
+
+        // setup mock server
+
         // setup message client
         TestInputStream messageClientIn = new TestInputStream();
         TestOutputStream messageClientOut = new TestOutputStream();