Exemple #1
0
        public void TestNotifcationTriggerForSolicitMessages(Boolean leaseFoud, Boolean reuse, Boolean hasUniqueIdentifierValue, Boolean prefixRequest, Boolean hadPrefix, Boolean rapitCommitEnabled, Boolean shouldHaveOldBinding, Boolean shouldHaveNewBinding)
        {
            Random random = new Random();
            IPv6HeaderInformation headerInformation =
                new IPv6HeaderInformation(IPv6Address.FromString("fe80::1"), IPv6Address.FromString("fe80::2"));

            DUID clientDuid = new UUIDDUID(random.NextGuid());

            UInt32      iaId          = random.NextUInt32();
            IPv6Address leasedAddress = IPv6Address.FromString("fe80::5");

            UInt32 prefixIaId           = random.NextUInt32();
            Byte   exisitngPrefixLength = 62;
            DHCPv6PrefixDelegation existingDelegation = DHCPv6PrefixDelegation.FromValues(
                IPv6Address.FromString("2a4c::0"), new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(exisitngPrefixLength)), prefixIaId);

            var packetOptions = new List <DHCPv6PacketOption>
            {
                new DHCPv6PacketIdentifierOption(DHCPv6PacketOptionTypes.ClientIdentifier, clientDuid),
                new DHCPv6PacketIdentityAssociationNonTemporaryAddressesOption(iaId, TimeSpan.Zero, TimeSpan.Zero, Array.Empty <DHCPv6PacketSuboption>()),
                new DHCPv6PacketTrueOption(DHCPv6PacketOptionTypes.RapitCommit),
            };

            if (prefixRequest == true)
            {
                packetOptions.Add(
                    new DHCPv6PacketIdentityAssociationPrefixDelegationOption(prefixIaId, TimeSpan.Zero, TimeSpan.Zero, Array.Empty <DHCPv6PacketSuboption>()));
            }


            DHCPv6Packet innerPacket = DHCPv6Packet.AsInner(random.NextUInt16(),
                                                            DHCPv6PacketTypes.Solicit, packetOptions);

            DHCPv6Packet packet = DHCPv6RelayPacket.AsOuterRelay(headerInformation, true, 1, random.GetIPv6Address(), random.GetIPv6Address(),
                                                                 new DHCPv6PacketOption[] { new DHCPv6PacketByteArrayOption(DHCPv6PacketOptionTypes.InterfaceId, random.NextBytes(10)) }, innerPacket);

            Byte[] uniqueIdentifierValue = random.NextBytes(20);

            CreateScopeResolverInformation resolverInformations = new CreateScopeResolverInformation {
                Typename = "something"
            };

            Mock <IScopeResolver <DHCPv6Packet, IPv6Address> > resolverMock = new Mock <IScopeResolver <DHCPv6Packet, IPv6Address> >(MockBehavior.Strict);

            resolverMock.Setup(x => x.PacketMeetsCondition(packet)).Returns(true);
            resolverMock.SetupGet(x => x.HasUniqueIdentifier).Returns(hasUniqueIdentifierValue);
            if (hasUniqueIdentifierValue == true)
            {
                resolverMock.Setup(x => x.GetUniqueIdentifier(packet)).Returns(uniqueIdentifierValue);
            }

            var scopeResolverMock = new Mock <IScopeResolverManager <DHCPv6Packet, IPv6Address> >(MockBehavior.Strict);

            scopeResolverMock.Setup(x => x.InitializeResolver(resolverInformations)).Returns(resolverMock.Object);

            Mock <ILoggerFactory> factoryMock = new Mock <ILoggerFactory>(MockBehavior.Strict);

            factoryMock.Setup(x => x.CreateLogger(It.IsAny <String>())).Returns(Mock.Of <ILogger <DHCPv6RootScope> >());

            var rootScope = new DHCPv6RootScope(Guid.NewGuid(), scopeResolverMock.Object, factoryMock.Object);

            Guid scopeId = random.NextGuid();
            var  expetecNewLeaseAddress = IPv6Address.FromString("fe80::0");
            var  events = 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),
                        reuseAddressIfPossible: reuse,
                        rapitCommitEnabled: rapitCommitEnabled,
                        prefixDelgationInfo: DHCPv6PrefixDelgationInfo.FromValues(existingDelegation.NetworkAddress, new IPv6SubnetMaskIdentifier(40), new IPv6SubnetMaskIdentifier(64)),
                        addressAllocationStrategy: DHCPv6ScopeAddressProperties.AddressAllocationStrategies.Next),
                    ResolverInformation = resolverInformations,
                    Name = "Testscope",
                    Id   = scopeId,
                })
            };

            if (leaseFoud == true || hasUniqueIdentifierValue == true)
            {
                Guid leaseId = random.NextGuid();
                events.Add(new DHCPv6LeaseCreatedEvent
                {
                    EntityId                      = leaseId,
                    Address                       = leasedAddress,
                    ClientIdentifier              = hasUniqueIdentifierValue == false ? clientDuid : new UUIDDUID(random.NextGuid()),
                    IdentityAssocationId          = iaId,
                    ScopeId                       = scopeId,
                    HasPrefixDelegation           = hadPrefix,
                    UniqueIdentiifer              = hasUniqueIdentifierValue == true ? uniqueIdentifierValue : Array.Empty <Byte>(),
                    PrefixLength                  = hadPrefix == true ? exisitngPrefixLength : (Byte)0,
                    IdentityAssocationIdForPrefix = hadPrefix == true ? prefixIaId : (UInt32)0,
                    DelegatedNetworkAddress       = hadPrefix == true ? existingDelegation.NetworkAddress : IPv6Address.Empty,
                    StartedAt                     = DateTime.UtcNow.AddDays(-1),
                    ValidUntil                    = DateTime.UtcNow.AddDays(1),
                });
                events.Add(new DHCPv6LeaseActivatedEvent
                {
                    EntityId = leaseId,
                    ScopeId  = scopeId,
                });
            }

            rootScope.Load(events);

            var serverPropertiesResolverMock = new Mock <IDHCPv6ServerPropertiesResolver>(MockBehavior.Strict);

            serverPropertiesResolverMock.Setup(x => x.GetServerDuid()).Returns(new UUIDDUID(Guid.NewGuid()));

            var _ = rootScope.HandleSolicit(packet, serverPropertiesResolverMock.Object);

            if (shouldHaveNewBinding == false && shouldHaveOldBinding == false)
            {
                CheckEmptyTrigger(rootScope);
            }
            else
            {
                var trigger = CheckTrigger <PrefixEdgeRouterBindingUpdatedTrigger>(rootScope);

                Assert.Equal(scopeId, trigger.ScopeId);

                if (shouldHaveNewBinding == true)
                {
                    Assert.NotNull(trigger.NewBinding);

                    Assert.Equal(64, trigger.NewBinding.Mask.Identifier);
                    if ((hasUniqueIdentifierValue || leaseFoud) == true && reuse == true)
                    {
                        Assert.Equal(leasedAddress, trigger.NewBinding.Host);
                    }
                    else
                    {
                        Assert.Equal(expetecNewLeaseAddress, trigger.NewBinding.Host);
                    }
                }
                else
                {
                    Assert.Null(trigger.NewBinding);
                }

                if (shouldHaveOldBinding == true)
                {
                    Assert.NotNull(trigger.OldBinding);

                    Assert.Equal(exisitngPrefixLength, trigger.OldBinding.Mask.Identifier);
                    Assert.Equal(existingDelegation.NetworkAddress, trigger.OldBinding.Prefix);
                    Assert.Equal(leasedAddress, trigger.OldBinding.Host);
                }
                else
                {
                    Assert.Null(trigger.OldBinding);
                }
            }
        }
