Example #1
0
        protected static void DHCPv4ScopeAddressesAreExhaustedEvent(int index, DHCPv4RootScope rootScope, Guid scopeId)
        {
            IEnumerable <DomainEvent> changes = rootScope.GetChanges();

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

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

            Assert.Equal(castedEvent.EntityId, scopeId);
        }
Example #2
0
        protected static void CheckRevokedEvent(Int32 index, Guid scopeId, Guid leaseId, DHCPv4RootScope rootScope)
        {
            IEnumerable <DomainEvent> changes = rootScope.GetChanges();
            var @event = changes.ElementAt(index);

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

            DHCPv4LeaseRevokedEvent revokedEvent = (DHCPv4LeaseRevokedEvent)@event;

            Assert.Equal(scopeId, revokedEvent.ScopeId);
            Assert.Equal(leaseId, revokedEvent.EntityId);
        }
Example #3
0
        public async Task Handle_HasNoChildren(Boolean requestedToDeleteChildrenAsWell)
        {
            Random random = new Random();

            Guid id = random.NextGuid();

            String resolverName = random.GetAlphanumericString();

            Mock <IScopeResolverManager <DHCPv4Packet, IPv4Address> > scopeResolverMock = new Mock <IScopeResolverManager <DHCPv4Packet, IPv4Address> >();

            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);

            rootScope.Load(new[] {
                new DHCPv4ScopeAddedEvent
                {
                    Instructions = new DHCPv4ScopeCreateInstruction
                    {
                        Id = id,
                        ResolverInformation = new CreateScopeResolverInformation
                        {
                            Typename = resolverName,
                        }
                    }
                }
            });

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

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

            var command = new DeleteDHCPv4ScopeCommand(id, requestedToDeleteChildrenAsWell);

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

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

            Assert.True(result);

            Assert.Single(rootScope.GetChanges());

            storageMock.Verify();
        }
Example #4
0
        private void CheckDeclineEvent(int index, Guid leaseId, IPv4Address address, Guid scopeId, DHCPv4RootScope rootScope)
        {
            IEnumerable <DomainEvent> changes = rootScope.GetChanges();

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

            DHCPv4AddressSuspendedEvent @event = (DHCPv4AddressSuspendedEvent)changes.ElementAt(index);

            Assert.NotNull(@event);
            Assert.Equal(scopeId, @event.ScopeId);
            Assert.Equal(leaseId, @event.EntityId);
            Assert.Equal(address, @event.Address);
            Assert.True((@event.SuspendedTill - DateTime.UtcNow).TotalMinutes >= (5 - 1));
        }
Example #5
0
        public static T TestResult<T>(
        DHCPv4Packet input,
        DHCPv4Packet result,
        Boolean emptyResponseExpected,
        Boolean expectedSuccessfullHandling,
        Guid? scopeId,
        DHCPv4RootScope rootScope) where T : DHCPv4PacketHandledEvent
        {
            var changes = rootScope.GetChanges();
            Assert.Single(changes);

            DomainEvent domainEvent = changes.First();
            Assert.IsAssignableFrom<T>(domainEvent);

            T castedDomainEvents = (T)domainEvent;

            if (scopeId.HasValue == false)
            {
                Assert.False(castedDomainEvents.ScopeId.HasValue);
            }
            else
            {
                Assert.True(castedDomainEvents.ScopeId.HasValue);
                Assert.Equal(scopeId.Value, castedDomainEvents.ScopeId.Value);
            }
            Assert.Equal(input, castedDomainEvents.Request);
            if (emptyResponseExpected == true)
            {
                Assert.Equal(DHCPv4Packet.Empty, result);
                Assert.Equal(DHCPv4Packet.Empty, castedDomainEvents.Response);
            }
            else
            {
                Assert.NotEqual(DHCPv4Packet.Empty, result);
                Assert.NotEqual(DHCPv4Packet.Empty, castedDomainEvents.Response);
                Assert.Equal(result, castedDomainEvents.Response);
            }
            if (expectedSuccessfullHandling == false)
            {
                Assert.False(castedDomainEvents.WasSuccessfullHandled);
            }
            else
            {
                Assert.True(castedDomainEvents.WasSuccessfullHandled);
            }

            return castedDomainEvents;
        }
Example #6
0
        private static void CheckLeaseReleasedEvent(Int32 index,
                                                    Guid scopeId, DHCPv4RootScope rootScope,
                                                    Guid leaseId
                                                    )
        {
            IEnumerable <DomainEvent> changes = rootScope.GetChanges();

            Assert.NotNull(changes);

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

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

            Assert.NotNull(createdEvent);

            Assert.Equal(scopeId, createdEvent.ScopeId);
            Assert.Equal(leaseId, createdEvent.EntityId);
        }
