Sends a message between the sender and receiver.
(
TsiFrameProtector sender,
TsiFrameProtector receiver,
int recvFragmentSize,
String message,
RegisterRef ref)
| 117 | |
| 118 | /** Sends a message between the sender and receiver. */ |
| 119 | private static void sendMessage( |
| 120 | TsiFrameProtector sender, |
| 121 | TsiFrameProtector receiver, |
| 122 | int recvFragmentSize, |
| 123 | String message, |
| 124 | RegisterRef ref) |
| 125 | throws GeneralSecurityException { |
| 126 | |
| 127 | ByteBuf plaintextBuffer = Unpooled.wrappedBuffer(message.getBytes(UTF_8)); |
| 128 | final List<ByteBuf> protectOut = new ArrayList<>(); |
| 129 | List<Object> unprotectOut = new ArrayList<>(); |
| 130 | |
| 131 | sender.protectFlush( |
| 132 | Collections.singletonList(plaintextBuffer), |
| 133 | new Consumer<ByteBuf>() { |
| 134 | @Override |
| 135 | public void accept(ByteBuf buf) { |
| 136 | protectOut.add(buf); |
| 137 | } |
| 138 | }, |
| 139 | alloc); |
| 140 | assertThat(protectOut.size()).isEqualTo(1); |
| 141 | |
| 142 | ByteBuf protect = ref.register(protectOut.get(0)); |
| 143 | while (protect.isReadable()) { |
| 144 | ByteBuf buf = protect; |
| 145 | if (recvFragmentSize > 0) { |
| 146 | int size = Math.min(protect.readableBytes(), recvFragmentSize); |
| 147 | buf = protect.readSlice(size); |
| 148 | } |
| 149 | receiver.unprotect(buf, unprotectOut, alloc); |
| 150 | } |
| 151 | ByteBuf plaintextRecvd = getDirectBuffer(message.getBytes(UTF_8).length, ref); |
| 152 | for (Object unprotect : unprotectOut) { |
| 153 | ByteBuf unprotectBuf = ref.register((ByteBuf) unprotect); |
| 154 | plaintextRecvd.writeBytes(unprotectBuf); |
| 155 | } |
| 156 | assertThat(plaintextRecvd).isEqualTo(Unpooled.wrappedBuffer(message.getBytes(UTF_8))); |
| 157 | } |
| 158 | |
| 159 | /** Ping pong test. */ |
| 160 | public static void pingPongTest(Handshakers handshakers, RegisterRef ref) |
no test coverage detected