Ejemplo n.º 1
0
        private void GenerateScopeTree(DHCPv4Scope scope, ICollection <DHCPv4ScopeTreeViewItem> parentChildren)
        {
            List <DHCPv4ScopeTreeViewItem> childItems = new List <DHCPv4ScopeTreeViewItem>();
            var node = new DHCPv4ScopeTreeViewItem
            {
                Id                  = scope.Id,
                StartAddress        = scope.AddressRelatedProperties.Start.ToString(),
                EndAddress          = scope.AddressRelatedProperties.End.ToString(),
                Name                = scope.Name,
                ChildScopes         = childItems,
                ExcludedAddresses   = scope.AddressRelatedProperties.ExcludedAddresses.Select(x => x.ToString()).ToArray(),
                SubnetMask          = scope.AddressRelatedProperties.Mask == null ? new Byte?() : (Byte)scope.AddressRelatedProperties.Mask.GetSlashNotation(),
                FirstGatewayAddress = (scope.Properties?.Properties ?? Array.Empty <DHCPv4ScopeProperty>()).OfType <DHCPv4AddressListScopeProperty>().Where(x => x.OptionIdentifier == (Byte)DHCPv4OptionTypes.Router).Select(x => x.Addresses.First().ToString()).FirstOrDefault(),
            };

            parentChildren.Add(node);

            if (scope.GetChildScopes().Any())
            {
                foreach (var item in scope.GetChildScopes())
                {
                    GenerateScopeTree(item, childItems);
                }
            }
        }
Ejemplo n.º 2
0
        private void CheckTreeItem(DHCPv4Scope item, DHCPv4ScopeTreeViewItem viewItem)
        {
            Assert.Equal(item.Name, viewItem.Name);
            Assert.Equal(item.Id, viewItem.Id);
            Assert.Equal(item.AddressRelatedProperties.Start.ToString(), viewItem.StartAddress);
            Assert.Equal(item.AddressRelatedProperties.End.ToString(), viewItem.EndAddress);
            Assert.Equal(item.AddressRelatedProperties.ExcludedAddresses.Select(x => x.ToString()).ToArray(), viewItem.ExcludedAddresses);
            Assert.Equal((item.Properties?.Properties ?? Array.Empty <DHCPv4ScopeProperty>()).Where(x => x.OptionIdentifier == (Byte)DHCPv4OptionTypes.Router).Cast <DHCPv4AddressListScopeProperty>().Select(x => x.Addresses.First().ToString()).FirstOrDefault(), viewItem.FirstGatewayAddress);

            if (item.GetChildScopes().Any() == true)
            {
                Assert.Equal(item.GetChildScopes().Count(), viewItem.ChildScopes.Count());
                Int32 index = 0;
                foreach (var childScope in item.GetChildScopes())
                {
                    var childViewItem = viewItem.ChildScopes.ElementAt(index);
                    CheckTreeItem(childScope, childViewItem);

                    index++;
                }
            }
            else
            {
                Assert.Empty(viewItem.ChildScopes);
            }
        }
Ejemplo n.º 3
0
        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);
            }
        }
Ejemplo n.º 4
0
        protected static DHCPv4Lease CheckLease(
            Int32 index, Int32 expectedAmount, IPv4Address expectedAdress,
            Guid scopeId, DHCPv4RootScope rootScope, DateTime expectedCreationData, Boolean checkExpires = true,
            Byte[] uniqueIdentifier = null, Boolean shouldBePending = true)
        {
            DHCPv4Scope scope  = rootScope.GetScopeById(scopeId);
            var         leases = scope.Leases.GetAllLeases();

            Assert.Equal(expectedAmount, leases.Count());

            DHCPv4Lease lease = leases.ElementAt(index);

            Assert.NotNull(lease);
            Assert.Equal(expectedAdress, lease.Address);
            if (checkExpires == true)
            {
                Int32 expiresInMinutes = (Int32)(lease.End - DateTime.UtcNow).TotalMinutes;
                Assert.True(expiresInMinutes >= 60 * 24 - 4 && expiresInMinutes <= 60 * 24);
            }

            Assert.True((expectedCreationData - lease.Start).TotalMinutes < 2);
            Assert.Equal(shouldBePending, lease.IsPending());
            if (uniqueIdentifier == null)
            {
                Assert.Empty(lease.UniqueIdentifier);
            }
            else
            {
                Assert.NotNull(lease.UniqueIdentifier);
                Assert.Equal(uniqueIdentifier, lease.UniqueIdentifier);
            }

            return(lease);
        }
