예제 #1
0
        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);
        }
예제 #2
0
        protected static void DHCPv6ScopeAddressesAreExhaustedEvent(int index, DHCPv6RootScope rootScope, Guid scopeId)
        {
            IEnumerable <DomainEvent> changes = rootScope.GetChanges();

            Assert.IsAssignableFrom <DHCPv6ScopeAddressesAreExhaustedEvent>(changes.ElementAt(index));

            DHCPv6ScopeAddressesAreExhaustedEvent castedEvent = (DHCPv6ScopeAddressesAreExhaustedEvent)changes.ElementAt(index);

            Assert.Equal(castedEvent.EntityId, scopeId);
        }
예제 #3
0
        public async Task Handle_HasNoChildren(Boolean requestedToDeleteChildrenAsWell)
        {
            Random random = new Random();

            Guid id = 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 = id,
                        ResolverInformation = new CreateScopeResolverInformation
                        {
                            Typename = resolverName,
                        }
                    }
                }
            });


            Mock <IDHCPv6StorageEngine> storageMock = new Mock <IDHCPv6StorageEngine>(MockBehavior.Strict);

            storageMock.Setup(x => x.Save(rootScope)).ReturnsAsync(true).Verifiable();

            var command = new DeleteDHCPv6ScopeCommand(id, requestedToDeleteChildrenAsWell);

            var handler = new DeleteDHCPv6ScopeCommandHandler(storageMock.Object, rootScope,
                                                              Mock.Of <ILogger <DeleteDHCPv6ScopeCommandHandler> >());

            Boolean result = await handler.Handle(command, CancellationToken.None);

            Assert.True(result);

            Assert.Single(rootScope.GetChanges());

            storageMock.Verify();
        }
예제 #4
0
        protected static void CheckLeaseActivatedEvent(Int32 index, Guid scopeId, Guid leaseId, DHCPv6RootScope rootScope)
        {
            IEnumerable <DomainEvent> changes = rootScope.GetChanges();
            var @event = changes.ElementAt(index);

            Assert.NotNull(@event);
            Assert.IsAssignableFrom <DHCPv6LeaseActivatedEvent>(@event);

            DHCPv6LeaseActivatedEvent castedEvent = (DHCPv6LeaseActivatedEvent)@event;

            Assert.Equal(scopeId, castedEvent.ScopeId);
            Assert.Equal(leaseId, castedEvent.EntityId);
        }
예제 #5
0
        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);
            }
        }
예제 #6
0
        private void CheckHandeledEvent(Int32 index, DHCPv6Packet requestPacket, DHCPv6Packet result, DHCPv6RootScope rootScope, Guid?scopeId, DHCPv6ReleaseHandledEvent.ReleaseError error)
        {
            IEnumerable <DomainEvent> changes = rootScope.GetChanges();

            Assert.IsAssignableFrom <DHCPv6ReleaseHandledEvent>(changes.ElementAt(index));

            DHCPv6ReleaseHandledEvent handeledEvent = (DHCPv6ReleaseHandledEvent)changes.ElementAt(index);

            Assert.Equal(requestPacket, handeledEvent.Request);
            Assert.Equal(result, handeledEvent.Response);
            Assert.Equal(scopeId, handeledEvent.ScopeId);
            Assert.Equal(error == DHCPv6ReleaseHandledEvent.ReleaseError.NoError, handeledEvent.WasSuccessfullHandled);
            Assert.Equal(error, handeledEvent.Error);
        }
예제 #7
0
        protected static void CheckLeaseReleasedEvent(
            Guid scopeId,
            DHCPv6RootScope rootScope, Guid leaseId, Boolean onlyPrefix)
        {
            IEnumerable <DomainEvent> changes = rootScope.GetChanges();

            Assert.NotNull(changes);

            Assert.IsAssignableFrom <DHCPv6LeaseReleasedEvent>(changes.First());

            DHCPv6LeaseReleasedEvent castedEvent = (DHCPv6LeaseReleasedEvent)changes.First();

            Assert.NotNull(castedEvent);

            Assert.Equal(scopeId, castedEvent.ScopeId);
            Assert.Equal(leaseId, castedEvent.EntityId);
            Assert.Equal(onlyPrefix, castedEvent.OnlyPrefix);
        }
