public void NotContains(string networks, string othernets)
    {
        IpAddressNetworkV4 network  = (IpAddressNetworkV4)networks;
        IpAddressNetworkV4 othernet = (IpAddressNetworkV4)othernets;

        network.Contains(othernet).Should().BeFalse();
    }
    public void ValidFormatsTests(string input, string expectedIp, byte expectedMask)
    {
        IpAddressNetworkV4 ip = IpAddressNetworkV4.Parse(input);

        ip.Mask.Should().Be(expectedMask);
        ip.NetworkAddress.Should().Be(IPAddress.Parse(expectedIp));
    }
    public void SupernetTests(string expecteds, string[] nets)
    {
        IpAddressNetworkV4 expected = (IpAddressNetworkV4)expecteds;

        IpAddressNetworkV4 supernet = IpAddressNetworkV4.MakeSupernet(nets.Select(s => (IpAddressNetworkV4)s));

        supernet.Should().Be(expected);
    }
    public void OtherTests(string address, byte mask, string start, string end)
    {
        IpAddressNetworkV4 ip = new IpAddressNetworkV4(IPAddress.Parse(address), mask);

        ip.Mask.Should().Be(mask);
        ip.NetworkAddress.Should().Be(IPAddress.Parse(start));
        ip.BroadcastAddress.Should().Be(IPAddress.Parse(end));
    }
    public void ContainsOrEqual(string networks, string othernets)
    {
        IpAddressNetworkV4 network  = (IpAddressNetworkV4)networks;
        IpAddressNetworkV4 othernet = (IpAddressNetworkV4)othernets;

        network.ContainsOrEqual(othernet).Should().BeTrue();
        othernet.IsContainedInOrEqual(network).Should().BeTrue();
    }
Beispiel #6
0
 public void Setup()
 {
     _addressV4  = (IpAddressV4)"174.64.25.18";
     _networkV4  = (IpAddressNetworkV4)"174.64.25.18/24";
     _addressV6  = (IpAddressV6)"2001:0dff:44ff:0:1744:ffff";
     _networkV6  = (IpAddressNetworkV6)"2001:0dff:44ff:0:1744:ffff/64";
     _sb         = new StringBuilder();
     _charBuffer = new char[60];
 }
    public void BasicTest(string test, string expectedNetwork, string expectedBroadcast, byte expectedMask, uint expectedSubnetSize, uint expectedSubnetHostsSize)
    {
        IpAddressNetworkV4 net = IpAddressNetworkV4.Parse(test);

        net.NetworkAddress.Should().Be((IpAddressV4)expectedNetwork);
        net.BroadcastAddress.Should().Be((IpAddressV4)expectedBroadcast);
        net.Mask.Should().Be(expectedMask);
        net.SubnetSize.Should().Be(expectedSubnetSize);
        net.SubnetHostsSize.Should().Be(expectedSubnetHostsSize);
    }
Beispiel #8
0
    /// <inheritdoc cref="Docs.IIPAddressNetworkDocs{IpAddressNetworkV4}.ContainsOrEqual(IpAddressNetworkV4)"/>
    public bool ContainsOrEqual(IpAddressNetworkV4 other)
    {
        if (other.Mask < _mask)
        {
            return(false);
        }

        IpAddressV4 sharedNetwork = NetworkMask & other.NetworkAddress;

        return(sharedNetwork == _networkAddress);
    }
    public void ComparisonTests(string smallers, string largers)
    {
        IpAddressNetworkV4 smaller = (IpAddressNetworkV4)smallers;
        IpAddressNetworkV4 larger  = (IpAddressNetworkV4)largers;

        (smaller < larger).Should().BeTrue();
        (smaller <= larger).Should().BeTrue();
        (larger > smaller).Should().BeTrue();
        (larger >= smaller).Should().BeTrue();

        smaller.CompareTo(larger).Should().BeNegative();
        larger.CompareTo(smaller).Should().BePositive();
    }
    public void FullPropertyTest()
    {
        IpAddressNetworkV4 net = IpAddressNetworkV4.Parse("192.168.45.17/22");

        net.Mask.Should().Be(22);
        net.ToString().Should().Be("192.168.44.0/22");
        net.NetworkAddress.Should().Be((IpAddressV4)"192.168.44.0");
        net.BroadcastAddress.Should().Be((IpAddressV4)"192.168.47.255");
        net.NetworkMask.Should().Be((IpAddressV4)"255.255.252.0");
        net.NetworkWildcardMask.Should().Be((IpAddressV4)"0.0.3.255");
        net.SubnetSize.Should().Be(1024);
        net.SubnetHostsSize.Should().Be(1022);
    }
    public void EqualTests(string lefts, string rights)
    {
        IpAddressNetworkV4 left  = (IpAddressNetworkV4)lefts;
        IpAddressNetworkV4 right = (IpAddressNetworkV4)rights;

        (left < right).Should().BeFalse();
        (left <= right).Should().BeTrue();
        (left == right).Should().BeTrue();
        (right > left).Should().BeFalse();
        (right >= left).Should().BeTrue();
        (right == left).Should().BeTrue();

        left.CompareTo(right).Should().Be(0);
        right.CompareTo(left).Should().Be(0);
    }