Ejemplo n.º 5
0
        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);
            }
        }
Ejemplo n.º 6
0
        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);
            }
        }
Ejemplo n.º 7
0
        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));
        }
Ejemplo n.º 8
0
 private DHCPv4LeaseOverview GetLeaseOverview(DHCPv4Lease lease, DHCPv4Scope scope) => new DHCPv4LeaseOverview
 {
     Address          = lease.Address.ToString(),
     MacAddress       = lease.Identifier.HwAddress,
     Started          = lease.Start,
     ExpectedEnd      = lease.End,
     Id               = lease.Id,
     UniqueIdentifier = lease.UniqueIdentifier,
     State            = lease.State,
     Scope            = new DHCPv4ScopeOverview
     {
         Id   = scope.Id,
         Name = scope.Name,
     }
 };
Ejemplo n.º 9
0
        public void DHCPv4Scope_GetChildScopes()
        {
            Random random = new Random();

            GenerateScopeTree(random, out Dictionary <Guid, List <Guid> > directChildRelations, out Dictionary <Guid, List <Guid> > allChildRelations, out List <DomainEvent> events);

            DHCPv4RootScope rootScope = GetRootScope();

            rootScope.Load(events);

            foreach (var item in directChildRelations)
            {
                DHCPv4Scope scope = rootScope.GetScopeById(item.Key);
                IEnumerable <DHCPv4Scope> childScopes = scope.GetChildScopes();

                List <Guid> childScopesId = childScopes.Select(x => x.Id).ToList();

                Assert.Equal(item.Value.OrderBy(x => x), childScopesId.OrderBy(x => x));
            }
        }
Ejemplo n.º 10
0
        public void DHCPv4Scope_GetChildIds()
        {
            Random random = new Random();

            GenerateScopeTree(random, out Dictionary <Guid, List <Guid> > directChildRelations, out Dictionary <Guid, List <Guid> > allChildRelations, out List <DomainEvent> events);

            DHCPv4RootScope rootScope = GetRootScope();

            rootScope.Load(events);

            foreach (var item in directChildRelations)
            {
                DHCPv4Scope        scope           = rootScope.GetScopeById(item.Key);
                IEnumerable <Guid> actualDirectIds = scope.GetChildIds(true);

                Assert.Equal(item.Value.OrderBy(x => x), actualDirectIds.OrderBy(x => x));

                IEnumerable <Guid> allChildIds = scope.GetChildIds(false);

                Assert.Equal(allChildRelations[item.Key].OrderBy(x => x), allChildIds.OrderBy(x => x));
            }
        }
Ejemplo n.º 11
0
        private void GetAllLesesRecursivly(ICollection <DHCPv4LeaseOverview> collection, DHCPv4Scope scope)
        {
            var items = scope.Leases.GetAllLeases().Select(x => GetLeaseOverview(x, scope)).ToList();

            foreach (var item in items)
            {
                collection.Add(item);
            }

            var children = scope.GetChildScopes();

            if (children.Any() == false)
            {
                return;
            }

            foreach (var item in children)
            {
                GetAllLesesRecursivly(collection, item);
            }
        }
Ejemplo n.º 12
0
        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);
            }
        }
