public void HandleSpecificModule_IsValidStructWIthArguments_ReturnsParsedValue() { // Arrange string userInput = "None, 80"; string stringNumber = "80"; string enumOption = "None"; string[] arrayOfUserValues = { enumOption, stringNumber }; ushort number = 80; IpV4FragmentationOptions ipV4FragmentationOptions = IpV4FragmentationOptions.MoreFragments; Type typeOfEnum = typeof(IpV4Fragmentation); IpV4Fragmentation ipV4Fragmentation = new IpV4Fragmentation(ipV4FragmentationOptions, number); object[] parameters = new object[2] { ipV4FragmentationOptions, number }; _moduleStructHandlerMock.Setup(x => x.CheckIfStructMustHaveArgumentsAndReturnObject(typeOfEnum, userInput)) .Returns(ipV4Fragmentation); // Act var result = _target.HandleSpecificModule(typeOfEnum, userInput); // Assert result.Should().BeOfType <IpV4Fragmentation>(); result.GetType().GetProperty("Offset").GetValue(result).Should().Be(80); result.GetType().GetProperty("Options").GetValue(result).Should().Be(IpV4FragmentationOptions.MoreFragments); }
public static IpV4Fragmentation NextIpV4Fragmentation(this Random random) { IpV4FragmentationOptions ipV4FragmentationFlags = random.NextEnum <IpV4FragmentationOptions>(); ushort ipV4FragmentationOffset = (ushort)(random.NextUShort() / 8 * 8); return(new IpV4Fragmentation(ipV4FragmentationFlags, ipV4FragmentationOffset)); }
public void CheckIfStructMustHaveArgumentsAndReturnObject_IsValidStructWithManyArguments_ReturnsObject() { // Arrange ushort offset = 80; IpV4FragmentationOptions options = IpV4FragmentationOptions.MoreFragments; IpV4Fragmentation ipV4Fragmentation = new IpV4Fragmentation(options, offset); string input = $"{options}, {offset}"; object[] array = new object[2] { options, offset }; _genericInstanceCreator.Setup(x => x.TryCreateNewInstance <IpV4Fragmentation?>(array)) .Returns(ipV4Fragmentation); _moduleStructArgumentsHandler.Setup(x => x.InputToParametersForIpV4Fragmentation(input)) .Returns(array); // Act var result = _target.CheckIfStructMustHaveArgumentsAndReturnObject(typeof(IpV4Fragmentation), input); // Assert result.Should().BeOfType <IpV4Fragmentation>(); var pp = result.GetType().GetProperty("Offset").GetValue(result).Should().Be(offset); result.GetType().GetProperty("Options").GetValue(result).Should().Be(options); }
public IpV4Fragmentation(IpV4FragmentationOptions options, ushort offset) { this = new IpV4Fragmentation((ushort)(options | (IpV4FragmentationOptions)((int)offset / 8))); if ((int)offset % 8 != 0) { throw new ArgumentException("offset must divide by 8", "offset"); } if ((options & (IpV4FragmentationOptions)8191) != IpV4FragmentationOptions.None) { throw new ArgumentException("invalid options " + (object)options); } }
/// <summary> /// Creates fragmentation field value according to the given information. /// </summary> /// <param name="options">Options for fragmentation (must be one of the values of the enum).</param> /// <param name="offset">This field indicates where in the complete datagram this fragment belongs. Measured in bytes but must divide by 8.</param> public IpV4Fragmentation(IpV4FragmentationOptions options, ushort offset) : this((ushort)((ushort)options | (offset / 8))) { if (offset % 8 != 0) { throw new ArgumentException("offset must divide by 8", "offset"); } if (((ushort)options & 0x1FFF) != 0) { throw new ArgumentException("invalid options " + options); } }