Beispiel #12
0
    public void IPv4Utilities()
    {
        IpAddressNetworkV4 network = IpAddressNetworkV4.Parse("192.168.1.0/24");
        IpAddressNetworkV4 ip      = IpAddressNetworkV4.Parse("192.168.1.1");

        Assert.True(network.Contains(ip));
        Assert.True(network.ContainsOrEqual(ip));
        Assert.True(ip.IsContainedIn((IpAddressNetworkV4)"192.168.1.0/24"));
        Assert.True(ip.IsContainedIn((IpAddressNetworkV4)"192.168.0.0/16"));
        Assert.False(ip.IsContainedIn((IpAddressNetworkV4)"192.168.1.128/25"));

        Assert.Equal("192.168.1.1", ip.ToString());
        Assert.Equal("192.168.1.1/32", ip.ToString(true));
        Assert.Equal("192.168.1.0/24", network.ToString());
        Assert.Equal("192.168.1/24", network.ToPrefixString());
    }
    public void SplitTest()
    {
        // Check that Split fails fast on mask
        IpAddressNetworkV4 net = (IpAddressNetworkV4)"192.168.1.0/24";

        Assert.Throws <ArgumentOutOfRangeException>(() => net.Split(33));
        Assert.Throws <ArgumentOutOfRangeException>(() => net.Split(9));
        Assert.Throws <ArgumentOutOfRangeException>(() => net.Split(0));

        // Generic split in the lower-end of the IPv4 struct
        net = (IpAddressNetworkV4)"192.168.1.0/24";
        List <IpAddressNetworkV4> splits = net.Split(2).ToList();

        splits.Should().ContainInOrder(new IpAddressNetworkV4[]
        {
            (IpAddressNetworkV4)"192.168.1.0/26",
            (IpAddressNetworkV4)"192.168.1.64/26",
            (IpAddressNetworkV4)"192.168.1.128/26",
            (IpAddressNetworkV4)"192.168.1.192/26"
        });

        // Generic split in the lower-end of the IPv6 struct
        net    = (IpAddressNetworkV4)"10.0.0.0/8";
        splits = net.Split(2).ToList();
        splits.Should().ContainInOrder(new IpAddressNetworkV4[]
        {
            (IpAddressNetworkV4)"10.0.0.0/10",
            (IpAddressNetworkV4)"10.64.0.0/10",
            (IpAddressNetworkV4)"10.128.0.0/10",
            (IpAddressNetworkV4)"10.192.0.0/10"
        });

        // Major split from 0.0.0.0/0 to 0.0.0.0/32, to ensure it can work (we will _not_ enumerate it)
        net    = (IpAddressNetworkV4)"0.0.0.0/0";
        splits = net.Split(32).Take(5).ToList();
        splits.Should().ContainInOrder(new IpAddressNetworkV4[]
        {
            (IpAddressNetworkV4)"0.0.0.0/32",
            (IpAddressNetworkV4)"0.0.0.1/32",
            (IpAddressNetworkV4)"0.0.0.2/32",
            (IpAddressNetworkV4)"0.0.0.3/32",
            (IpAddressNetworkV4)"0.0.0.4/32"
        });
    }
Beispiel #14
0
    public static bool TryParse(ReadOnlySpan <char> value, out IpAddressNetworkV4 result)
    {
        Tokenizer tokenizer = new Tokenizer(value);

        if (!TryParse(ref tokenizer, out result))
        {
            return(false);
        }

        // We now expect the end
        ParsedToken token = tokenizer.ParseAndAdvance();

        if (token.Type != TokenType.None)
        {
            // Something other than EOF was found
            result = default;
            return(false);
        }

        return(true);
    }
 public void InvalidFormatsTests(string test)
 {
     IpAddressNetworkV4.TryParse(test, out _).Should().BeFalse();
 }
 protected override int Compare(IpAddressNetworkV4 network, IpAddressNetworkV4 other) => network.CompareTo(other);
