public DHCPv6ConfirmHandledEvent(Guid scopeId, DHCPv6Packet request, ConfirmErrors error) : this(scopeId, request, DHCPv6Packet.Empty, error) { if (error == ConfirmErrors.NoError) { throw new ArgumentException("if a request has no error, a response packet is needed"); } }
public DHCPv6InformRequestHandledEvent(Guid scopeId, DHCPv6Packet request, InformRequestErros error) : this(scopeId, request, DHCPv6Packet.Empty, error) { if (error == InformRequestErros.NoError) { throw new ArgumentException("if a request has no error, a response packet is needed"); } }
public void FromByteArray_BooleanOption() { Random random = new Random(); UInt32 transactionId = (UInt32)random.Next(0, 256 * 256 * 256); IPv6HeaderInformation header = new IPv6HeaderInformation(IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::2")); UInt16 firstIdentifier = 4000; UInt16 secondIdentifier = 4002; DHCPv6Packet packet = new DHCPv6Packet(header, transactionId, DHCPv6PacketTypes.Solicit, new List <DHCPv6PacketOption> { new DHCPv6PacketBooleanOption(firstIdentifier, true), new DHCPv6PacketBooleanOption(secondIdentifier, false), } ); try { DHCPv6PacketOptionFactory.AddOptionType(firstIdentifier, (data) => DHCPv6PacketBooleanOption.FromByteArray(data, 0), true); DHCPv6PacketOptionFactory.AddOptionType(secondIdentifier, (data) => DHCPv6PacketBooleanOption.FromByteArray(data, 0), true); CheckByteRepresentation(packet, header); } finally { DHCPv6PacketOptionFactory.RemoveOptionType(firstIdentifier); DHCPv6PacketOptionFactory.RemoveOptionType(secondIdentifier); } }
public void GetInnerPacket_MultipleEncapsulated() { IPv6HeaderInformation header = new IPv6HeaderInformation(IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::2")); Int32 depth = 10; DHCPv6Packet expectedInnerPacket = DHCPv6Packet.AsInner(1, DHCPv6PacketTypes.REPLY, Array.Empty <DHCPv6PacketOption>()); DHCPv6Packet innerPacket = expectedInnerPacket; for (int i = 0; i < depth; i++) { DHCPv6RelayPacket outerPacket = DHCPv6RelayPacket.AsInnerRelay( true, 1, IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::2"), Array.Empty <DHCPv6PacketOption>(), innerPacket); innerPacket = outerPacket; } DHCPv6RelayPacket inputPacket = DHCPv6RelayPacket.AsOuterRelay( header, true, 1, IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::2"), Array.Empty <DHCPv6PacketOption>(), innerPacket); DHCPv6Packet actualInnerPacket = inputPacket.GetInnerPacket(); Assert.Equal(expectedInnerPacket, actualInnerPacket); }
public void SerializeAndDeserialize() { Random random = new Random(); JsonSerializerSettings settings = new JsonSerializerSettings(); settings.Converters.Add(new IPv6AddressJsonConverter()); settings.Converters.Add(new DHCPv6PacketJsonConverter()); settings.Converters.Add(new IPv6HeaderInformationJsonConverter()); IPv6HeaderInformation header = new IPv6HeaderInformation( random.GetIPv6Address(), random.GetIPv6Address()); DHCPv6Packet input = new DHCPv6Packet(header, random.NextUInt16(), DHCPv6PacketTypes.Solicit, new List <DHCPv6PacketOption> { new DHCPv6PacketIdentifierOption(DHCPv6PacketOptionTypes.ClientIdentifier, new UUIDDUID(Guid.NewGuid())), new DHCPv6PacketIdentifierOption(DHCPv6PacketOptionTypes.ServerIdentifer, new UUIDDUID(Guid.NewGuid())), } ); String serialized = JsonConvert.SerializeObject(input, settings); var actual = JsonConvert.DeserializeObject <DHCPv6Packet>(serialized, settings); Assert.Equal(input, actual); }
public void FromByteArray_IdentityAssociationForPrefixAddressesOption() { Random random = new Random(); UInt32 transactionId = (UInt32)random.Next(0, 256 * 256 * 256); IPv6HeaderInformation header = new IPv6HeaderInformation(IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::2")); DHCPv6Packet packet = new DHCPv6Packet(header, transactionId, DHCPv6PacketTypes.Solicit, new List <DHCPv6PacketOption> { new DHCPv6PacketIdentityAssociationPrefixDelegationOption( random.NextUInt16(), TimeSpan.FromSeconds(random.Next(0, 36000)), TimeSpan.FromSeconds(random.Next(0, 36000)), new List <DHCPv6PacketSuboption> { new DHCPv6PacketIdentityAssociationPrefixDelegationSuboption( TimeSpan.FromSeconds(random.Next(0, 36000)), TimeSpan.FromSeconds(random.Next(0, 36000)), random.NextByte(), IPv6Address.FromString("fe80::20"), new List <DHCPv6PacketSuboption>() { new DHCPv6PacketStatusCodeSuboption(DHCPv6StatusCodes.Success, "Thanks for using DaAPI") } ) } ), new DHCPv6PacketIPAddressOption(DHCPv6PacketOptionTypes.ServerUnicast, IPv6Address.FromString("fe80::1")), } ); CheckByteRepresentation(packet, header); }
public void GetUniqueIdentifier() { Random random = new Random(); var resolver = new DHCPv6SimpleZyxelIESResolver(); Byte[] macAddress = random.NextBytes(6); Int32 slotId = random.NextByte(); Int32 portId = random.NextByte(); DHCPv6Packet packet = GetPacket(random, macAddress, slotId, portId); Byte[] actual = resolver.GetUniqueIdentifier(packet); for (int i = 0; i < 4; i++) { Assert.Equal(0, actual[i]); } for (int i = 0; i < macAddress.Length; i++) { Assert.Equal(macAddress[i], actual[i + 4]); } Byte[] expectedInterfaceValue = GetExpectedByteSequence(slotId, portId); for (int i = 0; i < expectedInterfaceValue.Length; i++) { Assert.Equal(expectedInterfaceValue[i], actual[i + macAddress.Length + 4]); } }
public void PacketMeetsCondition(Boolean shouldMeetCondition) { Random random = new Random(); String ipAddress = "fe80::1"; IPv6Address address = IPv6Address.FromString(ipAddress); Mock <ISerializer> serializerMock = new Mock <ISerializer>(MockBehavior.Strict); serializerMock.Setup(x => x.Deserialze <IPv6Address>(ipAddress)).Returns(address).Verifiable(); var resolver = new DHCPv6RelayAgentResolver(); resolver.ApplyValues(new Dictionary <String, String> { { "RelayAgentAddress", ipAddress }, }, serializerMock.Object); var packet = DHCPv6RelayPacket.AsOuterRelay(new IPv6HeaderInformation(IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::2")), true, 1, random.GetIPv6Address(), random.GetIPv6Address(), Array.Empty <DHCPv6PacketOption>(), DHCPv6RelayPacket.AsInnerRelay( true, 0, shouldMeetCondition == true ? IPv6Address.FromString(ipAddress) : IPv6Address.FromString("2004::1"), IPv6Address.FromString("fe80::1"), new DHCPv6PacketOption[] { }, DHCPv6Packet.AsInner(random.NextUInt16(), DHCPv6PacketTypes.Solicit, Array.Empty <DHCPv6PacketOption>()))); Boolean result = resolver.PacketMeetsCondition(packet); Assert.Equal(shouldMeetCondition, result); serializerMock.Verify(); }
public void PacketMeetsCondition(Boolean isInSubnet) { Random random = new Random(); String ipAddress = "fe80::0"; Byte subnetmaskLength = 32; IPv6Address address = IPv6Address.FromString(ipAddress); IPv6SubnetMask mask = new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(subnetmaskLength)); Mock <ISerializer> serializerMock = new Mock <ISerializer>(MockBehavior.Strict); serializerMock.Setup(x => x.Deserialze <IPv6Address>(ipAddress)).Returns(address).Verifiable(); serializerMock.Setup(x => x.Deserialze <IPv6SubnetMask>(subnetmaskLength.ToString())).Returns(mask).Verifiable(); DHCPv6RelayAgentSubnetResolver resolver = new DHCPv6RelayAgentSubnetResolver(); resolver.ApplyValues(new Dictionary <String, String> { { "NetworkAddress", ipAddress }, { "SubnetMask", subnetmaskLength.ToString() }, }, serializerMock.Object); var packet = DHCPv6RelayPacket.AsOuterRelay(new IPv6HeaderInformation(IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::2")), true, 1, random.GetIPv6Address(), random.GetIPv6Address(), Array.Empty <DHCPv6PacketOption>(), DHCPv6RelayPacket.AsInnerRelay( true, 0, isInSubnet == true ? IPv6Address.FromString("fe80::1") : IPv6Address.FromString("2004::1"), IPv6Address.FromString("fe80::1"), new DHCPv6PacketOption[] { }, DHCPv6Packet.AsInner(random.NextUInt16(), DHCPv6PacketTypes.Solicit, Array.Empty <DHCPv6PacketOption>()))); Boolean result = resolver.PacketMeetsCondition(packet); Assert.Equal(isInSubnet, result); serializerMock.Verify(); }
private DHCPv6Packet GetPacket(Random random, Byte[] remoteId, Int32 slotId, Int32 portId, Int32 enterpriseId = 0, Boolean includeRelevantOptions = true) { IPv6HeaderInformation headerInformation = new IPv6HeaderInformation(IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::2")); var packetOptions = new List <DHCPv6PacketOption> { new DHCPv6PacketTrueOption(DHCPv6PacketOptionTypes.RapitCommit), }; DHCPv6Packet innerPacket = DHCPv6Packet.AsInner(random.NextUInt16(), DHCPv6PacketTypes.Solicit, packetOptions); DHCPv6Packet innerRelayPacket = DHCPv6RelayPacket.AsInnerRelay(true, 1, random.GetIPv6Address(), random.GetIPv6Address(), new DHCPv6PacketOption[] { new DHCPv6PacketIdentifierOption(DHCPv6PacketOptionTypes.ClientIdentifier, new UUIDDUID(random.NextGuid())), includeRelevantOptions == true ? (DHCPv6PacketOption)(new DHCPv6PacketRemoteIdentifierOption((UInt32)enterpriseId, remoteId)) : new DHCPv6PacketTrueOption(DHCPv6PacketOptionTypes.RapitCommit), includeRelevantOptions == true ? (DHCPv6PacketOption)(new DHCPv6PacketByteArrayOption(DHCPv6PacketOptionTypes.InterfaceId, GetExpectedByteSequence(slotId, portId))) : new DHCPv6PacketTimeOption(DHCPv6PacketOptionTypes.ElapsedTime, 10, DHCPv6PacketTimeOption.DHCPv6PacketTimeOptionUnits.Minutes) }, innerPacket); DHCPv6Packet outerRelayPacket = DHCPv6RelayPacket.AsOuterRelay(headerInformation, true, 1, random.GetIPv6Address(), random.GetIPv6Address(), new DHCPv6PacketOption[] { new DHCPv6PacketByteArrayOption(DHCPv6PacketOptionTypes.InterfaceId, random.NextBytes(10)), new DHCPv6PacketRemoteIdentifierOption(9, random.NextBytes(12)) }, innerRelayPacket); return(outerRelayPacket); }
public void PacketMeetsCondition_False_WrongPacketType() { Random random = new Random(); UInt32 enterpriseNumber = random.NextUInt32(); Int32 agentIndex = 2; DHCPv6RemoteIdentifierEnterpriseNumberResolver resolver = new DHCPv6RemoteIdentifierEnterpriseNumberResolver( Mock.Of <ILogger <DHCPv6RemoteIdentifierEnterpriseNumberResolver> >()); Mock <ISerializer> serializerMock = new Mock <ISerializer>(MockBehavior.Strict); serializerMock.Setup(x => x.Deserialze <UInt32>(enterpriseNumber.ToString())).Returns(enterpriseNumber).Verifiable(); serializerMock.Setup(x => x.Deserialze <Int32?>(agentIndex.ToString())).Returns(agentIndex).Verifiable(); resolver.ApplyValues(new Dictionary <String, String> { { "EnterpriseNumber", enterpriseNumber.ToString() }, { "RelayAgentIndex", agentIndex.ToString() }, }, serializerMock.Object); DHCPv6Packet packet = DHCPv6Packet.AsInner(random.NextUInt16(), DHCPv6PacketTypes.Solicit, Array.Empty <DHCPv6PacketOption>()); Boolean result = resolver.PacketMeetsCondition(packet); Assert.False(result); serializerMock.Verify(); }
public void FromByteArray_RelayPacket_SingleRelayPacket() { Random random = new Random(); UInt32 transactionId = (UInt32)random.Next(0, 256 * 256 * 256); IPv6HeaderInformation header = new IPv6HeaderInformation(IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::2")); DHCPv6Packet innerPacket = DHCPv6Packet.AsInner(transactionId, DHCPv6PacketTypes.Solicit, new List <DHCPv6PacketOption> { new DHCPv6PacketTrueOption(DHCPv6PacketOptionTypes.RapitCommit), new DHCPv6PacketTrueOption(DHCPv6PacketOptionTypes.ReconfigureAccepte), }); DHCPv6RelayPacket relayPacket = DHCPv6RelayPacket.AsOuterRelay( header, true, random.NextByte(), IPv6Address.FromString("fe80::acde"), IPv6Address.FromString("fe80::acdf"), new List <DHCPv6PacketOption> { new DHCPv6PacketByteArrayOption(DHCPv6PacketOptionTypes.InterfaceId, random.NextBytes(23)), new DHCPv6PacketRemoteIdentifierOption(random.NextUInt32(), random.NextBytes(15)) }, innerPacket); CheckByteRepresentation(relayPacket, header); }
public void PacketMeetsCondition(Boolean shouldMeetCondition) { Random random = new Random(); String duidBytes = "000300015CA62DD98800"; LinkLayerAddressDUID duid = new LinkLayerAddressDUID( LinkLayerAddressDUID.DUIDLinkLayerTypes.Ethernet, new Byte[] { 0x5c, 0xa6, 0x2d, 0xd9, 0x88, 0x00 }); Mock <ISerializer> serializerMock = new Mock <ISerializer>(MockBehavior.Strict); serializerMock.Setup(x => x.Deserialze <String>(duidBytes)).Returns(duidBytes).Verifiable(); var resolver = new DHCPv6ClientDUIDResolver(); resolver.ApplyValues(new Dictionary <String, String> { { "ClientDuid", duidBytes }, }, serializerMock.Object); var packet = DHCPv6RelayPacket.AsOuterRelay(new IPv6HeaderInformation(IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::2")), true, 1, random.GetIPv6Address(), random.GetIPv6Address(), Array.Empty <DHCPv6PacketOption>(), DHCPv6RelayPacket.AsInnerRelay( true, 0, IPv6Address.FromString("2004::1"), IPv6Address.FromString("fe80::1"), new DHCPv6PacketOption[] { }, DHCPv6Packet.AsInner(random.NextUInt16(), DHCPv6PacketTypes.Solicit, new[] { new DHCPv6PacketIdentifierOption(DHCPv6PacketOptionTypes.ClientIdentifier, shouldMeetCondition == true ? (DUID)duid : new UUIDDUID(random.NextGuid())) }))); Boolean result = resolver.PacketMeetsCondition(packet); Assert.Equal(shouldMeetCondition, result); serializerMock.Verify(); }
public async Task ShouldPacketBeFilterd_False() { DHCPv6Packet packet = DHCPv6Packet.AsInner(1, DHCPv6PacketTypes.Solicit, new List <DHCPv6PacketOption>()); List <Mock <IDHCPv6PacketFilter> > filterMocks = new List <Mock <IDHCPv6PacketFilter> >(); for (int i = 0; i < 10; i++) { Mock <IDHCPv6PacketFilter> filterMock = new Mock <IDHCPv6PacketFilter>(MockBehavior.Strict); filterMock.Setup(x => x.ShouldPacketBeFiltered(packet)).ReturnsAsync(false).Verifiable(); filterMocks.Add(filterMock); } IDHCPv6PacketFilterEngine engine = new SimpleDHCPv6PacketFilterEngine(filterMocks.Select(x => x.Object), Mock.Of <ILogger <SimpleDHCPv6PacketFilterEngine> >()); (bool, string)result = await engine.ShouldPacketBeFilterd(packet); Assert.False(result.Item1); Assert.True(String.IsNullOrEmpty(result.Item2)); foreach (var item in filterMocks) { item.Verify(); } }
public void FromByteArray_IPAddressOption() { Random random = new Random(); UInt32 transactionId = (UInt32)random.Next(0, 256 * 256 * 256); IPv6HeaderInformation header = new IPv6HeaderInformation(IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::2")); UInt16 identifier = 6000; DHCPv6Packet packet = new DHCPv6Packet(header, transactionId, DHCPv6PacketTypes.Solicit, new List <DHCPv6PacketOption> { new DHCPv6PacketIPAddressOption(DHCPv6PacketOptionTypes.ServerUnicast, IPv6Address.FromString("fe80::1")), new DHCPv6PacketIPAddressOption(identifier, IPv6Address.FromString("fe80::2")), } ); try { DHCPv6PacketOptionFactory.AddOptionType(identifier, (data) => DHCPv6PacketIPAddressOption.FromByteArray(data, 0), true); CheckByteRepresentation(packet, header); } finally { DHCPv6PacketOptionFactory.RemoveOptionType(identifier); } }
private DHCPv6Packet GetReleasePacket( Random random, out IPv6Address usedAddress, out DUID clientDuid, out UInt32 iaId, Boolean withIdentity, params DHCPv6PacketOption[] options) { IPv6HeaderInformation headerInformation = new IPv6HeaderInformation(IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::2")); clientDuid = new UUIDDUID(random.NextGuid()); iaId = random.NextBoolean() == false?random.NextUInt32() : 0; usedAddress = random.GetIPv6Address(); var packetOptions = new List <DHCPv6PacketOption>(options) { new DHCPv6PacketIdentifierOption(DHCPv6PacketOptionTypes.ClientIdentifier, clientDuid), }; if (withIdentity == true) { packetOptions.Add(new DHCPv6PacketIdentityAssociationNonTemporaryAddressesOption(iaId, TimeSpan.FromSeconds(random.Next()), TimeSpan.FromSeconds(random.Next()), new DHCPv6PacketSuboption[] { new DHCPv6PacketIdentityAssociationAddressSuboption(usedAddress, TimeSpan.FromSeconds(random.Next()), TimeSpan.FromSeconds(random.Next()), Array.Empty <DHCPv6PacketSuboption>()) })); } DHCPv6Packet packet = DHCPv6Packet.AsOuter(headerInformation, random.NextUInt16(), DHCPv6PacketTypes.RELEASE, packetOptions); return(packet); }
public void HandleRequest_ScopeNotFound(Boolean withPrefixDelegation) { Random random = new Random(); UInt32 prefixId = random.NextUInt32(); Byte prefixLength = (Byte)random.Next(20, 60); IPv6Address prefixNetworkAddress = IPv6Address.FromString("fd01::0"); DHCPv6PacketOption[] options = Array.Empty <DHCPv6PacketOption>(); if (withPrefixDelegation == true) { options = new DHCPv6PacketOption[] { new DHCPv6PacketIdentityAssociationPrefixDelegationOption(prefixId, TimeSpan.FromSeconds(random.Next()), TimeSpan.FromSeconds(random.Next()), new DHCPv6PacketSuboption[] { new DHCPv6PacketIdentityAssociationPrefixDelegationSuboption(TimeSpan.FromSeconds(random.Next()), TimeSpan.FromSeconds(random.Next()), prefixLength, prefixNetworkAddress, Array.Empty <DHCPv6PacketSuboption>()) }) }; } var packet = GetRequestPacket(random, out _, out DUID _, out UInt32 _, true, options); GetMockupResolver(DHCPv6Packet.Empty, out Mock <IScopeResolverManager <DHCPv6Packet, IPv6Address> > scopeResolverMock); DHCPv6RootScope rootScope = GetRootScope(scopeResolverMock); DHCPv6Packet result = rootScope.HandleRequest(packet, GetServerPropertiesResolver()); Assert.Equal(DHCPv6Packet.Empty, result); CheckEventAmount(1, rootScope); CheckHandeledEvent(0, packet, result, rootScope, null, DHCPv6RequestHandledEvent.RequestErrors.ScopeNotFound); }
public async Task Handle_ResponseAvaiable() { IPv6HeaderInformation headerInformation = new IPv6HeaderInformation( IPv6Address.FromString("fe80::2"), IPv6Address.FromString("fe80::1")); DHCPv6Packet packet = DHCPv6Packet.AsOuter(headerInformation, 1, DHCPv6PacketTypes.Solicit, new List <DHCPv6PacketOption>()); DHCPv6Packet reseponsePacket = DHCPv6Packet.AsOuter(IPv6HeaderInformation.AsResponse(headerInformation), 1, DHCPv6PacketTypes.ADVERTISE, new List <DHCPv6PacketOption>()); Mock <IDHCPv6LeaseEngine> leaseEngineMock = new Mock <IDHCPv6LeaseEngine>(MockBehavior.Strict); leaseEngineMock.Setup(x => x.HandlePacket(packet)).ReturnsAsync(reseponsePacket).Verifiable(); Mock <IServiceBus> serviceBusMock = new Mock <IServiceBus>(MockBehavior.Strict); serviceBusMock.Setup(x => x.Publish(It.Is <DHCPv6PacketReadyToSendMessage>(y => y.Packet == reseponsePacket))).Returns(Task.CompletedTask).Verifiable(); ValidDHCPv6PacketArrivedMessageHandler handler = new ValidDHCPv6PacketArrivedMessageHandler( serviceBusMock.Object, leaseEngineMock.Object, Mock.Of <ILogger <ValidDHCPv6PacketArrivedMessageHandler> >()); await handler.Handle(new ValidDHCPv6PacketArrivedMessage(packet), CancellationToken.None); leaseEngineMock.Verify(); serviceBusMock.Verify(); }
public void PacketMeetsCondition_False_OptionNotPresented() { Random random = new Random(); String value = random.GetAlphanumericString(); Byte[] remoteIdentifierAsBytes = ASCIIEncoding.ASCII.GetBytes(value); DHCPv6Packet packet = GetPacket(random, remoteIdentifierAsBytes, false); Mock <ISerializer> serializerMock = new Mock <ISerializer>(MockBehavior.Strict); serializerMock.Setup(x => x.Deserialze <String>(value)).Returns(value).Verifiable(); serializerMock.Setup(x => x.Deserialze <UInt16>("0")).Returns(0).Verifiable(); serializerMock.Setup(x => x.Deserialze <Boolean>("true")).Returns(true).Verifiable(); var resolver = new DHCPv6MilegateResolver(); resolver.ApplyValues(new Dictionary <String, String> { { "Value", value }, { "Index", "0" }, { "IsCaseSenstiveMatch", "true" }, }, serializerMock.Object); Boolean result = resolver.PacketMeetsCondition(packet); Assert.False(result); serializerMock.Verify(); }
private DHCPv6Packet GetPacket(Random random, Byte[] remoteId, Boolean includeRemoteIdOption = true) { IPv6HeaderInformation headerInformation = new IPv6HeaderInformation(IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::2")); var packetOptions = new List <DHCPv6PacketOption> { new DHCPv6PacketTrueOption(DHCPv6PacketOptionTypes.RapitCommit), }; DHCPv6Packet innerPacket = DHCPv6Packet.AsInner(random.NextUInt16(), DHCPv6PacketTypes.Solicit, packetOptions); DHCPv6Packet innerRelayPacket = DHCPv6RelayPacket.AsInnerRelay(true, 1, random.GetIPv6Address(), random.GetIPv6Address(), new DHCPv6PacketOption[] { new DHCPv6PacketByteArrayOption(DHCPv6PacketOptionTypes.InterfaceId, random.NextBytes(10)), includeRemoteIdOption == true ? (DHCPv6PacketOption)(new DHCPv6PacketRemoteIdentifierOption(3561, remoteId)) : new DHCPv6PacketTrueOption(DHCPv6PacketOptionTypes.RapitCommit) }, innerPacket); DHCPv6Packet outerRelayPacket = DHCPv6RelayPacket.AsOuterRelay(headerInformation, true, 1, random.GetIPv6Address(), random.GetIPv6Address(), new DHCPv6PacketOption[] { new DHCPv6PacketByteArrayOption(DHCPv6PacketOptionTypes.InterfaceId, random.NextBytes(10)), new DHCPv6PacketRemoteIdentifierOption(9, random.NextBytes(12)) }, innerRelayPacket); return(outerRelayPacket); }
public async Task ShouldPacketBeFilterd_True(Int32 filterAmount, Int32 index) { DHCPv6Packet packet = DHCPv6Packet.AsInner(1, DHCPv6PacketTypes.Solicit, new List <DHCPv6PacketOption>()); List <Mock <IDHCPv6PacketFilter> > filterMocks = new List <Mock <IDHCPv6PacketFilter> >(); for (int i = 0; i < filterAmount; i++) { Mock <IDHCPv6PacketFilter> filterMock = new Mock <IDHCPv6PacketFilter>(MockBehavior.Strict); filterMock.Setup(x => x.ShouldPacketBeFiltered(packet)).ReturnsAsync(i == index).Verifiable(); filterMocks.Add(filterMock); } IDHCPv6PacketFilterEngine engine = new SimpleDHCPv6PacketFilterEngine(filterMocks.Select(x => x.Object), Mock.Of <ILogger <SimpleDHCPv6PacketFilterEngine> >()); (bool, string)result = await engine.ShouldPacketBeFilterd(packet); Assert.True(result.Item1); Assert.False(String.IsNullOrEmpty(result.Item2)); for (int i = 0; i < filterMocks.Count; i++) { var filterMock = filterMocks[i]; if (i <= index) { filterMock.Verify(); } else { filterMock.Verify(x => x.ShouldPacketBeFiltered(packet), Times.Never); } } }
public void HandleRequest_OnlyPrefix_LeaseFound_LeaseIsPending() { Random random = new Random(); UInt32 prefixId = random.NextUInt32(); Byte prefixLength = (Byte)random.Next(20, 60); IPv6Address prefixNetworkAddress = IPv6Address.FromString("fd01::0"); var packet = GetRequestPacket(random, out IPv6Address leasedAddress, out DUID clientDuid, out UInt32 iaId, false, new DHCPv6PacketIdentityAssociationPrefixDelegationOption(prefixId, TimeSpan.FromSeconds(random.Next()), TimeSpan.FromSeconds(random.Next()), new DHCPv6PacketSuboption[] { new DHCPv6PacketIdentityAssociationPrefixDelegationSuboption(TimeSpan.FromSeconds(random.Next()), TimeSpan.FromSeconds(random.Next()), prefixLength, prefixNetworkAddress, Array.Empty <DHCPv6PacketSuboption>()) })); var resolverInformations = GetMockupResolver(packet, out Mock <IScopeResolverManager <DHCPv6Packet, IPv6Address> > scopeResolverMock); Guid scopeId = random.NextGuid(); Guid leaseId = random.NextGuid(); DHCPv6RootScope rootScope = GetRootScope(scopeResolverMock); rootScope.Load(new List <DomainEvent> { new DHCPv6ScopeEvents.DHCPv6ScopeAddedEvent( new DHCPv6ScopeCreateInstruction { AddressProperties = new DHCPv6ScopeAddressProperties( IPv6Address.FromString("fe80::0"), IPv6Address.FromString("fe80::ff"), new List <IPv6Address> { IPv6Address.FromString("fe80::1") }, preferredLifeTime: TimeSpan.FromDays(0.5), validLifeTime: TimeSpan.FromDays(1), addressAllocationStrategy: DHCPv6ScopeAddressProperties.AddressAllocationStrategies.Next), ResolverInformation = resolverInformations, Name = "Testscope", Id = scopeId, }), new DHCPv6LeaseCreatedEvent { EntityId = leaseId, Address = leasedAddress, ClientIdentifier = clientDuid, IdentityAssocationId = iaId, ScopeId = scopeId, StartedAt = DateTime.UtcNow, ValidUntil = DateTime.UtcNow.AddDays(1), HasPrefixDelegation = true, PrefixLength = prefixLength, IdentityAssocationIdForPrefix = prefixId, DelegatedNetworkAddress = prefixNetworkAddress, }, }); DHCPv6Packet result = rootScope.HandleRequest(packet, GetServerPropertiesResolver()); CheckErrorPacket(packet, leasedAddress, 0, result, DHCPv6PrefixDelegation.FromValues(prefixNetworkAddress, new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(prefixLength)), prefixId), DHCPv6StatusCodes.NoAddrsAvail); CheckEventAmount(1, rootScope); CheckHandeledEvent(0, packet, result, rootScope, scopeId, DHCPv6RequestHandledEvent.RequestErrors.LeasePendingButOnlyPrefixRequested); }
public Task <Boolean> ShouldPacketBeFiltered(DHCPv6Packet packet) { _logger.LogDebug("ShouldPacketBeFiltered"); Boolean result = base.FilterByRateLimit(packet.Header.Source, PacketsPerSecons); return(Task.FromResult(result)); }
private static DHCPv6RelayPacket GeneratePacket(Random random, uint enterpriseNumber) { return(DHCPv6RelayPacket.AsOuterRelay( new IPv6HeaderInformation(IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::2")), true, 0, IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::1"), new DHCPv6PacketOption[] { new DHCPv6PacketRemoteIdentifierOption(enterpriseNumber, random.NextBytes(20)), }, DHCPv6Packet.AsInner(random.NextUInt16(), DHCPv6PacketTypes.Solicit, Array.Empty <DHCPv6PacketOption>()))); }
public DHCPv6ConfirmHandledEvent(DHCPv6Packet request, DHCPv6Packet response, ConfirmErrors error) : base(request, response, error == ConfirmErrors.NoError) { if (error != ConfirmErrors.ScopeNotFound) { throw new ArgumentException("a scope id has to be specified if an error different from 'ScopeNotFound' is used"); } Error = error; }
public DHCPv6DeclineHandledEvent(DHCPv6Packet request, DeclineErros error) : base(request, DHCPv6Packet.Empty, false) { if (error != DeclineErros.ScopeNotFound) { throw new ArgumentException("a scope id has to be specified if an error different from 'ScopeNotFound' is used"); } Error = error; }
public void HandleConfirm_LeaseNotFound(Boolean withAddress, Boolean withPrefix) { Random random = new Random(); var packet = GetRelayedConfirmPacket(random, out IPv6Address leasedAddress, out DUID _, out UInt32 iaId, out DHCPv6PrefixDelegation prefix, withAddress, withPrefix); var resolverInformations = GetMockupResolver(packet, out Mock <IScopeResolverManager <DHCPv6Packet, IPv6Address> > scopeResolverMock); Guid scopeId = random.NextGuid(); Guid leaseId = random.NextGuid(); DateTime leaseCreatedAt = DateTime.UtcNow; DHCPv6RootScope rootScope = GetRootScope(scopeResolverMock); rootScope.Load(new List <DomainEvent> { new DHCPv6ScopeEvents.DHCPv6ScopeAddedEvent( new DHCPv6ScopeCreateInstruction { AddressProperties = new DHCPv6ScopeAddressProperties( IPv6Address.FromString("fe80::0"), IPv6Address.FromString("fe80::ff"), new List <IPv6Address> { IPv6Address.FromString("fe80::1") }, t1: DHCPv6TimeScale.FromDouble(0.5), t2: DHCPv6TimeScale.FromDouble(0.75), preferredLifeTime: TimeSpan.FromDays(0.5), validLifeTime: TimeSpan.FromDays(1), reuseAddressIfPossible: true), ResolverInformation = resolverInformations, Name = "Testscope", Id = scopeId, }), new DHCPv6LeaseCreatedEvent { EntityId = leaseId, Address = leasedAddress, ClientIdentifier = new UUIDDUID(random.NextGuid()), IdentityAssocationId = iaId, ScopeId = scopeId, StartedAt = leaseCreatedAt, ValidUntil = DateTime.UtcNow.AddDays(1), HasPrefixDelegation = withPrefix, PrefixLength = withPrefix == false ? (Byte)0 : prefix.Mask.Identifier, DelegatedNetworkAddress = withPrefix == false ? IPv6Address.Empty : prefix.NetworkAddress, IdentityAssocationIdForPrefix = withPrefix == false ? 0 : prefix.IdentityAssociation, }, }); DHCPv6Packet result = rootScope.HandleConfirm(packet, GetServerPropertiesResolver()); Assert.Equal(DHCPv6Packet.Empty, result); CheckEventAmount(1, rootScope); CheckHandeledEvent(0, packet, result, rootScope, scopeId, DHCPv6ConfirmHandledEvent.ConfirmErrors.LeaseNotFound); }
public DHCPv6Packet GetPacket() { if (_packet == null) { _packet = DHCPv6Packet.FromByteArray(Content, new IPv6HeaderInformation(IPv6Address.FromString(Header.Source), IPv6Address.FromString(Header.Destination))); } return(_packet); }
public async Task HandleInvalidPacket() { Random random = new Random(); String dbName = $"{random.Next()}"; var(client, serviceBus) = GetTestClient(dbName); try { IPv6HeaderInformation headerInformation = new IPv6HeaderInformation( IPv6Address.FromString("fe80::2"), IPv6Address.FromString("fe80::1")); DHCPv6Packet packet = DHCPv6RelayPacket.AsOuterRelay(headerInformation, true, 1, IPv6Address.FromString("fe80::3"), IPv6Address.FromString("fe80::4"), Array.Empty <DHCPv6PacketOption>(), DHCPv6Packet.AsInner(1, DHCPv6PacketTypes.ADVERTISE, new List <DHCPv6PacketOption>())); var message = new DHCPv6PacketArrivedMessage(packet); await serviceBus.Publish(message); await Task.Delay(2000); StorageContext initicalContext = DatabaseTestingUtility.GetTestDatabaseContext(dbName); Int32 tries = 10; while (tries-- > 0) { if (initicalContext.DHCPv6PacketEntries.Count() == 1) { break; } await Task.Delay(1000); } Assert.Equal(1, await initicalContext.DHCPv6PacketEntries.AsQueryable().CountAsync()); var firstEntry = await initicalContext.DHCPv6PacketEntries.AsQueryable().FirstAsync(); Assert.NotEqual(Guid.Empty, firstEntry.Id); Assert.False(firstEntry.InvalidRequest); Assert.Equal(packet.GetSize(), firstEntry.RequestSize); Assert.Equal(DHCPv6PacketTypes.ADVERTISE, firstEntry.RequestType); Assert.Equal(nameof(DHCPv6PacketConsistencyFilter), firstEntry.FilteredBy); Assert.True((DateTime.UtcNow - firstEntry.Timestamp).TotalSeconds < 20); Assert.NotEqual(DateTime.MinValue, firstEntry.TimestampDay); Assert.NotEqual(DateTime.MinValue, firstEntry.TimestampWeek); Assert.NotEqual(DateTime.MinValue, firstEntry.TimestampMonth); } finally { await DatabaseTestingUtility.DeleteDatabase(dbName); } }
public void GetUniqueIdentifier() { Random random = new Random(); var resolver = new DHCPv6MilegateResolver(); Byte[] identifierValue = random.NextBytes(40); DHCPv6Packet packet = GetPacket(random, identifierValue); Assert.Equal(identifierValue, resolver.GetUniqueIdentifier(packet)); }