org.apache.tomcat.unittest.TesterContext Java Examples

The following examples show how to use org.apache.tomcat.unittest.TesterContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestResponse.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private void doTestSendRedirect(String input, String expectedLocation) throws Exception {
    // Set-up.
    // Note: Not sufficient for testing relative -> absolute
    Connector connector = new Connector();
    org.apache.coyote.Response cResponse = new org.apache.coyote.Response();
    Response response = new Response();
    response.setConnector(connector);
    response.setCoyoteResponse(cResponse);
    Request request = new Request();
    org.apache.coyote.Request cRequest = new org.apache.coyote.Request();
    request.setCoyoteRequest(cRequest);
    Context context = new TesterContext();
    request.getMappingData().context = context;
    response.setRequest(request);
    // Do test
    response.sendRedirect(input);
    String location = response.getHeader("Location");
    Assert.assertEquals(expectedLocation,  location);
}
 
Example #2
Source File: TestDigestAuthenticator.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void bug54521() throws LifecycleException {
    DigestAuthenticator digestAuthenticator = new DigestAuthenticator();
    TesterContext context = new TesterContext();
    context.setServletContext(new TesterServletContext());
    digestAuthenticator.setContainer(context);
    digestAuthenticator.start();
    Request request = new TesterRequest();
    final int count = 1000;

    Set<String> nonces = new HashSet<>();

    for (int i = 0; i < count; i++) {
        nonces.add(digestAuthenticator.generateNonce(request));
    }

    Assert.assertEquals(count,  nonces.size());
}
 
Example #3
Source File: TestJNDIRealm.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
private JNDIRealm buildRealm(String password, String digest) throws javax.naming.NamingException,
        NoSuchFieldException, IllegalAccessException, LifecycleException {
    Context context = new TesterContext();
    JNDIRealm realm = new JNDIRealm();
    realm.setContainer(context);
    realm.setUserSearch("");
    realm.setDigest(digest);

    Field field = JNDIRealm.class.getDeclaredField("context");
    field.setAccessible(true);
    field.set(realm, mockDirContext(mockSearchResults(password)));

    realm.start();

    return realm;
}
 
Example #4
Source File: TestRealmBase.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private void doTestDigestDigestPasswords(String password,
        String digest, String digestedPassword) throws Exception {
    Context context = new TesterContext();
    TesterMapRealm realm = new TesterMapRealm();
    realm.setContainer(context);
    MessageDigestCredentialHandler ch = new MessageDigestCredentialHandler();
    ch.setAlgorithm(digest);
    realm.setCredentialHandler(ch);
    realm.start();

    realm.addUser(USER1, digestedPassword);

    Principal p = realm.authenticate(USER1, password);

    Assert.assertNotNull(p);
    Assert.assertEquals(USER1, p.getName());
}
 
Example #5
Source File: TestDigestAuthenticator.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void bug54521() throws LifecycleException {
    DigestAuthenticator digestAuthenticator = new DigestAuthenticator();
    digestAuthenticator.setContainer(new TesterContext());
    digestAuthenticator.start();
    Request request = new TesterRequest();
    final int count = 1000;

    Set<String> nonces = new HashSet<String>();

    for (int i = 0; i < count; i++) {
        nonces.add(digestAuthenticator.generateNonce(request));
    }

    Assert.assertEquals(count,  nonces.size());
}
 
Example #6
Source File: TestDigestAuthenticator.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void bug54521() throws LifecycleException {
    DigestAuthenticator digestAuthenticator = new DigestAuthenticator();
    digestAuthenticator.setContainer(new TesterContext());
    digestAuthenticator.start();
    Request request = new TesterRequest();
    final int count = 1000;

    Set<String> nonces = new HashSet<String>();

    for (int i = 0; i < count; i++) {
        nonces.add(digestAuthenticator.generateNonce(request));
    }

    Assert.assertEquals(count,  nonces.size());
}
 
Example #7
Source File: TestJNDIRealm.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
private JNDIRealm buildRealm(String password, String digest) throws javax.naming.NamingException,
        NoSuchFieldException, IllegalAccessException, LifecycleException {
    Context context = new TesterContext();
    JNDIRealm realm = new JNDIRealm();
    realm.setContainer(context);
    realm.setUserSearch("");
    realm.setDigest(digest);

    Field field = JNDIRealm.class.getDeclaredField("context");
    field.setAccessible(true);
    field.set(realm, mockDirContext(mockSearchResults(password)));

    realm.start();

    return realm;
}
 
Example #8
Source File: TestResponse.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
private void doTestSendRedirect(String input, String expectedLocation) throws Exception {
    // Set-up.
    // Note: Not sufficient for testing relative -> absolute
    Connector connector = new Connector();
    org.apache.coyote.Response cResponse = new org.apache.coyote.Response();
    Response response = new Response();
    response.setConnector(connector);
    response.setCoyoteResponse(cResponse);
    Request request = new Request();
    org.apache.coyote.Request cRequest = new org.apache.coyote.Request();
    request.setCoyoteRequest(cRequest);
    Context context = new TesterContext();
    request.setContext(context);
    response.setRequest(request);
    // Do test
    response.sendRedirect(input);
    String location = response.getHeader("Location");
    Assert.assertEquals(expectedLocation,  location);
}
 
Example #9
Source File: TestRemoteIpFilter.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public Context getContext() {
    // Lazt init
    if (super.getContext() == null) {
        super.setContext(new TesterContext());
    }
    return super.getContext();
}
 
