public void DHCPv6AddressSuspendedEvent() { Random random = new Random(); DHCPv6AddressSuspendedEvent expected = new DHCPv6AddressSuspendedEvent { Address = random.GetIPv6Address(), EntityId = random.NextGuid(), ScopeId = random.NextGuid(), SuspendedTill = DateTime.UtcNow.AddDays(0.5), Timestamp = DateTime.UtcNow, }; var actual = SerializeAndDeserialze(expected); Assert.Equal(expected.Address, actual.Address); Assert.Equal(expected.EntityId, actual.EntityId); Assert.Equal(expected.ScopeId, actual.ScopeId); Assert.Equal(expected.SuspendedTill, actual.SuspendedTill); Assert.Equal(expected.Timestamp, actual.Timestamp); }
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 AddressesAreInUse() { Random random = new Random(123); DHCPv6RootScope rootScope = GetRootScope(); Guid scopeId = Guid.NewGuid(); rootScope.Load(new[] { new DHCPv6ScopeAddedEvent(new DHCPv6ScopeCreateInstruction { Id = scopeId, }) }); Int32 leaseAmount = random.Next(10, 30); Dictionary <DHCPv6Lease, Boolean> expectedResults = new(); for (int i = 0; i < leaseAmount; i++) { Guid leaseId = Guid.NewGuid(); rootScope.Load(new[] { new DHCPv6LeaseCreatedEvent { ScopeId = scopeId, EntityId = leaseId, Address = random.GetIPv6Address(), ClientIdentifier = new UUIDDUID(random.NextGuid()), } }); DHCPv6Lease lease = rootScope.GetRootScopes().First().Leases.GetLeaseById(leaseId); 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; } else if (randomValue < 4 / possiblities) { eventToAdd = new DHCPv6LeaseCanceledEvent(leaseId, LeaseCancelReasons.NotSpecified); addressIsInUse = false; } if (eventToAdd != null) { rootScope.Load(new[] { eventToAdd }); } expectedResults.Add(lease, addressIsInUse); } foreach (var item in expectedResults) { DHCPv6Lease lease = item.Key; Boolean actual = lease.AddressIsInUse(); Assert.Equal(item.Value, actual); } }
public void CanBeExtended() { Random random = new Random(1345); DHCPv6RootScope rootScope = GetRootScope(); Guid scopeId = Guid.NewGuid(); rootScope.Load(new[] { new DHCPv6ScopeAddedEvent(new DHCPv6ScopeCreateInstruction { Id = scopeId, }), }); Int32 leaseAmount = random.Next(10, 30); Dictionary <DHCPv6Lease, Boolean> expectedResults = new(); for (int i = 0; i < leaseAmount; i++) { Guid leaseId = Guid.NewGuid(); rootScope.Load(new[] { new DHCPv6LeaseCreatedEvent { ScopeId = scopeId, EntityId = leaseId, Address = random.GetIPv6Address(), ClientIdentifier = new UUIDDUID(random.NextGuid()), } }); DHCPv6Lease lease = rootScope.GetRootScopes().First().Leases.GetLeaseById(leaseId); Boolean shouldBeExtentable = random.NextBoolean(); DomainEvent @event = null; if (shouldBeExtentable == true) { Int32 nextValue = random.Next(0, 4); switch (nextValue) { case 0: @event = new DHCPv6LeaseActivatedEvent(leaseId); break; case 1: @event = new DHCPv6LeaseReleasedEvent(leaseId, false); break; case 2: @event = new DHCPv6LeaseRenewedEvent(leaseId, DateTime.UtcNow.AddHours(3), TimeSpan.FromHours(1), TimeSpan.FromHours(2), false, false); break; case 3: @event = new DHCPv6LeaseExpiredEvent(leaseId); break; default: break; } } else { Int32 nextValue = random.Next(0, 3); switch (nextValue) { case 0: @event = new DHCPv6LeaseCanceledEvent(leaseId); break; case 1: @event = new DHCPv6LeaseRevokedEvent(leaseId); break; case 2: @event = new DHCPv6AddressSuspendedEvent(leaseId, random.GetIPv6Address(), DateTime.UtcNow.AddHours(3)); break; default: break; } } rootScope.Load(new[] { @event }); expectedResults.Add(lease, shouldBeExtentable); } foreach (var item in expectedResults) { DHCPv6Lease lease = item.Key; Boolean actual = lease.CanBeExtended(); Assert.Equal(item.Value, actual); } }