public async Task <Boolean> Handle(UpdateDHCPv6ScopeParentCommand request, CancellationToken cancellationToken) { _logger.LogDebug("Handle started"); var scope = _rootScope.GetScopeById(request.ScopeId); if (scope == DHCPv6Scope.NotFound) { return(false); } if (request.ParentScopeId.HasValue == true) { if (_rootScope.GetScopeById(request.ParentScopeId.Value) == DHCPv6Scope.NotFound) { return(false); } } Boolean moveResult = _rootScope.UpdateParent(request.ScopeId, request.ParentScopeId); if (moveResult == true) { Boolean result = await _store.Save(_rootScope); return(result); } return(false); }
protected static void CheckLeaseRenewdEvent( Guid scopeId, DHCPv6RootScope rootScope, DHCPv6Lease lease, Boolean expectReset, Boolean expectedPrefixReset) { IEnumerable <DomainEvent> changes = rootScope.GetChanges(); Assert.NotNull(changes); Assert.Equal(2, changes.Count()); Assert.IsAssignableFrom <DHCPv6LeaseRenewedEvent>(changes.First()); DHCPv6LeaseRenewedEvent createdEvent = (DHCPv6LeaseRenewedEvent)changes.First(); Assert.NotNull(createdEvent); Assert.Equal(scopeId, createdEvent.ScopeId); Assert.NotEqual(Guid.Empty, lease.Id); Assert.Equal(lease.Id, createdEvent.EntityId); Assert.Equal(expectReset, createdEvent.Reset); Assert.Equal(expectedPrefixReset, createdEvent.ResetPrefix); Assert.Equal(lease.End, createdEvent.End); var addressProperties = rootScope.GetScopeById(scopeId).AddressRelatedProperties; Assert.True(Math.Abs(((addressProperties.T1.Value * addressProperties.ValidLeaseTime.Value) - createdEvent.RenewSpan).TotalSeconds) < 20); Assert.True(Math.Abs(((addressProperties.T2.Value * addressProperties.ValidLeaseTime.Value) - createdEvent.ReboundSpan).TotalSeconds) < 20); }
public async Task <Boolean> Handle(UpdateDHCPv6ScopeCommand request, CancellationToken cancellationToken) { _logger.LogDebug("Handle started"); var scope = _rootScope.GetScopeById(request.ScopeId); if (scope == DHCPv6Scope.NotFound) { return(false); } Guid?parentId = scope.HasParentScope() == false ? new Guid?() : scope.ParentScope.Id; var properties = GetScopeProperties(request); var addressProperties = GetScopeAddressProperties(request); if (request.Name != scope.Name) { _rootScope.UpdateScopeName(request.ScopeId, ScopeName.FromString(request.Name)); } if (request.Description != scope.Description) { _rootScope.UpdateScopeDescription(request.ScopeId, ScopeDescription.FromString(request.Description)); } if (request.ParentId != parentId) { _rootScope.UpdateParent(request.ScopeId, request.ParentId); } _rootScope.UpdateScopeResolver(request.ScopeId, GetResolverInformation(request)); if (addressProperties != scope.AddressRelatedProperties) { _rootScope.UpdateAddressProperties(request.ScopeId, addressProperties); } if (properties != scope.Properties) { _rootScope.UpdateScopeProperties(request.ScopeId, properties); } Boolean result = await _store.Save(_rootScope); if (result == true) { var triggers = _rootScope.GetTriggers(); if (triggers.Any() == true) { await _serviceBus.Publish(new NewTriggerHappendMessage(triggers)); _rootScope.ClearTriggers(); } } return(result); }
public override Task <Boolean> IsValid(NotifcationTrigger trigger) { if (trigger is PrefixEdgeRouterBindingUpdatedTrigger == false) { _logger.LogError("condition {name} has invalid trigger. expected trigger type is {expectedType} actual is {type}", nameof(DHCPv6ScopeIdNotificationCondition), typeof(PrefixEdgeRouterBindingUpdatedTrigger), trigger.GetType()); return(Task.FromResult(false)); } var castedTrigger = (PrefixEdgeRouterBindingUpdatedTrigger)trigger; if (ScopeIds.Contains(castedTrigger.ScopeId) == true) { _logger.LogDebug("triggers scope id {scopeId} found in condtition. Condition is true", castedTrigger.ScopeId); return(Task.FromResult(true)); } else { _logger.LogDebug("triggers scope id {scopeId} not found scope list. Checking if children are included", castedTrigger.ScopeId); if (IncludesChildren == false) { _logger.LogDebug("children shouldn't be included. Conditition evalutated to false"); return(Task.FromResult(false)); } else { foreach (var scopeId in ScopeIds) { _logger.LogDebug("checking scopes recursivly for machting id"); _logger.LogDebug("check if {triggerId} is a child of {scopeId}", castedTrigger.ScopeId, castedTrigger.ScopeId); var scope = _rootScope.GetScopeById(scopeId); if (scope == null) { _logger.LogError("scope with id {scopeId} not found", scopeId); continue; } foreach (var item in scope.GetChildScopes()) { Boolean found = SearchChildScope(item, castedTrigger.ScopeId); if (found == true) { _logger.LogDebug("a machting child scope found. Condition evaluted to true"); return(Task.FromResult(true)); } } } _logger.LogDebug("no child found. Condition evaluted to false"); return(Task.FromResult(false)); } } }
public async Task <IActionResult> GetHandledDHCPv6PacketByScopeId([FromRoute(Name = "id")] Guid scopeId, [Range(1, 1000)][FromQuery] Int32 amount = 100) { if (_rootScope.GetScopeById(scopeId) == DHCPv6Scope.NotFound) { return(NotFound("scope not found")); } var entries = await _storage.GetHandledDHCPv6PacketByScopeId(scopeId, amount); return(base.Ok(entries)); }
protected static void CheckLeaseCreatedEvent( Int32 index, DUID clientDuid, UInt32 iaId, Guid scopeId, DHCPv6RootScope rootScope, IPv6Address expectedAdress, DHCPv6Lease lease, Byte[] uniqueIdentifier = null, Guid?ancestorId = null, Boolean checkLeaseTimes = true ) { IEnumerable <DomainEvent> changes = rootScope.GetChanges(); Assert.NotNull(changes); Assert.IsAssignableFrom <DHCPv6LeaseCreatedEvent>(changes.ElementAt(index)); DHCPv6LeaseCreatedEvent createdEvent = (DHCPv6LeaseCreatedEvent)changes.ElementAt(index); Assert.NotNull(createdEvent); Assert.Equal(scopeId, createdEvent.ScopeId); Assert.Equal(expectedAdress, createdEvent.Address); Assert.Equal(clientDuid, createdEvent.ClientIdentifier); Assert.Equal(iaId, createdEvent.IdentityAssocationId); Assert.Equal(lease.Id, createdEvent.EntityId); Assert.Equal(ancestorId, createdEvent.AncestorId); if (uniqueIdentifier == null) { Assert.Null(createdEvent.UniqueIdentiifer); } else { Assert.Equal(uniqueIdentifier, createdEvent.UniqueIdentiifer); } Assert.Equal(lease.Start, createdEvent.StartedAt); Assert.Equal(lease.End, createdEvent.ValidUntil); var addressProperties = rootScope.GetScopeById(scopeId).AddressRelatedProperties; if (checkLeaseTimes == true) { Assert.Equal(addressProperties.T1.Value * addressProperties.ValidLeaseTime.Value, createdEvent.RenewalTime); Assert.Equal(addressProperties.T2.Value * addressProperties.ValidLeaseTime.Value, createdEvent.PreferredLifetime); } if (lease.PrefixDelegation == DHCPv6PrefixDelegation.None) { Assert.Equal(IPv6Address.Empty, createdEvent.DelegatedNetworkAddress); Assert.Equal(0, createdEvent.PrefixLength); Assert.False(createdEvent.HasPrefixDelegation); } }
protected static DHCPv6Lease CheckLease( Int32 index, Int32 expectedAmount, IPv6Address expectedAdress, Guid scopeId, DHCPv6RootScope rootScope, DateTime expectedCreationData, DUID clientDuid, UInt32 iaId, Boolean shouldBePending, Boolean shouldHavePrefix, Byte[] uniqueIdentifier = null, Boolean checkExpire = true) { DHCPv6Scope scope = rootScope.GetScopeById(scopeId); var leases = scope.Leases.GetAllLeases(); Assert.Equal(expectedAmount, leases.Count()); DHCPv6Lease lease = leases.ElementAt(index); Assert.NotNull(lease); Assert.Equal(expectedAdress, lease.Address); if (checkExpire == true) { Int32 expiresInMinutes = (Int32)(lease.End - DateTime.UtcNow).TotalMinutes; Assert.True(expiresInMinutes >= 60 * 24 - 4 && expiresInMinutes <= 60 * 24); } Assert.True((expectedCreationData - lease.Start).TotalMinutes < 2); if (shouldBePending == true) { Assert.True(lease.IsPending()); } else { Assert.True(lease.IsActive()); } Assert.Equal(clientDuid, lease.ClientDUID); Assert.Equal(iaId, lease.IdentityAssocicationId); if (uniqueIdentifier == null) { Assert.Empty(lease.UniqueIdentifier); } else { Assert.NotNull(lease.UniqueIdentifier); Assert.Equal(uniqueIdentifier, lease.UniqueIdentifier); } if (shouldHavePrefix == false) { Assert.Equal(DHCPv6PrefixDelegation.None, lease.PrefixDelegation); } return(lease); }
public async Task <Boolean> Handle(DeleteDHCPv6ScopeCommand request, CancellationToken cancellationToken) { if (rootScope.GetScopeById(request.ScopeId) == DHCPv6Scope.NotFound) { logger.LogInformation("unable to delete the scope {scopeId}. Scope not found"); return(false); } rootScope.DeleteScope(request.ScopeId, request.IncludeChildren); Boolean result = await storageEngine.Save(rootScope); if (result == false) { logger.LogError("unable to delete the scope {scopeId}. Saving changes failed", request.ScopeId); } return(result); }
protected static DHCPv6Lease CheckLeaseForPrefix( Int32 index, Int32 expectedAmount, IPv6Address minAddress, IPv6Address maxAddress, Guid scopeId, DHCPv6RootScope rootScope, DUID clientDuid, UInt32 prefixIaId, Byte prefixLength, Boolean shouldBePending, Byte[] uniqueIdentifier = null) { DHCPv6Scope scope = rootScope.GetScopeById(scopeId); var leases = scope.Leases.GetAllLeases(); Assert.Equal(expectedAmount, leases.Count()); DHCPv6Lease lease = leases.ElementAt(index); Assert.Equal(clientDuid, lease.ClientDUID); Assert.NotNull(lease); Assert.NotEqual(DHCPv6PrefixDelegation.None, lease.PrefixDelegation); Assert.Equal(prefixIaId, lease.PrefixDelegation.IdentityAssociation); Assert.Equal(prefixLength, lease.PrefixDelegation.Mask.Identifier); Assert.True((new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(prefixLength))).IsIPv6AdressANetworkAddress(lease.PrefixDelegation.NetworkAddress)); Assert.True(lease.PrefixDelegation.NetworkAddress.IsBetween(minAddress, maxAddress)); if (shouldBePending == true) { Assert.True(lease.IsPending()); } else { Assert.True(lease.IsActive()); } if (uniqueIdentifier == null) { Assert.Empty(lease.UniqueIdentifier); } else { Assert.NotNull(lease.UniqueIdentifier); Assert.Equal(uniqueIdentifier, lease.UniqueIdentifier); } return(lease); }
public void GetChildScopes() { Random random = new Random(); GenerateScopeTree(random, out Dictionary <Guid, List <Guid> > directChildRelations, out Dictionary <Guid, List <Guid> > allChildRelations, out List <DomainEvent> events); DHCPv6RootScope rootScope = GetRootScope(); rootScope.Load(events); foreach (var item in directChildRelations) { DHCPv6Scope scope = rootScope.GetScopeById(item.Key); IEnumerable <DHCPv6Scope> childScopes = scope.GetChildScopes(); List <Guid> childScopesId = childScopes.Select(x => x.Id).ToList(); Assert.Equal(item.Value.OrderBy(x => x), childScopesId.OrderBy(x => x)); } }
public void GetChildIds() { Random random = new Random(); GenerateScopeTree(random, out Dictionary <Guid, List <Guid> > directChildRelations, out Dictionary <Guid, List <Guid> > allChildRelations, out List <DomainEvent> events); DHCPv6RootScope rootScope = GetRootScope(); rootScope.Load(events); foreach (var item in directChildRelations) { DHCPv6Scope scope = rootScope.GetScopeById(item.Key); IEnumerable <Guid> actualDirectIds = scope.GetChildIds(true); Assert.Equal(item.Value.OrderBy(x => x), actualDirectIds.OrderBy(x => x)); IEnumerable <Guid> allChildIds = scope.GetChildIds(false); Assert.Equal(allChildRelations[item.Key].OrderBy(x => x), allChildIds.OrderBy(x => x)); } }
public IActionResult GetLeasesByScope([FromRoute(Name = "id")] Guid scopeId, [FromQuery(Name = "includeChildren")] Boolean includeChildren = false) { _logger.LogDebug("GetLeasesByScope"); var scope = _rootScope.GetScopeById(scopeId); if (scope == DHCPv6Scope.NotFound) { return(NotFound($"no scope with id {scopeId} found")); } List <DHCPv6LeaseOverview> result; if (includeChildren == false) { result = scope.Leases.GetAllLeases().Select(x => GetLeaseOverview(x, scope)).ToList(); } else { result = new List <DHCPv6LeaseOverview>(); GetAllLesesRecursivly(result, scope); } return(base.Ok(result.OrderBy(x => x.State).ThenBy(x => x.Address).ToList())); }
public void DHCPv6Scope_ScopePropertiesInherientce() { Random random = new Random(); Byte onylGrandParentOptionIdentifier = 47; Byte onlyParentOptionIdentifier = 55; Byte onlyChildOptionIdentifier = 90; Byte overridenByParentOptionIdentifier = 100; Byte overridenByChildOptionIdentifier = 110; Byte deletedByChildOptionIdentifier = 140; Dictionary <Byte, String> inputs = new Dictionary <byte, String> { { onylGrandParentOptionIdentifier, random.GetAlphanumericString() }, { onlyParentOptionIdentifier, random.GetAlphanumericString() }, { onlyChildOptionIdentifier, random.GetAlphanumericString() }, { overridenByParentOptionIdentifier, random.GetAlphanumericString() }, { overridenByChildOptionIdentifier, random.GetAlphanumericString() }, { deletedByChildOptionIdentifier, random.GetAlphanumericString() }, }; DHCPv6ScopeProperties grantParentProperties = new DHCPv6ScopeProperties( new DHCPv6TextScopeProperty(onylGrandParentOptionIdentifier, inputs[onylGrandParentOptionIdentifier]), new DHCPv6TextScopeProperty(overridenByParentOptionIdentifier, random.GetAlphanumericString()), new DHCPv6TextScopeProperty(overridenByChildOptionIdentifier, random.GetAlphanumericString()), new DHCPv6TextScopeProperty(deletedByChildOptionIdentifier, random.GetAlphanumericString()) ); DHCPv6ScopeProperties parentProperties = new DHCPv6ScopeProperties( new DHCPv6TextScopeProperty(onlyParentOptionIdentifier, inputs[onlyParentOptionIdentifier]), new DHCPv6TextScopeProperty(overridenByParentOptionIdentifier, inputs[overridenByParentOptionIdentifier]), new DHCPv6TextScopeProperty(deletedByChildOptionIdentifier, random.GetAlphanumericString()) ); DHCPv6ScopeProperties childProperties = new DHCPv6ScopeProperties( new DHCPv6TextScopeProperty(onlyChildOptionIdentifier, inputs[onlyChildOptionIdentifier]), new DHCPv6TextScopeProperty(overridenByChildOptionIdentifier, inputs[overridenByChildOptionIdentifier]) ); childProperties.RemoveFromInheritance(deletedByChildOptionIdentifier); DHCPv6ScopeProperties expectedProperties = new DHCPv6ScopeProperties( new DHCPv6TextScopeProperty(onylGrandParentOptionIdentifier, inputs[onylGrandParentOptionIdentifier]), new DHCPv6TextScopeProperty(onlyParentOptionIdentifier, inputs[onlyParentOptionIdentifier]), new DHCPv6TextScopeProperty(onlyChildOptionIdentifier, inputs[onlyChildOptionIdentifier]), new DHCPv6TextScopeProperty(overridenByParentOptionIdentifier, inputs[overridenByParentOptionIdentifier]), new DHCPv6TextScopeProperty(overridenByChildOptionIdentifier, inputs[overridenByChildOptionIdentifier]) ); Guid grantParentId = Guid.NewGuid(); Guid parentId = Guid.NewGuid(); Guid childId = Guid.NewGuid(); List <DomainEvent> events = new List <DomainEvent> { new DHCPv6ScopeAddedEvent(new DHCPv6ScopeCreateInstruction { Id = grantParentId, ScopeProperties = grantParentProperties, }), new DHCPv6ScopeAddedEvent(new DHCPv6ScopeCreateInstruction { Id = parentId, ParentId = grantParentId, ScopeProperties = parentProperties, }), new DHCPv6ScopeAddedEvent(new DHCPv6ScopeCreateInstruction { Id = childId, ParentId = parentId, ScopeProperties = childProperties, }), }; DHCPv6RootScope rootScope = GetRootScope(); rootScope.Load(events); var scope = rootScope.GetScopeById(childId); var actualProperties = scope.GetScopeProperties(); Assert.Equal(expectedProperties, actualProperties); }
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 HandleRelease_NoLeaseFound(Boolean withPrefixDelegation, Boolean isUnicast) { Random random = new Random(); UInt32 prefixId = random.NextUInt32(); 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()), new DHCPv6PacketSuboption[] { new DHCPv6PacketIdentityAssociationPrefixDelegationSuboption(TimeSpan.FromSeconds(random.Next()), TimeSpan.FromSeconds(random.Next()), prefixLength, prefixNetworkAddress, Array.Empty <DHCPv6PacketSuboption>()) }) }; } var packet = isUnicast == true? GetReleasePacket(random, out IPv6Address leasedAddress, out _, out UInt32 iaId, true, options) : GetRelayedReleasePacket(random, out leasedAddress, out _, out iaId, true, options); 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::01"), IPv6Address.FromString("fe80::ff"), new List <IPv6Address> { IPv6Address.FromString("fe80::02") }, 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 = new UUIDDUID(random.NextGuid()), 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, } }); DHCPv6Packet result = rootScope.HandleRelease(packet, GetServerPropertiesResolver()); CheckErrorPacket(packet, leasedAddress, iaId, result, withPrefixDelegation == false ? DHCPv6PrefixDelegation.None : DHCPv6PrefixDelegation.FromValues(prefixNetworkAddress, new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(prefixLength)), prefixId), DHCPv6StatusCodes.NoBinding); DHCPv6Lease lease = rootScope.GetScopeById(scopeId).Leases.GetLeaseById(leaseId); Assert.Equal(LeaseStates.Active, lease.State); CheckEventAmount(1, rootScope); CheckHandeledEvent(0, packet, result, rootScope, scopeId, DHCPv6ReleaseHandledEvent.ReleaseError.NoLeaseFound); }
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 Handle_WithParentId(Boolean storeResult) { Random random = new(); Guid scopeId = random.NextGuid(); Guid parentId = random.NextGuid(); String name = random.GetAlphanumericString(); String description = random.GetAlphanumericString(); String resolverName = random.GetAlphanumericString(); Mock <IScopeResolver <DHCPv6Packet, IPv6Address> > resolverMock = new Mock <IScopeResolver <DHCPv6Packet, IPv6Address> >(MockBehavior.Strict); Mock <IScopeResolverManager <DHCPv6Packet, IPv6Address> > scopeResolverMock = new Mock <IScopeResolverManager <DHCPv6Packet, IPv6Address> >(); scopeResolverMock.Setup(x => x.InitializeResolver(It.Is <CreateScopeResolverInformation>(y => y.Typename == resolverName ))).Returns(resolverMock.Object).Verifiable(); Mock <ILoggerFactory> factoryMock = new Mock <ILoggerFactory>(MockBehavior.Strict); factoryMock.Setup(x => x.CreateLogger(It.IsAny <String>())).Returns(Mock.Of <ILogger <DHCPv6RootScope> >()); DHCPv6RootScope rootScope = new DHCPv6RootScope(random.NextGuid(), scopeResolverMock.Object, factoryMock.Object); rootScope.Load(new DomainEvent[] { new DHCPv6ScopeAddedEvent { Instructions = new DHCPv6ScopeCreateInstruction { Name = random.GetAlphanumericString(), Description = random.GetAlphanumericString(), Id = parentId, ParentId = null, ResolverInformation = new CreateScopeResolverInformation { Typename = resolverName, }, ScopeProperties = new DHCPv6ScopeProperties(), AddressProperties = DHCPv6ScopeAddressProperties.Empty, } }, new DHCPv6ScopeAddedEvent { Instructions = new DHCPv6ScopeCreateInstruction { Name = random.GetAlphanumericString(), Description = random.GetAlphanumericString(), Id = scopeId, ParentId = null, ResolverInformation = new CreateScopeResolverInformation { Typename = resolverName, }, ScopeProperties = new DHCPv6ScopeProperties(), AddressProperties = DHCPv6ScopeAddressProperties.Empty, } }, }); Mock <IDHCPv6StorageEngine> storageMock = new Mock <IDHCPv6StorageEngine>(MockBehavior.Strict); storageMock.Setup(x => x.Save(rootScope)).ReturnsAsync(storeResult).Verifiable(); var command = new UpdateDHCPv6ScopeParentCommand(scopeId, parentId); var handler = new UpdateDHCPv6ScopeParentCommandHandler(storageMock.Object, rootScope, Mock.Of <ILogger <UpdateDHCPv6ScopeParentCommandHandler> >()); Boolean result = await handler.Handle(command, CancellationToken.None); Assert.Equal(storeResult, result); var parentScope = rootScope.GetScopeById(parentId); var childIds = parentScope.GetChildIds(true); Assert.Single(childIds); Assert.Equal(scopeId, childIds.First()); scopeResolverMock.Verify(); storageMock.Verify(); }
public async Task Handle_WithChildren(Boolean requestedToDeleteChildrenAsWell) { Random random = new Random(); Guid grantParentId = random.NextGuid(); Guid parentId = random.NextGuid(); Guid childId = random.NextGuid(); String resolverName = random.GetAlphanumericString(); Mock <IScopeResolverManager <DHCPv6Packet, IPv6Address> > scopeResolverMock = new Mock <IScopeResolverManager <DHCPv6Packet, IPv6Address> >(); scopeResolverMock.Setup(x => x.InitializeResolver(It.Is <CreateScopeResolverInformation>(y => y.Typename == resolverName ))).Returns(Mock.Of <IScopeResolver <DHCPv6Packet, IPv6Address> >()).Verifiable(); Mock <ILoggerFactory> factoryMock = new Mock <ILoggerFactory>(MockBehavior.Strict); factoryMock.Setup(x => x.CreateLogger(It.IsAny <String>())).Returns(Mock.Of <ILogger <DHCPv6RootScope> >()); DHCPv6RootScope rootScope = new DHCPv6RootScope(random.NextGuid(), scopeResolverMock.Object, factoryMock.Object); rootScope.Load(new[] { new DHCPv6ScopeAddedEvent { Instructions = new DHCPv6ScopeCreateInstruction { Id = grantParentId, ResolverInformation = new CreateScopeResolverInformation { Typename = resolverName, }, AddressProperties = new DHCPv6ScopeAddressProperties(IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::2")), } }, new DHCPv6ScopeAddedEvent { Instructions = new DHCPv6ScopeCreateInstruction { Id = parentId, ParentId = grantParentId, ResolverInformation = new CreateScopeResolverInformation { Typename = resolverName, }, AddressProperties = new DHCPv6ScopeAddressProperties(IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::2")), } }, new DHCPv6ScopeAddedEvent { Instructions = new DHCPv6ScopeCreateInstruction { Id = childId, ParentId = parentId, ResolverInformation = new CreateScopeResolverInformation { Typename = resolverName, }, AddressProperties = new DHCPv6ScopeAddressProperties(IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::2")), } }, }); Mock <IDHCPv6StorageEngine> storageMock = new Mock <IDHCPv6StorageEngine>(MockBehavior.Strict); storageMock.Setup(x => x.Save(rootScope)).ReturnsAsync(true).Verifiable(); var command = new DeleteDHCPv6ScopeCommand(parentId, requestedToDeleteChildrenAsWell); var handler = new DeleteDHCPv6ScopeCommandHandler(storageMock.Object, rootScope, Mock.Of <ILogger <DeleteDHCPv6ScopeCommandHandler> >()); Boolean result = await handler.Handle(command, CancellationToken.None); Assert.True(result); var changes = rootScope.GetChanges(); if (requestedToDeleteChildrenAsWell == true) { Assert.Null(rootScope.GetScopeById(childId)); Assert.Equal(2, changes.Count()); var firstEvent = changes.ElementAt(0) as DHCPv6ScopeDeletedEvent; Assert.Equal(childId, firstEvent.EntityId); var secondEvent = changes.ElementAt(1) as DHCPv6ScopeDeletedEvent; Assert.Equal(parentId, secondEvent.EntityId); } else { Assert.Single(changes); } Assert.Null(rootScope.GetScopeById(parentId)); Assert.NotNull(rootScope.GetScopeById(grantParentId)); storageMock.Verify(); }