예제 #8
0
        protected static void CheckDHCPv6LeasePrefixAddedEvent(
            Int32 index, UInt32 prefixIaId,
            Guid scopeId, DHCPv6RootScope rootScope,
            DHCPv6Lease lease
            )
        {
            IEnumerable <DomainEvent> changes = rootScope.GetChanges();

            Assert.NotNull(changes);

            Assert.IsAssignableFrom <DHCPv6LeasePrefixAddedEvent>(changes.ElementAt(index));

            DHCPv6LeasePrefixAddedEvent createdEvent = (DHCPv6LeasePrefixAddedEvent)changes.ElementAt(index);

            Assert.NotNull(createdEvent);

            Assert.Equal(scopeId, createdEvent.ScopeId);
            Assert.Equal(lease.PrefixDelegation.NetworkAddress, createdEvent.NetworkAddress);
            Assert.Equal(lease.PrefixDelegation.IdentityAssociation, createdEvent.PrefixAssociationId);
            Assert.Equal(lease.PrefixDelegation.Mask.Identifier, createdEvent.PrefixLength);
            Assert.Equal(lease.PrefixDelegation.IdentityAssociation, prefixIaId);
        }
예제 #9
0
        public void HandleRequest_OnlyPrefix_LeaseFound_LeaseIsActive()
        {
            Random random = new Random();

            UInt32      prefixId             = random.NextUInt32();
            Byte        prefixLength         = (Byte)random.Next(20, 60);
            IPv6Address prefixNetworkAddress = IPv6Address.FromString("fd01::0");

            var packet = GetRequestPacket(random, out IPv6Address leasedAddress, out DUID clientDuid, out UInt32 iaId, false, 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 resolverInformations = GetMockupResolver(packet, out Mock <IScopeResolverManager <DHCPv6Packet, IPv6Address> > scopeResolverMock);

            Guid scopeId = random.NextGuid();
            Guid leaseId = random.NextGuid();

            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           = true,
                    PrefixLength                  = prefixLength,
                    IdentityAssocationIdForPrefix = prefixId,
                    DelegatedNetworkAddress       = prefixNetworkAddress,
                },
                new DHCPv6LeaseActivatedEvent
                {
                    EntityId = leaseId,
                    ScopeId  = scopeId,
                },
            });

            DHCPv6Packet result = rootScope.HandleRequest(packet, GetServerPropertiesResolver());

            CheckPacket(packet, leasedAddress, 0, result, DHCPv6PacketTypes.REPLY, DHCPv6PrefixDelegation.FromValues(prefixNetworkAddress, new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(prefixLength)), prefixId));

            CheckEventAmount(2, rootScope);

            DHCPv6LeasePrefixActvatedEvent @event = rootScope.GetChanges().First() as DHCPv6LeasePrefixActvatedEvent;

            Assert.NotNull(@event);

            Assert.Equal(leaseId, @event.EntityId);
            Assert.Equal(scopeId, @event.ScopeId);

            Assert.Equal(prefixNetworkAddress, @event.NetworkAddress);
            Assert.Equal(prefixLength, @event.PrefixLength);
            Assert.Equal(prefixId, @event.PrefixAssociationId);

            CheckHandeledEvent(1, packet, result, rootScope, scopeId, DHCPv6RequestHandledEvent.RequestErrors.NoError);
        }
예제 #10
0
        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();
        }
예제 #11
0
        protected void CheckEventAmount(int expectedAmount, DHCPv6RootScope rootScope)
        {
            var changes = rootScope.GetChanges();

            Assert.Equal(expectedAmount, changes.Count());
        }