AuthMetadataCodec { public static CharSequence readCustomAuthType(ByteBuf metadata) { if (metadata.readableBytes() < 2) { throw new IllegalStateException( "Unable to decode custom Auth type. Not enough readable bytes"); } byte encodedLength = metadata.readByte(); if (encodedLength < 0) { throw new IllegalStateException( "Unable to decode custom Auth type. Incorrect auth type length"); } int realLength = encodedLength + 1; if (metadata.readableBytes() < realLength) { throw new IllegalArgumentException( "Unable to decode custom Auth type. Malformed length or auth type string"); } return metadata.readCharSequence(realLength, CharsetUtil.US_ASCII); } private  AuthMetadataCodec(); static ByteBuf encodeMetadata(
      ByteBufAllocator allocator, String customAuthType, ByteBuf metadata); static ByteBuf encodeMetadata(
      ByteBufAllocator allocator, WellKnownAuthType authType, ByteBuf metadata); static ByteBuf encodeSimpleMetadata(
      ByteBufAllocator allocator, char[] username, char[] password); static ByteBuf encodeBearerMetadata(ByteBufAllocator allocator, char[] token); static ByteBuf encodeMetadataWithCompression(
      ByteBufAllocator allocator, String authType, ByteBuf metadata); static boolean isWellKnownAuthType(ByteBuf metadata); static WellKnownAuthType readWellKnownAuthType(ByteBuf metadata); static CharSequence readCustomAuthType(ByteBuf metadata); static ByteBuf readPayload(ByteBuf metadata); static ByteBuf readUsername(ByteBuf simpleAuthMetadata); static ByteBuf readPassword(ByteBuf simpleAuthMetadata); static char[] readUsernameAsCharArray(ByteBuf simpleAuthMetadata); static char[] readPasswordAsCharArray(ByteBuf simpleAuthMetadata); static char[] readBearerTokenAsCharArray(ByteBuf bearerAuthMetadata);  }
@Test void shouldThrowExceptionOnEmptyMetadata() { Assertions.assertThatThrownBy(() -> AuthMetadataCodec.readCustomAuthType(Unpooled.EMPTY_BUFFER)) .hasMessage("Unable to decode custom Auth type. Not enough readable bytes"); }
@Test void shouldThrowExceptionOnMalformedMetadata_length() { Assertions.assertThatThrownBy( () -> AuthMetadataCodec.readCustomAuthType( ByteBufAllocator.DEFAULT.buffer().writeByte(127).writeChar('a').writeChar('b'))) .hasMessage("Unable to decode custom Auth type. Malformed length or auth type string"); }