1   /*
2    * Wallace IMAP Server
3    * Copyright (C) 2004  Robert Newson
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  package net.sf.wallace.messages;
20  
21  import junitx.framework.StringAssert;
22  import net.sf.acegisecurity.AuthenticationException;
23  import net.sf.acegisecurity.AuthenticationManager;
24  import net.sf.acegisecurity.BadCredentialsException;
25  import net.sf.acegisecurity.DisabledException;
26  import net.sf.acegisecurity.LockedException;
27  import net.sf.wallace.commands.AbstractCommandTestCase;
28  
29  import org.easymock.MockControl;
30  
31  /***
32   * 
33   * 
34   * @author rnewson
35   */
36  public final class LoginMessageTest extends AbstractCommandTestCase {
37  
38      private MockControl control;
39  
40      private AuthenticationManager authenticationManager;
41  
42      protected void setUp() {
43          super.setUp();
44          control = MockControl.createStrictControl(AuthenticationManager.class);
45          authenticationManager = (AuthenticationManager) control.getMock();
46          wallaceServerConfig.setAuthenticationManager(authenticationManager);
47      }
48  
49      public void testDisabledUser() throws Exception {
50          authenticationManager.authenticate(null);
51          control.setDefaultMatcher(MockControl.ALWAYS_MATCHER);
52          control.setThrowable(new DisabledException("user disabled."));
53  
54          StringAssert.assertStartsWith("001 NO", testLoginMessage("disableduser", "nopassword"));
55      }
56  
57      public void testLockedUser() throws Exception {
58          authenticationManager.authenticate(null);
59          control.setDefaultMatcher(MockControl.ALWAYS_MATCHER);
60          control.setThrowable(new LockedException("user locked."));
61  
62          StringAssert.assertStartsWith("001 NO", testLoginMessage("lockeduser", "nopassword"));
63      }
64  
65      public void testGenericAuthFailure() throws Exception {
66          authenticationManager.authenticate(null);
67          control.setDefaultMatcher(MockControl.ALWAYS_MATCHER);
68          control.setThrowable(new AuthenticationException("") {
69              private static final long serialVersionUID = 1L;
70          });
71  
72          StringAssert.assertStartsWith("001 NO", testLoginMessage("", ""));
73      }
74  
75      public void testGoodUser() throws Exception {
76          control.expectAndReturn(authenticationManager.authenticate(null), null);
77          control.setDefaultMatcher(MockControl.ALWAYS_MATCHER);
78  
79          StringAssert.assertStartsWith("001 OK", testLoginMessage("gooduser", "password"));
80      }
81  
82      public void testBadUser() throws Exception {
83          authenticationManager.authenticate(null);
84          control.setDefaultMatcher(MockControl.ALWAYS_MATCHER);
85          control.setThrowable(new BadCredentialsException("unknown user."));
86  
87          StringAssert.assertStartsWith("001 NO", testLoginMessage("baduser", "wrongpassword"));
88      }
89  
90      private String testLoginMessage(final String username, final String password) throws Exception {
91          control.replay();
92          final LoginMessage message = new LoginMessage();
93          message.setTag("001");
94          message.setUsername(username);
95          message.setPassword(password);
96          message.getCommand().execute(wallaceSession);
97          control.verify();
98          return getResponse().toString();
99      }
100 
101 }