Exemple #2
0
        public async Task SaveAndHydrateRootScope_AddAndRemoveLeases()
        {
            Random random = new Random();

            await ExecuteWithStreamErase(random, async (eventStore) =>
            {
                Mock <IDHCPv6ServerPropertiesResolver> propertyResolverMock;
                DHCPv6StorageEngine engine;
                PrepareEngine(random, eventStore, out propertyResolverMock, out engine);

                DHCPv6RootScope initialRootScope = await engine.GetRootScope();

                Guid rootScopeId = random.NextGuid();

                initialRootScope.AddScope(new DHCPv6ScopeCreateInstruction
                {
                    AddressProperties = new DHCPv6ScopeAddressProperties(
                        IPv6Address.FromString("fe80::0"),
                        IPv6Address.FromString("fe80::ff"),
                        new List <IPv6Address> {
                        IPv6Address.FromString("fe80::1")
                    },
                        preferredLifeTime: TimeSpan.FromDays(0.5),
                        validLifeTime: TimeSpan.FromDays(1),
                        rapitCommitEnabled: true,
                        informsAreAllowd: true,
                        supportDirectUnicast: false,
                        reuseAddressIfPossible: false,
                        acceptDecline: true,
                        t1: DHCPv6TimeScale.FromDouble(0.6),
                        t2: DHCPv6TimeScale.FromDouble(0.8),
                        addressAllocationStrategy: DHCPv6ScopeAddressProperties.AddressAllocationStrategies.Next),
                    ScopeProperties     = DHCPv6ScopeProperties.Empty,
                    ResolverInformation = new CreateScopeResolverInformation
                    {
                        Typename            = nameof(DHCPv6RemoteIdentifierEnterpriseNumberResolver),
                        PropertiesAndValues = new Dictionary <String, String>
                        {
                            { nameof(DHCPv6RemoteIdentifierEnterpriseNumberResolver.EnterpriseNumber), random.NextUInt16().ToString() },
                            { nameof(DHCPv6RemoteIdentifierEnterpriseNumberResolver.RelayAgentIndex), 0.ToString() },
                        }
                    },
                    Name = "Testscope",
                    Id   = rootScopeId,
                });

                var clientDuid = new UUIDDUID(random.NextGuid());

                DHCPv6Packet solicitPacket = DHCPv6Packet.AsInner(14, DHCPv6PacketTypes.Solicit, new List <DHCPv6PacketOption>
                {
                    new DHCPv6PacketTrueOption(DHCPv6PacketOptionTypes.RapitCommit),
                    new DHCPv6PacketIdentifierOption(DHCPv6PacketOptionTypes.ClientIdentifier, clientDuid),
                    new DHCPv6PacketIdentityAssociationNonTemporaryAddressesOption(15, TimeSpan.Zero, TimeSpan.Zero, new List <DHCPv6PacketSuboption>()),
                });

                DHCPv6RelayPacket outerSolicitPacket = DHCPv6RelayPacket.AsOuterRelay(new IPv6HeaderInformation(random.GetIPv6Address(), random.GetIPv6Address()),
                                                                                      true, 0, IPv6Address.FromString("fe80::2"), IPv6Address.FromString("fe80::2"), new DHCPv6PacketOption[]
                {
                    new DHCPv6PacketRemoteIdentifierOption(random.NextUInt16(), new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }),
                }, solicitPacket);


                initialRootScope.HandleSolicit(outerSolicitPacket, propertyResolverMock.Object);

                var firstLease = initialRootScope.GetRootScopes().First().Leases.GetAllLeases().First();

                await engine.Save(initialRootScope);

                initialRootScope = await engine.GetRootScope();

                DHCPv6Packet releasePacket = DHCPv6Packet.AsInner(14, DHCPv6PacketTypes.RELEASE, new List <DHCPv6PacketOption>
                {
                    new DHCPv6PacketIdentifierOption(DHCPv6PacketOptionTypes.ClientIdentifier, clientDuid),
                    new DHCPv6PacketIdentityAssociationNonTemporaryAddressesOption(15, TimeSpan.FromSeconds(random.Next()), TimeSpan.FromSeconds(random.Next()), new DHCPv6PacketSuboption[]
                    {
                        new DHCPv6PacketIdentityAssociationAddressSuboption(firstLease.Address, TimeSpan.FromSeconds(random.Next()), TimeSpan.FromSeconds(random.Next()), Array.Empty <DHCPv6PacketSuboption>())
                    })
                });

                DHCPv6RelayPacket outerreleasePacket = DHCPv6RelayPacket.AsOuterRelay(new IPv6HeaderInformation(random.GetIPv6Address(), random.GetIPv6Address()),
                                                                                      true, 0, IPv6Address.FromString("fe80::2"), IPv6Address.FromString("fe80::2"), new DHCPv6PacketOption[]
                {
                    new DHCPv6PacketRemoteIdentifierOption(random.NextUInt16(), new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }),
                }, releasePacket);

                firstLease = initialRootScope.GetRootScopes().First().Leases.GetAllLeases().First();

                initialRootScope.HandleRelease(outerreleasePacket, propertyResolverMock.Object);

                Assert.Equal(LeaseStates.Released, firstLease.State);
                Assert.Empty(initialRootScope.GetRootScopes().First().Leases.GetAllLeases());

                await engine.Save(initialRootScope);

                await Task.Delay(1000);

                initialRootScope.DropUnusedLeasesOlderThan(DateTime.Now);

                Assert.Empty(initialRootScope.GetRootScopes().First().Leases.GetAllLeases());

                await engine.Save(initialRootScope);

                var rehydratedRootScope = await engine.GetRootScope();

                Assert.Empty(rehydratedRootScope.GetRootScopes().First().Leases.GetAllLeases());
            });
        }