Example #10
Source File: TestPersistentManager.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testMinIdleSwap() throws Exception {
    PersistentManager manager = new PersistentManager();
    manager.setStore(new TesterStore());

    Host host = new TesterHost();
    Context context = new TesterContext();
    context.setParent(host);

    manager.setContext(context);

    manager.setMaxActiveSessions(2);
    manager.setMinIdleSwap(0);

    manager.start();

    // Create the maximum number of sessions
    manager.createSession(null);
    manager.createSession(null);

    // Given the minIdleSwap settings, this should swap one out to get below
    // the limit
    manager.processPersistenceChecks();
    Assert.assertEquals(1, manager.getActiveSessions());
    Assert.assertEquals(2, manager.getActiveSessionsFull());

    manager.createSession(null);
    Assert.assertEquals(2, manager.getActiveSessions());
    Assert.assertEquals(3, manager.getActiveSessionsFull());
}
 
Example #11
Source File: TestRealmBase.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private void doTestDigestDigestPasswords(String password,
        String digest, String digestedPassword) throws Exception {
    Context context = new TesterContext();
    TesterMapRealm realm = new TesterMapRealm();
    realm.setContainer(context);
    realm.setDigest(digest);
    realm.start();

    realm.addUser(USER1, digestedPassword);

    Principal p = realm.authenticate(USER1, password);

    Assert.assertNotNull(p);
    Assert.assertEquals(USER1, p.getName());
}
 
Example #12
Source File: TestPersistentManager.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testMinIdleSwap() throws Exception {
    PersistentManager manager = new PersistentManager();
    manager.setStore(new TesterStore());

    Host host = new TesterHost();
    Context context = new TesterContext();
    context.setParent(host);

    manager.setContainer(context);

    manager.setMaxActiveSessions(2);
    manager.setMinIdleSwap(0);

    manager.start();

    // Create the maximum number of sessions
    manager.createSession(null);
    manager.createSession(null);

    // Given the minIdleSwap settings, this should swap one out to get below
    // the limit
    manager.processPersistenceChecks();
    Assert.assertEquals(1,  manager.getActiveSessions());
    Assert.assertEquals(2,  manager.getActiveSessionsFull());

    manager.createSession(null);
    Assert.assertEquals(2,  manager.getActiveSessions());
    Assert.assertEquals(3,  manager.getActiveSessionsFull());
}
 
Example #13
Source File: TestRemoteIpFilter.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public Context getContext() {
    // Lazt init
    if (super.getContext() == null) {
        super.setContext(new TesterContext());
    }
    return super.getContext();
}
 
Example #14
Source File: TestRealmBase.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private void doTestDigestDigestPasswords(String password,
        String digest, String digestedPassword) throws Exception {
    Context context = new TesterContext();
    TesterMapRealm realm = new TesterMapRealm();
    realm.setContainer(context);
    realm.setDigest(digest);
    realm.start();

    realm.addUser(USER1, digestedPassword);

    Principal p = realm.authenticate(USER1, password);

    Assert.assertNotNull(p);
    Assert.assertEquals(USER1, p.getName());
}
 
Example #15
Source File: TestRemoteIpFilter.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public Context getContext() {
    // Lazt init
    if (super.getContext() == null) {
        getMappingData().context = new TesterContext();
    }
    return super.getContext();
}
 
Example #16
Source File: TestJNDIRealm.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private JNDIRealm buildRealm(String password) throws javax.naming.NamingException,
        NoSuchFieldException, IllegalAccessException, LifecycleException {
    Context context = new TesterContext();
    JNDIRealm realm = new JNDIRealm();
    realm.setContainer(context);
    realm.setUserSearch("");

    Field field = JNDIRealm.class.getDeclaredField("context");
    field.setAccessible(true);
    field.set(realm, mockDirContext(mockSearchResults(password)));

    realm.start();

    return realm;
}
 
Example #17
Source File: TesterWebResourceRoot.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public Context getContext() {
    return new TesterContext();
}
 
Example #18
Source File: TestPersistentManager.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testBug62175() throws Exception {
    final PersistentManager manager = new PersistentManager();
    final AtomicInteger sessionExpireCounter = new AtomicInteger();

    Store mockStore = EasyMock.createNiceMock(Store.class);
    EasyMock.expect(mockStore.load(EasyMock.anyString())).andAnswer(new IAnswer<Session>() {

        @Override
        public Session answer() throws Throwable {
            return timedOutSession(manager, sessionExpireCounter);
        }
    }).anyTimes();

    EasyMock.replay(mockStore);

    manager.setStore(mockStore);

    Host host = new TesterHost();

    final RequestCachingSessionListener requestCachingSessionListener = new RequestCachingSessionListener();

    final Context context = new TesterContext() {

        @Override
        public Object[] getApplicationLifecycleListeners() {
            return new Object[] { requestCachingSessionListener };
        }

        @Override
        public Manager getManager() {
            return manager;
        }
    };
    context.setParent(host);

    Request req = new Request() {
        @Override
        public Context getContext() {
            return context;
        }
    };
    req.setRequestedSessionId("invalidSession");
    HttpServletRequest request = new RequestFacade(req);
    requestCachingSessionListener.request = request;

    manager.setContext(context);

    manager.start();

    Assert.assertNull(request.getSession(false));
    Assert.assertEquals(1, sessionExpireCounter.get());

}