Search in sources :

Example 1 with SettingsGenerator

use of org.eclipse.jetty.http2.generator.SettingsGenerator in project jetty.project by eclipse.

the class SettingsGenerateParseTest method testGenerateParseInvalidSettings.

@Test
public void testGenerateParseInvalidSettings() throws Exception {
    SettingsGenerator generator = new SettingsGenerator(new HeaderGenerator());
    final AtomicInteger errorRef = new AtomicInteger();
    Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

        @Override
        public void onConnectionFailure(int error, String reason) {
            errorRef.set(error);
        }
    }, 4096, 8192);
    Map<Integer, Integer> settings1 = new HashMap<>();
    settings1.put(13, 17);
    ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
    generator.generateSettings(lease, settings1, true);
    // Modify the length of the frame to make it invalid
    ByteBuffer bytes = lease.getByteBuffers().get(0);
    bytes.putShort(1, (short) (bytes.getShort(1) - 1));
    for (ByteBuffer buffer : lease.getByteBuffers()) {
        while (buffer.hasRemaining()) {
            parser.parse(ByteBuffer.wrap(new byte[] { buffer.get() }));
        }
    }
    Assert.assertEquals(ErrorCode.FRAME_SIZE_ERROR.code, errorRef.get());
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HeaderGenerator(org.eclipse.jetty.http2.generator.HeaderGenerator) SettingsGenerator(org.eclipse.jetty.http2.generator.SettingsGenerator) Test(org.junit.Test)

Example 2 with SettingsGenerator

use of org.eclipse.jetty.http2.generator.SettingsGenerator in project jetty.project by eclipse.

the class SettingsGenerateParseTest method testGenerateParseOneByteAtATime.

@Test
public void testGenerateParseOneByteAtATime() throws Exception {
    SettingsGenerator generator = new SettingsGenerator(new HeaderGenerator());
    final List<SettingsFrame> frames = new ArrayList<>();
    Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

        @Override
        public void onSettings(SettingsFrame frame) {
            frames.add(frame);
        }
    }, 4096, 8192);
    Map<Integer, Integer> settings1 = new HashMap<>();
    int key = 13;
    Integer value = 17;
    settings1.put(key, value);
    // Iterate a few times to be sure generator and parser are properly reset.
    for (int i = 0; i < 2; ++i) {
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        generator.generateSettings(lease, settings1, true);
        frames.clear();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            while (buffer.hasRemaining()) {
                parser.parse(ByteBuffer.wrap(new byte[] { buffer.get() }));
            }
        }
        Assert.assertEquals(1, frames.size());
        SettingsFrame frame = frames.get(0);
        Map<Integer, Integer> settings2 = frame.getSettings();
        Assert.assertEquals(1, settings2.size());
        Assert.assertEquals(value, settings2.get(key));
        Assert.assertTrue(frame.isReply());
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HeaderGenerator(org.eclipse.jetty.http2.generator.HeaderGenerator) SettingsGenerator(org.eclipse.jetty.http2.generator.SettingsGenerator) Test(org.junit.Test)

Example 3 with SettingsGenerator

use of org.eclipse.jetty.http2.generator.SettingsGenerator in project jetty.project by eclipse.

the class SettingsGenerateParseTest method testGenerateParse.

private List<SettingsFrame> testGenerateParse(Map<Integer, Integer> settings) {
    SettingsGenerator generator = new SettingsGenerator(new HeaderGenerator());
    final List<SettingsFrame> frames = new ArrayList<>();
    Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

        @Override
        public void onSettings(SettingsFrame frame) {
            frames.add(frame);
        }
    }, 4096, 8192);
    // Iterate a few times to be sure generator and parser are properly reset.
    for (int i = 0; i < 2; ++i) {
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        generator.generateSettings(lease, settings, true);
        frames.clear();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            while (buffer.hasRemaining()) {
                parser.parse(buffer);
            }
        }
    }
    return frames;
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) HeaderGenerator(org.eclipse.jetty.http2.generator.HeaderGenerator) SettingsGenerator(org.eclipse.jetty.http2.generator.SettingsGenerator)

Aggregations

ByteBuffer (java.nio.ByteBuffer)3 HeaderGenerator (org.eclipse.jetty.http2.generator.HeaderGenerator)3 SettingsGenerator (org.eclipse.jetty.http2.generator.SettingsGenerator)3 Parser (org.eclipse.jetty.http2.parser.Parser)3 ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)3 MappedByteBufferPool (org.eclipse.jetty.io.MappedByteBufferPool)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Test (org.junit.Test)2