	public void testNegotiate() throws IOException, ServletException {
		String securityPackage = "Negotiate";
		IWindowsCredentialsHandle clientCredentials = null;
		WindowsSecurityContextImpl clientContext = null;
		_filter.setRoleFormat("both");		
		try {
			clientCredentials = WindowsCredentialsHandleImpl.getCurrent(securityPackage);
			clientCredentials.initialize();
			clientContext = new WindowsSecurityContextImpl();
			clientContext.setPrincipalName(WindowsAccountImpl.getCurrentUsername());
			clientContext.setCredentialsHandle(clientCredentials.getHandle());
			clientContext.setSecurityPackage(securityPackage);
			clientContext.initialize();
			SimpleFilterChain filterChain = new SimpleFilterChain();
			boolean authenticated = false;
			SimpleHttpRequest request = new SimpleHttpRequest();
	        while(true)
	        {
	    		String clientToken = Base64.encode(clientContext.getToken());
	    		request.addHeader("Authorization", securityPackage + " " + clientToken);
	    		SimpleHttpResponse response = new SimpleHttpResponse();
	    		_filter.doFilter(request, response, filterChain);
	    		Subject subject = (Subject) request.getSession().getAttribute("javax.security.auth.subject");
	    		authenticated = (subject != null && subject.getPrincipals().size() > 0);
	    		if (authenticated) {
	        		assertTrue(response.getHeaderNames().length >= 1);
	    			break;
	    		}
	    		assertTrue(response.getHeader("WWW-Authenticate").startsWith(securityPackage + " "));
	    		assertEquals("keep-alive", response.getHeader("Connection"));
	    		assertEquals(2, response.getHeaderNames().length);
	    		assertEquals(401, response.getStatus());
	    		String continueToken = response.getHeader("WWW-Authenticate").substring(securityPackage.length() + 1);
	    		byte[] continueTokenBytes = Base64.decode(continueToken);
	    		assertTrue(continueTokenBytes.length > 0);
	            SecBufferDesc continueTokenBuffer = new SecBufferDesc(Sspi.SECBUFFER_TOKEN, continueTokenBytes);
	            clientContext.initialize(clientContext.getHandle(), continueTokenBuffer);
	        }        
	        assertTrue(authenticated);
	        assertTrue(filterChain.getRequest() instanceof NegotiateRequestWrapper);
	        assertTrue(filterChain.getResponse() instanceof SimpleHttpResponse);	        
	        NegotiateRequestWrapper wrappedRequest = (NegotiateRequestWrapper) filterChain.getRequest();
	        assertEquals("NEGOTIATE", wrappedRequest.getAuthType());
	        assertEquals(Secur32Util.getUserNameEx(EXTENDED_NAME_FORMAT.NameSamCompatible), 
	        		wrappedRequest.getRemoteUser());
	        assertTrue(wrappedRequest.getUserPrincipal() instanceof WindowsPrincipal);
	        assertTrue(wrappedRequest.isUserInRole("Everyone"));
	        assertTrue(wrappedRequest.isUserInRole("S-1-1-0"));
		} finally {
			if (clientContext != null) {
				clientContext.dispose();
			}
			if (clientCredentials != null) {
				clientCredentials.dispose();
			}			
		}
	}

	public void testNegotiate() throws IOException, ServletException {
		String securityPackage = "Negotiate";
		IWindowsCredentialsHandle clientCredentials = null;
		WindowsSecurityContextImpl clientContext = null;
		_filter.setRoleFormat("both");		
		try {
			clientCredentials = WindowsCredentialsHandleImpl.getCurrent(securityPackage);
			clientCredentials.initialize();
			clientContext = new WindowsSecurityContextImpl();
			clientContext.setPrincipalName(WindowsAccountImpl.getCurrentUsername());
			clientContext.setCredentialsHandle(clientCredentials.getHandle());
			clientContext.setSecurityPackage(securityPackage);
			clientContext.initialize();
			SimpleFilterChain filterChain = new SimpleFilterChain();
			boolean authenticated = false;
			SimpleHttpRequest request = new SimpleHttpRequest();
	        while(true)
	        {
	    		String clientToken = Base64.encode(clientContext.getToken());
	    		request.addHeader("Authorization", securityPackage + " " + clientToken);
	    		SimpleHttpResponse response = new SimpleHttpResponse();
	    		_filter.doFilter(request, response, filterChain);
	    		Subject subject = (Subject) request.getSession().getAttribute("javax.security.auth.subject");
	    		authenticated = (subject != null && subject.getPrincipals().size() > 0);
	    		if (authenticated) {
	        		assertTrue(response.getHeaderNames().length >= 0);
	    			break;
	    		}
	    		assertTrue(response.getHeader("WWW-Authenticate").startsWith(securityPackage + " "));
	    		assertEquals("keep-alive", response.getHeader("Connection"));
	    		assertEquals(2, response.getHeaderNames().length);
	    		assertEquals(401, response.getStatus());
	    		String continueToken = response.getHeader("WWW-Authenticate").substring(securityPackage.length() + 1);
	    		byte[] continueTokenBytes = Base64.decode(continueToken);
	    		assertTrue(continueTokenBytes.length > 0);
	            SecBufferDesc continueTokenBuffer = new SecBufferDesc(Sspi.SECBUFFER_TOKEN, continueTokenBytes);
	            clientContext.initialize(clientContext.getHandle(), continueTokenBuffer);
	        }        
	        assertTrue(authenticated);
	        assertTrue(filterChain.getRequest() instanceof NegotiateRequestWrapper);
	        assertTrue(filterChain.getResponse() instanceof SimpleHttpResponse);	        
	        NegotiateRequestWrapper wrappedRequest = (NegotiateRequestWrapper) filterChain.getRequest();
	        assertEquals("NEGOTIATE", wrappedRequest.getAuthType());
	        assertEquals(Secur32Util.getUserNameEx(EXTENDED_NAME_FORMAT.NameSamCompatible), 
	        		wrappedRequest.getRemoteUser());
	        assertTrue(wrappedRequest.getUserPrincipal() instanceof WindowsPrincipal);
	        assertTrue(wrappedRequest.isUserInRole("Everyone"));
	        assertTrue(wrappedRequest.isUserInRole("S-1-1-0"));
		} finally {
			if (clientContext != null) {
				clientContext.dispose();
			}
			if (clientCredentials != null) {
				clientCredentials.dispose();
			}			
		}
	}
