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); } } }
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 DecodeSingleByteInteger() { var integerDecoder = new IntegerDecoder(); for (var i = 0; i < Iterations; i++) { integerDecoder.BeginTryDecode(_singleByte, _prefixLength, out _); } }
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 Decode_Given_an_invalid_number_Value_should_be_null() { var field = new Field { Raw = "10a" }; target = new IntegerDecoder { Pattern = @"(?!0+)-?[0-9]{1,6}", FailValidationResult = ValidationResultType.Warning }; target.Decode(field); Assert.IsNull(field.Value); }
public void Decode_Given_a_valid_number_ValidationResult_should_be_valid() { var field = new Field { Raw = "10" }; target = new IntegerDecoder { Pattern = @"(?!0+)-?[0-9]{1,6}", FailValidationResult = ValidationResultType.Warning }; target.Decode(field); Assert.AreEqual(ValidationResultType.Valid, field.ValidationResult); }
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 _); } } }
public void Decode_Given_an_invalid_number_ValidationResult_should_be_set_with_the_value_assigned_to_the_decoder(ValidationResultType failValidationResult) { var field = new Field { Raw = "10a" }; target = new IntegerDecoder { Pattern = @"(?!0+)-?[0-9]{1,6}", FailValidationResult = failValidationResult }; target.Decode(field); Assert.AreEqual(failValidationResult, field.ValidationResult); }
[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.StartDecode((byte)(encoded[0] & 0x7F), 7)) { for (int i = 1; !dec.Decode(encoded[i]); ++i) { } } return(dec.Value); }); }
[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.StartDecode(encoded[0], bits); int i = 1; for (; !finished && i < encoded.Length; ++i) { finished = decoder.Decode(encoded[i]); } Assert.True(finished); Assert.Equal(bytesWritten, i); Assert.Equal(value, decoder.Value); }
public void Decode_Given_that_property_pattern_is_not_set_Should_throw_an_exception() { var field = new Field { Raw = "10" }; target = new IntegerDecoder { FailValidationResult = ValidationResultType.Warning }; try { target.Decode(field); } catch (InvalidOperationException ex) { Assert.AreEqual("Property Pattern cannot be empty or null", ex.Message); return; } Assert.Fail("An exception was not thrown"); }
public void IntegerDecode(int i, int prefixLength, byte[] octets) { var decoder = new IntegerDecoder(); var result = decoder.BeginDecode(octets[0], prefixLength); if (octets.Length == 1) { Assert.True(result); } else { var j = 1; for (; j < octets.Length - 1; j++) { Assert.False(decoder.Decode(octets[j])); } Assert.True(decoder.Decode(octets[j])); } Assert.Equal(i, decoder.Value); }
public void TestBufferDerivation() { decoder = new DuplicatingIntegerDecoder(); buf.PutInt32(1); // Put some extra byte to make the decoder create an internal buffer. buf.Put((byte)0); buf.Flip(); decoder.Decode(session, buf, session.DecoderOutput); Assert.AreEqual(1, session.DecoderOutputQueue.Count); Assert.AreEqual(1, session.DecoderOutputQueue.Dequeue()); Assert.AreEqual(buf.Limit, buf.Position); // Keep appending to the internal buffer. // DuplicatingIntegerDecoder will keep duplicating the internal // buffer to disable auto-expansion, and CumulativeProtocolDecoder // should detect that user derived its internal buffer. // Consequently, CumulativeProtocolDecoder will perform // reallocation to avoid putting incoming data into // the internal buffer with auto-expansion disabled. for (int i = 2; i < 10; i++) { buf.Clear(); buf.PutInt32(i); // Put some extra byte to make the decoder keep the internal buffer. buf.Put((byte)0); buf.Flip(); buf.Position = 1; decoder.Decode(session, buf, session.DecoderOutput); Assert.AreEqual(1, session.DecoderOutputQueue.Count); Assert.AreEqual(i, session.DecoderOutputQueue.Dequeue()); Assert.AreEqual(buf.Limit, buf.Position); } }
public void SetUp() { buf = IoBuffer.Allocate(16); decoder = new IntegerDecoder(); session.SetTransportMetadata(new DefaultTransportMetadata("mina", "dummy", false, true, typeof(System.Net.IPEndPoint))); }