Example usage for io.netty.buffer ByteBuf readSlice

List of usage examples for io.netty.buffer ByteBuf readSlice

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf readSlice.

Prototype

public abstract ByteBuf readSlice(int length);

Source Link

Document

Returns a new slice of this buffer's sub-region starting at the current readerIndex and increases the readerIndex by the size of the new slice (= length ).

Usage

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parserHeader_frameOkFragment() throws GeneralSecurityException {
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);
    ByteBuf in = getDirectBuffer(AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes(),
            ref);// w ww.j  ava 2 s . c o m
    in.writeIntLE(FRAME_MIN_SIZE);
    in.writeIntLE(6);
    ByteBuf in1 = in.readSlice(AltsTsiFrameProtector.getHeaderBytes() - 1);
    ByteBuf in2 = in.readSlice(1);

    unprotector.unprotect(in1, out, alloc);
    assertThat(in1.readableBytes()).isEqualTo(0);

    unprotector.unprotect(in2, out, alloc);
    assertThat(in2.readableBytes()).isEqualTo(0);

    unprotector.destroy();
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parseHeader_frameFailFragment() throws GeneralSecurityException {
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);
    ByteBuf in = getDirectBuffer(AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes(),
            ref);// ww w.  j av  a2  s .com
    in.writeIntLE(FRAME_MIN_SIZE - 1);
    in.writeIntLE(6);
    ByteBuf in1 = in.readSlice(AltsTsiFrameProtector.getHeaderBytes() - 1);
    ByteBuf in2 = in.readSlice(1);

    unprotector.unprotect(in1, out, alloc);
    assertThat(in1.readableBytes()).isEqualTo(0);

    try {
        unprotector.unprotect(in2, out, alloc);
        fail("Exception expected");
    } catch (IllegalArgumentException ex) {
        assertThat(ex).hasMessageThat().contains("Invalid header field: frame size too small");
    }

    assertThat(in2.readableBytes()).isEqualTo(0);

    unprotector.destroy();
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parseFrame_twoFramesNoFragment() throws GeneralSecurityException {
    int payloadBytes = 1536;
    int payloadBytes1 = 1024;
    int payloadBytes2 = payloadBytes - payloadBytes1;
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);

    ByteBuf plain = getRandom(payloadBytes, ref);
    ByteBuf outFrame = getDirectBuffer(//  www . ja  va 2 s  . c  o m
            2 * (AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes()) + payloadBytes,
            ref);

    outFrame.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes1 + FakeChannelCrypter.getTagBytes());
    outFrame.writeIntLE(6);
    List<ByteBuf> framePlain1 = Collections.singletonList(plain.readSlice(payloadBytes1));
    ByteBuf frameOut1 = writeSlice(outFrame, payloadBytes1 + FakeChannelCrypter.getTagBytes());

    outFrame.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes2 + FakeChannelCrypter.getTagBytes());
    outFrame.writeIntLE(6);
    List<ByteBuf> framePlain2 = Collections.singletonList(plain);
    ByteBuf frameOut2 = writeSlice(outFrame, payloadBytes2 + FakeChannelCrypter.getTagBytes());

    crypter.encrypt(frameOut1, framePlain1);
    crypter.encrypt(frameOut2, framePlain2);
    plain.readerIndex(0);

    unprotector.unprotect(outFrame, out, alloc);
    assertThat(out.size()).isEqualTo(1);
    ByteBuf out1 = ref((ByteBuf) out.get(0));
    assertThat(out1).isEqualTo(plain);
    assertThat(outFrame.refCnt()).isEqualTo(1);
    assertThat(outFrame.readableBytes()).isEqualTo(0);

    unprotector.destroy();
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parseFrame_twoFramesNoFragment_Leftover() throws GeneralSecurityException {
    int payloadBytes = 1536;
    int payloadBytes1 = 1024;
    int payloadBytes2 = payloadBytes - payloadBytes1;
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);

    ByteBuf plain = getRandom(payloadBytes, ref);
    ByteBuf protectedBuf = getDirectBuffer(
            2 * (AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes()) + payloadBytes
                    + AltsTsiFrameProtector.getHeaderBytes(),
            ref);//from   w w w  .j a  v  a2  s. c o m

    protectedBuf.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes1 + FakeChannelCrypter.getTagBytes());
    protectedBuf.writeIntLE(6);
    List<ByteBuf> framePlain1 = Collections.singletonList(plain.readSlice(payloadBytes1));
    ByteBuf frameOut1 = writeSlice(protectedBuf, payloadBytes1 + FakeChannelCrypter.getTagBytes());

    protectedBuf.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes2 + FakeChannelCrypter.getTagBytes());
    protectedBuf.writeIntLE(6);
    List<ByteBuf> framePlain2 = Collections.singletonList(plain);
    ByteBuf frameOut2 = writeSlice(protectedBuf, payloadBytes2 + FakeChannelCrypter.getTagBytes());
    // This is an invalid header length field, make sure it triggers an error
    // when the remainder of the header is given.
    protectedBuf.writeIntLE((byte) -1);

    crypter.encrypt(frameOut1, framePlain1);
    crypter.encrypt(frameOut2, framePlain2);
    plain.readerIndex(0);

    unprotector.unprotect(protectedBuf, out, alloc);
    assertThat(out.size()).isEqualTo(1);
    ByteBuf out1 = ref((ByteBuf) out.get(0));
    assertThat(out1).isEqualTo(plain);

    // The protectedBuf is buffered inside the unprotector.
    assertThat(protectedBuf.readableBytes()).isEqualTo(0);
    assertThat(protectedBuf.refCnt()).isEqualTo(2);

    protectedBuf.writeIntLE(6);
    try {
        unprotector.unprotect(protectedBuf, out, alloc);
        fail("Exception expected");
    } catch (IllegalArgumentException ex) {
        assertThat(ex).hasMessageThat().contains("Invalid header field: frame size too small");
    }

    unprotector.destroy();

    // Make sure that unprotector does not hold onto buffered ByteBuf instance after destroy.
    assertThat(protectedBuf.refCnt()).isEqualTo(1);

    // Make sure that destroying twice does not throw.
    unprotector.destroy();
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parseFrame_twoFramesFragmentSecond() throws GeneralSecurityException {
    int payloadBytes = 1536;
    int payloadBytes1 = 1024;
    int payloadBytes2 = payloadBytes - payloadBytes1;
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);

    ByteBuf plain = getRandom(payloadBytes, ref);
    ByteBuf protectedBuf = getDirectBuffer(
            2 * (AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes()) + payloadBytes
                    + AltsTsiFrameProtector.getHeaderBytes(),
            ref);/*ww w.jav  a2s. c  o  m*/

    protectedBuf.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes1 + FakeChannelCrypter.getTagBytes());
    protectedBuf.writeIntLE(6);
    List<ByteBuf> framePlain1 = Collections.singletonList(plain.readSlice(payloadBytes1));
    ByteBuf frameOut1 = writeSlice(protectedBuf, payloadBytes1 + FakeChannelCrypter.getTagBytes());

    protectedBuf.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes2 + FakeChannelCrypter.getTagBytes());
    protectedBuf.writeIntLE(6);
    List<ByteBuf> framePlain2 = Collections.singletonList(plain);
    ByteBuf frameOut2 = writeSlice(protectedBuf, payloadBytes2 + FakeChannelCrypter.getTagBytes());

    crypter.encrypt(frameOut1, framePlain1);
    crypter.encrypt(frameOut2, framePlain2);
    plain.readerIndex(0);

    unprotector
            .unprotect(
                    protectedBuf.readSlice(payloadBytes + AltsTsiFrameProtector.getHeaderBytes()
                            + FakeChannelCrypter.getTagBytes() + AltsTsiFrameProtector.getHeaderBytes()),
                    out, alloc);
    assertThat(out.size()).isEqualTo(1);
    ByteBuf out1 = ref((ByteBuf) out.get(0));
    assertThat(out1).isEqualTo(plain.readSlice(payloadBytes1));
    assertThat(protectedBuf.refCnt()).isEqualTo(2);

    unprotector.unprotect(protectedBuf, out, alloc);
    assertThat(out.size()).isEqualTo(2);
    ByteBuf out2 = ref((ByteBuf) out.get(1));
    assertThat(out2).isEqualTo(plain);
    assertThat(protectedBuf.refCnt()).isEqualTo(1);

    unprotector.destroy();
}

