public IActionResult GetScopesAsTreeView() { List <DHCPv4ScopeTreeViewItem> result = new List <DHCPv4ScopeTreeViewItem>(); foreach (var item in _rootScope.GetRootScopes()) { GenerateScopeTree(item, result); } return(base.Ok(result)); }
public void DHCPv4Lease_IsCancelable() { Random random = new Random(); DHCPv4RootScope rootScope = GetRootScope(); Guid scopeId = Guid.NewGuid(); rootScope.Load(new[] { new DHCPv4ScopeAddedEvent(new DHCPv4ScopeCreateInstruction { Id = scopeId, }), }); Dictionary <DHCPv4Lease, Boolean> expectedResults = AddEventsForCancelableLeases(random, scopeId, rootScope); DHCPv4Scope scope = rootScope.GetRootScopes().First(); foreach (var item in expectedResults) { DHCPv4Lease lease = item.Key; Boolean actual = lease.IsCancelable(); Assert.Equal(item.Value, actual); } }
public void DHCPv4Leases_GetLeaseById() { Random random = new Random(); DHCPv4RootScope rootScope = GetRootScope(); Guid scopeId = Guid.NewGuid(); List <DomainEvent> events = new List <DomainEvent> { new DHCPv4ScopeAddedEvent(new DHCPv4ScopeCreateInstruction { 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 DHCPv4LeaseCreatedEvent { ScopeId = scopeId, EntityId = leaseId, Address = random.GetIPv4Address(), ClientIdenfier = DHCPv4ClientIdentifier.FromHwAddress(random.NextBytes(6)).GetBytes(), }); existingIds.Add(leaseId); } rootScope.Load(events); DHCPv4Scope scope = rootScope.GetRootScopes().First(); foreach (Guid leaseId in existingIds) { DHCPv4Lease lease = scope.Leases.GetLeaseById(leaseId); Assert.True(lease != DHCPv4Lease.Empty); } Int32 notExisitngAmount = random.Next(30, 100); for (int i = 0; i < notExisitngAmount; i++) { Guid id = Guid.NewGuid(); if (existingIds.Contains(id) == true) { continue; } DHCPv4Lease lease = scope.Leases.GetLeaseById(id); Assert.True(lease == DHCPv4Lease.Empty); } }
public void DHCPv4Lease_IsActive() { Random random = new Random(); DHCPv4RootScope rootScope = GetRootScope(); Guid scopeId = Guid.NewGuid(); List <DomainEvent> events = new List <DomainEvent> { new DHCPv4ScopeAddedEvent(new DHCPv4ScopeCreateInstruction { 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 DHCPv4LeaseCreatedEvent { ScopeId = scopeId, EntityId = leaseId, Address = random.GetIPv4Address(), ClientIdenfier = DHCPv4ClientIdentifier.FromHwAddress(random.NextBytes(6)).GetBytes(), }); Boolean addressIsActive = random.NextDouble() > 0.5; if (addressIsActive == true) { events.Add(new DHCPv4LeaseActivatedEvent(leaseId)); } expectedResults.Add(leaseId, addressIsActive); } rootScope.Load(events); DHCPv4Scope scope = rootScope.GetRootScopes().First(); foreach (var item in expectedResults) { DHCPv4Lease lease = scope.Leases.GetLeaseById(item.Key); Boolean actual = lease.IsActive(); Assert.Equal(item.Value, actual); } }
public void DHCPv4Leases_GetSuspenedAddresses() { Random random = new Random(); DHCPv4RootScope rootScope = GetRootScope(); Guid scopeId = Guid.NewGuid(); List <DomainEvent> events = new List <DomainEvent> { new DHCPv4ScopeAddedEvent(new DHCPv4ScopeCreateInstruction { Id = scopeId, }), }; Int32 leaseAmount = random.Next(30, 60); List <IPv4Address> expectedUsedAddress = new List <IPv4Address>(); for (int i = 0; i < leaseAmount; i++) { Guid leaseId = Guid.NewGuid(); IPv4Address address = random.GetIPv4Address(); events.Add(new DHCPv4LeaseCreatedEvent { ScopeId = scopeId, EntityId = leaseId, Address = address, ClientIdenfier = DHCPv4ClientIdentifier.FromHwAddress(random.NextBytes(6)).GetBytes(), }); Boolean shouldBeSuspended = random.NextDouble() > 0.5; if (shouldBeSuspended == true) { events.Add(new DHCPv4AddressSuspendedEvent( leaseId, address, DateTime.UtcNow.AddHours(12))); expectedUsedAddress.Add(address); } } rootScope.Load(events); DHCPv4Scope scope = rootScope.GetRootScopes().First(); List <IPv4Address> actualAddresses = scope.Leases.GetSuspendedAddresses().ToList(); Assert.Equal(expectedUsedAddress.OrderBy(x => x), actualAddresses.OrderBy(x => x)); }
public async Task Handle(Boolean storeResult, Boolean useDynamicTime) { Random random = new Random(); Guid scopeId = random.NextGuid(); Guid leaseId = random.NextGuid(); String name = random.GetAlphanumericString(); String description = random.GetAlphanumericString(); IPv4Address start = random.GetIPv4Address(); IPv4Address end = start + 100; String resolverName = random.GetAlphanumericString(); Mock <IScopeResolver <DHCPv4Packet, IPv4Address> > resolverMock = new Mock <IScopeResolver <DHCPv4Packet, IPv4Address> >(MockBehavior.Strict); resolverMock.Setup(x => x.GetValues()).Returns(new Dictionary <String, String>()); Mock <IScopeResolverManager <DHCPv4Packet, IPv4Address> > scopeResolverMock = new Mock <IScopeResolverManager <DHCPv4Packet, IPv4Address> >(); scopeResolverMock.Setup(x => x.IsResolverInformationValid(It.Is <CreateScopeResolverInformation>(y => y.Typename == resolverName ))).Returns(true).Verifiable(); 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 <DHCPv4RootScope> >()); DHCPv4RootScope rootScope = new DHCPv4RootScope(random.NextGuid(), scopeResolverMock.Object, factoryMock.Object); rootScope.Load(new DomainEvent[] { new DHCPv4ScopeAddedEvent { Instructions = new DHCPv4ScopeCreateInstruction { Name = random.GetAlphanumericString(), Description = random.GetAlphanumericString(), Id = scopeId, ParentId = null, ResolverInformation = new CreateScopeResolverInformation { Typename = resolverName, }, ScopeProperties = new DHCPv4ScopeProperties(), AddressProperties = DHCPv4ScopeAddressProperties.Empty, } }, new DHCPv4LeaseCreatedEvent { EntityId = leaseId, Address = start - 100, ScopeId = scopeId, ClientIdenfier = DHCPv4ClientIdentifier.FromDuid(1, new UUIDDUID(random.NextGuid()), random.NextBytes(6)).GetBytes(), }, new DHCPv4LeaseActivatedEvent { EntityId = leaseId, ScopeId = scopeId, } }); Mock <IDHCPv4StorageEngine> storageMock = new Mock <IDHCPv4StorageEngine>(MockBehavior.Strict); storageMock.Setup(x => x.Save(rootScope)).ReturnsAsync(storeResult).Verifiable(); var command = new UpdateDHCPv4ScopeCommand(scopeId, name, description, null, useDynamicTime == false ? new DHCPv4ScopeAddressPropertyReqest { Start = start.ToString(), End = end.ToString(), ExcludedAddresses = Array.Empty <String>(), AcceptDecline = random.NextBoolean(), AddressAllocationStrategy = DHCPv4ScopeAddressPropertyReqest.AddressAllocationStrategies.Next, InformsAreAllowd = random.NextBoolean(), ReuseAddressIfPossible = random.NextBoolean(), SupportDirectUnicast = random.NextBoolean(), PreferredLifetime = TimeSpan.FromDays(0.5), RenewalTime = TimeSpan.FromDays(0.25), LeaseTime = TimeSpan.FromDays(1), MaskLength = (Byte)random.Next(10, 29), } : new DHCPv4ScopeAddressPropertyReqest { Start = start.ToString(), End = end.ToString(), ExcludedAddresses = Array.Empty <String>(), AcceptDecline = random.NextBoolean(), AddressAllocationStrategy = DHCPv4ScopeAddressPropertyReqest.AddressAllocationStrategies.Next, InformsAreAllowd = random.NextBoolean(), ReuseAddressIfPossible = random.NextBoolean(), SupportDirectUnicast = random.NextBoolean(), DynamicRenewTime = new DHCPv4DynamicRenewTimeRequest { Hours = 5, Minutes = 10, MinutesToEndOfLife = 45, MinutesToRebound = 35, }, MaskLength = (Byte)random.Next(10, 29), } , new CreateScopeResolverRequest { PropertiesAndValues = new Dictionary <String, String>(), Typename = resolverName, }, new DHCPv4ScopePropertyRequest[] { new DHCPv4AddressListScopePropertyRequest { OptionCode = 24, Type = DHCPv4ScopePropertyType.AddressList, Addresses = random.GetIPv4Addresses().Select(x => x.ToString()).ToArray(), }, new DHCPv4AddressScopePropertyRequest { OptionCode = 25, Type = DHCPv4ScopePropertyType.Address, Address = random.GetIPv4Address().ToString() }, new DHCPv4BooleanScopePropertyRequest { OptionCode = 26, Type = DHCPv4ScopePropertyType.Boolean, Value = random.NextBoolean() }, new DHCPv4NumericScopePropertyRequest { OptionCode = 27, Type = DHCPv4ScopePropertyType.UInt16, NumericType = DHCPv4NumericValueTypes.UInt16, Value = (UInt16)random.NextUInt16() }, new DHCPv4TextScopePropertyRequest { OptionCode = 28, Type = DHCPv4ScopePropertyType.Text, Value = random.GetAlphanumericString() }, new DHCPv4TimeScopePropertyRequest { OptionCode = 29, Type = DHCPv4ScopePropertyType.Time, Value = TimeSpan.FromSeconds(random.Next(10, 20)) }, new DHCPv4AddressListScopePropertyRequest { OptionCode = 64, MarkAsRemovedInInheritance = true, } } ); var serviceBusMock = new Mock <IServiceBus>(MockBehavior.Strict); serviceBusMock.Setup(x => x.Publish(It.Is <NewTriggerHappendMessage>(y => y.Triggers.Count() == 1 && ((PrefixEdgeRouterBindingUpdatedTrigger)y.Triggers.First()).OldBinding.Prefix == IPv4Address.FromString("fc70::0") ))).Returns(Task.CompletedTask); var handler = new UpdateDHCPv4ScopeCommandHandler(storageMock.Object, serviceBusMock.Object, rootScope, Mock.Of <ILogger <UpdateDHCPv4ScopeCommandHandler> >()); Boolean result = await handler.Handle(command, CancellationToken.None); Assert.Equal(storeResult, result); var scope = rootScope.GetRootScopes().First(); Assert.Equal(name, scope.Name); Assert.Equal(description, scope.Description); Assert.NotNull(scope.Resolver); Assert.Equal(start, scope.AddressRelatedProperties.Start); Assert.True(scope.Properties.IsMarkedAsRemovedFromInheritance(64)); Assert.False(scope.Properties.IsMarkedAsRemovedFromInheritance(24)); if (useDynamicTime == true) { Assert.True(scope.AddressRelatedProperties.UseDynamicRewnewTime); Assert.Equal(5, scope.AddressRelatedProperties.DynamicRenewTime.Hour); } else { Assert.False(scope.AddressRelatedProperties.UseDynamicRewnewTime); } scopeResolverMock.Verify(); storageMock.Verify(); serviceBusMock.Verify(); Assert.Empty(rootScope.GetTriggers()); }
public async Task Handle(Boolean withDynamicRenewTime) { Random random = new Random(); String name = random.GetAlphanumericString(); String description = random.GetAlphanumericString(); IPv4Address start = random.GetIPv4Address(); IPv4Address end = start + 100; String resolverName = random.GetAlphanumericString(); Mock <IScopeResolverManager <DHCPv4Packet, IPv4Address> > scopeResolverMock = new Mock <IScopeResolverManager <DHCPv4Packet, IPv4Address> >(); scopeResolverMock.Setup(x => x.IsResolverInformationValid(It.Is <CreateScopeResolverInformation>(y => y.Typename == resolverName ))).Returns(true).Verifiable(); scopeResolverMock.Setup(x => x.InitializeResolver(It.Is <CreateScopeResolverInformation>(y => y.Typename == resolverName ))).Returns(Mock.Of <IScopeResolver <DHCPv4Packet, IPv4Address> >()).Verifiable(); Mock <ILoggerFactory> factoryMock = new Mock <ILoggerFactory>(MockBehavior.Strict); factoryMock.Setup(x => x.CreateLogger(It.IsAny <String>())).Returns(Mock.Of <ILogger <DHCPv4RootScope> >()); DHCPv4RootScope rootScope = new DHCPv4RootScope(random.NextGuid(), scopeResolverMock.Object, factoryMock.Object); Mock <IDHCPv4StorageEngine> storageMock = new Mock <IDHCPv4StorageEngine>(MockBehavior.Strict); storageMock.Setup(x => x.Save(rootScope)).ReturnsAsync(true).Verifiable(); var command = new CreateDHCPv4ScopeCommand(name, description, null, withDynamicRenewTime == false ? new DHCPv4ScopeAddressPropertyReqest { Start = start.ToString(), End = end.ToString(), ExcludedAddresses = Array.Empty <String>(), AcceptDecline = random.NextBoolean(), AddressAllocationStrategy = DHCPv4ScopeAddressPropertyReqest.AddressAllocationStrategies.Next, InformsAreAllowd = random.NextBoolean(), ReuseAddressIfPossible = random.NextBoolean(), SupportDirectUnicast = random.NextBoolean(), RenewalTime = TimeSpan.FromDays(0.5), PreferredLifetime = TimeSpan.FromDays(0.8), LeaseTime = TimeSpan.FromDays(1), MaskLength = (Byte)random.Next(12, 20), } : new DHCPv4ScopeAddressPropertyReqest { Start = start.ToString(), End = end.ToString(), ExcludedAddresses = Array.Empty <String>(), AcceptDecline = random.NextBoolean(), AddressAllocationStrategy = DHCPv4ScopeAddressPropertyReqest.AddressAllocationStrategies.Next, InformsAreAllowd = random.NextBoolean(), ReuseAddressIfPossible = random.NextBoolean(), SupportDirectUnicast = random.NextBoolean(), MaskLength = (Byte)random.Next(12, 20), DynamicRenewTime = new DHCPv4DynamicRenewTimeRequest { Hours = 3, Minutes = 15, MinutesToEndOfLife = 40, MinutesToRebound = 20, } }, new CreateScopeResolverRequest { PropertiesAndValues = new Dictionary <String, String>(), Typename = resolverName, }, new DHCPv4ScopePropertyRequest[] { new DHCPv4AddressListScopePropertyRequest { OptionCode = 24, Type = DHCPv4ScopePropertyType.AddressList, Addresses = random.GetIPv4Addresses().Select(x => x.ToString()).ToArray(), }, new DHCPv4AddressScopePropertyRequest { OptionCode = 25, Type = DHCPv4ScopePropertyType.Address, Address = random.GetIPv4Address().ToString() }, new DHCPv4BooleanScopePropertyRequest { OptionCode = 26, Type = DHCPv4ScopePropertyType.Boolean, Value = random.NextBoolean() }, new DHCPv4NumericScopePropertyRequest { OptionCode = 27, Type = DHCPv4ScopePropertyType.UInt16, NumericType = DHCPv4NumericValueTypes.UInt16, Value = (UInt16)random.NextUInt16() }, new DHCPv4TextScopePropertyRequest { OptionCode = 28, Type = DHCPv4ScopePropertyType.Text, Value = random.GetAlphanumericString() }, new DHCPv4TimeScopePropertyRequest { OptionCode = 29, Type = DHCPv4ScopePropertyType.Time, Value = TimeSpan.FromSeconds(random.Next(10, 20)) }, new DHCPv4AddressListScopePropertyRequest { OptionCode = 64, MarkAsRemovedInInheritance = true, } } ); var handler = new CreateDHCPv4ScopeCommandHandler(storageMock.Object, rootScope, Mock.Of <ILogger <CreateDHCPv4ScopeCommandHandler> >()); Guid?result = await handler.Handle(command, CancellationToken.None); Assert.True(result.HasValue); var scope = rootScope.GetRootScopes().First(); Assert.True(scope.Properties.IsMarkedAsRemovedFromInheritance(64)); Assert.False(scope.Properties.IsMarkedAsRemovedFromInheritance(24)); if (withDynamicRenewTime == false) { Assert.False(scope.AddressRelatedProperties.UseDynamicRewnewTime); } else { Assert.True(scope.AddressRelatedProperties.UseDynamicRewnewTime); Assert.Equal(3, scope.AddressRelatedProperties.DynamicRenewTime.Hour); } scopeResolverMock.Verify(); storageMock.Verify(); }
public void DHCPv4Lease_AddressesAreInUse() { Random random = new Random(); DHCPv4RootScope rootScope = GetRootScope(); Guid scopeId = Guid.NewGuid(); rootScope.Load(new[] { new DHCPv4ScopeAddedEvent(new DHCPv4ScopeCreateInstruction { Id = scopeId, }) }); Int32 leaseAmount = random.Next(10, 30); Dictionary <Guid, Boolean> expectedResults = new Dictionary <Guid, bool>(); Dictionary <Guid, DHCPv4Lease> leases = new Dictionary <Guid, DHCPv4Lease>(); for (int i = 0; i < leaseAmount; i++) { Guid leaseId = Guid.NewGuid(); rootScope.Load(new[] { new DHCPv4LeaseCreatedEvent { ScopeId = scopeId, EntityId = leaseId, Address = random.GetIPv4Address(), ClientIdenfier = DHCPv4ClientIdentifier.FromHwAddress(random.NextBytes(6)).GetBytes(), } }); leases.Add(leaseId, 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 DHCPv4LeaseReleasedEvent(leaseId); addressIsInUse = false; } else if (randomValue < 2 / possiblities) { eventToAdd = new DHCPv4LeaseRevokedEvent(leaseId); addressIsInUse = false; } else if (randomValue < 3 / possiblities) { eventToAdd = new DHCPv4AddressSuspendedEvent(leaseId, random.GetIPv4Address(), DateTime.UtcNow.AddHours(12)); addressIsInUse = false; } if (eventToAdd != null) { rootScope.Load(new[] { eventToAdd }); } expectedResults.Add(leaseId, addressIsInUse); } foreach (var item in expectedResults) { DHCPv4Lease lease = leases[item.Key]; Boolean actual = lease.AddressIsInUse(); Assert.Equal(item.Value, actual); } }
public static Dictionary <DHCPv4Lease, Boolean> AddEventsForCancelableLeases( Random random, Guid scopeId, DHCPv4RootScope scope) { Dictionary <LeaseStates, Func <Guid, DHCPv4ScopeRelatedEvent> > cancalableStateBuilder = new Dictionary <LeaseStates, Func <Guid, DHCPv4ScopeRelatedEvent> > { { LeaseStates.Pending, (id) => null }, { LeaseStates.Active, (id) => new DHCPv4LeaseActivatedEvent(id) }, }; Dictionary <LeaseStates, Func <Guid, DHCPv4ScopeRelatedEvent> > nonCancalableStateBuilder = new Dictionary <LeaseStates, Func <Guid, DHCPv4ScopeRelatedEvent> > { { LeaseStates.Inactive, (id) => new DHCPv4LeaseExpiredEvent(id) }, { LeaseStates.Canceled, (id) => new DHCPv4LeaseCanceledEvent(id) }, { LeaseStates.Released, (id) => new DHCPv4LeaseReleasedEvent(id) }, { LeaseStates.Revoked, (id) => new DHCPv4LeaseRevokedEvent(id) }, }; Int32 leaseAmount = random.Next(20, 40); Dictionary <DHCPv4Lease, Boolean> expectedCancallations = new(); for (int i = 0; i < leaseAmount; i++) { Guid leaseId = Guid.NewGuid(); scope.Load(new[] { new DHCPv4LeaseCreatedEvent { ScopeId = scopeId, EntityId = leaseId, ClientIdenfier = DHCPv4ClientIdentifier.FromHwAddress(random.NextBytes(6)).GetBytes(), Address = random.GetIPv4Address(), } }); DHCPv4Lease lease = scope.GetRootScopes().First().Leases.GetLeaseById(leaseId); Boolean shouldBeCancelable = random.NextDouble() > 0.5; Dictionary <LeaseStates, Func <Guid, DHCPv4ScopeRelatedEvent> > eventCreatorDict = null; if (shouldBeCancelable == true) { eventCreatorDict = cancalableStateBuilder; } else { eventCreatorDict = nonCancalableStateBuilder; } var entry = eventCreatorDict.ElementAt(random.Next(0, eventCreatorDict.Count)); DHCPv4ScopeRelatedEvent stateChangingEvent = entry.Value(leaseId); if (stateChangingEvent != null) { stateChangingEvent.ScopeId = scopeId; scope.Load(new[] { stateChangingEvent }); } expectedCancallations.Add(lease, shouldBeCancelable); } return(expectedCancallations); }
public void DHCPv4Lease_MatchesUniqueIdentiifer() { Random random = new Random(); DHCPv4RootScope rootScope = GetRootScope(); Guid scopeId = Guid.NewGuid(); Byte[] uniqueIdentifier = random.NextBytes(10); List <DomainEvent> events = new List <DomainEvent> { new DHCPv4ScopeAddedEvent(new DHCPv4ScopeCreateInstruction { 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 DHCPv4LeaseCreatedEvent { ScopeId = scopeId, EntityId = leaseId, UniqueIdentifier = identifier, Address = random.GetIPv4Address(), ClientIdenfier = DHCPv4ClientIdentifier.FromHwAddress(random.NextBytes(6)).GetBytes(), }); expectedResults.Add(leaseId, matches); } rootScope.Load(events); DHCPv4Scope scope = rootScope.GetRootScopes().First(); foreach (var item in expectedResults) { DHCPv4Lease lease = scope.Leases.GetLeaseById(item.Key); Boolean actual = lease.MatchesUniqueIdentiifer(uniqueIdentifier); Assert.Equal(item.Value, actual); } }
public void GetScopesAsTreeView() { Random random = new Random(); var events = new List <DomainEvent>(); Int32 rootScopeAmount = random.Next(10, 30); List <Guid> rootScopeIds = new List <Guid>(rootScopeAmount); for (int i = 0; i < rootScopeAmount; i++) { Guid scopeId = Guid.NewGuid(); IPv4Address start = random.GetIPv4Address(); IPv4Address end = start + 100; var excluded = new[] { start + 1, start + 3 }; DHCPv4ScopeProperties properties = null; if (random.NextBoolean() == true) { DHCPv4AddressListScopeProperty gwAddress = new( DHCPv4OptionTypes.Router, new[] { start + 0, start + 30 }); properties = new DHCPv4ScopeProperties(gwAddress); } else { properties = new(); } events.Add(new DHCPv4ScopeAddedEvent(new DHCPv4ScopeCreateInstruction { Id = scopeId, Name = random.GetAlphanumericString(), AddressProperties = new DHCPv4ScopeAddressProperties(start, end, excluded, maskLength: random.NextBoolean() == true ? (Byte)random.Next(0, 32) : null), ScopeProperties = properties })); rootScopeIds.Add(scopeId); GenerateScopeTree( random.NextDouble(), random, new List <Guid> { scopeId }, events); } var scopeResolverMock = new Mock <IScopeResolverManager <DHCPv4Packet, IPv4Address> >(MockBehavior.Strict); scopeResolverMock.Setup(x => x.InitializeResolver(It.IsAny <CreateScopeResolverInformation>())).Returns(Mock.Of <IScopeResolver <DHCPv4Packet, IPv4Address> >()); Mock <ILoggerFactory> factoryMock = new Mock <ILoggerFactory>(MockBehavior.Strict); factoryMock.Setup(x => x.CreateLogger(It.IsAny <String>())).Returns(Mock.Of <ILogger <DHCPv4RootScope> >()); DHCPv4RootScope rootScope = new DHCPv4RootScope(random.NextGuid(), scopeResolverMock.Object, factoryMock.Object); rootScope.Load(events); var controller = new DHCPv4ScopeController( Mock.Of <IMediator>(MockBehavior.Strict), Mock.Of <IScopeResolverManager <DHCPv4Packet, IPv4Address> >(MockBehavior.Strict), rootScope); var actionResult = controller.GetScopesAsTreeView(); var result = actionResult.EnsureOkObjectResult <IEnumerable <DHCPv4ScopeTreeViewItem> >(true); Assert.Equal(rootScopeAmount, result.Count()); Int32 index = 0; foreach (var item in rootScope.GetRootScopes()) { var scope = result.ElementAt(index); CheckTreeItem(item, scope); index++; } }
public void DHCPv4Leases_IsAddressActive() { Random random = new Random(); DHCPv4RootScope rootScope = GetRootScope(); Guid scopeId = Guid.NewGuid(); List <DomainEvent> events = new List <DomainEvent> { new DHCPv4ScopeAddedEvent(new DHCPv4ScopeCreateInstruction { Id = scopeId, }), }; Int32 leaseAmount = random.Next(30, 60); Dictionary <IPv4Address, Boolean> expectedUsedAddress = new Dictionary <IPv4Address, bool>(); for (int i = 0; i < leaseAmount; i++) { Guid leaseId = Guid.NewGuid(); IPv4Address address = random.GetIPv4Address(); events.Add(new DHCPv4LeaseCreatedEvent { ScopeId = scopeId, EntityId = leaseId, Address = address, ClientIdenfier = DHCPv4ClientIdentifier.FromHwAddress(random.NextBytes(6)).GetBytes(), }); Boolean addressIsInUse = random.NextDouble() > 0.5; if (addressIsInUse == true) { events.Add(new DHCPv4LeaseActivatedEvent(leaseId)); } expectedUsedAddress.Add(address, addressIsInUse); } rootScope.Load(events); DHCPv4Scope 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++) { IPv4Address notExisitingAddress = random.GetIPv4Address(); if (expectedUsedAddress.ContainsKey(notExisitingAddress) == true) { continue; } Boolean actual = scope.Leases.IsAddressActive(notExisitingAddress); Assert.False(actual); } }
public void DHCPv4Leases_GetUsedAddresses() { Random random = new Random(); DHCPv4RootScope rootScope = GetRootScope(); Guid scopeId = Guid.NewGuid(); List <DomainEvent> events = new List <DomainEvent> { new DHCPv4ScopeAddedEvent(new DHCPv4ScopeCreateInstruction { Id = scopeId, }), }; Int32 leaseAmount = random.Next(30, 60); List <IPv4Address> expectedUsedAddress = new List <IPv4Address>(); for (int i = 0; i < leaseAmount; i++) { Guid leaseId = Guid.NewGuid(); IPv4Address address = random.GetIPv4Address(); events.Add(new DHCPv4LeaseCreatedEvent { ScopeId = scopeId, EntityId = leaseId, Address = address, ClientIdenfier = DHCPv4ClientIdentifier.FromHwAddress(random.NextBytes(6)).GetBytes(), }); DomainEvent eventToAdd = null; Boolean addressIsInUse = true; Double randomValue = random.NextDouble(); Double possiblities = 5.0; if (randomValue < 1 / possiblities) { eventToAdd = new DHCPv4LeaseReleasedEvent(leaseId); addressIsInUse = false; } else if (randomValue < 2 / possiblities) { eventToAdd = new DHCPv4LeaseRevokedEvent(leaseId); addressIsInUse = false; } else if (randomValue < 3 / possiblities) { eventToAdd = new DHCPv4AddressSuspendedEvent(leaseId, random.GetIPv4Address(), DateTime.UtcNow.AddHours(12)); addressIsInUse = false; } if (eventToAdd != null) { events.Add(eventToAdd); } if (addressIsInUse == true) { expectedUsedAddress.Add(address); } } rootScope.Load(events); DHCPv4Scope scope = rootScope.GetRootScopes().First(); List <IPv4Address> actualAddresses = scope.Leases.GetUsedAddresses().ToList(); Assert.Equal(expectedUsedAddress, actualAddresses); }