Beispiel #1
0
        public void HandleRelease_ScopeNotFound_ByResolver()
        {
            Random random = new Random();

            DHCPv6PacketOption[] options = Array.Empty <DHCPv6PacketOption>();

            var packet = GetRelayedReleasePacket(random, out IPv6Address _, out DUID _, out UInt32 _, true, options);

            GetMockupResolver(DHCPv6Packet.Empty, out Mock <IScopeResolverManager <DHCPv6Packet, IPv6Address> > scopeResolverMock);

            DHCPv6RootScope rootScope = GetRootScope(scopeResolverMock);

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

            Assert.Equal(DHCPv6Packet.Empty, result);

            CheckEventAmount(1, rootScope);
            CheckHandeledEvent(0, packet, result, rootScope, null, DHCPv6ReleaseHandledEvent.ReleaseError.ScopeNotFound);
        }
Beispiel #2
0
        public void HandleRelease_ScopeNotFound_ByAddress()
        {
            Random random = new Random();

            var packet = GetReleasePacket(random, out _, out _, out _, true);

            var resolverInformations = GetMockupResolver(packet, out Mock <IScopeResolverManager <DHCPv6Packet, IPv6Address> > scopeResolverMock);

            Guid scopeId = random.NextGuid();

            DHCPv6RootScope rootScope = GetRootScope(scopeResolverMock);

            rootScope.Load(new List <DomainEvent> {
                new DHCPv6ScopeEvents.DHCPv6ScopeAddedEvent(
                    new DHCPv6ScopeCreateInstruction
                {
                    AddressProperties = new DHCPv6ScopeAddressProperties(
                        IPv6Address.FromString("fe80::10"),
                        IPv6Address.FromString("fe80::ff"),
                        new List <IPv6Address> {
                        IPv6Address.FromString("fe80::12")
                    },
                        preferredLifeTime: TimeSpan.FromDays(0.5),
                        validLifeTime: TimeSpan.FromDays(1),
                        reuseAddressIfPossible: false,
                        addressAllocationStrategy: DHCPv6ScopeAddressProperties.AddressAllocationStrategies.Next),
                    ResolverInformation = resolverInformations,
                    Name = "Testscope",
                    Id   = scopeId,
                }),
            });

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

            Assert.Equal(DHCPv6Packet.Empty, result);

            //CheckErrorPacket(leasedAddress, iaId, result, DHCPv6PrefixDelegation.None,DHCPv6StatusCodes.NoBinding);
            CheckEventAmount(1, rootScope);
            CheckHandeledEvent(0, packet, result, rootScope, null, DHCPv6ReleaseHandledEvent.ReleaseError.ScopeNotFound);
        }
Beispiel #3
0
        public void HandleRelease_NoLeaseFound(Boolean withPrefixDelegation, Boolean isUnicast)
        {
            Random random = new Random();

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

            DHCPv6PacketOption[] options = Array.Empty <DHCPv6PacketOption>();
            if (withPrefixDelegation == true)
            {
                options = new DHCPv6PacketOption[] {
                    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 packet = isUnicast == true?
                         GetReleasePacket(random, out IPv6Address leasedAddress, out _, out UInt32 iaId, true, options) :
                             GetRelayedReleasePacket(random, out leasedAddress, out _, out iaId, true, options);

            var resolverInformations = GetMockupResolver(packet, out Mock <IScopeResolverManager <DHCPv6Packet, IPv6Address> > scopeResolverMock);

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

            DateTime leaseCreatedAt = DateTime.UtcNow;

            DHCPv6RootScope rootScope = GetRootScope(scopeResolverMock);

            rootScope.Load(new List <DomainEvent> {
                new DHCPv6ScopeEvents.DHCPv6ScopeAddedEvent(
                    new DHCPv6ScopeCreateInstruction
                {
                    AddressProperties = new DHCPv6ScopeAddressProperties(
                        IPv6Address.FromString("fe80::01"),
                        IPv6Address.FromString("fe80::ff"),
                        new List <IPv6Address> {
                        IPv6Address.FromString("fe80::02")
                    },
                        preferredLifeTime: TimeSpan.FromDays(0.5),
                        validLifeTime: TimeSpan.FromDays(1),
                        reuseAddressIfPossible: false,
                        prefixDelgationInfo: DHCPv6PrefixDelgationInfo.FromValues(IPv6Address.FromString("2140:1::0"), new IPv6SubnetMaskIdentifier(32), new IPv6SubnetMaskIdentifier(prefixLength)),
                        addressAllocationStrategy: DHCPv6ScopeAddressProperties.AddressAllocationStrategies.Next),
                    ResolverInformation = resolverInformations,
                    Name = "Testscope",
                    Id   = scopeId,
                }),
                new DHCPv6LeaseCreatedEvent
                {
                    EntityId                      = leaseId,
                    Address                       = leasedAddress,
                    ClientIdentifier              = new UUIDDUID(random.NextGuid()),
                    IdentityAssocationId          = iaId,
                    ScopeId                       = scopeId,
                    StartedAt                     = leaseCreatedAt,
                    ValidUntil                    = DateTime.UtcNow.AddDays(1),
                    HasPrefixDelegation           = withPrefixDelegation,
                    PrefixLength                  = prefixLength,
                    IdentityAssocationIdForPrefix = prefixId,
                    DelegatedNetworkAddress       = prefixNetworkAddress,
                },
                new DHCPv6LeaseActivatedEvent
                {
                    EntityId = leaseId,
                    ScopeId  = scopeId,
                }
            });

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

            CheckErrorPacket(packet, leasedAddress, iaId, result, withPrefixDelegation == false ? DHCPv6PrefixDelegation.None : DHCPv6PrefixDelegation.FromValues(prefixNetworkAddress, new IPv6SubnetMask(new IPv6SubnetMaskIdentifier(prefixLength)), prefixId), DHCPv6StatusCodes.NoBinding);

            DHCPv6Lease lease = rootScope.GetScopeById(scopeId).Leases.GetLeaseById(leaseId);

            Assert.Equal(LeaseStates.Active, lease.State);

            CheckEventAmount(1, rootScope);
            CheckHandeledEvent(0, packet, result, rootScope, scopeId, DHCPv6ReleaseHandledEvent.ReleaseError.NoLeaseFound);
        }
Beispiel #4
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());
            });
        }