Beispiel #17
0
 public static bool TryParse(string value, out IpAddressNetworkV4 result)
 {
     return(TryParse(value.AsSpan(), out result));
 }
 protected override int Mask(IpAddressNetworkV4 network) => network.Mask;
 protected override bool Contains(IpAddressNetworkV4 network, IpAddressNetworkV4 other) => network.ContainsOrEqual(other);
Beispiel #20
0
 public IpAddressNetworkV4 WithCidr04() => IpAddressNetworkV4.Parse("192.168.10.1/32");
Beispiel #21
0
 public bool IsContainedInOrEqual(IpAddressNetworkV4 other)
 {
     return(other.ContainsOrEqual(this));
 }
Beispiel #22
0
 public void IPv4NetworkFormats(string network)
 {
     Assert.True(IpAddressNetworkV4.TryParse(network, out _));
 }
Beispiel #23
0
 /// <inheritdoc cref="Docs.IIPAddressNetworkDocs{IpAddressNetworkV4}.Contains(IpAddressNetworkV4)"/>
 public bool Contains(IpAddressNetworkV4 other)
 {
     // In addition to ContainsOrEqual, the mask should be smaller as to not be equal
     return(_mask < other._mask && ContainsOrEqual(other));
 }
Beispiel #24
0
 public IpAddressNetworkV4 CurrentIPv4UnstableParser()
 {
     return(IpAddressNetworkV4.ParseUnstable("192.168.10.1"));
 }
    public void ToStringTests(string networks, string expected)
    {
        IpAddressNetworkV4 network = (IpAddressNetworkV4)networks;

        network.ToString().Should().Be(expected);
    }
    [InlineData("10.0.0.1", "10.255.255.255", "10.0.0.0/8")] //Test that we remove address bits to create a network id address
    public void RangeCtor(string start, string end, string expected)
    {
        IpAddressNetworkV4 ip = new IpAddressNetworkV4(IPAddress.Parse(start), IPAddress.Parse(end));

        ip.Should().Be((IpAddressNetworkV4)expected);
    }
Beispiel #27
0
 public IpAddressNetworkV4 NoCidr01() => IpAddressNetworkV4.Parse("192");
Beispiel #28
0
    internal static bool TryParse(ref Tokenizer tokenizer, out IpAddressNetworkV4 result)
    {
        // Shortest IPv4 is 1 char (0)
        // Longest IPv4 is 18 chars (255.255.255.255/32)
        if (tokenizer.Length < 1 || tokenizer.Length > 18)
        {
            result = default;
            return(false);
        }

        uint ip   = 0;
        byte cidr = 32;

        ParsedToken token = default;

        // Parse the IP
        int i;

        for (i = 0; i < 4; i++)
        {
            token = tokenizer.ParseAndAdvance(false);

            // Read a dot, or break on slashes
            if (i > 0)
            {
                if (token.Type == TokenType.Slash)
                {
                    break;
                }

                if (token.Type != TokenType.Dot)
                {
                    // We expected a dot, but we didn't get one
                    result = default;
                    return(false);
                }

                // Advance once more
                token = tokenizer.ParseAndAdvance(false);
            }

            // Read a number
            if (token.Type != TokenType.Number || token.Value > byte.MaxValue)
            {
                // We expected a 0..255 number, but we didn't get one
                result = default;
                return(false);
            }

            ip <<= 8;
            ip  += token.Value;
        }

        // Assume the remainder of the IP is 0's
        for (; i < 4; i++)
        {
            ip <<= 8;
        }

        // Parse the Cidr
        if (token.Type != TokenType.Slash)
        {
            token = tokenizer.ParseAndAdvance(false);
        }

        if (token.Type == TokenType.Slash)
        {
            // Read a number, as the cidr
            token = tokenizer.ParseAndAdvance(false);

            if (token.Type != TokenType.Number || token.Value > 32)
            {
                // We expected a 0..32 number, but we didn't get one
                result = default;
                return(false);
            }

            cidr = (byte)token.Value;
        }

        result = new IpAddressNetworkV4(ip, cidr);
        return(true);
    }
Beispiel #29
0
 public bool IsContainedIn(IpAddressNetworkV4 network) => network.Contains(this);
Beispiel #30
0
 public IpAddressNetworkV4 NoCidr02() => IpAddressNetworkV4.Parse("192.168");