Search in sources :

Example 91 with Test

use of org.junit.Test in project jetty.project by eclipse.

the class HTTP2ServerTest method testBadPingWrongPayload.

@Test
public void testBadPingWrongPayload() throws Exception {
    startServer(new HttpServlet() {
    });
    ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
    generator.control(lease, new PrefaceFrame());
    generator.control(lease, new SettingsFrame(new HashMap<>(), false));
    generator.control(lease, new PingFrame(new byte[8], false));
    // Modify the length of the frame to a wrong one.
    lease.getByteBuffers().get(2).putShort(0, (short) 7);
    final CountDownLatch latch = new CountDownLatch(1);
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            output.write(BufferUtil.toArray(buffer));
        }
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onGoAway(GoAwayFrame frame) {
                Assert.assertEquals(ErrorCode.FRAME_SIZE_ERROR.code, frame.getError());
                latch.countDown();
            }
        }, 4096, 8192);
        parseResponse(client, parser);
        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HashMap(java.util.HashMap) PingFrame(org.eclipse.jetty.http2.frames.PingFrame) HttpServlet(javax.servlet.http.HttpServlet) OutputStream(java.io.OutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) Socket(java.net.Socket) Test(org.junit.Test)

Example 92 with Test

use of org.junit.Test in project jetty.project by eclipse.

the class HTTP2ServerTest method testCommitFailure.

@Test
public void testCommitFailure() throws Exception {
    final long delay = 1000;
    final AtomicBoolean broken = new AtomicBoolean();
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            try {
                // Wait for the SETTINGS frames to be exchanged.
                Thread.sleep(delay);
                broken.set(true);
            } catch (InterruptedException x) {
                throw new InterruptedIOException();
            }
        }
    });
    server.stop();
    ServerConnector connector2 = new ServerConnector(server, new HTTP2ServerConnectionFactory(new HttpConfiguration())) {

        @Override
        protected ChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException {
            return new SocketChannelEndPoint(channel, selectSet, key, getScheduler()) {

                @Override
                public void write(Callback callback, ByteBuffer... buffers) throws IllegalStateException {
                    if (broken.get())
                        callback.failed(new IOException("explicitly_thrown_by_test"));
                    else
                        super.write(callback, buffers);
                }
            };
        }
    };
    server.addConnector(connector2);
    server.start();
    ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
    generator.control(lease, new PrefaceFrame());
    generator.control(lease, new SettingsFrame(new HashMap<>(), false));
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    generator.control(lease, new HeadersFrame(1, metaData, null, true));
    try (Socket client = new Socket("localhost", connector2.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
        // The server will close the connection abruptly since it
        // cannot write and therefore cannot even send the GO_AWAY.
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter(), 4096, 8192);
        boolean closed = parseResponse(client, parser, 2 * delay);
        Assert.assertTrue(closed);
    }
}
Also used : ManagedSelector(org.eclipse.jetty.io.ManagedSelector) ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) InterruptedIOException(java.io.InterruptedIOException) SocketChannel(java.nio.channels.SocketChannel) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServerConnector(org.eclipse.jetty.server.ServerConnector) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) SelectionKey(java.nio.channels.SelectionKey) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callback(org.eclipse.jetty.util.Callback) SocketChannelEndPoint(org.eclipse.jetty.io.SocketChannelEndPoint) Socket(java.net.Socket) Test(org.junit.Test)

Example 93 with Test

use of org.junit.Test in project jetty.project by eclipse.

the class HTTP2ServerTest method testBadPingWrongStreamId.

@Test
public void testBadPingWrongStreamId() throws Exception {
    startServer(new HttpServlet() {
    });
    ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
    generator.control(lease, new PrefaceFrame());
    generator.control(lease, new SettingsFrame(new HashMap<>(), false));
    generator.control(lease, new PingFrame(new byte[8], false));
    // Modify the streamId of the frame to non zero.
    lease.getByteBuffers().get(2).putInt(4, 1);
    final CountDownLatch latch = new CountDownLatch(1);
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            output.write(BufferUtil.toArray(buffer));
        }
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onGoAway(GoAwayFrame frame) {
                Assert.assertEquals(ErrorCode.PROTOCOL_ERROR.code, frame.getError());
                latch.countDown();
            }
        }, 4096, 8192);
        parseResponse(client, parser);
        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HashMap(java.util.HashMap) PingFrame(org.eclipse.jetty.http2.frames.PingFrame) HttpServlet(javax.servlet.http.HttpServlet) OutputStream(java.io.OutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) Socket(java.net.Socket) Test(org.junit.Test)

Example 94 with Test

use of org.junit.Test in project jetty.project by eclipse.

the class HttpClientTransportOverHTTP2Test method testRequestIdleTimeoutSendsResetFrame.