Ejemplo n.º 13
0
        public void DHCPv4Scope_ScopePropertiesInherientce()
        {
            Random random = new Random();

            Byte onylGrandParentOptionIdentifier   = 47;
            Byte onlyParentOptionIdentifier        = 55;
            Byte onlyChildOptionIdentifier         = 90;
            Byte overridenByParentOptionIdentifier = 100;
            Byte overridenByChildOptionIdentifier  = 110;

            Dictionary <Byte, IPv4Address> inputs = new Dictionary <byte, IPv4Address>
            {
                { onylGrandParentOptionIdentifier, random.GetIPv4Address() },
                { onlyParentOptionIdentifier, random.GetIPv4Address() },
                { onlyChildOptionIdentifier, random.GetIPv4Address() },
                { overridenByParentOptionIdentifier, random.GetIPv4Address() },
                { overridenByChildOptionIdentifier, random.GetIPv4Address() },
            };

            DHCPv4ScopeProperties grantParentProperties = new DHCPv4ScopeProperties(
                new DHCPv4AddressScopeProperty(onylGrandParentOptionIdentifier, inputs[onylGrandParentOptionIdentifier]),
                new DHCPv4AddressScopeProperty(overridenByParentOptionIdentifier, random.GetIPv4Address()),
                new DHCPv4AddressScopeProperty(overridenByChildOptionIdentifier, random.GetIPv4Address())
                );

            DHCPv4ScopeProperties parentProperties = new DHCPv4ScopeProperties(
                new DHCPv4AddressScopeProperty(onlyParentOptionIdentifier, inputs[onlyParentOptionIdentifier]),
                new DHCPv4AddressScopeProperty(overridenByParentOptionIdentifier, inputs[overridenByParentOptionIdentifier])
                );

            DHCPv4ScopeProperties childProperties = new DHCPv4ScopeProperties(
                new DHCPv4AddressScopeProperty(onlyChildOptionIdentifier, inputs[onlyChildOptionIdentifier]),
                new DHCPv4AddressScopeProperty(overridenByChildOptionIdentifier, inputs[overridenByChildOptionIdentifier])
                );

            DHCPv4ScopeProperties expectedProperties = new DHCPv4ScopeProperties(
                new DHCPv4AddressScopeProperty(onylGrandParentOptionIdentifier, inputs[onylGrandParentOptionIdentifier]),
                new DHCPv4AddressScopeProperty(onlyParentOptionIdentifier, inputs[onlyParentOptionIdentifier]),
                new DHCPv4AddressScopeProperty(onlyChildOptionIdentifier, inputs[onlyChildOptionIdentifier]),
                new DHCPv4AddressScopeProperty(overridenByParentOptionIdentifier, inputs[overridenByParentOptionIdentifier]),
                new DHCPv4AddressScopeProperty(overridenByChildOptionIdentifier, inputs[overridenByChildOptionIdentifier])
                );

            Guid grantParentId = Guid.NewGuid();
            Guid parentId      = Guid.NewGuid();
            Guid childId       = Guid.NewGuid();

            List <DomainEvent> events = new List <DomainEvent>
            {
                new DHCPv4ScopeAddedEvent(new DHCPv4ScopeCreateInstruction
                {
                    Id = grantParentId,
                    ScopeProperties = grantParentProperties,
                }),
                new DHCPv4ScopeAddedEvent(new DHCPv4ScopeCreateInstruction
                {
                    Id              = parentId,
                    ParentId        = grantParentId,
                    ScopeProperties = parentProperties,
                }),
                new DHCPv4ScopeAddedEvent(new DHCPv4ScopeCreateInstruction
                {
                    Id              = childId,
                    ParentId        = parentId,
                    ScopeProperties = childProperties,
                }),
            };

            DHCPv4RootScope rootScope = GetRootScope();

            rootScope.Load(events);

            DHCPv4Scope           scope            = rootScope.GetScopeById(childId);
            DHCPv4ScopeProperties actualProperties = scope.GetScopeProperties();

            Assert.Equal(expectedProperties, actualProperties);
        }
