singrdk/base/Libraries/System.Net.IP/TestIPv6.cs

397 lines
13 KiB
C#
Raw Permalink Normal View History

2008-03-05 09:52:00 -05:00
///////////////////////////////////////////////////////////////////////////////
//
// Microsoft Research Singularity / Netstack
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// File: TestIPv6.cs
//
using System;
#if !SINGULARITY
using System.Net;
#endif // !SINGULARITY
namespace System.Net.IP
{
internal class TestFailedException : Exception
{
internal TestFailedException(string msg)
: base(String.Format("TestFailedException \"{0}\"", msg))
{
}
}
public class TestIPv6
{
private static void TestBasics()
{
// Create an IPv6 address from a set of uints
IPv6 a = new IPv6(0xf0e1d2c3, 0xb4a59687, 0x78695a4b, 0x3c2d1e0f);
// Then check it against equivalent array of Bytes
byte[] xBytes = { 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87,
0x78, 0x69, 0x5a, 0x4b, 0x3c, 0x2d, 0x1e, 0x0f };
byte[] aBytes = a.GetAddressBytes();
if (aBytes.Length != 16)
throw new TestFailedException("GetAddressBytes Length");
2008-11-17 18:29:00 -05:00
for (int i = 0; i < aBytes.Length; i++) {
2008-03-05 09:52:00 -05:00
if (aBytes[i] != xBytes[i])
throw new TestFailedException("GetAddressBytes Values");
}
// Instantiate from array of bytes and then check
a = IPv6.ParseBytes(xBytes);
aBytes = a.GetAddressBytes();
if (aBytes.Length != 16)
throw new TestFailedException("GetAddressBytes Length");
2008-11-17 18:29:00 -05:00
for (int i = 0; i < aBytes.Length; i++) {
2008-03-05 09:52:00 -05:00
if (aBytes[i] != xBytes[i])
throw new TestFailedException("GetAddressBytes Values");
}
// Test System.Net.IPAddress Constructor
IPAddress ipa = new IPAddress(aBytes);
2008-11-17 18:29:00 -05:00
if (new IPv6(ipa) != a) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("IPv6(IPAddress) Constructor");
}
// Test basic string parsing
a = IPv6.Parse("f0e1:d2c3:b4a5:9687:7869:5a4b:3c2d:1e0f");
aBytes = a.GetAddressBytes();
if (aBytes.Length != 16)
throw new TestFailedException("GetAddressBytes Length");
2008-11-17 18:29:00 -05:00
for (int i = 0; i < aBytes.Length; i++) {
2008-03-05 09:52:00 -05:00
if (aBytes[i] != xBytes[i])
throw new TestFailedException("GetAddressBytes Values");
}
}
private static void TestCompare()
{
IPv6 a = new IPv6(0x11111111, 0x22222222, 0x33333333, 0x44444444);
IPv6 b = new IPv6(0x11111111, 0x22222222, 0x33333333, 0x44444443);
2008-11-17 18:29:00 -05:00
if ((a == a) == false) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("a == a");
}
2008-11-17 18:29:00 -05:00
if ((a != b) == false) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("a == b");
}
2008-11-17 18:29:00 -05:00
if ((a > b) == false) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("a > b");
}
2008-11-17 18:29:00 -05:00
if ((a >= b) == false) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("a >= b");
}
2008-11-17 18:29:00 -05:00
if ((a >= a) == false) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("a >= a");
}
2008-11-17 18:29:00 -05:00
if ((a > a) == true) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("a > a");
}
2008-11-17 18:29:00 -05:00
if ((a < b) == true) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("a < b");
}
2008-11-17 18:29:00 -05:00
if ((a <= b) == true) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("a <= b");
}
2008-11-17 18:29:00 -05:00
if ((a <= a) != true) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("a <= a");
}
2008-11-17 18:29:00 -05:00
if ((a < a) == true) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("a < a");
}
}
private static void TestRoll()
{
IPv6 a = new IPv6(0x01234567, 0x89abcdef, 0x02461357, 0x8ace9bdf);
if ((a << 4) !=
new IPv6(0x12345678, 0x9abcdef0, 0x24613578, 0xace9bdf0))
{
throw new TestFailedException("a << 4");
}
2008-11-17 18:29:00 -05:00
if ((a << 36) != new IPv6(0x9abcdef0, 0x24613578, 0xace9bdf0, 0)) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("a << 36");
}
2008-11-17 18:29:00 -05:00
if ((a << 68) != new IPv6(0x24613578, 0xace9bdf0, 0, 0)) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("a << 68");
}
2008-11-17 18:29:00 -05:00
if ((a << 100) != new IPv6(0xace9bdf0, 0, 0, 0)) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("a << 100");
}
2008-11-17 18:29:00 -05:00
if ((a << 128) != new IPv6(0, 0, 0, 0)) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("a << 128");
}
if ((a >> 4) !=
new IPv6(0x00123456, 0x789abcde, 0xf0246135, 0x78ace9bd))
{
throw new TestFailedException("a >> 4");
}
2008-11-17 18:29:00 -05:00
if ((a >> 36) != new IPv6(0, 0x00123456, 0x789abcde, 0xf0246135)) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("a >> 36");
}
2008-11-17 18:29:00 -05:00
if ((a >> 68) != new IPv6(0, 0, 0x00123456, 0x789abcde)) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("a >> 68");
}
2008-11-17 18:29:00 -05:00
if ((a >> 100) != new IPv6(0, 0, 0, 0x00123456)) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("a >> 100");
}
2008-11-17 18:29:00 -05:00
if ((a >> 128) != new IPv6(0, 0, 0, 0)) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("a >> 128");
}
}
private static void TestBits()
{
IPv6 a = new IPv6(0x7f7f7f7f, 0x7f7f7f7f, 0x7f7f7f7f, 0x7f7f7f7f);
IPv6 b = new IPv6(0xc1c1c1c1, 0xc1c1c1c1, 0xc1c1c1c1, 0xc1c1c1c1);
IPv6 goal;
goal = new IPv6(~0U, ~0U, ~0U, ~0U);
2008-11-17 18:29:00 -05:00
if ((a | b) != goal) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("OR");
}
goal = new IPv6(0x41414141, 0x41414141, 0x41414141, 0x41414141);
2008-11-17 18:29:00 -05:00
if ((a & b) != goal) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("AND");
}
goal = new IPv6(0xbebebebe, 0xbebebebe, 0xbebebebe, 0xbebebebe);
2008-11-17 18:29:00 -05:00
if ((a ^ b) != goal) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("XOR");
}
goal = new IPv6(0x80808080, 0x80808080, 0x80808080, 0x80808080);
2008-11-17 18:29:00 -05:00
if (~a != goal) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("NOT");
}
// Test netmask
2008-11-17 18:29:00 -05:00
for (int i = 0; i < 128; i++) {
2008-03-05 09:52:00 -05:00
IPv6 mask = IPv6.NetMask(i);
goal = IPv6.AllOnes << (128 - i);
2008-11-17 18:29:00 -05:00
if (mask != goal) {
2008-03-05 09:52:00 -05:00
Console.WriteLine("{0} {1} {2}", i, mask, goal);
throw new TestFailedException("NetMask");
}
goal = ~(IPv6.AllOnes >> i);
2008-11-17 18:29:00 -05:00
if (mask != goal) {
2008-03-05 09:52:00 -05:00
Console.WriteLine("{0} {1} {2}", i, mask, goal);
throw new TestFailedException("NetMask2");
}
}
2008-11-17 18:29:00 -05:00
try {
2008-03-05 09:52:00 -05:00
IPv6 n = IPv6.NetMask(129);
throw new TestFailedException("Bad Netmask +");
}
2008-11-17 18:29:00 -05:00
catch (ArgumentException) {
2008-03-05 09:52:00 -05:00
}
2008-11-17 18:29:00 -05:00
try {
2008-03-05 09:52:00 -05:00
IPv6 n = IPv6.NetMask(-1);
throw new TestFailedException("Bad Netmask -");
}
2008-11-17 18:29:00 -05:00
catch (ArgumentException) {
2008-03-05 09:52:00 -05:00
}
}
private struct SrepV6Pair
{
public string srep;
public IPv6 v6rep;
public SrepV6Pair(string s, IPv6 a)
{
srep = s;
v6rep = a;
}
};
private static void TestParse()
{
//
// grumble, grumble, no clean struct array initializer syntax !?!
//
// Okay, these are pairs of Valid IPv6 string representations
// and their manually parsed IPv6 representations.
//
SrepV6Pair [] pairs = new SrepV6Pair[] {
new SrepV6Pair (
"FfEe:DdCc:BbAa:9876:5432:1001:2345:6789",
new IPv6(0xffeeddcc, 0xbbaa9876, 0x54321001, 0x23456789)
),
new SrepV6Pair (
"ffe8:c181::192:101",
new IPv6(0xffe8c181U, 0x0U, 0x0U, 0x01920101U)
),
new SrepV6Pair (
"::",
IPv6.Zero
),
new SrepV6Pair (
"0:0:0:0:0:0:0:0",
IPv6.Zero
),
new SrepV6Pair (
"1::",
new IPv6(0x00010000U, 0x0U, 0x0U, 0x0U)
),
new SrepV6Pair (
"12::",
new IPv6(0x00120000U, 0x0U, 0x0U, 0x0U)
),
new SrepV6Pair (
"123::",
new IPv6(0x01230000U, 0x0U, 0x0U, 0x0U)
),
new SrepV6Pair (
"1234::",
new IPv6(0x12340000U, 0x0U, 0x0U, 0x0U)
),
new SrepV6Pair (
"1234:5678:9abc:def0::",
new IPv6(0x12345678U, 0x9abcdef0U, 0x0U, 0x0U)
),
new SrepV6Pair (
"::1",
new IPv6(0x0U, 0x0U, 0x0U, 0x1U)
),
new SrepV6Pair (
"::12",
new IPv6(0x0U, 0x0U, 0x0U, 0x12U)
),
new SrepV6Pair (
"::123",
new IPv6(0x0U, 0x0U, 0x0U, 0x123U)
),
new SrepV6Pair (
"::1234",
new IPv6(0x0U, 0x0U, 0x0U, 0x1234U)
),
new SrepV6Pair (
"::1234:5678:9abc:def0",
new IPv6(0x0U, 0x0U, 0x12345678U, 0x9abcdef0U)
),
};
2008-11-17 18:29:00 -05:00
for (int i = 0; i < pairs.Length; i++) {
2008-03-05 09:52:00 -05:00
IPv6 a = IPv6.Parse(pairs[i].srep);
2008-11-17 18:29:00 -05:00
if (a != pairs[i].v6rep) {
2008-03-05 09:52:00 -05:00
Console.WriteLine("{0} {1}", a, pairs[i].v6rep);
throw new TestFailedException(
String.Format("Parse {0}", i)
);
}
IPv6 dup = IPv6.Parse(a.ToString());
2008-11-17 18:29:00 -05:00
if (dup != a) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException(
String.Format("ToString {0}", i)
);
}
}
//
// Test bogus strings
//
string [] badSreps = new string [] {
"Frank",
"00000::",
"0:1:2:3:4:5:6:7:8",
"0.1.2.3.4::0.1.2.3.4.",
"0:1:2:3:4:5:6:7:trailing junk",
};
2008-11-17 18:29:00 -05:00
for (int i = 0; i < badSreps.Length; i++) {
try {
2008-03-05 09:52:00 -05:00
IPv6 bad = IPv6.Parse(badSreps[i]);
throw new TestFailedException(badSreps[i]);
}
2008-11-17 18:29:00 -05:00
catch (FormatException) {
2008-03-05 09:52:00 -05:00
}
}
}
private static void TestMath()
{
IPv6 a = IPv6.Zero;
2008-11-17 18:29:00 -05:00
if (--a != IPv6.AllOnes) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("--");
}
2008-11-17 18:29:00 -05:00
if (++a != IPv6.Zero) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException("++");
}
}
public static void TestCount()
{
2008-11-17 18:29:00 -05:00
for (int i = 0; i < IPv6.BitCount; i++) {
2008-03-05 09:52:00 -05:00
IPv6 addr = IPv6.NetMask(i);
2008-11-17 18:29:00 -05:00
if (IPv6.GetMaskLength(addr) != i) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException(
String.Format("TestCount {0}", i)
);
}
IPv6 addrDup = IPv6.Parse(addr.ToString());
2008-11-17 18:29:00 -05:00
if (addrDup != addr) {
2008-03-05 09:52:00 -05:00
throw new TestFailedException(
String.Format("TestCountDup {0}", i)
);
}
}
}
public static int Main()
{
2008-11-17 18:29:00 -05:00
try {
2008-03-05 09:52:00 -05:00
TestBasics();
TestCompare();
TestRoll();
TestBits();
TestParse();
TestMath();
TestCount();
}
2008-11-17 18:29:00 -05:00
catch (Exception e) {
2008-03-05 09:52:00 -05:00
Console.WriteLine("FAILED\nException {0}\nStack:\n{1}",
e.Message, e.StackTrace);
return 1;
}
Console.WriteLine("Passed.");
return 0;
}
}
} // namespace System.Net.IP