AuthMetadataCodec { public static ByteBuf encodeSimpleMetadata( ByteBufAllocator allocator, char[] username, char[] password) { int usernameLength = CharByteBufUtil.utf8Bytes(username); if (usernameLength > 65535) { throw new IllegalArgumentException( "Username should be shorter than or equal to 65535 bytes length in UTF-8 encoding"); } int passwordLength = CharByteBufUtil.utf8Bytes(password); int capacity = AUTH_TYPE_ID_LENGTH + USERNAME_BYTES_LENGTH + usernameLength + passwordLength; final ByteBuf buffer = allocator .buffer(capacity, capacity) .writeByte(WellKnownAuthType.SIMPLE.getIdentifier() | STREAM_METADATA_KNOWN_MASK) .writeShort(usernameLength); CharByteBufUtil.writeUtf8(buffer, username); CharByteBufUtil.writeUtf8(buffer, password); return buffer; } 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 shouldCorrectlyEncodeData() { String username = "test"; String password = "tset1234"; int usernameLength = username.length(); int passwordLength = password.length(); ByteBuf byteBuf = AuthMetadataCodec.encodeSimpleMetadata( ByteBufAllocator.DEFAULT, username.toCharArray(), password.toCharArray()); byteBuf.markReaderIndex(); checkSimpleAuthMetadataEncoding( username, password, usernameLength, passwordLength, byteBuf.retain()); byteBuf.resetReaderIndex(); checkSimpleAuthMetadataEncodingUsingDecoders( username, password, usernameLength, passwordLength, byteBuf); }
@Test void shouldCorrectlyEncodeData1() { String username = "𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎"; String password = "tset1234"; int usernameLength = username.getBytes(CharsetUtil.UTF_8).length; int passwordLength = password.length(); ByteBuf byteBuf = AuthMetadataCodec.encodeSimpleMetadata( ByteBufAllocator.DEFAULT, username.toCharArray(), password.toCharArray()); byteBuf.markReaderIndex(); checkSimpleAuthMetadataEncoding( username, password, usernameLength, passwordLength, byteBuf.retain()); byteBuf.resetReaderIndex(); checkSimpleAuthMetadataEncodingUsingDecoders( username, password, usernameLength, passwordLength, byteBuf); }
@Test void shouldCorrectlyEncodeData2() { String username = "𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎1234567#4? "; String password = "tset1234"; int usernameLength = username.getBytes(CharsetUtil.UTF_8).length; int passwordLength = password.length(); ByteBuf byteBuf = AuthMetadataCodec.encodeSimpleMetadata( ByteBufAllocator.DEFAULT, username.toCharArray(), password.toCharArray()); byteBuf.markReaderIndex(); checkSimpleAuthMetadataEncoding( username, password, usernameLength, passwordLength, byteBuf.retain()); byteBuf.resetReaderIndex(); checkSimpleAuthMetadataEncodingUsingDecoders( username, password, usernameLength, passwordLength, byteBuf); }
@Test void shouldThrowExceptionIfUsernameLengthExitsAllowedBounds() { StringBuilder usernameBuilder = new StringBuilder(); String usernamePart = "𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎𠸏𠹷𠺝𠺢𠻗𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵𢫕𢭃𢯊𢱑𢱕𢳂𢴈𢵌𢵧𢺳𣲷𤓓𤶸𤷪𥄫𦉘𦟌𦧲𦧺𧨾𨅝𨈇𨋢𨳊𨳍𨳒𩶘𠜎𠜱𠝹"; for (int i = 0; i < 65535 / usernamePart.length(); i++) { usernameBuilder.append(usernamePart); } String password = "tset1234"; Assertions.assertThatThrownBy( () -> AuthMetadataCodec.encodeSimpleMetadata( ByteBufAllocator.DEFAULT, usernameBuilder.toString().toCharArray(), password.toCharArray())) .hasMessage( "Username should be shorter than or equal to 65535 bytes length in UTF-8 encoding"); }