From source file:io.grpc.alts.internal.FakeChannelCrypter.java

License:Apache License

@Override
public void decrypt(ByteBuf out, ByteBuf ciphertextAndTag) throws GeneralSecurityException {
    checkState(!destroyCalled);//  w ww  .ja va2 s.com
    ByteBuf ciphertext = ciphertextAndTag.readSlice(ciphertextAndTag.readableBytes() - TAG_BYTES);
    decrypt(out, /*tag=*/ ciphertextAndTag, Collections.singletonList(ciphertext));
}

From source file:io.grpc.alts.internal.TsiTest.java

License:Apache License

/** Sends a message between the sender and receiver. */
private static void sendMessage(TsiFrameProtector sender, TsiFrameProtector receiver, int recvFragmentSize,
        String message, RegisterRef ref) throws GeneralSecurityException {

    ByteBuf plaintextBuffer = Unpooled.wrappedBuffer(message.getBytes(UTF_8));
    final List<ByteBuf> protectOut = new ArrayList<>();
    List<Object> unprotectOut = new ArrayList<>();

    sender.protectFlush(Collections.singletonList(plaintextBuffer), new Consumer<ByteBuf>() {
        @Override/*w w w .  ja  va2s  .  co m*/
        public void accept(ByteBuf buf) {
            protectOut.add(buf);
        }
    }, alloc);
    assertThat(protectOut.size()).isEqualTo(1);

    ByteBuf protect = ref.register(protectOut.get(0));
    while (protect.isReadable()) {
        ByteBuf buf = protect;
        if (recvFragmentSize > 0) {
            int size = Math.min(protect.readableBytes(), recvFragmentSize);
            buf = protect.readSlice(size);
        }
        receiver.unprotect(buf, unprotectOut, alloc);
    }
    ByteBuf plaintextRecvd = getDirectBuffer(message.getBytes(UTF_8).length, ref);
    for (Object unprotect : unprotectOut) {
        ByteBuf unprotectBuf = ref.register((ByteBuf) unprotect);
        plaintextRecvd.writeBytes(unprotectBuf);
    }
    assertThat(plaintextRecvd).isEqualTo(Unpooled.wrappedBuffer(message.getBytes(UTF_8)));
}

