public void SerializeAndDeserialize() { Random random = new Random(); JsonSerializerSettings settings = new JsonSerializerSettings(); settings.Converters.Add(new IPv6AddressJsonConverter()); settings.Converters.Add(new DHCPv6ScopeAddressPropertiesConverter()); settings.Converters.Add(new DHCPv6PrefixDelgationInfoJsonConverter()); var input = new DHCPv6ScopeAddressProperties( IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::A"), new List <IPv6Address> { IPv6Address.FromString("fe80::4"), IPv6Address.FromString("fe80::5") }, DHCPv6TimeScale.FromDouble(0.5), DHCPv6TimeScale.FromDouble(0.7), TimeSpan.FromMinutes(random.Next(10, 20)), TimeSpan.FromMinutes(random.Next(40, 60)), random.NextBoolean(), DHCPv6ScopeAddressProperties.AddressAllocationStrategies.Next, random.NextBoolean(), random.NextBoolean(), random.NextBoolean(), random.NextBoolean(), DHCPv6PrefixDelgationInfo.FromValues(IPv6Address.FromString("2001:e68:5423:5ffd::0"), new IPv6SubnetMaskIdentifier(64), new IPv6SubnetMaskIdentifier(70))); String serialized = JsonConvert.SerializeObject(input, settings); var actual = JsonConvert.DeserializeObject <DHCPv6ScopeAddressProperties>(serialized, settings); Assert.Equal(input, actual); }
public void DHCPv6ScopeAddressPropertiesUpdatedEvent() { Random random = new Random(); DHCPv6ScopeAddressPropertiesUpdatedEvent expected = new DHCPv6ScopeAddressPropertiesUpdatedEvent { EntityId = random.NextGuid(), Timestamp = DateTime.UtcNow, AddressProperties = new DHCPv6ScopeAddressProperties( IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::A"), new List <IPv6Address> { IPv6Address.FromString("fe80::4"), IPv6Address.FromString("fe80::5") }, DHCPv6TimeScale.FromDouble(0.5), DHCPv6TimeScale.FromDouble(0.85), TimeSpan.FromMinutes(random.Next(10, 20)), TimeSpan.FromMinutes(random.Next(40, 60)), random.NextBoolean(), DHCPv6ScopeAddressProperties.AddressAllocationStrategies.Next, random.NextBoolean(), random.NextBoolean(), random.NextBoolean(), random.NextBoolean()) }; var actual = SerializeAndDeserialze(expected); Assert.Equal(expected.EntityId, actual.EntityId); Assert.Equal(expected.Timestamp, actual.Timestamp); Assert.Equal(expected.AddressProperties, actual.AddressProperties); }
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 void DHCPv6ScopeAddedEvent() { Random random = new Random(); DHCPv6ScopeAddedEvent expected = new DHCPv6ScopeAddedEvent { Timestamp = DateTime.UtcNow, Instructions = new DHCPv6ScopeCreateInstruction { Description = random.GetAlphanumericString(20), Name = random.GetAlphanumericString(4), ParentId = random.NextGuid(), Id = random.NextGuid(), ResolverInformation = new Beer.DaAPI.Core.Scopes.CreateScopeResolverInformation { Typename = random.GetAlphanumericString(20), PropertiesAndValues = new Dictionary <String, String> { { random.GetAlphanumericString(20), random.GetAlphanumericString(50) } } }, AddressProperties = new DHCPv6ScopeAddressProperties( IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::A"), new List <IPv6Address> { IPv6Address.FromString("fe80::4"), IPv6Address.FromString("fe80::5") }, DHCPv6TimeScale.FromDouble(0.5), DHCPv6TimeScale.FromDouble(0.85), TimeSpan.FromMinutes(random.Next(10, 20)), TimeSpan.FromMinutes(random.Next(40, 60)), random.NextBoolean(), DHCPv6ScopeAddressProperties.AddressAllocationStrategies.Next, random.NextBoolean(), random.NextBoolean(), random.NextBoolean(), random.NextBoolean()) } }; var actual = SerializeAndDeserialze(expected); Assert.Equal(expected.Timestamp, actual.Timestamp); Assert.Equal(expected.Instructions.Description, actual.Instructions.Description); Assert.Equal(expected.Instructions.Name, actual.Instructions.Name); Assert.Equal(expected.Instructions.ParentId, actual.Instructions.ParentId); Assert.Equal(expected.Instructions.Id, actual.Instructions.Id); Assert.Equal(expected.Instructions.ResolverInformation.Typename, actual.Instructions.ResolverInformation.Typename); Assert.Equal(expected.Instructions.ResolverInformation.PropertiesAndValues.ElementAt(0), actual.Instructions.ResolverInformation.PropertiesAndValues.ElementAt(0)); Assert.Equal(expected.Instructions.AddressProperties.AcceptDecline, actual.Instructions.AddressProperties.AcceptDecline); Assert.Equal(expected.Instructions.AddressProperties.AddressAllocationStrategy, actual.Instructions.AddressProperties.AddressAllocationStrategy); Assert.Equal(expected.Instructions.AddressProperties.End, actual.Instructions.AddressProperties.End); Assert.Equal(expected.Instructions.AddressProperties.ExcludedAddresses, actual.Instructions.AddressProperties.ExcludedAddresses, new IPv6AddressEquatableComparer()); Assert.Equal(expected.Instructions.AddressProperties.InformsAreAllowd, actual.Instructions.AddressProperties.InformsAreAllowd); Assert.Equal(expected.Instructions.AddressProperties.RapitCommitEnabled, actual.Instructions.AddressProperties.RapitCommitEnabled); Assert.Equal(expected.Instructions.AddressProperties.ReuseAddressIfPossible, actual.Instructions.AddressProperties.ReuseAddressIfPossible); Assert.Equal(expected.Instructions.AddressProperties.Start, actual.Instructions.AddressProperties.Start); Assert.Equal(expected.Instructions.AddressProperties.SupportDirectUnicast, actual.Instructions.AddressProperties.SupportDirectUnicast); Assert.Equal(expected.Instructions.AddressProperties.PreferredLeaseTime, actual.Instructions.AddressProperties.PreferredLeaseTime); Assert.Equal(expected.Instructions.AddressProperties.ValidLeaseTime, actual.Instructions.AddressProperties.ValidLeaseTime); }
protected DHCPv6RootScope GetRootScope(Random random, out DHCPv6Packet packet) { packet = GetSolicitPacket(random, out _, out _); var resolverInformations = GetMockupResolver(packet, out Mock <IScopeResolverManager <DHCPv6Packet, IPv6Address> > scopeResolverMock); Guid scopeId = random.NextGuid(); Mock <ILoggerFactory> factoryMock = new Mock <ILoggerFactory>(MockBehavior.Strict); factoryMock.Setup(x => x.CreateLogger(It.IsAny <String>())).Returns(Mock.Of <ILogger <DHCPv6RootScope> >()); var rootScope = new DHCPv6RootScope(Guid.NewGuid(), scopeResolverMock.Object, factoryMock.Object); 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.25), t2: DHCPv6TimeScale.FromDouble(0.75), preferredLifeTime: TimeSpan.FromDays(0.5), validLifeTime: TimeSpan.FromDays(1), rapitCommitEnabled: true, addressAllocationStrategy: DHCPv6ScopeAddressProperties.AddressAllocationStrategies.Next, prefixDelgationInfo: DHCPv6PrefixDelgationInfo.FromValues( IPv6Address.FromString("2a64:40::0"), new IPv6SubnetMaskIdentifier(64), new IPv6SubnetMaskIdentifier(64)) ), ResolverInformation = resolverInformations, Name = "Testscope", Id = scopeId, }) }); return(rootScope); }
protected static DHCPv6ScopeAddressProperties GetScopeAddressProperties(IScopeChangeCommand request) => request.AddressProperties.DynamicRenewTime == null ? new DHCPv6ScopeAddressProperties ( IPv6Address.FromString(request.AddressProperties.Start), IPv6Address.FromString(request.AddressProperties.End), request.AddressProperties.ExcludedAddresses.Select(x => IPv6Address.FromString(x)), request.AddressProperties.T1 == null ? null : DHCPv6TimeScale.FromDouble(request.AddressProperties.T1.Value), request.AddressProperties.T2 == null ? null : DHCPv6TimeScale.FromDouble(request.AddressProperties.T2.Value), preferredLifeTime: request.AddressProperties.PreferredLifeTime, validLifeTime: request.AddressProperties.ValidLifeTime, reuseAddressIfPossible: request.AddressProperties.ReuseAddressIfPossible, addressAllocationStrategy: (Beer.DaAPI.Core.Scopes.ScopeAddressProperties <DHCPv6ScopeAddressProperties, IPv6Address> .AddressAllocationStrategies?)request.AddressProperties.AddressAllocationStrategy, supportDirectUnicast: request.AddressProperties.SupportDirectUnicast, acceptDecline: request.AddressProperties.AcceptDecline, informsAreAllowd: request.AddressProperties.InformsAreAllowd, rapitCommitEnabled: request.AddressProperties.RapitCommitEnabled, prefixDelgationInfo: request.AddressProperties.PrefixDelgationInfo == null ? null : DHCPv6PrefixDelgationInfo.FromValues( IPv6Address.FromString(request.AddressProperties.PrefixDelgationInfo.Prefix), new IPv6SubnetMaskIdentifier(request.AddressProperties.PrefixDelgationInfo.PrefixLength), new IPv6SubnetMaskIdentifier(request.AddressProperties.PrefixDelgationInfo.AssingedPrefixLength)) ) : new DHCPv6ScopeAddressProperties ( IPv6Address.FromString(request.AddressProperties.Start), IPv6Address.FromString(request.AddressProperties.End), request.AddressProperties.ExcludedAddresses.Select(x => IPv6Address.FromString(x)), GetDynamicRenewTime(request.AddressProperties.DynamicRenewTime), reuseAddressIfPossible: request.AddressProperties.ReuseAddressIfPossible, addressAllocationStrategy: (Beer.DaAPI.Core.Scopes.ScopeAddressProperties <DHCPv6ScopeAddressProperties, IPv6Address> .AddressAllocationStrategies?)request.AddressProperties.AddressAllocationStrategy, supportDirectUnicast: request.AddressProperties.SupportDirectUnicast, acceptDecline: request.AddressProperties.AcceptDecline, informsAreAllowd: request.AddressProperties.InformsAreAllowd, rapitCommitEnabled: request.AddressProperties.RapitCommitEnabled, prefixDelgationInfo: request.AddressProperties.PrefixDelgationInfo == null ? null : DHCPv6PrefixDelgationInfo.FromValues( IPv6Address.FromString(request.AddressProperties.PrefixDelgationInfo.Prefix), new IPv6SubnetMaskIdentifier(request.AddressProperties.PrefixDelgationInfo.PrefixLength), new IPv6SubnetMaskIdentifier(request.AddressProperties.PrefixDelgationInfo.AssingedPrefixLength)));
//[Fact] public async Task SendAndReceive() { Random random = new Random(); String dbName = $"mydb-{random.Next()}"; String prefix = random.GetAlphanumericString(); try { UInt32 enterpriseNumber = (UInt32)random.Next(); StorageContext initicalContext = DatabaseTestingUtility.GetTestDatabaseContext(dbName); await initicalContext.Database.MigrateAsync(); await initicalContext.SaveInitialServerConfiguration(new DHCPv6ServerProperties { ServerDuid = new UUIDDUID(new Guid()) }); initicalContext.Dispose(); IPv6Address expectedAdress = IPv6Address.FromString("fe80::0"); var services = new ServiceCollection(); services.AddScoped <ServiceFactory>(p => p.GetService); services.AddSingleton <DHCPv6RootScope>(sp => { var storageEngine = sp.GetRequiredService <IDHCPv6StorageEngine>(); var scope = storageEngine.GetRootScope().GetAwaiter().GetResult(); return(scope); }); services.AddTransient <IDHCPv6ServerPropertiesResolver, DatabaseDHCPv6ServerPropertiesResolver>(); services.AddSingleton <ISerializer, JSONBasedSerializer>(); services.AddSingleton <IScopeResolverManager <DHCPv6Packet, IPv6Address>, DHCPv6ScopeResolverManager>(); services.AddSingleton <IServiceBus, MediaRBasedServiceBus>(); services.AddSingleton <IDHCPv6PacketFilterEngine, SimpleDHCPv6PacketFilterEngine>(); services.AddSingleton <IDHCPv6InterfaceEngine, DHCPv6InterfaceEngine>(); services.AddSingleton <IDHCPv6LeaseEngine, DHCPv6LeaseEngine>(); services.AddSingleton <IDHCPv6StorageEngine, DHCPv6StorageEngine>(); services.AddSingleton <IDHCPv6ReadStore, StorageContext>(); services.AddSingleton(new EventStoreBasedStoreConnenctionOptions(new EventStoreClient(EventStoreClientSettings.Create("esdb://127.0.0.1:2113?tls=false")), prefix)); services.AddSingleton <IDHCPv6EventStore, EventStoreBasedStore>(); services.AddSingleton(DatabaseTestingUtility.GetTestDbContextOptions(dbName)); services.AddTransient <INotificationHandler <DHCPv6PacketArrivedMessage> >(sp => new DHCPv6PacketArrivedMessageHandler( sp.GetRequiredService <IServiceBus>(), sp.GetRequiredService <IDHCPv6PacketFilterEngine>(), sp.GetService <ILogger <DHCPv6PacketArrivedMessageHandler> >())); services.AddTransient <INotificationHandler <ValidDHCPv6PacketArrivedMessage> >(sp => new ValidDHCPv6PacketArrivedMessageHandler( sp.GetRequiredService <IServiceBus>(), sp.GetRequiredService <IDHCPv6LeaseEngine>(), sp.GetService <ILogger <ValidDHCPv6PacketArrivedMessageHandler> >())); services.AddTransient <INotificationHandler <DHCPv6PacketReadyToSendMessage> >(sp => new DHCPv6PacketReadyToSendMessageHandler( sp.GetRequiredService <IDHCPv6InterfaceEngine>(), sp.GetService <ILogger <DHCPv6PacketReadyToSendMessageHandler> >())); services.AddTransient <DHCPv6RateLimitBasedFilter>(); services.AddTransient <DHCPv6PacketConsistencyFilter>(); services.AddTransient <DHCPv6PacketServerIdentifierFilter>((sp) => new DHCPv6PacketServerIdentifierFilter( new UUIDDUID(new Guid()), sp.GetService <ILogger <DHCPv6PacketServerIdentifierFilter> >())); services.AddLogging(); var provider = services.BuildServiceProvider(); DHCPv6RootScope initialRootScope = provider.GetService <DHCPv6RootScope>(); initialRootScope.AddScope(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), rapitCommitEnabled: true, informsAreAllowd: true, supportDirectUnicast: true, reuseAddressIfPossible: false, acceptDecline: true, t1: DHCPv6TimeScale.FromDouble(0.6), t2: DHCPv6TimeScale.FromDouble(0.8), addressAllocationStrategy: DHCPv6ScopeAddressProperties.AddressAllocationStrategies.Next), ScopeProperties = DHCPv6ScopeProperties.Empty, ResolverInformation = new CreateScopeResolverInformation { Typename = nameof(DHCPv6RemoteIdentifierEnterpriseNumberResolver), PropertiesAndValues = new Dictionary <String, String> { { nameof(DHCPv6RemoteIdentifierEnterpriseNumberResolver.EnterpriseNumber), enterpriseNumber.ToString() }, { nameof(DHCPv6RemoteIdentifierEnterpriseNumberResolver.RelayAgentIndex), 0.ToString() }, } }, Name = "Testscope", Id = Guid.NewGuid(), }); IDHCPv6PacketFilterEngine packetFilterEngine = provider.GetService <IDHCPv6PacketFilterEngine>(); packetFilterEngine.AddFilter(provider.GetService <DHCPv6RateLimitBasedFilter>()); packetFilterEngine.AddFilter(provider.GetService <DHCPv6PacketConsistencyFilter>()); packetFilterEngine.AddFilter(provider.GetService <DHCPv6PacketServerIdentifierFilter>()); IDHCPv6InterfaceEngine interfaceEngine = provider.GetService <IDHCPv6InterfaceEngine>(); var possibleListener = interfaceEngine.GetPossibleListeners(); var listener = possibleListener.First(); interfaceEngine.OpenListener(listener); IPAddress address = new IPAddress(listener.Address.GetBytes()); IPEndPoint ownEndPoint = new IPEndPoint(address, 546); IPEndPoint serverEndPoint = new IPEndPoint(address, 547); UdpClient client = new UdpClient(ownEndPoint); DHCPv6Packet packet = DHCPv6Packet.AsInner(14, DHCPv6PacketTypes.Solicit, new List <DHCPv6PacketOption> { new DHCPv6PacketTrueOption(DHCPv6PacketOptionTypes.RapitCommit), new DHCPv6PacketIdentifierOption(DHCPv6PacketOptionTypes.ClientIdentifier, new UUIDDUID(Guid.NewGuid())), new DHCPv6PacketIdentityAssociationNonTemporaryAddressesOption(15, TimeSpan.Zero, TimeSpan.Zero, new List <DHCPv6PacketSuboption>()), }); DHCPv6RelayPacket outerPacket = DHCPv6RelayPacket.AsOuterRelay(new IPv6HeaderInformation(listener.Address, listener.Address), true, 0, IPv6Address.FromString("fe80::2"), IPv6Address.FromString("fe80::2"), new DHCPv6PacketOption[] { new DHCPv6PacketRemoteIdentifierOption(enterpriseNumber, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }), }, packet); byte[] packetStream = outerPacket.GetAsStream(); await client.SendAsync(packetStream, packetStream.Length, serverEndPoint); await Task.Delay(2000); var content = await client.ReceiveAsync(); Byte[] receivedBytes = content.Buffer; DHCPv6Packet response = DHCPv6Packet.FromByteArray(receivedBytes, new IPv6HeaderInformation(listener.Address, listener.Address)); var iaOption = response.GetInnerPacket().GetNonTemporaryIdentiyAssocation(15); Assert.NotNull(iaOption); Assert.Single(iaOption.Suboptions); Assert.IsAssignableFrom <DHCPv6PacketIdentityAssociationAddressSuboption>(iaOption.Suboptions.First()); Assert.Equal(expectedAdress, ((DHCPv6PacketIdentityAssociationAddressSuboption)iaOption.Suboptions.First()).Address); } finally { await DatabaseTestingUtility.DeleteDatabase(dbName); await EventStoreClientDisposer.CleanUp(prefix, null); } }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var info = serializer.Deserialize <EeasySerialibleVersionOfDHCPv6ScopeAddressProperties>(reader); DHCPv6ScopeAddressProperties result = new DHCPv6ScopeAddressProperties(info.Start, info.End, info.ExcludedAddresses, t1: info.T1.HasValue == true ? DHCPv6TimeScale.FromDouble(info.T1.Value) : null, t2: info.T2.HasValue == true ? DHCPv6TimeScale.FromDouble(info.T2.Value) : null, preferredLifeTime: info.PreferredLeaseTime, validLifeTime: info.ValidLeaseTime, reuseAddressIfPossible: info.ReuseAddressIfPossible, addressAllocationStrategy: info.AddressAllocationStrategy, supportDirectUnicast: info.SupportDirectUnicast, acceptDecline: info.AcceptDecline, informsAreAllowd: info.InformsAreAllowd, rapitCommitEnabled: info.RapitCommitEnabled, info.PrefixDelgationInfo); return(result); }
public void DHCPv6Scope_AddressPropertiesInherientce() { Random random = new Random(); for (int i = 0; i < 100; i++) { IPv6Address grantParentStart = random.GetIPv6Address(); IPv6Address grantParentEnd = random.GetIPv6AddressGreaterThan(grantParentStart); List <IPv6Address> grantParentExcludedAddresses = random.GetIPv6AddressesBetween(grantParentStart, grantParentEnd); DHCPv6TimeScale grantParentT1 = DHCPv6TimeScale.FromDouble(0.2); DHCPv6TimeScale grantParentT2 = DHCPv6TimeScale.FromDouble(0.6); TimeSpan grantParentPreferredLifeTime = TimeSpan.FromMinutes(random.Next(10, 30)); TimeSpan grantParentValuidLifeTime = TimeSpan.FromMinutes(random.Next(40, 60)); Boolean grantParentReuseAddressIfPossible = random.NextBoolean(); var grantParentAllocationStrategy = DHCPv6ScopeAddressProperties.AddressAllocationStrategies.Next; Boolean grantParentSupportDirectUnicast = random.NextBoolean(); Boolean grantParentAcceptDecline = random.NextBoolean(); Boolean grantParentInformsAreAllowd = random.NextBoolean(); DHCPv6ScopeAddressProperties grantParentProperties = new DHCPv6ScopeAddressProperties( grantParentStart, grantParentEnd, grantParentExcludedAddresses, grantParentT1, grantParentT2, grantParentPreferredLifeTime, grantParentValuidLifeTime, grantParentReuseAddressIfPossible, grantParentAllocationStrategy, grantParentInformsAreAllowd, grantParentAcceptDecline, grantParentInformsAreAllowd, null, DHCPv6PrefixDelgationInfo.FromValues(IPv6Address.FromString("2001:e68:5423:5ffd::0"), new IPv6SubnetMaskIdentifier(64), new IPv6SubnetMaskIdentifier(70))); IPv6Address parentStart = random.GetIPv6Address(); IPv6Address parentEnd = random.GetIPv6AddressGreaterThan(parentStart); List <IPv6Address> parentExcludedAddresses = random.GetIPv6AddressesBetween(parentStart, parentEnd); TimeSpan? parentPreferedLifeTime = null; TimeSpan? parentValuidLifetime = null; DHCPv6TimeScale parentT1 = DHCPv6TimeScale.FromDouble(0.3); DHCPv6TimeScale parentT2 = null; Boolean?parentReuseAddressIfPossible = null; DHCPv6ScopeAddressProperties.AddressAllocationStrategies?parentAllocationStrategy = null; Boolean?parentSupportDirectUnicast = null; Boolean?parentAcceptDecline = null; Boolean?parentInformsAreAllowd = null; if (random.NextBoolean() == true) { parentPreferedLifeTime = TimeSpan.FromMinutes(random.Next()); } if (random.NextBoolean() == true) { parentValuidLifetime = TimeSpan.FromMinutes(random.Next()); } if (random.NextBoolean() == true) { parentReuseAddressIfPossible = random.NextBoolean(); } if (random.NextBoolean() == true) { parentAllocationStrategy = DHCPv6ScopeAddressProperties.AddressAllocationStrategies.Random; } if (random.NextBoolean() == true) { parentSupportDirectUnicast = random.NextBoolean(); } if (random.NextBoolean() == true) { parentAcceptDecline = random.NextBoolean(); } if (random.NextBoolean() == true) { parentInformsAreAllowd = random.NextBoolean(); } DHCPv6ScopeAddressProperties parentProperties = new DHCPv6ScopeAddressProperties( parentStart, parentEnd, parentExcludedAddresses, parentT1, parentT2, parentPreferedLifeTime, parentValuidLifetime, parentReuseAddressIfPossible, parentAllocationStrategy, parentInformsAreAllowd, parentAcceptDecline, parentInformsAreAllowd, null, DHCPv6PrefixDelgationInfo.FromValues(IPv6Address.FromString("2001:e68:5423:5ffe::0"), new IPv6SubnetMaskIdentifier(64), new IPv6SubnetMaskIdentifier(70)) ); IPv6Address childStart = random.GetIPv6Address(); IPv6Address childEnd = random.GetIPv6AddressGreaterThan(childStart); List <IPv6Address> childExcludedAddresses = random.GetIPv6AddressesBetween(childStart, childEnd); DHCPv6TimeScale childT1 = null; DHCPv6TimeScale childT2 = DHCPv6TimeScale.FromDouble(0.9); TimeSpan?childPreferredLifeTime = null; TimeSpan?childValidLifeTime = null; Boolean?childReuseAddressIfPossible = null; DHCPv6ScopeAddressProperties.AddressAllocationStrategies?childAllocationStrategy = null; Boolean?childSupportDirectUnicast = random.NextDouble() > 0.5; Boolean?childAcceptDecline = random.NextDouble() > 0.5; Boolean?childInformsAreAllowd = random.NextDouble() > 0.5; if (random.NextBoolean() == true) { childPreferredLifeTime = TimeSpan.FromMinutes(random.Next()); } if (random.NextBoolean() == true) { childValidLifeTime = TimeSpan.FromMinutes(random.Next()); } if (random.NextBoolean() == true) { childReuseAddressIfPossible = random.NextBoolean(); } if (random.NextBoolean() == true) { childAllocationStrategy = DHCPv6ScopeAddressProperties.AddressAllocationStrategies.Random; } if (random.NextBoolean() == true) { childSupportDirectUnicast = random.NextBoolean(); } if (random.NextBoolean() == true) { childAcceptDecline = random.NextBoolean(); } if (random.NextBoolean() == true) { childInformsAreAllowd = random.NextBoolean(); } var childPrefixDelegationInfo = DHCPv6PrefixDelgationInfo.FromValues(IPv6Address.FromString("2001:e68:5423:5ffe::0"), new IPv6SubnetMaskIdentifier(64), new IPv6SubnetMaskIdentifier(70)); DHCPv6ScopeAddressProperties childProperties = new DHCPv6ScopeAddressProperties( childStart, childEnd, childExcludedAddresses, childT1, childT2, childPreferredLifeTime, childValidLifeTime, childReuseAddressIfPossible, childAllocationStrategy, childSupportDirectUnicast, childAcceptDecline, childInformsAreAllowd, null, childPrefixDelegationInfo ); Guid grantParentId = Guid.NewGuid(); Guid parentId = Guid.NewGuid(); Guid childId = Guid.NewGuid(); List <DomainEvent> events = new List <DomainEvent> { new DHCPv6ScopeAddedEvent(new DHCPv6ScopeCreateInstruction { Id = grantParentId, AddressProperties = grantParentProperties, }), new DHCPv6ScopeAddedEvent(new DHCPv6ScopeCreateInstruction { Id = parentId, ParentId = grantParentId, AddressProperties = parentProperties, }), new DHCPv6ScopeAddedEvent(new DHCPv6ScopeCreateInstruction { Id = childId, ParentId = parentId, AddressProperties = childProperties, }), }; DHCPv6RootScope rootScope = GetRootScope(); rootScope.Load(events); DHCPv6Scope scope = rootScope.GetScopeById(childId); DHCPv6ScopeAddressProperties actualProperties = scope.GetAddressProperties(); DHCPv6ScopeAddressProperties expectedProperties = new DHCPv6ScopeAddressProperties( childStart, childEnd, grantParentExcludedAddresses.Union(parentExcludedAddresses).Union(childExcludedAddresses).Where(x => x.IsBetween(childStart, childEnd)), childT1 ?? (parentT1 ?? grantParentT1), childT2 ?? (parentT2 ?? grantParentT2), childPreferredLifeTime.HasValue == true ? childPreferredLifeTime.Value : (parentPreferedLifeTime.HasValue == true ? parentPreferedLifeTime.Value : grantParentPreferredLifeTime), childValidLifeTime.HasValue == true ? childValidLifeTime.Value : (parentValuidLifetime.HasValue == true ? parentValuidLifetime.Value : grantParentValuidLifeTime), childReuseAddressIfPossible.HasValue == true ? childReuseAddressIfPossible.Value : (parentReuseAddressIfPossible.HasValue == true ? parentReuseAddressIfPossible.Value : grantParentReuseAddressIfPossible), childAllocationStrategy.HasValue == true ? childAllocationStrategy.Value : (parentAllocationStrategy.HasValue == true ? parentAllocationStrategy.Value : grantParentAllocationStrategy), childSupportDirectUnicast.HasValue == true ? childSupportDirectUnicast.Value : (parentSupportDirectUnicast.HasValue == true ? parentSupportDirectUnicast.Value : grantParentSupportDirectUnicast), childAcceptDecline.HasValue == true ? childAcceptDecline.Value : (parentAcceptDecline.HasValue == true ? parentAcceptDecline.Value : grantParentAcceptDecline), childInformsAreAllowd.HasValue == true ? childInformsAreAllowd.Value : (parentInformsAreAllowd.HasValue == true ? parentInformsAreAllowd.Value : grantParentInformsAreAllowd), null, childPrefixDelegationInfo ); Assert.Equal(expectedProperties, actualProperties); } }
public void TestNotifcationTriggerForSolicitMessages(Boolean reuse, Boolean prefixRequest, Boolean hadPrefix, Boolean shouldHaveOldBinding, Boolean shouldHaveNewBinding) { Random random = new Random(); IPv6HeaderInformation headerInformation = new IPv6HeaderInformation(IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::2")); DUID clientDuid = new UUIDDUID(random.NextGuid()); UInt32 iaId = random.NextBoolean() == false?random.NextUInt32() : 0; IPv6Address leasedAddress = IPv6Address.FromString("fe80::5"); UInt32 prefixIaId = random.NextBoolean() == false?random.NextUInt32() : 0; Byte exisitngPrefixLength = 62; DHCPv6PrefixDelegation existingDelegation = DHCPv6PrefixDelegation.FromValues( IPv6Address.FromString("2a4c::0"), new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(exisitngPrefixLength)), prefixIaId); var packetOptions = new List <DHCPv6PacketOption> { new DHCPv6PacketIdentifierOption(DHCPv6PacketOptionTypes.ClientIdentifier, clientDuid), new DHCPv6PacketIdentityAssociationNonTemporaryAddressesOption(iaId, TimeSpan.Zero, TimeSpan.Zero, Array.Empty <DHCPv6PacketSuboption>()), }; if (prefixRequest == true) { packetOptions.Add( new DHCPv6PacketIdentityAssociationPrefixDelegationOption(prefixIaId, TimeSpan.Zero, TimeSpan.Zero, Array.Empty <DHCPv6PacketSuboption>())); } DHCPv6Packet innerPacket = DHCPv6Packet.AsInner(random.NextUInt16(), DHCPv6PacketTypes.REBIND, packetOptions); DHCPv6Packet packet = DHCPv6RelayPacket.AsOuterRelay(headerInformation, true, 1, random.GetIPv6Address(), random.GetIPv6Address(), new DHCPv6PacketOption[] { new DHCPv6PacketByteArrayOption(DHCPv6PacketOptionTypes.InterfaceId, random.NextBytes(10)) }, innerPacket); CreateScopeResolverInformation resolverInformations = new CreateScopeResolverInformation { Typename = "something" }; Mock <IScopeResolver <DHCPv6Packet, IPv6Address> > resolverMock = new Mock <IScopeResolver <DHCPv6Packet, IPv6Address> >(MockBehavior.Strict); resolverMock.Setup(x => x.PacketMeetsCondition(packet)).Returns(true); resolverMock.SetupGet(x => x.HasUniqueIdentifier).Returns(false); var scopeResolverMock = new Mock <IScopeResolverManager <DHCPv6Packet, IPv6Address> >(MockBehavior.Strict); scopeResolverMock.Setup(x => x.InitializeResolver(resolverInformations)).Returns(resolverMock.Object); Mock <ILoggerFactory> factoryMock = new Mock <ILoggerFactory>(MockBehavior.Strict); factoryMock.Setup(x => x.CreateLogger(It.IsAny <String>())).Returns(Mock.Of <ILogger <DHCPv6RootScope> >()); var rootScope = new DHCPv6RootScope(Guid.NewGuid(), scopeResolverMock.Object, factoryMock.Object); Guid scopeId = random.NextGuid(); Guid leaseId = random.NextGuid(); var expetecNewLeaseAddress = IPv6Address.FromString("fe80::0"); var events = 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: reuse, prefixDelgationInfo: DHCPv6PrefixDelgationInfo.FromValues(existingDelegation.NetworkAddress, new IPv6SubnetMaskIdentifier(40), new IPv6SubnetMaskIdentifier(64)), addressAllocationStrategy: DHCPv6ScopeAddressProperties.AddressAllocationStrategies.Next), ResolverInformation = resolverInformations, Name = "Testscope", Id = scopeId, }), new DHCPv6LeaseCreatedEvent { EntityId = leaseId, Address = leasedAddress, ClientIdentifier = clientDuid, IdentityAssocationId = iaId, ScopeId = scopeId, HasPrefixDelegation = hadPrefix, UniqueIdentiifer = Array.Empty <Byte>(), PrefixLength = hadPrefix == true ? exisitngPrefixLength : (Byte)0, IdentityAssocationIdForPrefix = hadPrefix == true ? prefixIaId : (UInt32)0, DelegatedNetworkAddress = hadPrefix == true ? existingDelegation.NetworkAddress : IPv6Address.Empty, StartedAt = DateTime.UtcNow.AddDays(-1), ValidUntil = DateTime.UtcNow.AddDays(1), }, new DHCPv6LeaseActivatedEvent { EntityId = leaseId, ScopeId = scopeId, } }; rootScope.Load(events); var serverPropertiesResolverMock = new Mock <IDHCPv6ServerPropertiesResolver>(MockBehavior.Strict); serverPropertiesResolverMock.Setup(x => x.GetServerDuid()).Returns(new UUIDDUID(Guid.NewGuid())); var _ = rootScope.HandleRebind(packet, serverPropertiesResolverMock.Object); if (shouldHaveNewBinding == false && shouldHaveOldBinding == false) { CheckEmptyTrigger(rootScope); } else { var trigger = CheckTrigger <PrefixEdgeRouterBindingUpdatedTrigger>(rootScope); Assert.Equal(scopeId, trigger.ScopeId); if (shouldHaveNewBinding == true) { Assert.NotNull(trigger.NewBinding); Assert.Equal(64, trigger.NewBinding.Mask.Identifier); if (reuse == true) { Assert.Equal(leasedAddress, trigger.NewBinding.Host); } else { Assert.Equal(expetecNewLeaseAddress, trigger.NewBinding.Host); } } else { Assert.Null(trigger.NewBinding); } if (shouldHaveOldBinding == true) { Assert.NotNull(trigger.OldBinding); Assert.Equal(exisitngPrefixLength, trigger.OldBinding.Mask.Identifier); Assert.Equal(existingDelegation.NetworkAddress, trigger.OldBinding.Prefix); Assert.Equal(leasedAddress, trigger.OldBinding.Host); } else { Assert.Null(trigger.OldBinding); } } }
public void HandleRequest_LeaseFound_LeaseIsPending(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()), Array.Empty <DHCPv6PacketSuboption>()) }; } var packet = GetRequestPacket(random, out IPv6Address _, out DUID clientDuid, out UInt32 iaId, true, options); var resolverInformations = GetMockupResolver(packet, out Mock <IScopeResolverManager <DHCPv6Packet, IPv6Address> > scopeResolverMock); Guid scopeId = random.NextGuid(); Guid leaseId = random.NextGuid(); IPv6Address leasedAddress = IPv6Address.FromString("fe80::2"); 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), 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 = withPrefixDelegation, PrefixLength = prefixLength, IdentityAssocationIdForPrefix = prefixId, DelegatedNetworkAddress = prefixNetworkAddress, } }); DHCPv6Packet result = rootScope.HandleRequest(packet, GetServerPropertiesResolver()); CheckPacket(packet, leasedAddress, iaId, result, DHCPv6PacketTypes.REPLY, withPrefixDelegation == false ? DHCPv6PrefixDelegation.None : DHCPv6PrefixDelegation.FromValues(prefixNetworkAddress, new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(prefixLength)), prefixId)); CheckEventAmount(2, rootScope); CheckLeaseActivatedEvent(0, scopeId, leaseId, rootScope); CheckHandeledEvent(1, packet, result, rootScope, scopeId, DHCPv6RequestHandledEvent.RequestErrors.NoError); }
public async Task HandleRebind_LeaseFound_ReuseIsNotAllowed_TwoPackets(Boolean withPrefixDelegation) { Random random = new Random(); UInt32 prefixId = random.NextBoolean() == false?random.NextUInt32() : 0; Byte prefixLength = (Byte)random.Next(34, 64); 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()), Array.Empty <DHCPv6PacketSuboption>()) }; } var packet = GetRelayedRebindPacket(random, out IPv6Address _, out DUID clientDuid, out UInt32 iaId, true, options); var resolverInformations = GetMockupResolver(packet, out Mock <IScopeResolverManager <DHCPv6Packet, IPv6Address> > scopeResolverMock); Guid scopeId = random.NextGuid(); Guid leaseId = random.NextGuid(); IPv6Address leasedAddress = IPv6Address.FromString("fe80::01"); IPv6Address expectedAddress = IPv6Address.FromString("fe80::03"); DateTime leaseCreatedAt = DateTime.UtcNow.AddHours(-2); DHCPv6RootScope rootScope = GetRootScope(scopeResolverMock); rootScope.Load(new List <DomainEvent> { new DHCPv6ScopeEvents.DHCPv6ScopeAddedEvent( new DHCPv6ScopeCreateInstruction { AddressProperties = new DHCPv6ScopeAddressProperties( IPv6Address.FromString("fe80::01"), IPv6Address.FromString("fe80::ff"), new List <IPv6Address> { IPv6Address.FromString("fe80::02") }, t1: DHCPv6TimeScale.FromDouble(0.5), t2: DHCPv6TimeScale.FromDouble(0.75), preferredLifeTime: TimeSpan.FromDays(0.5), validLifeTime: TimeSpan.FromDays(1), reuseAddressIfPossible: false, prefixDelgationInfo: DHCPv6PrefixDelgationInfo.FromValues(IPv6Address.FromString("2140:1::0"), new IPv6SubnetMaskIdentifier(32), new IPv6SubnetMaskIdentifier(prefixLength)), addressAllocationStrategy: DHCPv6ScopeAddressProperties.AddressAllocationStrategies.Next), ResolverInformation = resolverInformations, Name = "Testscope", Id = scopeId, }), new DHCPv6LeaseCreatedEvent { EntityId = leaseId, Address = leasedAddress, ClientIdentifier = clientDuid, IdentityAssocationId = iaId, ScopeId = scopeId, StartedAt = leaseCreatedAt, ValidUntil = DateTime.UtcNow.AddDays(1), HasPrefixDelegation = withPrefixDelegation, PrefixLength = prefixLength, IdentityAssocationIdForPrefix = prefixId, DelegatedNetworkAddress = prefixNetworkAddress, }, new DHCPv6LeaseActivatedEvent { EntityId = leaseId, ScopeId = scopeId, } }); Guid serverId = random.NextGuid(); var revokedLease = rootScope.GetScopeById(scopeId).Leases.GetLeaseById(leaseId); DHCPv6Packet result = rootScope.HandleRebind(packet, GetServerPropertiesResolver(serverId)); await Task.Delay(3000); DHCPv6Packet secondResult = rootScope.HandleRebind(packet, GetServerPropertiesResolver(serverId)); Assert.Equal(result, secondResult); DHCPv6Lease lease = CheckLease(0, 1, expectedAddress, scopeId, rootScope, DateTime.UtcNow, clientDuid, iaId, false, withPrefixDelegation); CheckPacket(packet, expectedAddress, iaId, result, DHCPv6PacketTypes.REPLY, withPrefixDelegation == false ? DHCPv6PrefixDelegation.None : DHCPv6PrefixDelegation.FromValues(lease.PrefixDelegation.NetworkAddress, new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(prefixLength)), prefixId)); Assert.False(revokedLease.IsActive()); Assert.Equal(LeaseStates.Revoked, revokedLease.State); CheckEventAmount(5, rootScope); CheckRevokedEvent(0, scopeId, leaseId, rootScope); CheckLeaseCreatedEvent(1, clientDuid, iaId, scopeId, rootScope, expectedAddress, lease); CheckLeaseActivatedEvent(2, scopeId, lease.Id, rootScope); CheckHandeledEvent(3, packet, result, rootScope, scopeId, DHCPv6RebindHandledEvent.RebindErrors.NoError); CheckHandeledEvent(4, packet, result, rootScope, scopeId, DHCPv6RebindHandledEvent.RebindErrors.NoError); }
public async Task SaveAndHydrateRootScope_AddScopes() { Random random = new Random(); await ExecuteWithStreamErase(random, async (eventStore) => { Mock <IDHCPv6ServerPropertiesResolver> propertyResolverMock; DHCPv6StorageEngine engine; PrepareEngine(random, eventStore, out propertyResolverMock, out engine); DHCPv6RootScope initialRootScope = await engine.GetRootScope(); Guid rootScopeId = random.NextGuid(); Guid firstChildScopeId = random.NextGuid(); Guid secondChildScopeId = random.NextGuid(); initialRootScope.AddScope(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), rapitCommitEnabled: true, informsAreAllowd: true, supportDirectUnicast: true, reuseAddressIfPossible: false, acceptDecline: true, t1: DHCPv6TimeScale.FromDouble(0.6), t2: DHCPv6TimeScale.FromDouble(0.8), addressAllocationStrategy: DHCPv6ScopeAddressProperties.AddressAllocationStrategies.Next), ScopeProperties = DHCPv6ScopeProperties.Empty, ResolverInformation = new CreateScopeResolverInformation { Typename = nameof(DHCPv6RemoteIdentifierEnterpriseNumberResolver), PropertiesAndValues = new Dictionary <String, String> { { nameof(DHCPv6RemoteIdentifierEnterpriseNumberResolver.EnterpriseNumber), random.NextUInt16().ToString() }, { nameof(DHCPv6RemoteIdentifierEnterpriseNumberResolver.RelayAgentIndex), 0.ToString() }, } }, Name = "Testscope", Id = rootScopeId, }); initialRootScope.AddScope(new DHCPv6ScopeCreateInstruction { AddressProperties = new DHCPv6ScopeAddressProperties( IPv6Address.FromString("fe80::0"), IPv6Address.FromString("fe80::ff"), new List <IPv6Address> { IPv6Address.FromString("fe80::1") }), ScopeProperties = DHCPv6ScopeProperties.Empty, ResolverInformation = new CreateScopeResolverInformation { Typename = nameof(DHCPv6RemoteIdentifierEnterpriseNumberResolver), PropertiesAndValues = new Dictionary <String, String> { { nameof(DHCPv6RemoteIdentifierEnterpriseNumberResolver.EnterpriseNumber), random.NextUInt16().ToString() }, { nameof(DHCPv6RemoteIdentifierEnterpriseNumberResolver.RelayAgentIndex), 0.ToString() }, } }, Name = "First Child Testscope", Id = firstChildScopeId, ParentId = rootScopeId, }); initialRootScope.AddScope(new DHCPv6ScopeCreateInstruction { AddressProperties = new DHCPv6ScopeAddressProperties( IPv6Address.FromString("fe80::0"), IPv6Address.FromString("fe80::ff"), new List <IPv6Address> { IPv6Address.FromString("fe80::1") }), ScopeProperties = DHCPv6ScopeProperties.Empty, ResolverInformation = new CreateScopeResolverInformation { Typename = nameof(DHCPv6RemoteIdentifierEnterpriseNumberResolver), PropertiesAndValues = new Dictionary <String, String> { { nameof(DHCPv6RemoteIdentifierEnterpriseNumberResolver.EnterpriseNumber), random.NextUInt16().ToString() }, { nameof(DHCPv6RemoteIdentifierEnterpriseNumberResolver.RelayAgentIndex), 0.ToString() }, } }, Name = "Second child Testscope", Id = secondChildScopeId, ParentId = firstChildScopeId, }); await engine.Save(initialRootScope); var rehydratedRoot = await engine.GetRootScope(); Assert.Single(rehydratedRoot.GetRootScopes()); var rehydratedRootScope = rehydratedRoot.GetRootScopes().First(); Assert.Equal(rootScopeId, rehydratedRootScope.Id); Assert.Single(rehydratedRootScope.GetChildScopes()); var rehydratedFirstLevelChild = rehydratedRootScope.GetChildScopes().First(); Assert.Equal(firstChildScopeId, rehydratedFirstLevelChild.Id); Assert.Single(rehydratedFirstLevelChild.GetChildScopes()); var rehydratedSecondLevelChild = rehydratedFirstLevelChild.GetChildScopes().First(); Assert.Equal(secondChildScopeId, rehydratedSecondLevelChild.Id); }); }
public async Task SaveAndHydrateRootScope_AddAndRemoveLeases() { Random random = new Random(); await ExecuteWithStreamErase(random, async (eventStore) => { Mock <IDHCPv6ServerPropertiesResolver> propertyResolverMock; DHCPv6StorageEngine engine; PrepareEngine(random, eventStore, out propertyResolverMock, out engine); DHCPv6RootScope initialRootScope = await engine.GetRootScope(); Guid rootScopeId = random.NextGuid(); initialRootScope.AddScope(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), rapitCommitEnabled: true, informsAreAllowd: true, supportDirectUnicast: false, reuseAddressIfPossible: false, acceptDecline: true, t1: DHCPv6TimeScale.FromDouble(0.6), t2: DHCPv6TimeScale.FromDouble(0.8), addressAllocationStrategy: DHCPv6ScopeAddressProperties.AddressAllocationStrategies.Next), ScopeProperties = DHCPv6ScopeProperties.Empty, ResolverInformation = new CreateScopeResolverInformation { Typename = nameof(DHCPv6RemoteIdentifierEnterpriseNumberResolver), PropertiesAndValues = new Dictionary <String, String> { { nameof(DHCPv6RemoteIdentifierEnterpriseNumberResolver.EnterpriseNumber), random.NextUInt16().ToString() }, { nameof(DHCPv6RemoteIdentifierEnterpriseNumberResolver.RelayAgentIndex), 0.ToString() }, } }, Name = "Testscope", Id = rootScopeId, }); var clientDuid = new UUIDDUID(random.NextGuid()); DHCPv6Packet solicitPacket = DHCPv6Packet.AsInner(14, DHCPv6PacketTypes.Solicit, new List <DHCPv6PacketOption> { new DHCPv6PacketTrueOption(DHCPv6PacketOptionTypes.RapitCommit), new DHCPv6PacketIdentifierOption(DHCPv6PacketOptionTypes.ClientIdentifier, clientDuid), new DHCPv6PacketIdentityAssociationNonTemporaryAddressesOption(15, TimeSpan.Zero, TimeSpan.Zero, new List <DHCPv6PacketSuboption>()), }); DHCPv6RelayPacket outerSolicitPacket = DHCPv6RelayPacket.AsOuterRelay(new IPv6HeaderInformation(random.GetIPv6Address(), random.GetIPv6Address()), true, 0, IPv6Address.FromString("fe80::2"), IPv6Address.FromString("fe80::2"), new DHCPv6PacketOption[] { new DHCPv6PacketRemoteIdentifierOption(random.NextUInt16(), new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }), }, solicitPacket); initialRootScope.HandleSolicit(outerSolicitPacket, propertyResolverMock.Object); var firstLease = initialRootScope.GetRootScopes().First().Leases.GetAllLeases().First(); await engine.Save(initialRootScope); initialRootScope = await engine.GetRootScope(); DHCPv6Packet releasePacket = DHCPv6Packet.AsInner(14, DHCPv6PacketTypes.RELEASE, new List <DHCPv6PacketOption> { new DHCPv6PacketIdentifierOption(DHCPv6PacketOptionTypes.ClientIdentifier, clientDuid), new DHCPv6PacketIdentityAssociationNonTemporaryAddressesOption(15, TimeSpan.FromSeconds(random.Next()), TimeSpan.FromSeconds(random.Next()), new DHCPv6PacketSuboption[] { new DHCPv6PacketIdentityAssociationAddressSuboption(firstLease.Address, TimeSpan.FromSeconds(random.Next()), TimeSpan.FromSeconds(random.Next()), Array.Empty <DHCPv6PacketSuboption>()) }) }); DHCPv6RelayPacket outerreleasePacket = DHCPv6RelayPacket.AsOuterRelay(new IPv6HeaderInformation(random.GetIPv6Address(), random.GetIPv6Address()), true, 0, IPv6Address.FromString("fe80::2"), IPv6Address.FromString("fe80::2"), new DHCPv6PacketOption[] { new DHCPv6PacketRemoteIdentifierOption(random.NextUInt16(), new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }), }, releasePacket); firstLease = initialRootScope.GetRootScopes().First().Leases.GetAllLeases().First(); initialRootScope.HandleRelease(outerreleasePacket, propertyResolverMock.Object); Assert.Equal(LeaseStates.Released, firstLease.State); Assert.Empty(initialRootScope.GetRootScopes().First().Leases.GetAllLeases()); await engine.Save(initialRootScope); await Task.Delay(1000); initialRootScope.DropUnusedLeasesOlderThan(DateTime.Now); Assert.Empty(initialRootScope.GetRootScopes().First().Leases.GetAllLeases()); await engine.Save(initialRootScope); var rehydratedRootScope = await engine.GetRootScope(); Assert.Empty(rehydratedRootScope.GetRootScopes().First().Leases.GetAllLeases()); }); }
public async Task HandleRenew_OnlyPrefix_LeaseFound_NotReuseAddress(Boolean isUnicast, Boolean sendSecondPacket) { Random random = new Random(); UInt32 prefixId = random.NextUInt32(); Byte prefixLength = 36; IPv6Address prefixNetworkAddress = IPv6Address.FromString("2140:1::0"); IPv6Address leasedAddress = IPv6Address.FromString("fe80::4"); DHCPv6PacketOption[] 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 = isUnicast == true? GetRenewPacket(random, out IPv6Address _, out DUID clientDuid, out UInt32 _, false, options) : GetRelayedRenewPacket(random, out IPv6Address _, out clientDuid, out UInt32 _, false, options); var resolverInformations = GetMockupResolver(packet, out Mock <IScopeResolverManager <DHCPv6Packet, IPv6Address> > scopeResolverMock); Guid scopeId = random.NextGuid(); Guid leaseId = random.NextGuid(); UInt32 iaId = random.NextUInt32(); DateTime leaseCreatedAt = DateTime.UtcNow.AddHours(-random.Next(4, 10)); DHCPv6RootScope rootScope = GetRootScope(scopeResolverMock); rootScope.Load(new List <DomainEvent> { new DHCPv6ScopeEvents.DHCPv6ScopeAddedEvent( new DHCPv6ScopeCreateInstruction { AddressProperties = new DHCPv6ScopeAddressProperties( IPv6Address.FromString("fe80::01"), IPv6Address.FromString("fe80::ff"), new List <IPv6Address> { IPv6Address.FromString("fe80::02") }, preferredLifeTime: TimeSpan.FromDays(0.5), validLifeTime: TimeSpan.FromDays(1), t1: DHCPv6TimeScale.FromDouble(0.5), t2: DHCPv6TimeScale.FromDouble(0.75), reuseAddressIfPossible: false, prefixDelgationInfo: DHCPv6PrefixDelgationInfo.FromValues(IPv6Address.FromString("2140:1::0"), new IPv6SubnetMaskIdentifier(32), new IPv6SubnetMaskIdentifier(prefixLength)), addressAllocationStrategy: DHCPv6ScopeAddressProperties.AddressAllocationStrategies.Random), ResolverInformation = resolverInformations, Name = "Testscope", Id = scopeId, }), new DHCPv6LeaseCreatedEvent { EntityId = leaseId, Address = leasedAddress, ClientIdentifier = clientDuid, IdentityAssocationId = iaId, ScopeId = scopeId, StartedAt = leaseCreatedAt, ValidUntil = DateTime.UtcNow.AddDays(1), HasPrefixDelegation = true, PrefixLength = prefixLength, IdentityAssocationIdForPrefix = prefixId, DelegatedNetworkAddress = prefixNetworkAddress, }, new DHCPv6LeaseActivatedEvent { EntityId = leaseId, ScopeId = scopeId, }, new DHCPv6LeaseRenewedEvent { EntityId = leaseId, ScopeId = scopeId, ResetPrefix = true, End = DateTime.UtcNow.AddDays(1) } }); DHCPv6Packet result = rootScope.HandleRenew(packet, GetServerPropertiesResolver()); DHCPv6Lease lease = CheckLease(0, 1, leasedAddress, scopeId, rootScope, leaseCreatedAt, clientDuid, iaId, false, true); CheckPacket(packet, leasedAddress, 0, result, DHCPv6PacketTypes.REPLY, DHCPv6PrefixDelegation.FromValues(lease.PrefixDelegation.NetworkAddress, new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(prefixLength)), prefixId)); DHCPv6Packet secondResponse = null; if (sendSecondPacket == true) { await Task.Delay(1000); secondResponse = rootScope.HandleRenew(packet, GetServerPropertiesResolver()); CheckPacket(packet, leasedAddress, 0, secondResponse, DHCPv6PacketTypes.REPLY, DHCPv6PrefixDelegation.FromValues(lease.PrefixDelegation.NetworkAddress, new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(prefixLength)), prefixId)); } CheckPacket(packet, leasedAddress, 0, result, DHCPv6PacketTypes.REPLY, DHCPv6PrefixDelegation.FromValues(lease.PrefixDelegation.NetworkAddress, new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(prefixLength)), prefixId)); CheckEventAmount(sendSecondPacket == false ? 2 : 3, rootScope); CheckDHCPv6LeasePrefixAddedEvent(0, prefixId, scopeId, rootScope, lease); CheckHandeledEvent(1, packet, result, rootScope, scopeId, DHCPv6RenewHandledEvent.RenewErrors.NoError); if (sendSecondPacket == true) { CheckHandeledEvent(2, packet, secondResponse, rootScope, scopeId, DHCPv6RenewHandledEvent.RenewErrors.NoError); } var trigger = CheckTrigger <PrefixEdgeRouterBindingUpdatedTrigger>(rootScope); Assert.Null(trigger.OldBinding); Assert.NotNull(trigger.NewBinding); Assert.Equal(leasedAddress, trigger.NewBinding.Host); Assert.Equal(lease.PrefixDelegation.NetworkAddress, trigger.NewBinding.Prefix); Assert.Equal(new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(prefixLength)), trigger.NewBinding.Mask); }