Search in sources :

Example 81 with ByteChunk

use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.

the class TestStandardContext method testBug50015.

@Test
public void testBug50015() throws Exception {
    // Test that configuring servlet security constraints programmatically
    // does work.
    // Set up a container
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    // Setup realm
    TesterMapRealm realm = new TesterMapRealm();
    realm.addUser("tomcat", "tomcat");
    realm.addUserRole("tomcat", "tomcat");
    ctx.setRealm(realm);
    // Configure app for BASIC auth
    LoginConfig lc = new LoginConfig();
    lc.setAuthMethod("BASIC");
    ctx.setLoginConfig(lc);
    ctx.getPipeline().addValve(new BasicAuthenticator());
    // Add ServletContainerInitializer
    ServletContainerInitializer sci = new Bug50015SCI();
    ctx.addServletContainerInitializer(sci, null);
    // Start the context
    tomcat.start();
    // Request the first servlet
    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/bug50015", bc, null);
    // Check for a 401
    assertNotSame("OK", bc.toString());
    assertEquals(401, rc);
}
Also used : Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) ServletContainerInitializer(javax.servlet.ServletContainerInitializer) Tomcat(org.apache.catalina.startup.Tomcat) TesterMapRealm(org.apache.catalina.startup.TesterMapRealm) BasicAuthenticator(org.apache.catalina.authenticator.BasicAuthenticator) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) LoginConfig(org.apache.tomcat.util.descriptor.web.LoginConfig) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 82 with ByteChunk

use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.

the class TestStandardContextAliases method testDirContextAliases.

@Test
public void testDirContextAliases() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    File lib = new File("webapps/examples/WEB-INF/lib");
    ctx.setResources(new StandardRoot(ctx));
    ctx.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.POST, "/WEB-INF/lib", lib.getAbsolutePath(), null, "/");
    Tomcat.addServlet(ctx, "test", new TestServlet());
    ctx.addServletMappingDecoded("/", "test");
    tomcat.start();
    ByteChunk res = getUrl("http://localhost:" + getPort() + "/");
    String result = res.toString();
    if (result == null) {
        result = "";
    }
    assertTrue(result.indexOf("00-PASS") > -1);
    assertTrue(result.indexOf("01-PASS") > -1);
    assertTrue(result.indexOf("02-PASS") > -1);
}
Also used : Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) StandardRoot(org.apache.catalina.webresources.StandardRoot) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 83 with ByteChunk

use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.

the class TestRestCsrfPreventionFilter2 method doTest.

private void doTest(String method, String uri, BasicCredentials credentials, byte[] body, boolean useCookie, int expectedRC, String expectedResponse, String nonce, boolean expectCsrfRH, String expectedCsrfRHV) throws Exception {
    Map<String, List<String>> reqHeaders = new HashMap<>();
    Map<String, List<String>> respHeaders = new HashMap<>();
    addNonce(reqHeaders, nonce, n -> Objects.nonNull(n));
    if (useCookie) {
        addCookies(reqHeaders, l -> Objects.nonNull(l) && l.size() > 0);
    }
    addCredentials(reqHeaders, credentials, c -> Objects.nonNull(c));
    ByteChunk bc = new ByteChunk();
    int rc;
    if (METHOD_GET.equals(method)) {
        rc = getUrl(HTTP_PREFIX + getPort() + uri, bc, reqHeaders, respHeaders);
    } else {
        rc = postUrl(body, HTTP_PREFIX + getPort() + uri, bc, reqHeaders, respHeaders);
    }
    assertEquals(expectedRC, rc);
    if (expectedRC == HttpServletResponse.SC_OK) {
        assertEquals(expectedResponse, bc.toString());
        List<String> newCookies = respHeaders.get(SERVER_COOKIE_HEADER);
        saveCookies(newCookies, l -> Objects.nonNull(l) && l.size() > 0);
    }
    if (!expectCsrfRH) {
        assertNull(respHeaders.get(Constants.CSRF_REST_NONCE_HEADER_NAME));
    } else {
        List<String> respHeaderValue = respHeaders.get(Constants.CSRF_REST_NONCE_HEADER_NAME);
        assertNotNull(respHeaderValue);
        if (Objects.nonNull(expectedCsrfRHV)) {
            assertTrue(respHeaderValue.contains(expectedCsrfRHV));
        } else {
            validNonce = respHeaderValue.get(0);
        }
    }
}
Also used : HashMap(java.util.HashMap) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) ArrayList(java.util.ArrayList) List(java.util.List) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint)

Example 84 with ByteChunk

use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.

the class TestReplicatedContext method testBug57425.

@Test
public void testBug57425() throws LifecycleException, IOException {
    Tomcat tomcat = getTomcatInstance();
    Host host = tomcat.getHost();
    if (host instanceof StandardHost) {
        ((StandardHost) host).setContextClass(ReplicatedContext.class.getName());
    }
    File root = new File("test/webapp");
    Context context = tomcat.addWebapp(host, "", root.getAbsolutePath());
    Tomcat.addServlet(context, "test", new AccessContextServlet());
    context.addServletMappingDecoded("/access", "test");
    tomcat.start();
    ByteChunk result = getUrl("http://localhost:" + getPort() + "/access");
    Assert.assertEquals("OK", result.toString());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) StandardHost(org.apache.catalina.core.StandardHost) StandardHost(org.apache.catalina.core.StandardHost) Host(org.apache.catalina.Host) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 85 with ByteChunk

use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.

the class TestVirtualContext method assertPageContains.

private void assertPageContains(String pageUrl, String expectedBody, int expectedStatus) throws IOException {
    ByteChunk res = new ByteChunk();
    // Note: With a read timeout of 3s the ASF CI buildbot was consistently
    //       seeing failures with this test. The failures were due to the
    //       JSP initialisation taking longer than the read timeout. The
    //       root cause of this is the frequent poor IO performance of the
    //       VM running the buildbot instance. Increasing this to 10s should
    //       avoid these failures.
    int sc = getUrl("http://localhost:" + getPort() + pageUrl, res, 10000, null, null);
    assertEquals(expectedStatus, sc);
    if (expectedStatus == 200) {
        String result = res.toString();
        assertTrue(result, result.indexOf(expectedBody) >= 0);
    }
}
Also used : ByteChunk(org.apache.tomcat.util.buf.ByteChunk)

Aggregations

ByteChunk (org.apache.tomcat.util.buf.ByteChunk)274 Test (org.junit.Test)201 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)180 Tomcat (org.apache.catalina.startup.Tomcat)129 Context (org.apache.catalina.Context)98 File (java.io.File)49 List (java.util.List)48 AsyncContext (javax.servlet.AsyncContext)40 HashMap (java.util.HashMap)35 Wrapper (org.apache.catalina.Wrapper)22 StandardContext (org.apache.catalina.core.StandardContext)21 ArrayList (java.util.ArrayList)20 TesterContext (org.apache.tomcat.unittest.TesterContext)18 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)16 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)13 ServletResponseWrapper (javax.servlet.ServletResponseWrapper)13 ServletContext (javax.servlet.ServletContext)10 WsContextListener (org.apache.tomcat.websocket.server.WsContextListener)10 TesterAccessLogValve (org.apache.catalina.valves.TesterAccessLogValve)9 InitialContext (javax.naming.InitialContext)8