Ejemplo n.º 1
0
  /** Tests the consequences of erroneous lines inserted into the socket stream. */
  @Test
  public void writeTestWithError() {
    setup(new Properties());
    Assert.assertTrue("mta online", mta.isOnline());

    List<MTAMessage> messages = new LinkedList<MTAMessage>();

    Socket socket = null;
    try {
      socket = new Socket(MTA_HOST, mtaPort);
      socket.setSoTimeout(500);
      write(socket, "GET blabla\r\nHost: blabla\r\n\r\n");
      getLogger().info("read from socket after GET chunk: " + read(socket));

      // This time we authenticate first
      writeln(socket, getLoginMessageString());

      // Now we write some stuff to the socket
      messages.add(writeMessage(socket, getAliveMessage("1")));
      writeBullshitToSocket(WRITE_ERRORS_SOME, messages, socket);

      for (MTAMessage message : messages) {
        new MessageAssertWaiter(true, message)
            .assertGetsOkay("message received: " + message, 5000, 200);
      }
    } catch (UnknownHostException e) {
      getLogger().error("Unknown host: " + MTA_HOST, e);
      Assert.fail("mta http server not at host localhost");
    } catch (IOException e) {
      getLogger().error("IOException", e);
      Assert.fail("mta http server not at port " + mtaPort);
    } finally {
      ClosingUtils.close(socket);
    }
  }
Ejemplo n.º 2
0
  private void logoutTestInner(boolean logIn) {
    Socket socket = null;
    try {
      socket = new Socket("localhost", mtaPort);
      socket.setSoLinger(false, 1);
      socket.setSoTimeout(500);
      write(socket, "GET blabla\r\nHost: blabla\r\n\r\n");
      getLogger().info("read from socket after GET chunk: " + read(socket));
      if (logIn) {
        writeln(socket, getLoginMessageString());
      }

    } catch (UnknownHostException e) {
      e.printStackTrace();
      Assert.fail("mta http server not at host localhost");
    } catch (IOException e) {
      e.printStackTrace();
      Assert.fail("mta http server not at port " + mtaPort);
    } finally {
      ClosingUtils.close(socket);
      getLogger().info("Socket closed. Obeying grace time for MTA to recognize closed socket.");
      int serverGraceMs = FakeHttpServer.LISTEN_TIMEOUT_MS;
      int connectionGraceMs = ConnectionThread.SOCKET_TIMEOUT_MS;
      int waitTimeMs = serverGraceMs + connectionGraceMs;
      sleep(waitTimeMs + 1000);
      getLogger().info("ended grace time");
    }
    if (logIn) {
      LogoutAsk logout = new LogoutAsk(MOCKSESSIONID);
      Message expected = new Message(TESTMTA_NAME, mta.getLoginLogoutReceiverName(), logout, "");
      new MessageSentAssertWaiter(expected).assertGetsOkay("Logout message sent", 1000, 200);
    }
  }
Ejemplo n.º 3
0
  /**
   * Tests if a serialized message written to a socket connected to the MTA socket is correctly
   * received.
   */
  @Test
  public void writeTest() throws IOException {
    setup(new Properties());
    Assert.assertTrue("mta online", mta.isOnline());
    MessageContent content = new LoginAsk("login", "secret");
    MTAMessage message = new MTAMessage(content, "rid");
    String encodedMessage = MTAMessageCoder.getInstance().encode(message);

    Socket socket = null;
    try {
      socket = new Socket(MTA_HOST, mtaPort);
      socket.setSoTimeout(500);
      write(socket, "GET blabla\r\nHost: blabla\r\n\r\n");
      getLogger().info("read from socket after GET chunk: " + read(socket));

      writeln(socket, encodedMessage);
    } catch (UnknownHostException e) {
      getLogger().error("Unknown host: " + MTA_HOST, e);
      Assert.fail("mta http server not at host localhost");
    } catch (IOException e) {
      getLogger().error("IOException", e);
      Assert.fail("mta http server not at port " + mtaPort);
    } finally {
      ClosingUtils.close(socket);
    }

    new MessageAssertWaiter(false, message).assertGetsOkay("message received");
    getLogger().info(mta.getReceivedStrings());
  }
Ejemplo n.º 4
0
  /**
   * Tests the situation where a client tries to log in but the message to the user agent is not
   * answered.
   */
  @Test
  public void testLoginButUserAgentDoesntAnswer() {
    setup(new Properties());
    Assert.assertTrue("mta online", mta.isOnline());

    Socket socket = null;
    try {
      socket = new Socket(MTA_HOST, mtaPort);
      socket.setSoTimeout(500);
      write(socket, "GET blabla\r\nHost: blabla\r\n\r\n");
      getLogger().info("read from socket after GET chunk: " + read(socket));
      writeln(socket, getLoginMessageString(TIMEOUT_LOGIN));

      new HttpErrorMessageWaiter(ErrorConstants.SERVER_NOT_READY)
          .assertGetsOkay("connection there");
    } catch (UnknownHostException e) {
      getLogger().error("Unknown host: " + MTA_HOST, e);
      Assert.fail("mta http server not at host localhost");
    } catch (IOException e) {
      getLogger().error("IOException", e);
      Assert.fail("mta http server not at port " + mtaPort);
    } finally {
      ClosingUtils.close(socket);
    }
  }
