public void DecodeSingleByteInteger() { for (var i = 0; i < Iterations; i++) { _integerDecoder.BeginTryDecode(_singleByte, _prefixLength, out _); } }
public void IntegerEncoderDecoderRoundtrips() { var decoder = new IntegerDecoder(); var range = 1 << 8; foreach (var i in Enumerable.Range(0, range).Concat(Enumerable.Range(int.MaxValue - range + 1, range))) { for (int n = 1; n <= 8; n++) { var integerBytes = new byte[6]; Assert.True(IntegerEncoder.Encode(i, n, integerBytes, out var length)); var decodeResult = decoder.BeginTryDecode(integerBytes[0], n, out var intResult); for (int j = 1; j < length; j++) { Assert.False(decodeResult); decodeResult = decoder.TryDecode(integerBytes[j], out intResult); } Assert.True(decodeResult); Assert.Equal(i, intResult); } } }
public void BeginEncodeHeaders_MaxHeaderTableSizeUpdated_SizeUpdateInHeaders() { Span <byte> buffer = new byte[1024 * 16]; var hpackEncoder = new DynamicHPackEncoder(); hpackEncoder.UpdateMaxHeaderTableSize(100); var enumerator = new Http2HeadersEnumerator(); // First request enumerator.Initialize(new Dictionary <string, StringValues>()); Assert.True(HPackHeaderWriter.BeginEncodeHeaders(hpackEncoder, enumerator, buffer, out var length)); Assert.Equal(2, length); const byte DynamicTableSizeUpdateMask = 0xe0; var integerDecoder = new IntegerDecoder(); Assert.False(integerDecoder.BeginTryDecode((byte)(buffer[0] & ~DynamicTableSizeUpdateMask), prefixLength: 5, out _)); Assert.True(integerDecoder.TryDecode(buffer[1], out var result)); Assert.Equal(100, result); // Second request enumerator.Initialize(new Dictionary <string, StringValues>()); Assert.True(HPackHeaderWriter.BeginEncodeHeaders(hpackEncoder, enumerator, buffer, out length)); Assert.Equal(0, length); }
public void IntegerEncoderDecoderRoundtrips() { IntegerDecoder decoder = new IntegerDecoder(); Span <byte> integerBytes = stackalloc byte[5]; for (int i = 0; i < 2048; ++i) { for (int prefixLength = 1; prefixLength <= 8; ++prefixLength) { integerBytes.Clear(); Assert.True(IntegerEncoder.Encode(i, prefixLength, integerBytes, out int length)); bool decodeResult = decoder.BeginTryDecode(integerBytes[0], prefixLength, out int intResult); for (int j = 1; j < length; j++) { Assert.False(decodeResult); decodeResult = decoder.TryDecode(integerBytes[j], out intResult); } Assert.True(decodeResult); Assert.Equal(i, intResult); } } }
private void OnByte(byte b) { int intResult; int prefixInt; switch (_state) { case State.Ready: if ((b & HeaderAcknowledgementMask) == HeaderAcknowledgementRepresentation) { prefixInt = HeaderAcknowledgementPrefixMask & b; if (_integerDecoder.BeginTryDecode((byte)prefixInt, HeaderAcknowledgementPrefix, out intResult)) { OnHeaderAcknowledgement(intResult); } else { _state = State.HeaderAckowledgement; } } else if ((b & StreamCancellationMask) == StreamCancellationRepresentation) { prefixInt = StreamCancellationPrefixMask & b; if (_integerDecoder.BeginTryDecode((byte)prefixInt, StreamCancellationPrefix, out intResult)) { OnStreamCancellation(intResult); } else { _state = State.StreamCancellation; } } else if ((b & InsertCountIncrementMask) == InsertCountIncrementRepresentation) { prefixInt = InsertCountIncrementPrefixMask & b; if (_integerDecoder.BeginTryDecode((byte)prefixInt, InsertCountIncrementPrefix, out intResult)) { OnInsertCountIncrement(intResult); } else { _state = State.InsertCountIncrement; } } break; } }
public void IntegerDecode_Throws_IfMaxExceeded(int prefixLength, byte[] octets) { var decoder = new IntegerDecoder(); var result = decoder.BeginTryDecode(octets[0], prefixLength, out var intResult); for (var j = 1; j < octets.Length - 1; j++) { Assert.False(decoder.TryDecode(octets[j], out intResult)); } Assert.Throws <HPackDecodingException>(() => decoder.TryDecode(octets[octets.Length - 1], out intResult)); }
public void DecodeMultiByteInteger() { var integerDecoder = new IntegerDecoder(); for (var i = 0; i < Iterations; i++) { integerDecoder.BeginTryDecode(_multiByte[0], _prefixLength, out _); for (var j = 1; j < _multiByte.Length; j++) { integerDecoder.TryDecode(_multiByte[j], out _); } } }
[InlineData(new byte[] { 0xFF, 0xFF, 0x00 })] // Encoded with 1 byte too many. public void HPack_Integer_TooLarge(byte[] encoded) { Assert.Throws <HPackDecodingException>(() => { var dec = new IntegerDecoder(); if (!dec.BeginTryDecode((byte)(encoded[0] & 0x7F), 7, out int intResult)) { for (int i = 1; !dec.TryDecode(encoded[i], out intResult); ++i) { } } return(intResult); }); }
public void HPack_IntegerDecode(int expectedResult, int bits, byte[] encoded) { IntegerDecoder integerDecoder = new IntegerDecoder(); bool finished = integerDecoder.BeginTryDecode(encoded[0], bits, out int actualResult); int i = 1; for (; !finished && i < encoded.Length; ++i) { finished = integerDecoder.TryDecode(encoded[i], out actualResult); } Assert.True(finished); Assert.Equal(encoded.Length, i); Assert.Equal(expectedResult, actualResult); }
public void HPack_IntegerRoundTrip(int value, int bits) { var decoder = new IntegerDecoder(); Span <byte> encoded = stackalloc byte[5]; Assert.True(IntegerEncoder.Encode(value, bits, encoded, out int bytesWritten)); bool finished = decoder.BeginTryDecode(encoded[0], bits, out int intResult); int i = 1; for (; !finished && i < encoded.Length; ++i) { finished = decoder.TryDecode(encoded[i], out intResult); } Assert.True(finished); Assert.Equal(bytesWritten, i); Assert.Equal(value, intResult); }
public void IntegerDecode(int i, int prefixLength, byte[] octets) { var decoder = new IntegerDecoder(); var result = decoder.BeginTryDecode(octets[0], prefixLength, out var intResult); if (octets.Length == 1) { Assert.True(result); } else { var j = 1; for (; j < octets.Length - 1; j++) { Assert.False(decoder.TryDecode(octets[j], out intResult)); } Assert.True(decoder.TryDecode(octets[j], out intResult)); } Assert.Equal(i, intResult); }
private void OnByte(byte b) { int intResult; int prefixInt; switch (_state) { case State.Ready: if ((b & DynamicTableCapacityMask) == DynamicTableCapacityRepresentation) { prefixInt = DynamicTableCapacityPrefixMask & b; if (_integerDecoder.BeginTryDecode((byte)prefixInt, DynamicTableCapacityPrefix, out intResult)) { OnDynamicTableCapacity(intResult); } else { _state = State.DynamicTableCapcity; } } else if ((b & InsertWithNameReferenceMask) == InsertWithNameReferenceRepresentation) { prefixInt = InsertWithNameReferencePrefixMask & b; _s = (InsertWithNameReferenceStaticMask & b) == InsertWithNameReferenceStaticMask; if (_integerDecoder.BeginTryDecode((byte)prefixInt, InsertWithNameReferencePrefix, out intResult)) { OnNameIndex(intResult); } else { _state = State.NameIndex; } } else if ((b & InsertWithoutNameReferenceMask) == InsertWithoutNameReferenceRepresentation) { prefixInt = InsertWithoutNameReferencePrefixMask & b; _huffman = (InsertWithoutNameReferenceHuffmanMask & b) == InsertWithoutNameReferenceHuffmanMask; if (_integerDecoder.BeginTryDecode((byte)prefixInt, InsertWithoutNameReferencePrefix, out intResult)) { OnStringLength(intResult, State.Name); } else { _state = State.NameIndex; } } else if ((b & DuplicateMask) == DuplicateRepresentation) { prefixInt = DuplicatePrefixMask & b; if (_integerDecoder.BeginTryDecode((byte)prefixInt, DuplicatePrefix, out intResult)) { OnDuplicate(intResult); } else { _state = State.Duplicate; } } break; case State.DynamicTableCapcity: if (_integerDecoder.TryDecode(b, out intResult)) { OnDynamicTableCapacity(intResult); } break; case State.NameIndex: if (_integerDecoder.TryDecode(b, out intResult)) { OnNameIndex(intResult); } break; case State.NameLength: if (_integerDecoder.TryDecode(b, out intResult)) { OnStringLength(intResult, nextState: State.Name); } break; case State.Name: _stringOctets[_stringIndex++] = b; if (_stringIndex == _stringLength) { OnString(nextState: State.ValueLength); } break; case State.ValueLength: _huffman = (b & HuffmanMask) != 0; // TODO confirm this. if (_integerDecoder.BeginTryDecode((byte)(b & ~HuffmanMask), StringLengthPrefix, out intResult)) { OnStringLength(intResult, nextState: State.Value); if (intResult == 0) { ProcessValue(); } } else { _state = State.ValueLengthContinue; } break; case State.ValueLengthContinue: if (_integerDecoder.TryDecode(b, out intResult)) { OnStringLength(intResult, nextState: State.Value); if (intResult == 0) { ProcessValue(); } } break; case State.Value: _stringOctets[_stringIndex++] = b; if (_stringIndex == _stringLength) { ProcessValue(); } break; case State.Duplicate: if (_integerDecoder.TryDecode(b, out intResult)) { OnDuplicate(intResult); } break; } }