Example #7
0
        protected static void CheckLeaseCreatedEvent(
            Int32 index, byte[] clientMacAdress,
            Guid scopeId, DHCPv4RootScope rootScope,
            IPv4Address expectedAdress, DHCPv4Lease lease,
            Byte[] uniqueIdentifier = null, Boolean checkRenewTimes = true
            )
        {
            IEnumerable <DomainEvent> changes = rootScope.GetChanges();

            Assert.NotNull(changes);

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

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

            Assert.NotNull(createdEvent);

            Assert.Equal(scopeId, createdEvent.ScopeId);
            Assert.Equal(expectedAdress, createdEvent.Address);

            var clientIdentifier = DHCPv4ClientIdentifier.FromOptionData(createdEvent.ClientIdenfier);

            Assert.Equal(clientMacAdress, clientIdentifier.HwAddress);
            Assert.Equal(lease.Id, createdEvent.EntityId);
            if (uniqueIdentifier == null)
            {
                Assert.Null(createdEvent.UniqueIdentifier);
            }
            else
            {
                Assert.Equal(uniqueIdentifier, createdEvent.UniqueIdentifier);
            }

            Assert.Equal(lease.Start, createdEvent.StartedAt);
            Assert.Equal(lease.End, createdEvent.ValidUntil);

            var addressProperties = rootScope.GetScopeById(scopeId).AddressRelatedProperties;

            if (checkRenewTimes == true)
            {
                Assert.Equal(addressProperties.RenewalTime.Value, createdEvent.RenewalTime);
                Assert.Equal(addressProperties.PreferredLifetime.Value, createdEvent.PreferredLifetime);
            }
        }
Example #8
0
        private void CheckHandeledEvent(
            Int32 index, ReleaseError error,
            DHCPv4Packet requestPacket,
            DHCPv4RootScope rootScope)
        {
            IEnumerable <DomainEvent> changes = rootScope.GetChanges();

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

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

            Assert.Equal(requestPacket, handeledEvent.Request);
            Assert.Equal(DHCPv4Packet.Empty, handeledEvent.Response);
            Assert.Equal(error, handeledEvent.Error);
            if (error == ReleaseError.NoError)
            {
                Assert.True(handeledEvent.WasSuccessfullHandled);
            }
            else
            {
                Assert.False(handeledEvent.WasSuccessfullHandled);
            }
        }
Example #9
0
        protected void CheckEventAmount(int expectedAmount, DHCPv4RootScope rootScope)
        {
            var changes = rootScope.GetChanges();

            Assert.Equal(expectedAmount, changes.Count());
        }
Example #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 <DHCPv4Packet, IPv4Address> > scopeResolverMock = new Mock <IScopeResolverManager <DHCPv4Packet, IPv4Address> >();

            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);

            rootScope.Load(new[] {
                new DHCPv4ScopeAddedEvent
                {
                    Instructions = new DHCPv4ScopeCreateInstruction
                    {
                        Id = grantParentId,
                        ResolverInformation = new CreateScopeResolverInformation
                        {
                            Typename = resolverName,
                        },
                        AddressProperties = new DHCPv4ScopeAddressProperties(IPv4Address.FromString("192.168.0.1"), IPv4Address.FromString("192.168.0.20")),
                    }
                },
                new DHCPv4ScopeAddedEvent
                {
                    Instructions = new DHCPv4ScopeCreateInstruction
                    {
                        Id                  = parentId,
                        ParentId            = grantParentId,
                        ResolverInformation = new CreateScopeResolverInformation
                        {
                            Typename = resolverName,
                        },
                        AddressProperties = new DHCPv4ScopeAddressProperties(IPv4Address.FromString("192.168.0.5"), IPv4Address.FromString("192.168.0.8")),
                    }
                },
                new DHCPv4ScopeAddedEvent
                {
                    Instructions = new DHCPv4ScopeCreateInstruction
                    {
                        Id                  = childId,
                        ParentId            = parentId,
                        ResolverInformation = new CreateScopeResolverInformation
                        {
                            Typename = resolverName,
                        },
                        AddressProperties = new DHCPv4ScopeAddressProperties(IPv4Address.FromString("192.168.0.7"), IPv4Address.FromString("192.168.0.7")),
                    }
                },
            });


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

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

            var command = new DeleteDHCPv4ScopeCommand(parentId, requestedToDeleteChildrenAsWell);

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

            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 DHCPv4ScopeDeletedEvent;
                Assert.Equal(childId, firstEvent.EntityId);

                var secondEvent = changes.ElementAt(1) as DHCPv4ScopeDeletedEvent;
                Assert.Equal(parentId, secondEvent.EntityId);
            }
            else
            {
                Assert.Single(changes);
            }

            Assert.Null(rootScope.GetScopeById(parentId));
            Assert.NotNull(rootScope.GetScopeById(grantParentId));

            storageMock.Verify();
        }