@Test public void processBytesFromPeerStartServerFinished() throws Exception { ByteBuffer transportBuffer = ByteBuffer.allocate(TRANSPORT_BUFFER_SIZE); ByteBuffer outputFrame = ByteBuffer.allocate(OUT_FRAME_SIZE); verify(mockServer, never()).startClientHandshake(); verify(mockServer, never()).next(Matchers.<ByteBuffer>any()); when(mockServer.startServerHandshake(transportBuffer)).thenReturn(outputFrame); when(mockServer.isFinished()).thenReturn(true); assertTrue(handshakerServer.processBytesFromPeer(transportBuffer)); }
@Override public boolean processBytesFromPeer(ByteBuffer bytes) throws GeneralSecurityException { if (outputFrame == null && isClient) { return true; } if (outputFrame != null && outputFrame.hasRemaining()) { return true; } int remaining = bytes.remaining(); if (outputFrame == null) { checkState(!isClient, "Client handshaker should not process any frame at the beginning."); outputFrame = handshaker.startServerHandshake(bytes); } else { outputFrame = handshaker.next(bytes); } if (handshaker.isFinished() || outputFrame.hasRemaining()) { return true; } if (!bytes.hasRemaining()) { return false; } checkState(bytes.remaining() < remaining, "Handshaker did not consume any bytes."); return processBytesFromPeer(bytes); }
AltsTsiHandshaker implements TsiHandshaker { @Override public boolean processBytesFromPeer(ByteBuffer bytes) throws GeneralSecurityException { if (outputFrame == null && isClient) { return true; } if (outputFrame != null && outputFrame.hasRemaining()) { return true; } int remaining = bytes.remaining(); if (outputFrame == null) { checkState(!isClient, "Client handshaker should not process any frame at the beginning."); outputFrame = handshaker.startServerHandshake(bytes); } else { outputFrame = handshaker.next(bytes); } if (handshaker.isFinished() || outputFrame.hasRemaining()) { return true; } if (!bytes.hasRemaining()) { return false; } checkState(bytes.remaining() < remaining, "Handshaker did not consume any bytes."); return processBytesFromPeer(bytes); } }
AltsTsiHandshaker implements TsiHandshaker { @Override public boolean processBytesFromPeer(ByteBuffer bytes) throws GeneralSecurityException { if (outputFrame == null && isClient) { return true; } if (outputFrame != null && outputFrame.hasRemaining()) { return true; } int remaining = bytes.remaining(); if (outputFrame == null) { checkState(!isClient, "Client handshaker should not process any frame at the beginning."); outputFrame = handshaker.startServerHandshake(bytes); } else { outputFrame = handshaker.next(bytes); } if (handshaker.isFinished() || outputFrame.hasRemaining()) { return true; } if (!bytes.hasRemaining()) { return false; } checkState(bytes.remaining() < remaining, "Handshaker did not consume any bytes."); return processBytesFromPeer(bytes); } private  AltsTsiHandshaker(
      boolean isClient, HandshakerServiceStub stub, AltsHandshakerOptions options); @VisibleForTesting  AltsTsiHandshaker(boolean isClient, AltsHandshakerClient handshaker); }
AltsTsiHandshaker implements TsiHandshaker { @Override public boolean processBytesFromPeer(ByteBuffer bytes) throws GeneralSecurityException { if (outputFrame == null && isClient) { return true; } if (outputFrame != null && outputFrame.hasRemaining()) { return true; } int remaining = bytes.remaining(); if (outputFrame == null) { checkState(!isClient, "Client handshaker should not process any frame at the beginning."); outputFrame = handshaker.startServerHandshake(bytes); } else { outputFrame = handshaker.next(bytes); } if (handshaker.isFinished() || outputFrame.hasRemaining()) { return true; } if (!bytes.hasRemaining()) { return false; } checkState(bytes.remaining() < remaining, "Handshaker did not consume any bytes."); return processBytesFromPeer(bytes); } private  AltsTsiHandshaker(
      boolean isClient, HandshakerServiceStub stub, AltsHandshakerOptions options); @VisibleForTesting  AltsTsiHandshaker(boolean isClient, AltsHandshakerClient handshaker); @Override boolean processBytesFromPeer(ByteBuffer bytes); @Override TsiPeer extractPeer(); @Override Object extractPeerObject(); static TsiHandshaker newClient(HandshakerServiceStub stub, AltsHandshakerOptions options); static TsiHandshaker newServer(HandshakerServiceStub stub, AltsHandshakerOptions options); @Override void getBytesToSendToPeer(ByteBuffer bytes); @Override boolean isInProgress(); @Override TsiFrameProtector createFrameProtector(int maxFrameSize, ByteBufAllocator alloc); @Override TsiFrameProtector createFrameProtector(ByteBufAllocator alloc); }
AltsTsiHandshaker implements TsiHandshaker { @Override public boolean processBytesFromPeer(ByteBuffer bytes) throws GeneralSecurityException { if (outputFrame == null && isClient) { return true; } if (outputFrame != null && outputFrame.hasRemaining()) { return true; } int remaining = bytes.remaining(); if (outputFrame == null) { checkState(!isClient, "Client handshaker should not process any frame at the beginning."); outputFrame = handshaker.startServerHandshake(bytes); } else { outputFrame = handshaker.next(bytes); } if (handshaker.isFinished() || outputFrame.hasRemaining()) { return true; } if (!bytes.hasRemaining()) { return false; } checkState(bytes.remaining() < remaining, "Handshaker did not consume any bytes."); return processBytesFromPeer(bytes); } private  AltsTsiHandshaker(
      boolean isClient, HandshakerServiceStub stub, AltsHandshakerOptions options); @VisibleForTesting  AltsTsiHandshaker(boolean isClient, AltsHandshakerClient handshaker); @Override boolean processBytesFromPeer(ByteBuffer bytes); @Override TsiPeer extractPeer(); @Override Object extractPeerObject(); static TsiHandshaker newClient(HandshakerServiceStub stub, AltsHandshakerOptions options); static TsiHandshaker newServer(HandshakerServiceStub stub, AltsHandshakerOptions options); @Override void getBytesToSendToPeer(ByteBuffer bytes); @Override boolean isInProgress(); @Override TsiFrameProtector createFrameProtector(int maxFrameSize, ByteBufAllocator alloc); @Override TsiFrameProtector createFrameProtector(ByteBufAllocator alloc); static final String TSI_SERVICE_ACCOUNT_PEER_PROPERTY; }