Example #1
0
        public void ThrowArgumentOutOfRangeException_WhenWouldMoveBeyondByteArrayLength()
        {
            var reader = new DhcpBinaryReader(TestBytes, 2, 4);

            Assert.Throws <ArgumentOutOfRangeException>(
                () => reader.Seek(250));
        }
Example #2
0
        public IReadOnlyList <IDhcpOption> DeserializeOptions(DhcpBinaryReader binaryReader)
        {
            var options = new List <IDhcpOption>();
            var optionsTaggedCollection = binaryReader.ReadValueToEnd().AsTaggedValueCollection();

            foreach (var optionTaggedItem in optionsTaggedCollection)
            {
                var descriptor = _optionTypeDescriptors.GetDescriptor((DhcpOptionTypeCode)optionTaggedItem.Key);

                if (descriptor == null)
                {
                    // Option not supported
                    continue;
                }

                object optionValue;

                if (descriptor.OptionValueType.IsAssignableFrom(typeof(DhcpBinaryValue)))
                {
                    optionValue = optionTaggedItem.Value;
                }
                else
                {
                    optionValue = optionTaggedItem.Value.As(descriptor.OptionValueType);
                }

                var option = CreateOption(descriptor, optionValue);

                options.Add(option);
            }

            return(options);
        }
Example #3
0
        public void ReturnNextByte()
        {
            var bytes  = new byte[] { 0x01, 0x02, 0x03 };
            var reader = new DhcpBinaryReader(bytes);

            Assert.Equal(0x01, reader.PeekByte());
        }
        public void ThrowInvalidOperationException_GivenLengthBeyondDataLength()
        {
            var reader = new DhcpBinaryReader(TestBytes);

            Assert.Throws <InvalidOperationException>(
                () => reader.ReadValue(50));
        }
        public void ThrowArgumentOutOfRangeException_GivenZeroOrNegativeLength(int length)
        {
            var reader = new DhcpBinaryReader(TestBytes);

            Assert.Throws <ArgumentOutOfRangeException>(
                () => reader.ReadValue(length));
        }
Example #6
0
        public void ThrowArgumentOutOfRangeException_WhenWouldMoveBeyondInitialOffset()
        {
            var reader = new DhcpBinaryReader(TestBytes, 2, 4);

            Assert.Throws <ArgumentOutOfRangeException>(
                () => reader.Seek(-1));
        }
Example #7
0
        public void ReturnTrue_WhenInMiddle_GivenOffset()
        {
            var reader = new DhcpBinaryReader(TestBytes, 2, 10);

            reader.ReadValue(4);

            Assert.True(reader.CanRead(2));
        }
Example #8
0
        public void ReturnFalse_WhenAtLimit()
        {
            var reader = new DhcpBinaryReader(TestBytes, 2, 4);

            reader.ReadValue(4);

            Assert.False(reader.CanRead(1));
        }
Example #9
0
        public void ReturnFalse_GivenLengthBeyondLimit()
        {
            var reader = new DhcpBinaryReader(TestBytes);

            reader.ReadValue(10);

            Assert.False(reader.CanRead(20));
        }
Example #10
0
        public void ReturnFalse_WhenAtEnd()
        {
            var reader = new DhcpBinaryReader(TestBytes);

            reader.ReadValueToEnd();

            Assert.False(reader.CanRead(1));
        }
Example #11
0
        public void ReadFromCurrentOffset()
        {
            var reader = new DhcpBinaryReader(TestBytes);

            reader.ReadValue(2);

            Assert.Equal(14, reader.ReadValueToEnd().AsBytes().Length);
        }
Example #12
0
        public void MoveByPassedOffsetForward()
        {
            var reader = new DhcpBinaryReader(TestBytes, 2, 10);

            reader.Seek(2);

            Assert.Equal(0x44, reader.ReadValue(1).AsByte());
        }
        public void ReadValueOfGivenLength()
        {
            var reader = new DhcpBinaryReader(TestBytes);

            var valueBytes = reader.ReadValue(3).AsBytes();

            Assert.Equal(3, valueBytes.Length);
        }
        public void ReadValueFromCurrentOffset()
        {
            var reader = new DhcpBinaryReader(TestBytes);

            reader.ReadValue(2);

            Assert.Equal(0x22, reader.ReadValue(1).AsByte());
        }
Example #15
0
        public void ThrowInvalidOperationException_WhenReaderCannotRead()
        {
            var reader = new DhcpBinaryReader(TestBytes);

            reader.ReadValue(TestBytes.Length);

            Assert.Throws <InvalidOperationException>(
                () => reader.ReadValueToEnd());
        }
        public void ReturnFalse_GivenBytesWithOnlyPaddingByte()
        {
            var bytes = new byte[] { 0x00 };

            var reader           = new DhcpBinaryReader(bytes);
            var collectionReader = new DhcpTaggedValueCollectionReader(reader);

            Assert.False(collectionReader.HasNextItem());
        }
Example #17
0
        public void ThrowArgumentOutOfRangeException_WhenWouldMoveBeyondByteArrayStart()
        {
            var reader = new DhcpBinaryReader(TestBytes, 2, 10);

            reader.ReadValue(2);

            Assert.Throws <ArgumentOutOfRangeException>(
                () => reader.Seek(-250));
        }
