ln.types/test/TypesTests.cs

95 lines
3.1 KiB
C#

using NUnit.Framework;
using System;
using ln.types.net;
using System.Linq;
using System.Collections.Generic;
namespace ln.types.test
{
[TestFixture()]
public class TypesTests
{
string[] ipv6TestPatterns = new string[] {
"::",
"::1",
"0.0.0.0/0",
"127.0.0.1"
};
[Test()]
public void IPv6Tests()
{
Console.WriteLine("Testing IPv6 class");
IPv6 localhostv6 = IPv6.Parse("::1");
IPv6 localhostv4 = IPv6.Parse("127.0.0.1");
foreach (String pattern in ipv6TestPatterns)
{
IPv6 ipv6 = IPv6.Parse(pattern);
IPv6 ipv6b = IPv6.Parse(ipv6.ToCIDR());
Assert.AreEqual(ipv6, ipv6b);
}
{
IPv6 ipv6 = IPv6.Parse("::/128") + 1;
Assert.AreEqual(ipv6, localhostv6);
}
{
Stack<IPv6> expectedNetworks = new Stack<IPv6>();
expectedNetworks.Push(IPv6.Parse("255.255.0.0/16"));
expectedNetworks.Push(IPv6.Parse("255.254.0.0/15"));
expectedNetworks.Push(IPv6.Parse("255.252.0.0/14"));
expectedNetworks.Push(IPv6.Parse("255.248.0.0/13"));
expectedNetworks.Push(IPv6.Parse("255.240.0.0/12"));
expectedNetworks.Push(IPv6.Parse("255.224.0.0/11"));
expectedNetworks.Push(IPv6.Parse("255.192.0.0/10"));
expectedNetworks.Push(IPv6.Parse("255.128.0.0/9"));
expectedNetworks.Push(IPv6.Parse("255.0.0.0/8"));
expectedNetworks.Push(IPv6.Parse("254.0.0.0/7"));
expectedNetworks.Push(IPv6.Parse("252.0.0.0/6"));
expectedNetworks.Push(IPv6.Parse("248.0.0.0/5"));
expectedNetworks.Push(IPv6.Parse("240.0.0.0/4"));
expectedNetworks.Push(IPv6.Parse("224.0.0.0/3"));
expectedNetworks.Push(IPv6.Parse("192.0.0.0/2"));
expectedNetworks.Push(IPv6.Parse("128.0.0.0/1"));
IPv6 pool = IPv6.V4Space;
for (int n = 0; n < 16; n++)
{
IPv6 expected = expectedNetworks.Pop();
Assert.IsTrue(pool.Contains(expected));
IPv6[] split = pool.Split(1).ToArray();
Assert.AreEqual(2, split.Length);
Assert.AreEqual(expected, split[1]);
pool = split[1];
}
IPv6 find = IPv6.Parse("10.117.0.0/16");
pool = IPv6.V4Space;
for (int n = 0; n < 16; n++)
{
IPv6[] split = pool.Split(1).ToArray();
Console.WriteLine("{0} => {1} / {2}", pool.ToCIDR(), split[0].ToCIDR(), split[1].ToCIDR());
if (split[0].Contains(find))
pool = split[0];
else
pool = split[1];
Console.WriteLine("using {0}", pool.ToCIDR());
}
}
}
}
}