Utility class that provides tests for implementations of TsiHandshaker.
| 36 | |
| 37 | /** Utility class that provides tests for implementations of {@link TsiHandshaker}. */ |
| 38 | public final class TsiTest { |
| 39 | private static final String DECRYPTION_FAILURE_RE = "Tag mismatch|BAD_DECRYPT"; |
| 40 | |
| 41 | private TsiTest() {} |
| 42 | |
| 43 | /** A {@code TsiHandshaker} pair for running tests. */ |
| 44 | public static class Handshakers { |
| 45 | private final TsiHandshaker client; |
| 46 | private final TsiHandshaker server; |
| 47 | |
| 48 | public Handshakers(TsiHandshaker client, TsiHandshaker server) { |
| 49 | this.client = client; |
| 50 | this.server = server; |
| 51 | } |
| 52 | |
| 53 | public TsiHandshaker getClient() { |
| 54 | return client; |
| 55 | } |
| 56 | |
| 57 | public TsiHandshaker getServer() { |
| 58 | return server; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | private static final int DEFAULT_TRANSPORT_BUFFER_SIZE = 2048; |
| 63 | |
| 64 | private static final UnpooledByteBufAllocator alloc = UnpooledByteBufAllocator.DEFAULT; |
| 65 | |
| 66 | private static final String EXAMPLE_MESSAGE1 = "hello world"; |
| 67 | private static final String EXAMPLE_MESSAGE2 = "oysteroystersoysterseateateat"; |
| 68 | |
| 69 | private static final int EXAMPLE_MESSAGE1_LEN = EXAMPLE_MESSAGE1.getBytes(UTF_8).length; |
| 70 | private static final int EXAMPLE_MESSAGE2_LEN = EXAMPLE_MESSAGE2.getBytes(UTF_8).length; |
| 71 | |
| 72 | static int getDefaultTransportBufferSize() { |
| 73 | return DEFAULT_TRANSPORT_BUFFER_SIZE; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Performs a handshake between the client handshaker and server handshaker using a transport of |
| 78 | * length transportBufferSize. |
| 79 | */ |
| 80 | static void performHandshake(int transportBufferSize, Handshakers handshakers) |
| 81 | throws GeneralSecurityException { |
| 82 | TsiHandshaker clientHandshaker = handshakers.getClient(); |
| 83 | TsiHandshaker serverHandshaker = handshakers.getServer(); |
| 84 | |
| 85 | byte[] transportBufferBytes = new byte[transportBufferSize]; |
| 86 | ByteBuffer transportBuffer = ByteBuffer.wrap(transportBufferBytes); |
| 87 | ((Buffer) transportBuffer).limit(0); // Start off with an empty buffer |
| 88 | |
| 89 | while (clientHandshaker.isInProgress() || serverHandshaker.isInProgress()) { |
| 90 | for (TsiHandshaker handshaker : new TsiHandshaker[] {clientHandshaker, serverHandshaker}) { |
| 91 | if (handshaker.isInProgress()) { |
| 92 | // Process any bytes on the wire. |
| 93 | if (transportBuffer.hasRemaining()) { |
| 94 | handshaker.processBytesFromPeer(transportBuffer); |
| 95 | } |