Ejemplo n.º 5
0
  /**
   * Tests if sending AliveTell messages without being authenticated results in the unexpected loss
   * of connection. Unexpected, because sometimes the MTA sends an AliveAsk to the client pretty
   * soon after the client establishes a connection. The client then answers with an AliveTell
   * without being authenticated, yet. In this case, the connection should not be dropped.
   */
  @Test
  public void writeTestAliveTellWithoutLogin() {
    setup(new Properties());

    Assert.assertTrue("mta online", mta.isOnline());

    List<MTAMessage> messages = new LinkedList<MTAMessage>();

    Socket socket = null;
    try {
      socket = new Socket(MTA_HOST, mtaPort);
      socket.setSoTimeout(500);
      write(socket, "GET blabla\r\nHost: blabla\r\n\r\n");
      getLogger().info("read from socket after GET chunk: " + read(socket));

      MessageContent content = new AliveTell();
      MTAMessage aliveTellMessage = new MTAMessage(content, "rid");

      try {
        writeMessage(socket, aliveTellMessage);

        new ConnectionNotTerminatedWaiter().assertStaysOkay("connection not terminated", 3000, 100);

        writeBullshitToSocket(WRITE_ERRORS_NONE, messages, socket);
      } catch (IOException e) {
        getLogger().debug("This is a totally expected IOException. No worries!", e);
      }

      // The first message is always received.
      MTAMessage expectedMessage = aliveTellMessage;
      new MessageAssertWaiter(false, expectedMessage)
          .assertGetsOkay("message received: " + expectedMessage, 5000, 200);

      // The following messages are not received because
      // the authentication failed and the stream was closed.
      for (MTAMessage message : messages) {
        new MessageNotForwardedAssertWaiter(message)
            .assertStaysOkay("message not forwarded: " + message, 3000, 200);
      }

      Assert.assertTrue("Connection has been terminated", mta.connectionTerminated());
    } catch (UnknownHostException e) {
      getLogger().error("Unknown host: " + MTA_HOST, e);
      Assert.fail("mta http server not at host localhost");
    } catch (IOException e) {
      getLogger().error("IOException", e);
      Assert.fail("mta http server not at port " + mtaPort);
    } finally {
      ClosingUtils.close(socket);
    }
  }
Ejemplo n.º 6
0
  /** Tests if an MOTD is sent if one is defined. */
  @Test
  public void testWithMOTDSet() {
    Properties p = new Properties();
    p.put(TextMessageManager.FILE_NAME_KEY, "foo");
    setup(p);
    ((MockTextMessageManager) mta.getTextManager()).setMotd("MOTD");
    Assert.assertTrue("mta online", mta.isOnline());

    Socket socket = null;
    try {
      socket = new Socket(MTA_HOST, mtaPort);
      socket.setSoTimeout(500);
      write(socket, "GET blabla\r\nHost: blabla\r\n\r\n");
      getLogger().info("read from socket after GET chunk: " + read(socket));

      // This time we authenticate first
      writeln(socket, getLoginMessageString());

      waitForLoginTell();

      // Now we can be sure we're logged in.

      TextMessageNotify text = null;
      for (MTAMessage m : mta.getSentToHttpMsgs()) {
        if (m.getContent() instanceof TextMessageNotify) {
          text = (TextMessageNotify) m.getContent();
        }
      }
      Assert.assertNotNull("motd sent", text);
      Assert.assertEquals("correct motd string sent", "MOTD", text.getContent());

    } catch (UnknownHostException e) {
      getLogger().error("Unknown host: " + MTA_HOST, e);
      Assert.fail("mta http server not at host localhost");
    } catch (IOException e) {
      getLogger().error("IOException", e);
      Assert.fail("mta http server not at port " + mtaPort);
    } finally {
      ClosingUtils.close(socket);
    }
  }
Ejemplo n.º 7
0
  /** Tests if an MOTD is sent if one is defined. */
  @Test
  public void testBroadcastMessage() {
    Properties p = new Properties();
    p.put(TextMessageManager.ALLOWED_SENDER_KEY, "admin");
    setup(p);
    Assert.assertTrue("mta online", mta.isOnline());

    Socket socket = null;
    try {
      socket = new Socket(MTA_HOST, mtaPort);
      socket.setSoTimeout(500);
      write(socket, "GET blabla\r\nHost: blabla\r\n\r\n");
      getLogger().info("read from socket after GET chunk: " + read(socket));

      // This time we authenticate first
      writeln(socket, getLoginMessageString());

      waitForLoginTell();

      TextMessageNotify content =
          new TextMessageNotify(Priority.WARN, "title", "BROADCAST", "admin");
      Message message = new Message("from", "to", content, "rid");
      mta.receive(message);

      new BroadcastWaiter("BROADCAST").assertGetsOkay("broadcast msg found");

    } catch (UnknownHostException e) {
      getLogger().error("Unknown host: " + MTA_HOST, e);
      Assert.fail("mta http server not at host localhost");
    } catch (IOException e) {
      getLogger().error("IOException", e);
      Assert.fail("mta http server not at port " + mtaPort);
    } finally {
      ClosingUtils.close(socket);
    }
  }