public void GetLeaseById() { Random random = new Random(); DHCPv6RootScope rootScope = GetRootScope(); Guid scopeId = Guid.NewGuid(); List <DomainEvent> events = new List <DomainEvent> { new DHCPv6ScopeAddedEvent(new DHCPv6ScopeCreateInstruction { Id = scopeId, }), }; Int32 leaseAmount = random.Next(3, 10); HashSet <Guid> existingIds = new HashSet <Guid>(); for (int i = 0; i < leaseAmount; i++) { Guid leaseId = Guid.NewGuid(); events.Add(new DHCPv6LeaseCreatedEvent { ScopeId = scopeId, EntityId = leaseId, Address = random.GetIPv6Address(), ClientIdentifier = new UUIDDUID(random.NextGuid()), }); existingIds.Add(leaseId); } rootScope.Load(events); DHCPv6Scope scope = rootScope.GetRootScopes().First(); foreach (Guid leaseId in existingIds) { DHCPv6Lease lease = scope.Leases.GetLeaseById(leaseId); Assert.True(lease != DHCPv6Lease.Empty); } Int32 notExisitngAmount = random.Next(30, 100); for (int i = 0; i < notExisitngAmount; i++) { Guid id = Guid.NewGuid(); if (existingIds.Contains(id) == true) { continue; } DHCPv6Lease lease = scope.Leases.GetLeaseById(id); Assert.True(lease == DHCPv6Lease.Empty); } }
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 void IsActive() { Random random = new Random(); DHCPv6RootScope rootScope = GetRootScope(); Guid scopeId = Guid.NewGuid(); List <DomainEvent> events = new List <DomainEvent> { new DHCPv6ScopeAddedEvent(new DHCPv6ScopeCreateInstruction { Id = scopeId, }), }; Int32 leaseAmount = random.Next(10, 30); Dictionary <Guid, Boolean> expectedResults = new Dictionary <Guid, bool>(); for (int i = 0; i < leaseAmount; i++) { Guid leaseId = Guid.NewGuid(); events.Add(new DHCPv6LeaseCreatedEvent { ScopeId = scopeId, EntityId = leaseId, Address = random.GetIPv6Address(), ClientIdentifier = new UUIDDUID(random.NextGuid()), }); Boolean addressIsActive = random.NextDouble() > 0.5; if (addressIsActive == true) { events.Add(new DHCPv6LeaseActivatedEvent(leaseId)); } expectedResults.Add(leaseId, addressIsActive); } rootScope.Load(events); DHCPv6Scope scope = rootScope.GetRootScopes().First(); foreach (var item in expectedResults) { DHCPv6Lease lease = scope.Leases.GetLeaseById(item.Key); Boolean actual = lease.IsActive(); Assert.Equal(item.Value, actual); } }
public void GetSuspenedAddresses() { Random random = new Random(); DHCPv6RootScope rootScope = GetRootScope(); Guid scopeId = Guid.NewGuid(); List <DomainEvent> events = new List <DomainEvent> { new DHCPv6ScopeAddedEvent(new DHCPv6ScopeCreateInstruction { Id = scopeId, }), }; Int32 leaseAmount = random.Next(30, 60); List <IPv6Address> expectedUsedAddress = new List <IPv6Address>(); for (int i = 0; i < leaseAmount; i++) { Guid leaseId = Guid.NewGuid(); IPv6Address address = random.GetIPv6Address(); events.Add(new DHCPv6LeaseCreatedEvent { ScopeId = scopeId, EntityId = leaseId, Address = address, ClientIdentifier = new UUIDDUID(random.NextGuid()), }); Boolean shouldBeSuspended = random.NextDouble() > 0.5; if (shouldBeSuspended == true) { events.Add(new DHCPv6AddressSuspendedEvent( leaseId, address, DateTime.UtcNow.AddHours(12))); expectedUsedAddress.Add(address); } } rootScope.Load(events); DHCPv6Scope scope = rootScope.GetRootScopes().First(); List <IPv6Address> actualAddresses = scope.Leases.GetSuspendedAddresses().ToList(); Assert.Equal(expectedUsedAddress.OrderBy(x => x), actualAddresses.OrderBy(x => x)); }
public void LimitLeasesPerClient() { Random random = new Random(); DHCPv6RootScope rootScope = GetRootScope(); Guid scopeId = Guid.NewGuid(); DUID clientIdentifier = new UUIDDUID(random.NextGuid()); List <DomainEvent> events = new List <DomainEvent> { new DHCPv6ScopeAddedEvent(new DHCPv6ScopeCreateInstruction { Id = scopeId, }), }; Int32 leaseAmount = random.Next(20, 40); for (int i = 0; i < leaseAmount; i++) { Guid leaseId = Guid.NewGuid(); events.Add(new DHCPv6LeaseCreatedEvent { ScopeId = scopeId, EntityId = leaseId, Address = random.GetIPv6Address(), ClientIdentifier = clientIdentifier }); events.Add(new DHCPv6LeaseActivatedEvent { ScopeId = scopeId, EntityId = leaseId, }); events.Add(new DHCPv6LeaseRevokedEvent { ScopeId = scopeId, EntityId = leaseId, }); } rootScope.Load(events); DHCPv6Scope scope = rootScope.GetRootScopes().First(); var leases = scope.Leases.GetAllLeases(); Assert.Empty(leases); }
private DHCPv6LeaseOverview GetLeaseOverview(DHCPv6Lease lease, DHCPv6Scope scope) => new DHCPv6LeaseOverview { Address = lease.Address.ToString(), ClientIdentifier = lease.ClientDUID, Started = lease.Start, ExpectedEnd = lease.End, Id = lease.Id, Prefix = lease.PrefixDelegation != DHCPv6PrefixDelegation.None ? new PrefixOverview { Address = lease.PrefixDelegation.NetworkAddress.ToString(), Mask = lease.PrefixDelegation.Mask.Identifier } : null, UniqueIdentifier = lease.UniqueIdentifier, State = lease.State, Scope = new ScopeOverview { Id = scope.Id, Name = scope.Name, } };
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)); } }
private Boolean SearchChildScope(DHCPv6Scope scope, Guid expectedId) { if (scope.Id == expectedId) { return(true); } else { foreach (var item in scope.GetChildScopes()) { Boolean result = SearchChildScope(item, expectedId); if (result == true) { return(result); } } return(false); } }
private void GenerateScopeList(DHCPv6Scope scope, ICollection <DHCPv6ScopeItem> collection) { var node = new DHCPv6ScopeItem { Id = scope.Id, StartAddress = scope.AddressRelatedProperties.Start.ToString(), EndAddress = scope.AddressRelatedProperties.End.ToString(), Name = scope.Name, }; collection.Add(node); if (scope.GetChildScopes().Any()) { foreach (var item in scope.GetChildScopes()) { GenerateScopeList(item, collection); } } }
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)); } }
private void GenerateScopeTree(DHCPv6Scope scope, ICollection <DHCPv6ScopeTreeViewItem> parentChildren) { List <DHCPv6ScopeTreeViewItem> childItems = new List <DHCPv6ScopeTreeViewItem>(); var node = new DHCPv6ScopeTreeViewItem { Id = scope.Id, StartAddress = scope.AddressRelatedProperties.Start.ToString(), EndAddress = scope.AddressRelatedProperties.End.ToString(), Name = scope.Name, ChildScopes = childItems, }; parentChildren.Add(node); if (scope.GetChildScopes().Any()) { foreach (var item in scope.GetChildScopes()) { GenerateScopeTree(item, childItems); } } }
private void CheckTreeItem(DHCPv6Scope item, DHCPv6ScopeTreeViewItem viewItem) { Assert.Equal(item.Name, viewItem.Name); Assert.Equal(item.Id, viewItem.Id); Assert.Equal(item.AddressRelatedProperties.Start.ToString(), viewItem.StartAddress); Assert.Equal(item.AddressRelatedProperties.End.ToString(), viewItem.EndAddress); if (item.GetChildScopes().Any() == true) { Assert.Equal(item.GetChildScopes().Count(), viewItem.ChildScopes.Count()); Int32 index = 0; foreach (var childScope in item.GetChildScopes()) { var childViewItem = viewItem.ChildScopes.ElementAt(index); CheckTreeItem(childScope, childViewItem); index++; } } else { Assert.Empty(viewItem.ChildScopes); } }
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); } }
private void GetAllLesesRecursivly(ICollection <DHCPv6LeaseOverview> collection, DHCPv6Scope scope) { var items = scope.Leases.GetAllLeases().Select(x => GetLeaseOverview(x, scope)).ToList(); foreach (var item in items) { collection.Add(item); } var children = scope.GetChildScopes(); if (children.Any() == false) { return; } foreach (var item in children) { GetAllLesesRecursivly(collection, item); } }
public void MatchesUniqueIdentiifer() { Random random = new Random(); DHCPv6RootScope rootScope = GetRootScope(); Guid scopeId = Guid.NewGuid(); Byte[] uniqueIdentifier = random.NextBytes(10); List <DomainEvent> events = new List <DomainEvent> { new DHCPv6ScopeAddedEvent(new DHCPv6ScopeCreateInstruction { Id = scopeId, }), }; Int32 leaseAmount = random.Next(3, 10); Dictionary <Guid, Boolean> expectedResults = new Dictionary <Guid, bool>(); for (int i = 0; i < leaseAmount; i++) { Guid leaseId = Guid.NewGuid(); Byte[] identifier = null; Boolean matches = false; Double randomValue = random.NextDouble(); if (randomValue > 0.75) { identifier = uniqueIdentifier; matches = true; } else if (randomValue > 0.5) { identifier = random.NextBytes(12); } else if (randomValue > 0.25) { identifier = new byte[0]; } events.Add(new DHCPv6LeaseCreatedEvent { ScopeId = scopeId, EntityId = leaseId, UniqueIdentiifer = identifier, Address = random.GetIPv6Address(), ClientIdentifier = new UUIDDUID(random.NextGuid()), }); expectedResults.Add(leaseId, matches); } rootScope.Load(events); DHCPv6Scope scope = rootScope.GetRootScopes().First(); foreach (var item in expectedResults) { DHCPv6Lease lease = scope.Leases.GetLeaseById(item.Key); Boolean actual = lease.MatchesUniqueIdentiifer(uniqueIdentifier); Assert.Equal(item.Value, actual); } }
public void GetUsedAddresses() { Random random = new Random(); DHCPv6RootScope rootScope = GetRootScope(); Guid scopeId = Guid.NewGuid(); List <DomainEvent> events = new List <DomainEvent> { new DHCPv6ScopeAddedEvent(new DHCPv6ScopeCreateInstruction { Id = scopeId, }), }; Int32 leaseAmount = random.Next(30, 60); List <IPv6Address> expectedUsedAddress = new List <IPv6Address>(); for (int i = 0; i < leaseAmount; i++) { Guid leaseId = Guid.NewGuid(); IPv6Address address = random.GetIPv6Address(); events.Add(new DHCPv6LeaseCreatedEvent { ScopeId = scopeId, EntityId = leaseId, Address = address, ClientIdentifier = new UUIDDUID(random.NextGuid()), }); DomainEvent eventToAdd = null; Boolean addressIsInUse = true; Double randomValue = random.NextDouble(); Double possiblities = 5.0; if (randomValue < 1 / possiblities) { eventToAdd = new DHCPv6LeaseReleasedEvent(leaseId, false); addressIsInUse = false; } else if (randomValue < 2 / possiblities) { eventToAdd = new DHCPv6LeaseRevokedEvent(leaseId); addressIsInUse = false; } else if (randomValue < 3 / possiblities) { eventToAdd = new DHCPv6AddressSuspendedEvent(leaseId, random.GetIPv6Address(), DateTime.UtcNow.AddHours(12)); addressIsInUse = false; } if (eventToAdd != null) { events.Add(eventToAdd); } if (addressIsInUse == true) { expectedUsedAddress.Add(address); } } rootScope.Load(events); DHCPv6Scope scope = rootScope.GetRootScopes().First(); List <IPv6Address> actualAddresses = scope.Leases.GetUsedAddresses().ToList(); Assert.Equal(expectedUsedAddress, actualAddresses); }
public void IsAddressActive() { Random random = new Random(); DHCPv6RootScope rootScope = GetRootScope(); Guid scopeId = Guid.NewGuid(); List <DomainEvent> events = new List <DomainEvent> { new DHCPv6ScopeAddedEvent(new DHCPv6ScopeCreateInstruction { Id = scopeId, }), }; Int32 leaseAmount = random.Next(30, 60); Dictionary <IPv6Address, Boolean> expectedUsedAddress = new Dictionary <IPv6Address, bool>(); for (int i = 0; i < leaseAmount; i++) { Guid leaseId = Guid.NewGuid(); IPv6Address address = random.GetIPv6Address(); events.Add(new DHCPv6LeaseCreatedEvent { ScopeId = scopeId, EntityId = leaseId, Address = address, ClientIdentifier = new UUIDDUID(random.NextGuid()), }); Boolean addressIsInUse = random.NextDouble() > 0.5; if (addressIsInUse == true) { events.Add(new DHCPv6LeaseActivatedEvent(leaseId)); } expectedUsedAddress.Add(address, addressIsInUse); } rootScope.Load(events); DHCPv6Scope scope = rootScope.GetRootScopes().First(); foreach (var item in expectedUsedAddress) { Boolean actual = scope.Leases.IsAddressActive(item.Key); Assert.Equal(item.Value, actual); } Int32 notExistingAddressesAmount = random.Next(30, 50); for (int i = 0; i < notExistingAddressesAmount; i++) { IPv6Address notExisitingAddress = random.GetIPv6Address(); if (expectedUsedAddress.ContainsKey(notExisitingAddress) == true) { continue; } Boolean actual = scope.Leases.IsAddressActive(notExisitingAddress); Assert.False(actual); } }