From source file:io.nodyn.http.HTTPParser.java

License:Apache License

protected boolean readRequestLine() {
    ByteBuf line = readLine();
    if (line == null) {
        return false;
    }/*from ww w . j  av a 2s .  c  om*/

    int space = line.indexOf(line.readerIndex(), line.readerIndex() + line.readableBytes(), (byte) ' ');
    if (space < 0) {
        setError(Error.INVALID_METHOD);
        return false;
    }

    int len = space - line.readerIndex();

    ByteBuf methodBuf = line.readSlice(len);

    String methodName = methodBuf.toString(UTF8);
    for (int i = 0; i < METHODS.length; ++i) {
        if (METHODS[i].equals(methodName)) {
            this.method = i;
            break;
        }
    }

    if (this.method == null) {
        setError(Error.INVALID_METHOD);
        return false;
    }

    if ("CONNECT".equals(methodName)) {
        this.upgrade = true;
    }

    // skip the space
    line.readByte();

    space = line.indexOf(line.readerIndex(), line.readerIndex() + line.readableBytes(), (byte) ' ');

    ByteBuf urlBuf = null;
    ByteBuf versionBuf = null;
    if (space < 0) {
        // HTTP/1.0
        urlBuf = line.readSlice(line.readableBytes());
    } else {
        len = space - line.readerIndex();
        urlBuf = line.readSlice(len);
        versionBuf = line.readSlice(line.readableBytes());
    }

    this.url = urlBuf.toString(UTF8).trim();

    if (versionBuf != null) {
        if (!readVersion(versionBuf)) {
            setError(Error.INVALID_VERSION);
            return false;
        }
    } else {
        this.versionMajor = 1;
        this.versionMinor = 0;
    }
    return true;
}

From source file:io.nodyn.http.HTTPParser.java

License:Apache License

protected boolean readStatusLine() {
    ByteBuf line = readLine();

    if (line == null) {
        return false;
    }/*from w  ww. ja v a 2 s.c o  m*/

    int space = line.indexOf(line.readerIndex(), line.readerIndex() + line.readableBytes(), (byte) ' ');

    if (space < 0) {
        setError(Error.INVALID_VERSION);
        return false;
    }

    int len = space - line.readerIndex();

    ByteBuf versionBuf = line.readSlice(len);

    if (!readVersion(versionBuf)) {
        setError(Error.INVALID_VERSION);
        return false;
    }

    // skip space
    line.readByte();

    space = line.indexOf(line.readerIndex(), line.readerIndex() + line.readableBytes(), (byte) ' ');

    if (space < 0) {
        setError(Error.INVALID_STATUS);
        return false;
    }

    len = space - line.readerIndex();

    ByteBuf statusBuf = line.readSlice(len);

    int status = -1;

    try {
        status = Integer.parseInt(statusBuf.toString(UTF8));
    } catch (NumberFormatException e) {
        setError(Error.INVALID_STATUS);
        return false;
    }

    if (status > 999 || status < 100) {
        setError(Error.INVALID_STATUS);
        return false;
    }

    this.statusCode = status;

    // skip space
    line.readByte();

    ByteBuf messageBuf = line.readSlice(line.readableBytes());

    this.statusMessage = messageBuf.toString(UTF8).trim();

    return true;
}

From source file:io.nodyn.http.HTTPParser.java

License:Apache License

protected boolean readHeader(ByteBuf line, List<String> target, boolean analyze) {
    int colonLoc = line.indexOf(line.readerIndex(), line.readerIndex() + line.readableBytes(), (byte) ':');

    if (colonLoc < 0) {
        // maybe it's a continued header
        if (line.readableBytes() > 1) {
            char c = (char) line.getByte(0);
            if (c == ' ' || c == '\t') {
                // it IS a continued header value
                int lastIndex = this.headers.size() - 1;
                String val = this.headers.get(lastIndex);
                val = val + " " + line.toString(ASCII).trim();
                this.headers.set(lastIndex, val);
                return true;
            }//from   ww w . j a  va2 s. com
        }
        return false;
    }

    int len = colonLoc - line.readerIndex();
    ByteBuf keyBuf = line.readSlice(len);

    // skip colon
    line.readByte();

    ByteBuf valueBuf = line.readSlice(line.readableBytes());

    String key = keyBuf.toString(UTF8).trim();
    String value = valueBuf.toString(UTF8).trim();

    target.add(key);
    target.add(value);

    if (analyze) {
        return analyzeHeader(key.toLowerCase(), value);
    }

    return true;
}