Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
        }
        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);
        }
Exemplo n.º 4
0
        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 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);
            }
        }
Exemplo n.º 6
0
        [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);
            });
        }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
        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 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);
            }
        }