Example #18
0
        public void ThrowInvalidOperationException_WhenAtEndOfBytes()
        {
            var bytes  = new byte[] { 0x01, 0x02, 0x03 };
            var reader = new DhcpBinaryReader(bytes);

            reader.ReadValueToEnd();

            Assert.Throws <InvalidOperationException>(
                () => reader.PeekByte());
        }
Example #19
0
        public void ThrowsInvalidOperationException_GivenBytesWithOnlyPaddingByte()
        {
            var bytes = new byte[] { 0x00 };

            var reader           = new DhcpBinaryReader(bytes);
            var collectionReader = new DhcpTaggedValueCollectionReader(reader);

            Assert.Throws <InvalidOperationException>(
                () => collectionReader.NextItem());
        }
        public void ReturnFalse_WhenNextItemIsEndByte()
        {
            var bytes = new byte[] { 0x01, 0x02, 0x00, 0x00, 0xff, 0x01, 0x02, 0x00, 0x00 };

            var reader           = new DhcpBinaryReader(bytes);
            var collectionReader = new DhcpTaggedValueCollectionReader(reader);

            collectionReader.NextItem();

            Assert.False(collectionReader.HasNextItem());
        }
        public void ReturnTrue_WhenHasMoreItems()
        {
            var bytes = new byte[] { 0x01, 0x02, 0x00, 0x00, 0x02, 0x02, 0x01, 0x01 };

            var reader           = new DhcpBinaryReader(bytes);
            var collectionReader = new DhcpTaggedValueCollectionReader(reader);

            collectionReader.NextItem();

            Assert.True(collectionReader.HasNextItem());
        }
        public void ReturnTrue_GivenBytesWithPadOptionAndOtherOptions()
        {
            var bytes = new byte[] { 0x00, 0x01, 0x02, 0x00, 0x00 };

            var reader           = new DhcpBinaryReader(bytes);
            var collectionReader = new DhcpTaggedValueCollectionReader(reader);

            collectionReader.NextItem();

            Assert.False(collectionReader.HasNextItem());
        }
Example #23
0
        public void SkipPaddingOption()
        {
            var bytes = new byte[] { 0x00, 0x01, 0x02, 0x00, 0x00 };

            var reader           = new DhcpBinaryReader(bytes);
            var collectionReader = new DhcpTaggedValueCollectionReader(reader);

            var item = collectionReader.NextItem();

            Assert.Equal(1, item.Tag);
        }
        public void DeserializeRebindingTimeOption()
        {
            var optionsBytes = "3b04000003d4ff".AsHexBytes();

            var reader = new DhcpBinaryReader(optionsBytes);

            var options = _optionsSerializer.DeserializeOptions(reader);

            var option = options.OfType <DhcpRebindingTimeOption>().Single();

            Assert.Equal(980, option.RebindingTime.TotalSeconds);
        }
Example #25
0
        public void ThrowsInvalidOperationException_WhenNextItemIsEndByte()
        {
            var bytes = new byte[] { 0x01, 0x02, 0x00, 0x00, 0xff, 0x01, 0x02, 0x00, 0x00 };

            var reader           = new DhcpBinaryReader(bytes);
            var collectionReader = new DhcpTaggedValueCollectionReader(reader);

            collectionReader.NextItem();

            Assert.Throws <InvalidOperationException>(
                () => collectionReader.NextItem());
        }
        public void DeserializeMessageOption()
        {
            var optionsBytes = "381168656c6c6f2e6578616d706c652e636f6dff".AsHexBytes();

            var reader = new DhcpBinaryReader(optionsBytes);

            var options = _optionsSerializer.DeserializeOptions(reader);

            var rootPathOption = options.OfType <DhcpMessageOption>().Single();

            Assert.Equal("hello.example.com", rootPathOption.Message);
        }
        public void DeserializeMaxMessageSizeOption()
        {
            var optionsBytes = "390203d4".AsHexBytes();

            var reader = new DhcpBinaryReader(optionsBytes);

            var options = _optionsSerializer.DeserializeOptions(reader);

            var option = options.OfType <DhcpMaxMessageSizeOption>().Single();

            Assert.Equal(980, option.MaxSize);
        }
        public void DeserializeRouterOption()
        {
            var optionsBytes = "0304c0a80101ff".AsHexBytes();

            var reader = new DhcpBinaryReader(optionsBytes);

            var options = _optionsSerializer.DeserializeOptions(reader);

            var routerOption = options.OfType <DhcpRouterOption>().Single();

            Assert.Equal(IPAddress.Parse("192.168.1.1"), routerOption.RouterAddresses.Single());
        }
        public void DeserializeNetWareDomainOption()
        {
            var optionsBytes = "3e1168656c6c6f2e6578616d706c652e636f6d".AsHexBytes();

            var reader = new DhcpBinaryReader(optionsBytes);

            var options = _optionsSerializer.DeserializeOptions(reader);

            var option = options.OfType <DhcpNetWareDomainOption>().Single();

            Assert.Equal("hello.example.com", option.Domain);
        }
        public void DeserializeClientIdOption()
        {
            var optionsBytes = "3d04000003d4".AsHexBytes();

            var reader = new DhcpBinaryReader(optionsBytes);

            var options = _optionsSerializer.DeserializeOptions(reader);

            var option = options.OfType <DhcpClientIdOption>().Single();

            Assert.Equal(980U, option.ClientId.AsUnsignedInt32());
        }