private void checkReply(SMTPClient sc) throws IOException { if (SMTPReply.isNegativeTransient(sc.getReplyCode())) { sc.disconnect(); throw new IOException("Transient SMTP error " + sc.getReplyCode()); } else if (SMTPReply.isNegativePermanent(sc.getReplyCode())) { sc.disconnect(); throw new IOException("Permanent SMTP error " + sc.getReplyCode()); } }
private void send(ProtocolServer server, InetSocketAddress bindedAddress, String msg) throws SocketException, IOException { SMTPClient client = createClient(); client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); client.helo("localhost"); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); client.setSender(SENDER); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.addRecipient(RCPT1); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.addRecipient(RCPT2); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); assertThat(client.sendShortMessageData(msg)).isTrue(); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.quit(); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.disconnect(); }
private void send(ProtocolServer server, InetSocketAddress bindedAddress, String msg) throws SocketException, IOException { SMTPClient client = createClient(); client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); client.helo("localhost"); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); client.setSender(SENDER); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.addRecipient(RCPT1); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.addRecipient(RCPT2); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); assertThat(client.sendShortMessageData(msg)).isTrue(); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.quit(); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.disconnect(); }
@Test public void testHandleAnnouncedMessageSizeLimitExceeded() throws Exception { smtpConfiguration.setMaxMessageSize(1); // set message limit to 1kb init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo " + InetAddress.getLocalHost()); smtpProtocol.sendCommand("MAIL FROM:<mail@localhost> SIZE=1025", null); assertThat(smtpProtocol.getReplyCode()) .as("expected error: max msg size exceeded") .isEqualTo(552); smtpProtocol.addRecipient("mail@localhost"); assertThat(smtpProtocol.getReplyCode()) .as("expected error") .isEqualTo(503); }
@Test public void testEhloResolvDefault() throws Exception { init(smtpConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol1.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol1.sendCommand("ehlo", "abgsfe3rsf.de"); // ehlo should not be checked. so this should give a 250 code assertThat(smtpProtocol1.getReplyCode()) .as("ehlo accepted") .isEqualTo(250); smtpProtocol1.quit(); }
@Test public void testMaxLineLength() throws Exception { init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); StringBuilder sb = new StringBuilder(); for (int i = 0; i < AbstractChannelPipelineFactory.MAX_LINE_LENGTH; i++) { sb.append("A"); } smtpProtocol.sendCommand("EHLO " + sb.toString()); System.out.println(smtpProtocol.getReplyString()); assertThat(smtpProtocol.getReplyCode()) .as("Line length exceed") .isEqualTo(500); smtpProtocol.sendCommand("EHLO test"); assertThat(smtpProtocol.getReplyCode()) .as("Line length ok") .isEqualTo(250); smtpProtocol.quit(); smtpProtocol.disconnect(); }
@Test public void testHeloResolvDefault() throws Exception { init(smtpConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol1.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol1.helo("abgsfe3rsf.de"); // helo should not be checked. so this should give a 250 code assertThat(smtpProtocol1.getReplyCode()) .as("Helo accepted") .isEqualTo(250); smtpProtocol1.quit(); }
@Test public void testStartTlsNotSupported() throws Exception { TestMessageHook hook = new TestMessageHook(); ProtocolServer server = null; try { server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); client.sendCommand("STARTTLS"); assertThat(SMTPReply.isNegativePermanent(client.getReplyCode())).isTrue(); client.quit(); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.disconnect(); Iterator<MailEnvelope> queued = hook.getQueued().iterator(); assertThat(queued.hasNext()).isFalse(); } finally { if (server != null) { server.unbind(); } } }
@Test public void testUnknownCommand() throws Exception { TestMessageHook hook = new TestMessageHook(); ProtocolServer server = null; try { server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); client.sendCommand("UNKNOWN"); assertThat(SMTPReply.isNegativePermanent(client.getReplyCode())).isTrue(); client.quit(); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.disconnect(); Iterator<MailEnvelope> queued = hook.getQueued().iterator(); assertThat(queued.hasNext()).isFalse(); } finally { if (server != null) { server.unbind(); } } }
@Test public void testAuthCancel() throws Exception { smtpConfiguration.setAuthorizedAddresses("127.0.0.1/8"); smtpConfiguration.setAuthorizingAnnounce(); init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo", InetAddress.getLocalHost().toString()); smtpProtocol.sendCommand("AUTH PLAIN"); assertThat(smtpProtocol.getReplyCode()) .as("start auth.") .isEqualTo(334); smtpProtocol.sendCommand("*"); assertThat(smtpProtocol.getReplyCode()) .as("cancel auth.") .isEqualTo(501); smtpProtocol.quit(); }
@Test public void testNoop() throws Exception { TestMessageHook hook = new TestMessageHook(); ProtocolServer server = null; try { server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); client.noop(); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); client.quit(); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.disconnect(); Iterator<MailEnvelope> queued = hook.getQueued().iterator(); assertThat(queued.hasNext()).isFalse(); } finally { if (server != null) { server.unbind(); } } }
@Test public void testRelayingDenied() throws Exception { smtpConfiguration.setAuthorizedAddresses("128.0.0.1/8"); init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo " + InetAddress.getLocalHost()); smtpProtocol.setSender("mail@sample.com"); smtpProtocol.addRecipient("maila@sample.com"); assertThat(smtpProtocol.getReplyCode()) .as("expected 550 error") .isEqualTo(550); }
@Test public void testInvalidHelo() throws Exception { TestMessageHook hook = new TestMessageHook(); ProtocolServer server = null; try { server = createServer(createProtocol(hook)); server.bind(); SMTPClient client = createClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).isTrue(); client.helo(""); assertThat(SMTPReply.isNegativePermanent(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.quit(); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.disconnect(); Iterator<MailEnvelope> queued = hook.getQueued().iterator(); assertThat(queued.hasNext()).isFalse(); } finally { if (server != null) { server.unbind(); } } }
InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); client.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); assertThat(SMTPReply.isNegativePermanent(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); assertThat(SMTPReply.isPositiveCompletion(client.getReplyCode())).as("Reply=" + client.getReplyString()).isTrue(); client.disconnect();
@Test public void startTlsCommandShouldWorkWhenAlone() throws Exception { smtpConfiguration.setStartTLS(); init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); // no message there, yet assertThat(queue.getLastMail()) .as("no mail received by mail server") .isNull();; smtpProtocol.sendCommand("EHLO " + InetAddress.getLocalHost()); smtpProtocol.sendCommand("STARTTLS"); assertThat(smtpProtocol.getReplyCode()).isEqualTo(220); smtpProtocol.disconnect(); }
@Test public void startTlsCommandShouldFailWhenFollowedByInjectedCommand() throws Exception { smtpConfiguration.setStartTLS(); init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); // no message there, yet assertThat(queue.getLastMail()) .as("no mail received by mail server") .isNull();; smtpProtocol.sendCommand("EHLO " + InetAddress.getLocalHost()); smtpProtocol.sendCommand("STARTTLS\r\nAUTH PLAIN"); assertThat(smtpProtocol.getReplyCode()).isEqualTo(451); smtpProtocol.disconnect(); }
@Test public void testHeloEnforcement() throws Exception { init(smtpConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol1.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(smtpProtocol1.isConnected()) .as("first connection taken") .isTrue(); // no message there, yet assertThat(queue.getLastMail()) .as("no mail received by mail server") .isNull(); String sender1 = "mail_sender1@localhost"; smtpProtocol1.setSender(sender1); assertThat(smtpProtocol1.getReplyCode()) .as("expected 503 error") .isEqualTo(503); smtpProtocol1.helo(InetAddress.getLocalHost().toString()); smtpProtocol1.setSender(sender1); smtpProtocol1.quit(); }
@Test public void testNoRecepientSpecified() throws Exception { init(smtpConfiguration); SMTPClient smtpProtocol = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo " + InetAddress.getLocalHost()); smtpProtocol.setSender("mail@sample.com"); // left out for test smtpProtocol.rcpt(new Address("mail@localhost")); smtpProtocol.sendShortMessageData("Subject: test\r\n\r\nTest body testNoRecepientSpecified\r\n"); assertThat(SMTPReply.isNegativePermanent(smtpProtocol.getReplyCode())) .as("sending succeeded without recepient") .isTrue(); smtpProtocol.quit(); // mail was propagated by SMTPServer assertThat(queue.getLastMail()) .as("no mail received by mail server") .isNull(); }
@Test public void testSenderDomainResolvRelayClient() throws Exception { smtpConfiguration.setSenderDomainResolv(); smtpConfiguration.setCheckAuthNetworks(true); init(smtpConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol1.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(smtpProtocol1.isConnected()) .as("first connection taken") .isTrue(); // no message there, yet assertThat(queue.getLastMail()) .as("no mail received by mail server") .isNull(); smtpProtocol1.helo(InetAddress.getLocalHost().toString()); String sender1 = "mail_sender1@xfwrqqfgfe.de"; String sender2 = "mail_sender2@james.apache.org"; smtpProtocol1.setSender(sender1); assertThat(smtpProtocol1.getReplyCode()) .as("expected 501 error") .isEqualTo(501); smtpProtocol1.setSender(sender2); smtpProtocol1.quit(); }
@Test public void testDNSRBLRejectWorks() throws Exception { smtpConfiguration.setAuthorizedAddresses("192.168.0.1/32"); smtpConfiguration.useRBL(true); init(smtpConfiguration); dnsServer.setLocalhostByName(InetAddress.getByName("127.0.0.1")); SMTPClient smtpProtocol = new SMTPClient(); InetSocketAddress bindedAddress = new ProtocolServerUtils(smtpServer).retrieveBindedAddress(); smtpProtocol.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpProtocol.sendCommand("ehlo", InetAddress.getLocalHost().toString()); String sender = "test_user_smtp@localhost"; smtpProtocol.setSender(sender); smtpProtocol.addRecipient("mail@sample.com"); assertThat(smtpProtocol.getReplyCode()) .as("reject") .isEqualTo(554); smtpProtocol.sendShortMessageData("Subject: test\r\n\r\nTest body testDNSRBLRejectWorks\r\n"); smtpProtocol.quit(); // mail was rejected by SMTPServer assertThat(queue.getLastMail()) .as("mail reject by mail server") .isNull(); }