This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 48b5a9f4ad32744694bdd0e81bf62a0584834da1 Author: Benoit Tellier <[email protected]> AuthorDate: Sun Nov 17 19:59:16 2019 +0700 [Refactoring] Rely on java Base64 in SMTP server test --- .../java/org/apache/james/smtpserver/Base64.java | 117 --------------------- .../apache/james/smtpserver/SMTPServerTest.java | 24 +++-- 2 files changed, 13 insertions(+), 128 deletions(-) diff --git a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/Base64.java b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/Base64.java deleted file mode 100644 index 7e46a26..0000000 --- a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/Base64.java +++ /dev/null @@ -1,117 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ -package org.apache.james.smtpserver; - -import java.io.BufferedReader; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; - -import javax.mail.MessagingException; -import javax.mail.internet.MimeUtility; - -/** - * Performs simple Base64 encoding and decode suitable for authentication. Note - * that this is not a general purpose codec. - */ -public class Base64 { - - /** - * Decode base64 encoded String - * - * @param b64string - * base64 String - * @return reader the BufferedReader which holds the decoded base64 text - * @throws MessagingException - * get thrown when an error was detected while trying to decode - * the String - */ - public static BufferedReader decode(String b64string) throws MessagingException { - return new BufferedReader(new InputStreamReader(MimeUtility.decode( - new ByteArrayInputStream(b64string.getBytes()), "base64"))); - } - - /** - * Decode base64 encoded String - * - * @param b64string - * base64 Sting - * @return returnString the String which holds the docoded base64 text - * @throws MessagingException - * get thrown when an error was detected while trying to decode - * the String - * @throws IOException - * get thrown when I/O error was detected - */ - public static String decodeAsString(String b64string) throws IOException, MessagingException { - if (b64string == null) { - return b64string; - } - String returnString = decode(b64string).readLine(); - if (returnString == null) { - return returnString; - } - return returnString.trim(); - } - - /** - * Encode String to base64 - * - * @param plaintext - * the plaintext to encode - * @return out the ByteArrayOutputStream holding the encoded given text - * @throws IOException - * get thrown when I/O error was detected - * @throws MessagingException - * get thrown when an error was detected while trying to encode - * the String - */ - public static ByteArrayOutputStream encode(String plaintext) throws IOException, MessagingException { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - byte[] in = plaintext.getBytes(); - ByteArrayOutputStream inStream = new ByteArrayOutputStream(); - inStream.write(in, 0, in.length); - // pad - if ((in.length % 3) == 1) { - inStream.write(0); - inStream.write(0); - } else if ((in.length % 3) == 2) { - inStream.write(0); - } - inStream.writeTo(MimeUtility.encode(out, "base64")); - return out; - } - - /** - * Encode String to base64 - * - * @param plaintext - * the plaintext to decode - * @return base64String the encoded String - * @throws IOException - * get thrown when I/O error was detected - * @throws MessagingException - * get thrown when an error was detected while trying to encode - * the String - */ - public static String encodeAsString(String plaintext) throws IOException, MessagingException { - return encode(plaintext).toString(); - } -} diff --git a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java index 9c75008..b0c094d 100644 --- a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java +++ b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java @@ -18,6 +18,7 @@ ****************************************************************/ package org.apache.james.smtpserver; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Fail.fail; import static org.mockito.Mockito.mock; @@ -34,6 +35,7 @@ import java.net.InetSocketAddress; import java.net.Socket; import java.net.UnknownHostException; import java.util.ArrayList; +import java.util.Base64; import java.util.Collection; import java.util.List; @@ -384,7 +386,7 @@ public class SMTPServerTest { // no message there, yet assertThat(queue.getLastMail()) .as("no mail received by mail server") - .isNull();; + .isNull(); smtpProtocol.sendCommand("EHLO " + InetAddress.getLocalHost()); String[] capabilityRes = smtpProtocol.getReplyStrings(); @@ -500,7 +502,7 @@ public class SMTPServerTest { // no message there, yet assertThat(queue.getLastMail()) .as("no mail received by mail server") - .isNull();; + .isNull(); smtpProtocol.sendCommand("EHLO " + InetAddress.getLocalHost()); String[] capabilityRes = smtpProtocol.getReplyStrings(); @@ -534,7 +536,7 @@ public class SMTPServerTest { // no message there, yet assertThat(queue.getLastMail()) .as("no mail received by mail server") - .isNull();; + .isNull(); smtpProtocol.sendCommand("EHLO " + InetAddress.getLocalHost()); smtpProtocol.sendCommand("STARTTLS"); @@ -555,7 +557,7 @@ public class SMTPServerTest { // no message there, yet assertThat(queue.getLastMail()) .as("no mail received by mail server") - .isNull();; + .isNull(); smtpProtocol.sendCommand("EHLO " + InetAddress.getLocalHost()); smtpProtocol.sendCommand("STARTTLS\r\nAUTH PLAIN"); @@ -594,7 +596,7 @@ public class SMTPServerTest { // no message there, yet assertThat(queue.getLastMail()) .as("no mail received by mail server") - .isNull();; + .isNull(); smtp.helo(InetAddress.getLocalHost().toString()); smtp.setSender("mail@localhost"); @@ -620,7 +622,7 @@ public class SMTPServerTest { // no message there, yet assertThat(queue.getLastMail()) .as("no mail received by mail server") - .isNull();; + .isNull(); smtp.helo(InetAddress.getLocalHost().toString()); smtp.setSender("mail@localhost"); @@ -1285,7 +1287,7 @@ public class SMTPServerTest { .isFalse(); smtpProtocol.sendCommand("AUTH PLAIN"); - smtpProtocol.sendCommand(Base64.encodeAsString("\0" + noexistUserName + "\0pwd\0")); + smtpProtocol.sendCommand(Base64.getEncoder().encodeToString(("\0" + noexistUserName + "\0pwd\0").getBytes(UTF_8))); // smtpProtocol.sendCommand(noexistUserName+"pwd".toCharArray()); assertThat(smtpProtocol.getReplyCode()) .as("expected error") @@ -1294,13 +1296,13 @@ public class SMTPServerTest { usersRepository.addUser(Username.of(userName), "pwd"); smtpProtocol.sendCommand("AUTH PLAIN"); - smtpProtocol.sendCommand(Base64.encodeAsString("\0" + userName + "\0wrongpwd\0")); + smtpProtocol.sendCommand(Base64.getEncoder().encodeToString(("\0" + userName + "\0wrongpwd\0").getBytes(UTF_8))); assertThat(smtpProtocol.getReplyCode()) .as("expected error") .isEqualTo(535); smtpProtocol.sendCommand("AUTH PLAIN"); - smtpProtocol.sendCommand(Base64.encodeAsString("\0" + userName + "\0pwd\0")); + smtpProtocol.sendCommand(Base64.getEncoder().encodeToString(("\0" + userName + "\0pwd\0").getBytes(UTF_8))); assertThat(smtpProtocol.getReplyCode()) .as("authenticated") .isEqualTo(235); @@ -1339,7 +1341,7 @@ public class SMTPServerTest { smtpProtocol.setSender(""); smtpProtocol.sendCommand("AUTH PLAIN"); - smtpProtocol.sendCommand(Base64.encodeAsString("\0" + userName + "\0pwd\0")); + smtpProtocol.sendCommand(Base64.getEncoder().encodeToString(("\0" + userName + "\0pwd\0").getBytes(UTF_8))); assertThat(smtpProtocol.getReplyCode()) .as("authenticated") .isEqualTo(235); @@ -1558,7 +1560,7 @@ public class SMTPServerTest { usersRepository.addUser(Username.of(userName), "pwd"); smtpProtocol.sendCommand("AUTH PLAIN"); - smtpProtocol.sendCommand(Base64.encodeAsString("\0" + userName + "\0pwd\0")); + smtpProtocol.sendCommand(Base64.getEncoder().encodeToString(("\0" + userName + "\0pwd\0").getBytes(UTF_8))); assertThat(smtpProtocol.getReplyCode()) .as("authenticated") .isEqualTo(235); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