Ejemplo n.º 14
0
        public void DHCPv4Scope_AddressPropertiesInherientce()
        {
            Random random = new Random();

            for (int i = 0; i < 100; i++)
            {
                IPv4Address        grantParentStart             = random.GetIPv4Address();
                IPv4Address        grantParentEnd               = random.GetIPv4AddressGreaterThan(grantParentStart);
                List <IPv4Address> grantParentExcludedAddresses = random.GetIPv4AddressesBetween(grantParentStart, grantParentEnd);

                TimeSpan grantParentValidLifeTime                     = TimeSpan.FromMinutes(random.Next());
                TimeSpan grantParentPrefferedValidLifeTime            = TimeSpan.FromMinutes(random.Next());
                TimeSpan grantParentRenewalTimePrefferedValidLifeTime = TimeSpan.FromMinutes(random.Next());

                Boolean grantParentReuseAddressIfPossible = random.NextBoolean();
                DHCPv4ScopeAddressProperties.AddressAllocationStrategies grantParentAllocationStrategy = DHCPv4ScopeAddressProperties.AddressAllocationStrategies.Next;

                Boolean grantParentSupportDirectUnicast = random.NextBoolean();
                Boolean grantParentAcceptDecline        = random.NextBoolean();
                Boolean grantParentInformsAreAllowd     = random.NextBoolean();

                Byte grantParentSubnetMask = (Byte)random.Next(18, 25);

                DHCPv4ScopeAddressProperties grantParentProperties = new DHCPv4ScopeAddressProperties(
                    grantParentStart, grantParentEnd, grantParentExcludedAddresses,
                    grantParentRenewalTimePrefferedValidLifeTime, grantParentPrefferedValidLifeTime, grantParentValidLifeTime,
                    grantParentSubnetMask,
                    grantParentReuseAddressIfPossible, grantParentAllocationStrategy,
                    grantParentInformsAreAllowd, grantParentAcceptDecline, grantParentInformsAreAllowd);

                IPv4Address        parentStart             = random.GetIPv4Address();
                IPv4Address        parentEnd               = random.GetIPv4AddressGreaterThan(parentStart);
                List <IPv4Address> parentExcludedAddresses = random.GetIPv4AddressesBetween(parentStart, parentEnd);

                TimeSpan?parentValidLifeTime                     = null;
                TimeSpan?parentPrefferedValidLifeTime            = null;
                TimeSpan?parentRenewalTimePrefferedValidLifeTime = null;

                Byte?parentSubnetMask = null;

                Boolean?parentReuseAddressIfPossible = null;
                DHCPv4ScopeAddressProperties.AddressAllocationStrategies?parentAllocationStrategy = null;

                Boolean?parentSupportDirectUnicast = null;
                Boolean?parentAcceptDecline        = null;
                Boolean?parentInformsAreAllowd     = null;

                if (random.NextBoolean() == true)
                {
                    parentValidLifeTime = TimeSpan.FromMinutes(random.Next());
                }
                if (random.NextBoolean() == true)
                {
                    parentPrefferedValidLifeTime = TimeSpan.FromMinutes(random.Next());
                }
                if (random.NextBoolean() == true)
                {
                    parentRenewalTimePrefferedValidLifeTime = TimeSpan.FromMinutes(random.Next());
                }
                if (random.NextBoolean() == true)
                {
                    parentSubnetMask = (Byte)random.Next(18, 25);
                }
                if (random.NextBoolean() == true)
                {
                    parentReuseAddressIfPossible = random.NextBoolean();
                }
                if (random.NextBoolean() == true)
                {
                    parentAllocationStrategy = DHCPv4ScopeAddressProperties.AddressAllocationStrategies.Random;
                }
                if (random.NextBoolean() == true)
                {
                    parentSupportDirectUnicast = random.NextBoolean();
                }
                if (random.NextBoolean() == true)
                {
                    parentAcceptDecline = random.NextBoolean();
                }
                if (random.NextBoolean() == true)
                {
                    parentInformsAreAllowd = random.NextBoolean();
                }

                DHCPv4ScopeAddressProperties parentProperties = new DHCPv4ScopeAddressProperties(
                    parentStart, parentEnd, parentExcludedAddresses,
                    parentRenewalTimePrefferedValidLifeTime, parentPrefferedValidLifeTime, parentValidLifeTime,
                    parentSubnetMask,
                    parentReuseAddressIfPossible, parentAllocationStrategy,
                    parentInformsAreAllowd, parentAcceptDecline, parentInformsAreAllowd);

                IPv4Address        childStart             = random.GetIPv4Address();
                IPv4Address        childEnd               = random.GetIPv4AddressGreaterThan(childStart);
                List <IPv4Address> childExcludedAddresses = random.GetIPv4AddressesBetween(childStart, childEnd);

                TimeSpan?childValidLifeTime                     = null;
                TimeSpan?childPrefferedValidLifeTime            = null;
                TimeSpan?childRenewalTimePrefferedValidLifeTime = null;

                Byte?childSubnetMask = null;


                Boolean?childReuseAddressIfPossible = null;
                DHCPv4ScopeAddressProperties.AddressAllocationStrategies?childAllocationStrategy = null;

                Boolean?childSupportDirectUnicast = random.NextDouble() > 0.5;
                Boolean?childAcceptDecline        = random.NextDouble() > 0.5;
                Boolean?childInformsAreAllowd     = random.NextDouble() > 0.5;

                if (random.NextBoolean() == true)
                {
                    childValidLifeTime = TimeSpan.FromMinutes(random.Next());
                }
                if (random.NextBoolean() == true)
                {
                    childPrefferedValidLifeTime = TimeSpan.FromMinutes(random.Next());
                }
                if (random.NextBoolean() == true)
                {
                    childRenewalTimePrefferedValidLifeTime = TimeSpan.FromMinutes(random.Next());
                }
                if (random.NextBoolean() == true)
                {
                    childSubnetMask = (Byte)random.Next(18, 25);
                }
                if (random.NextBoolean() == true)
                {
                    childReuseAddressIfPossible = random.NextBoolean();
                }
                if (random.NextBoolean() == true)
                {
                    childAllocationStrategy = DHCPv4ScopeAddressProperties.AddressAllocationStrategies.Random;
                }
                if (random.NextBoolean() == true)
                {
                    childSupportDirectUnicast = random.NextBoolean();
                }
                if (random.NextBoolean() == true)
                {
                    childAcceptDecline = random.NextBoolean();
                }
                if (random.NextBoolean() == true)
                {
                    childInformsAreAllowd = random.NextBoolean();
                }

                DHCPv4ScopeAddressProperties childProperties = new DHCPv4ScopeAddressProperties(
                    childStart, childEnd, childExcludedAddresses,
                    childRenewalTimePrefferedValidLifeTime, childPrefferedValidLifeTime, childValidLifeTime,
                    childSubnetMask,
                    childReuseAddressIfPossible, childAllocationStrategy,
                    childSupportDirectUnicast, childAcceptDecline, childInformsAreAllowd);

                Guid grantParentId = Guid.NewGuid();
                Guid parentId      = Guid.NewGuid();
                Guid childId       = Guid.NewGuid();

                List <DomainEvent> events = new List <DomainEvent>
                {
                    new DHCPv4ScopeAddedEvent(new DHCPv4ScopeCreateInstruction
                    {
                        Id = grantParentId,
                        AddressProperties = grantParentProperties,
                    }),
                    new DHCPv4ScopeAddedEvent(new DHCPv4ScopeCreateInstruction
                    {
                        Id                = parentId,
                        ParentId          = grantParentId,
                        AddressProperties = parentProperties,
                    }),
                    new DHCPv4ScopeAddedEvent(new DHCPv4ScopeCreateInstruction
                    {
                        Id                = childId,
                        ParentId          = parentId,
                        AddressProperties = childProperties,
                    }),
                };

                DHCPv4RootScope rootScope = GetRootScope();
                rootScope.Load(events);

                DHCPv4Scope scope = rootScope.GetScopeById(childId);
                DHCPv4ScopeAddressProperties actualProperties = scope.GetAddressProperties();

                DHCPv4ScopeAddressProperties expectedProperties = new DHCPv4ScopeAddressProperties(
                    childStart, childEnd, grantParentExcludedAddresses.Union(parentExcludedAddresses).Union(childExcludedAddresses).Where(x => x.IsBetween(childStart, childEnd)),
                    childRenewalTimePrefferedValidLifeTime.HasValue == true ? childRenewalTimePrefferedValidLifeTime.Value : (parentRenewalTimePrefferedValidLifeTime.HasValue == true ? parentRenewalTimePrefferedValidLifeTime.Value : grantParentRenewalTimePrefferedValidLifeTime),
                    childPrefferedValidLifeTime.HasValue == true ? childPrefferedValidLifeTime.Value : (parentPrefferedValidLifeTime.HasValue == true ? parentPrefferedValidLifeTime.Value : grantParentPrefferedValidLifeTime),
                    childValidLifeTime.HasValue == true ? childValidLifeTime.Value : (parentValidLifeTime.HasValue == true ? parentValidLifeTime.Value : grantParentValidLifeTime),
                    childSubnetMask.HasValue == true ? childSubnetMask.Value : (parentSubnetMask.HasValue == true ? parentSubnetMask.Value : grantParentSubnetMask),
                    childReuseAddressIfPossible.HasValue == true ? childReuseAddressIfPossible.Value : (parentReuseAddressIfPossible.HasValue == true ? parentReuseAddressIfPossible.Value : grantParentReuseAddressIfPossible),
                    childAllocationStrategy.HasValue == true ? childAllocationStrategy.Value : (parentAllocationStrategy.HasValue == true ? parentAllocationStrategy.Value : grantParentAllocationStrategy),
                    childSupportDirectUnicast.HasValue == true ? childSupportDirectUnicast.Value : (parentSupportDirectUnicast.HasValue == true ? parentSupportDirectUnicast.Value : grantParentSupportDirectUnicast),
                    childAcceptDecline.HasValue == true ? childAcceptDecline.Value : (parentAcceptDecline.HasValue == true ? parentAcceptDecline.Value : grantParentAcceptDecline),
                    childInformsAreAllowd.HasValue == true ? childInformsAreAllowd.Value : (parentInformsAreAllowd.HasValue == true ? parentInformsAreAllowd.Value : grantParentInformsAreAllowd)
                    );

                Assert.Equal(expectedProperties, actualProperties);
            }
        }
Ejemplo n.º 15
0
        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);
            }
        }
Ejemplo n.º 16
0
        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);
        }