@Test
public void testRequestIdleTimeoutSendsResetFrame() throws Exception {
    CountDownLatch resetLatch = new CountDownLatch(1);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            return new Stream.Listener.Adapter() {

                @Override
                public void onReset(Stream stream, ResetFrame frame) {
                    resetLatch.countDown();
                }
            };
        }
    });
    try {
        long idleTimeout = 1000;
        client.newRequest("localhost", connector.getLocalPort()).idleTimeout(idleTimeout, TimeUnit.MILLISECONDS).send();
        Assert.fail();
    } catch (ExecutionException e) {
    // Expected.
    }
    Assert.assertTrue(resetLatch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Stream(org.eclipse.jetty.http2.api.Stream) OutputStream(java.io.OutputStream) InputStream(java.io.InputStream) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Test(org.junit.Test)

Example 95 with Test

use of org.junit.Test in project jetty.project by eclipse.

the class HttpClientTransportOverHTTP2Test method testClientStopsServerDoesNotCloseClientCloses.

@Test
public void testClientStopsServerDoesNotCloseClientCloses() throws Exception {
    try (ServerSocket server = new ServerSocket(0)) {
        List<Session> sessions = new ArrayList<>();
        HTTP2Client h2Client = new HTTP2Client();
        HttpClient client = new HttpClient(new HttpClientTransportOverHTTP2(h2Client) {

            @Override
            protected HttpConnectionOverHTTP2 newHttpConnection(HttpDestination destination, Session session) {
                sessions.add(session);
                return super.newHttpConnection(destination, session);
            }
        }, null);
        QueuedThreadPool clientExecutor = new QueuedThreadPool();
        clientExecutor.setName("client");
        client.setExecutor(clientExecutor);
        client.start();
        CountDownLatch resultLatch = new CountDownLatch(1);
        client.newRequest("localhost", server.getLocalPort()).send(result -> {
            if (result.getResponse().getStatus() == HttpStatus.OK_200)
                resultLatch.countDown();
        });
        ByteBufferPool byteBufferPool = new MappedByteBufferPool();
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        Generator generator = new Generator(byteBufferPool);
        try (Socket socket = server.accept()) {
            socket.setSoTimeout(1000);
            OutputStream output = socket.getOutputStream();
            InputStream input = socket.getInputStream();
            ServerParser parser = new ServerParser(byteBufferPool, new ServerParser.Listener.Adapter() {

                @Override
                public void onHeaders(HeadersFrame request) {
                    // Server's preface.
                    generator.control(lease, new SettingsFrame(new HashMap<>(), false));
                    // Reply to client's SETTINGS.
                    generator.control(lease, new SettingsFrame(new HashMap<>(), true));
                    // Response.
                    MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
                    HeadersFrame response = new HeadersFrame(request.getStreamId(), metaData, null, true);
                    generator.control(lease, response);
                    try {
                        // Write the frames.
                        for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
                    } catch (Throwable x) {
                        x.printStackTrace();
                    }
                }
            }, 4096, 8192);
            byte[] bytes = new byte[1024];
            while (true) {
                try {
                    int read = input.read(bytes);
                    if (read < 0)
                        Assert.fail();
                    parser.parse(ByteBuffer.wrap(bytes, 0, read));
                } catch (SocketTimeoutException x) {
                    break;
                }
            }
            Assert.assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
            // The client will send a GO_AWAY, but the server will not close.
            client.stop();
            // Give some time to process the stop/close operations.
            Thread.sleep(1000);
            Assert.assertTrue(h2Client.getBeans(Session.class).isEmpty());
            for (Session session : sessions) {
                Assert.assertTrue(session.isClosed());
                Assert.assertTrue(((HTTP2Session) session).isDisconnected());
            }
        }
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ServerParser(org.eclipse.jetty.http2.parser.ServerParser) InputStream(java.io.InputStream) ServerSocket(java.net.ServerSocket) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) SocketTimeoutException(java.net.SocketTimeoutException) HttpClient(org.eclipse.jetty.client.HttpClient) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client) HttpDestination(org.eclipse.jetty.client.HttpDestination) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Generator(org.eclipse.jetty.http2.generator.Generator) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)597324 ArrayList (java.util.ArrayList)32672 QuickTest (com.hazelcast.test.annotation.QuickTest)27910 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)25464 File (java.io.File)24462 HashMap (java.util.HashMap)21030 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)19416 List (java.util.List)16717 IOException (java.io.IOException)13352 Map (java.util.Map)9238 Date (java.util.Date)9014 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8368 InputStream (java.io.InputStream)7753 Properties (java.util.Properties)7445 CountDownLatch (java.util.concurrent.CountDownLatch)7403 ByteArrayInputStream (java.io.ByteArrayInputStream)7401 HashSet (java.util.HashSet)7165 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6707 Response (javax.ws.rs.core.Response)6154 Ignore (org.junit.Ignore)5936