package tigase.auth.mechanisms;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.sasl.SaslException;

import junit.framework.TestCase;

import org.junit.Before;
import org.junit.Test;

import tigase.auth.XmppSaslException;

public class SaslANONYMOUSTest extends TestCase {

	private SaslANONYMOUS sasl;

	@Before
	public void setUp() {
		Map<? super String, ?> props = new HashMap<String, Object>();
		CallbackHandler callbackHandler = new CallbackHandler() {

			@Override
			public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
				for (Callback callback : callbacks) {
					if (callback instanceof NameCallback) {
						((NameCallback) callback).setName("somerandomname");
					} else
						throw new UnsupportedCallbackException(callback);
				}
			}
		};
		this.sasl = new SaslANONYMOUS(props, callbackHandler);
	}

	@Test
	public void testMalformedRequest() {
		try {
			sasl.evaluateResponse("a".getBytes());
			fail("Exception must be throwed");
		} catch (XmppSaslException e) {
			assertEquals("malformed-request", e.getSaslErrorElementName());
		} catch (SaslException e) {
		}

		assertFalse(sasl.isComplete());
		assertNull("Authorization ID must be null", sasl.getAuthorizationID());
	}

	@Test
	public void testSuccess() {

		try {
			sasl.evaluateResponse("".getBytes());
		} catch (Exception e) {
			e.printStackTrace();
			fail(e.getMessage());
		}

		assertTrue(sasl.isComplete());
		assertEquals("somerandomname", sasl.getAuthorizationID());
		assertTrue((Boolean) sasl.getNegotiatedProperty(SaslANONYMOUS.IS_ANONYMOUS_PROPERTY));

	}
}

@@ -1124,389 +1124,8 @@
 %09%7D%0A%0A
-%09@Test%0A%09public void testMalformedRequest() %7B%0A%09%09try %7B%0A%09%09%09sasl.evaluateResponse(%22a%22.getBytes());%0A%09%09%09fail(%22Exception must be throwed%22);%0A%09%09%7D catch (XmppSaslException e) %7B%0A%09%09%09assertEquals(%22malformed-request%22, e.getSaslErrorElementName());%0A%09%09%7D catch (SaslException e) %7B%0A%09%09%7D%0A%0A%09%09assertFalse(sasl.isComplete());%0A%09%09assertNull(%22Authorization ID must be null%22, sasl.getAuthorizationID());%0A%09%7D%0A%0A
 %09@Te

update junit tests: ANONYMOUS mechanism must ignore trace info