From 4ece734952685c5b7be45d86372f8b6b5fbabff4 Mon Sep 17 00:00:00 2001 From: David Newcomb Date: Mon, 13 Apr 2020 16:51:20 +0100 Subject: [PATCH] Changes for #268 from master --- .../icegreen/greenmail/pop3/Pop3State.java | 12 ++++- .../greenmail/pop3/commands/UserCommand.java | 10 ++-- .../icegreen/greenmail/user/UserManager.java | 16 ++++-- .../test/commands/POP3CommandTest.java | 49 ++++++++++++++++--- 4 files changed, 71 insertions(+), 16 deletions(-) diff --git a/greenmail-core/src/main/java/com/icegreen/greenmail/pop3/Pop3State.java b/greenmail-core/src/main/java/com/icegreen/greenmail/pop3/Pop3State.java index 89cb3c54e4..7dd7bd209e 100644 --- a/greenmail-core/src/main/java/com/icegreen/greenmail/pop3/Pop3State.java +++ b/greenmail-core/src/main/java/com/icegreen/greenmail/pop3/Pop3State.java @@ -24,7 +24,6 @@ public class Pop3State { public Pop3State(UserManager manager) { this.manager = manager; this.imapHostManager = manager.getImapHostManager(); - } public GreenMailUser getUser() { @@ -59,4 +58,15 @@ public void authenticate(String pass) public MailFolder getFolder() { return inbox; } + + public GreenMailUser findOrCreateUser(String username) throws UserException { + if (manager.hasUser(username)) { + return manager.getUser(username); + } + if (!manager.isAuthRequired()) { + return manager.createUser(username, username, username); + } + throw new UserException("Unable to find or create user '" + username +"'"); + } + } diff --git a/greenmail-core/src/main/java/com/icegreen/greenmail/pop3/commands/UserCommand.java b/greenmail-core/src/main/java/com/icegreen/greenmail/pop3/commands/UserCommand.java index f12e5344d6..32c1478d4b 100644 --- a/greenmail-core/src/main/java/com/icegreen/greenmail/pop3/commands/UserCommand.java +++ b/greenmail-core/src/main/java/com/icegreen/greenmail/pop3/commands/UserCommand.java @@ -8,9 +8,9 @@ import com.icegreen.greenmail.pop3.Pop3Connection; import com.icegreen.greenmail.pop3.Pop3State; +import com.icegreen.greenmail.user.GreenMailUser; import com.icegreen.greenmail.user.UserException; - public class UserCommand extends Pop3Command { @Override @@ -26,12 +26,16 @@ public void execute(Pop3Connection conn, Pop3State state, String[] args = cmd.split(" "); if (args.length < 2) { conn.println("-ERR Required syntax: USER "); - return; } String username = args[1]; - state.setUser(state.getUser(username)); + GreenMailUser user = state.findOrCreateUser(username); + if (null == user) { + conn.println("-ERR User '" + username + "' not found"); + return; + } + state.setUser(user); conn.println("+OK"); } catch (UserException nsue) { conn.println("-ERR " + nsue); diff --git a/greenmail-core/src/main/java/com/icegreen/greenmail/user/UserManager.java b/greenmail-core/src/main/java/com/icegreen/greenmail/user/UserManager.java index 2c30c3412f..b2a7d30aff 100644 --- a/greenmail-core/src/main/java/com/icegreen/greenmail/user/UserManager.java +++ b/greenmail-core/src/main/java/com/icegreen/greenmail/user/UserManager.java @@ -6,12 +6,16 @@ */ package com.icegreen.greenmail.user; -import com.icegreen.greenmail.imap.ImapHostManager; +import java.util.Collection; +import java.util.Collections; +import java.util.Locale; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.*; -import java.util.concurrent.ConcurrentHashMap; +import com.icegreen.greenmail.imap.ImapHostManager; public class UserManager { private static final Logger log = LoggerFactory.getLogger(UserManager.class); @@ -65,7 +69,7 @@ public boolean test(String userId, String password) { GreenMailUser u = getUser(userId); if (!authRequired) { - if(null == u) { // Auto create user + if (null == u) { // Auto create user try { createUser(userId, userId, password); } catch (UserException e) { @@ -88,6 +92,10 @@ public void setAuthRequired(boolean auth) { authRequired = auth; } + public boolean isAuthRequired() { + return authRequired; + } + public ImapHostManager getImapHostManager() { return imapHostManager; } diff --git a/greenmail-core/src/test/java/com/icegreen/greenmail/test/commands/POP3CommandTest.java b/greenmail-core/src/test/java/com/icegreen/greenmail/test/commands/POP3CommandTest.java index 5a3f0122f1..b711870a0f 100644 --- a/greenmail-core/src/test/java/com/icegreen/greenmail/test/commands/POP3CommandTest.java +++ b/greenmail-core/src/test/java/com/icegreen/greenmail/test/commands/POP3CommandTest.java @@ -1,24 +1,27 @@ package com.icegreen.greenmail.test.commands; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.CoreMatchers.startsWith; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.Socket; + import javax.mail.MessagingException; -import com.icegreen.greenmail.junit.GreenMailRule; -import com.icegreen.greenmail.pop3.commands.AuthCommand; -import com.icegreen.greenmail.user.UserException; -import com.icegreen.greenmail.util.ServerSetupTest; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.startsWith; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; +import com.icegreen.greenmail.junit.GreenMailRule; +import com.icegreen.greenmail.pop3.commands.AuthCommand; +import com.icegreen.greenmail.user.UserException; +import com.icegreen.greenmail.util.ServerSetupTest; public class POP3CommandTest { private static final String CRLF = "\r\n"; @@ -75,4 +78,34 @@ public void authPlainWithContinuation() throws IOException, UserException { } } + @Test + public void authDisabled() throws IOException, UserException { + try (Socket socket = new Socket(hostAddress, port)) { + assertThat(socket.isConnected(), is(equalTo(true))); + PrintStream printStream = new PrintStream(socket.getOutputStream()); + final BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); + + greenMail.getManagers().getUserManager().setAuthRequired(false); + + assertThat(reader.readLine(), is(startsWith("+OK POP3 GreenMail Server v"))); + printStream.print("USER blar@blar.com" + CRLF); + assertThat(reader.readLine(), is(equalTo("+OK"))); + } + } + + @Test + public void authEnabled() throws IOException, UserException { + try (Socket socket = new Socket(hostAddress, port)) { + assertThat(socket.isConnected(), is(equalTo(true))); + PrintStream printStream = new PrintStream(socket.getOutputStream()); + final BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); + + greenMail.getManagers().getUserManager().setAuthRequired(true); + + assertThat(reader.readLine(), is(startsWith("+OK POP3 GreenMail Server v"))); + printStream.print("USER blar@blar.com" + CRLF); + assertThat(reader.readLine(), is(not(equalTo("+OK")))); + } + } + }