### Question:
CookieAssertionLoginModule extends AbstractLoginModule { public boolean login() throws LoginException { HttpRequestCallback hcb = new HttpRequestCallback(); Callback[] callbacks = new Callback[] { hcb }; try { m_handler.handle( callbacks ); HttpServletRequest request = hcb.getRequest(); HttpSession session = ( request == null ) ? null : request.getSession( false ); String sid = ( session == null ) ? NULL : session.getId(); String name = (request != null) ? getUserCookie( request ) : null; if ( name == null ) { if ( log.isDebugEnabled() ) { log.debug( "No cookie " + PREFS_COOKIE_NAME + " present in session ID=: " + sid ); } throw new FailedLoginException( "The user cookie was not found." ); } if ( log.isDebugEnabled() ) { log.debug( "Logged in session ID=" + sid + "; asserted=" + name ); } m_principals.add( new WikiPrincipal( name, WikiPrincipal.FULL_NAME ) ); return true; } catch( IOException e ) { log.error( "IOException: " + e.getMessage() ); return false; } catch( UnsupportedCallbackException e ) { String message = "Unable to handle callback, disallowing login."; log.error( message, e ); throw new LoginException( message ); } }  boolean login(); static String getUserCookie( HttpServletRequest request ); static void setUserCookie( HttpServletResponse response, String name ); static void clearUserCookie( HttpServletResponse response ); static final String PREFS_COOKIE_NAME; }### Answer:
@Test public final void testLogin() { final MockHttpServletRequest request = m_engine.newHttpRequest(); try { Assertions.assertTrue( m_engine.getManager( AuthenticationManager.class ).allowsCookieAssertions() ); final Cookie cookie = new Cookie( CookieAssertionLoginModule.PREFS_COOKIE_NAME, "Bullwinkle" ); request.setCookies( new Cookie[] { cookie } ); m_subject = new Subject(); final CallbackHandler handler = new WebContainerCallbackHandler( m_engine, request ); final LoginModule module = new CookieAssertionLoginModule(); module.initialize( m_subject, handler, new HashMap<>(), new HashMap<>() ); module.login(); module.commit(); final Set< Principal > principals = m_subject.getPrincipals(); Assertions.assertEquals( 1, principals.size() ); Assertions.assertTrue( principals.contains( new WikiPrincipal( "Bullwinkle" ) ) ); Assertions.assertFalse( principals.contains( Role.ASSERTED ) ); Assertions.assertFalse( principals.contains( Role.ALL ) ); } catch( final LoginException e ) { System.err.println( e.getMessage() ); Assertions.assertTrue( false ); } }
@Test public final void testLogout() { final MockHttpServletRequest request = m_engine.newHttpRequest(); final Cookie cookie = new Cookie( CookieAssertionLoginModule.PREFS_COOKIE_NAME, "Bullwinkle" ); request.setCookies( new Cookie[] { cookie } ); try { final CallbackHandler handler = new WebContainerCallbackHandler( m_engine, request ); final LoginModule module = new CookieAssertionLoginModule(); module.initialize( m_subject, handler, new HashMap<>(), new HashMap<>() ); module.login(); module.commit(); final Set< Principal > principals = m_subject.getPrincipals(); Assertions.assertEquals( 1, principals.size() ); Assertions.assertTrue( principals.contains( new WikiPrincipal( "Bullwinkle" ) ) ); Assertions.assertFalse( principals.contains( Role.ANONYMOUS ) ); Assertions.assertFalse( principals.contains( Role.ALL ) ); module.logout(); Assertions.assertEquals( 0, principals.size() ); } catch( final LoginException e ) { System.err.println( e.getMessage() ); Assertions.assertTrue( false ); } }