All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.subethamail.smtp.test.command.AuthTest Maven / Gradle / Ivy

There is a newer version: 3.1.7
Show newest version
package org.subethamail.smtp.test.command;

import org.subethamail.smtp.auth.EasyAuthenticationHandlerFactory;
import org.subethamail.smtp.auth.LoginFailedException;
import org.subethamail.smtp.auth.UsernamePasswordValidator;
import org.subethamail.smtp.test.util.Client;
import org.subethamail.smtp.test.util.ServerTestCase;
import org.subethamail.smtp.util.Base64;
import org.subethamail.smtp.util.TextUtils;

/**
 * @author Marco Trevisan 
 * @author Jeff Schnitzer
 */
public class AuthTest extends ServerTestCase
{
	static final String REQUIRED_USERNAME = "myUserName";
	static final String REQUIRED_PASSWORD = "mySecret01";

	class RequiredUsernamePasswordValidator implements UsernamePasswordValidator
	{
		public void login(String username, String password) throws LoginFailedException
		{
			if (!username.equals(REQUIRED_USERNAME) || !password.equals(REQUIRED_PASSWORD))
			{
				throw new LoginFailedException();
			}
		}
	}

	/** */
	public AuthTest(String name)
	{
		super(name);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.subethamail.smtp.test.ServerTestCase#setUp()
	 */
	@Override
	protected void setUp() throws Exception
	{
		this.wiser = new TestWiser();
		this.wiser.setHostname("localhost");
		this.wiser.setPort(PORT);

		UsernamePasswordValidator validator = new RequiredUsernamePasswordValidator();

		EasyAuthenticationHandlerFactory fact = new EasyAuthenticationHandlerFactory(validator);
		this.wiser.getServer().setAuthenticationHandlerFactory(fact);

		this.wiser.start();
		this.c = new Client("localhost", PORT);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.subethamail.smtp.test.ServerTestCase#tearDown()
	 */
	@Override
	protected void tearDown() throws Exception
	{
		super.tearDown();
	}

	/**
	 * Test method for AUTH PLAIN.
	 * The sequence under test is as follows:
	 * 
    *
  1. HELO test
  2. *
  3. User starts AUTH PLAIN
  4. *
  5. User sends username+password
  6. *
  7. We expect login to be successful. Also the Base64 transformations are tested.
  8. *
  9. User issues another AUTH command
  10. *
  11. We expect an error message
  12. *
* {@link org.subethamail.smtp.command.AuthCommand#execute(java.lang.String, org.subethamail.smtp.server.Session)}. */ public void testAuthPlain() throws Exception { this.expect("220"); this.send("HELO foo.com"); this.expect("250"); this.send("AUTH PLAIN"); this.expect("334"); String authString = new String(new byte[] {0}) + REQUIRED_USERNAME + new String(new byte[] {0}) + REQUIRED_PASSWORD; String enc_authString = Base64.encodeToString(TextUtils.getAsciiBytes(authString), false); this.send(enc_authString); this.expect("235"); this.send("AUTH"); this.expect("503"); } /** * Test method for AUTH LOGIN. * The sequence under test is as follows: *
    *
  1. HELO test
  2. *
  3. User starts AUTH LOGIN
  4. *
  5. User sends username
  6. *
  7. User cancels authentication by sending "*"
  8. *
  9. User restarts AUTH LOGIN
  10. *
  11. User sends username
  12. *
  13. User sends password
  14. *
  15. We expect login to be successful. Also the Base64 transformations are tested.
  16. *
  17. User issues another AUTH command
  18. *
  19. We expect an error message
  20. *
* {@link org.subethamail.smtp.command.AuthCommand#execute(java.lang.String, org.subethamail.smtp.server.Session)}. */ public void testAuthLogin() throws Exception { this.expect("220"); this.send("HELO foo.com"); this.expect("250"); this.send("AUTH LOGIN"); this.expect("334"); String enc_username = Base64.encodeToString(TextUtils.getAsciiBytes(REQUIRED_USERNAME), false); this.send(enc_username); this.expect("334"); this.send("*"); this.expect("501"); this.send("AUTH LOGIN"); this.expect("334"); this.send(enc_username); this.expect("334"); String enc_pwd = Base64.encodeToString(TextUtils.getAsciiBytes(REQUIRED_PASSWORD), false); this.send(enc_pwd); this.expect("235"); this.send("AUTH"); this.expect("503"); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy