2008-03-05 09:52:00 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Microsoft Research Singularity / Netstack
|
|
|
|
//
|
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
//
|
|
|
|
// File: TestIPv4.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 TestIPv4
|
|
|
|
{
|
|
|
|
private static void TestBasics()
|
|
|
|
{
|
|
|
|
IPv4 a = new IPv4(0x1a2b3c4d);
|
|
|
|
IPv4 b = IPv4.Parse("0x1a.0x2b.0x3c.0x4d");
|
|
|
|
byte [] ipv4Bytes = new byte [4] { 0x1a, 0x2b, 0x3c, 0x4d };
|
|
|
|
IPv4 c = IPv4.ParseBytes(ipv4Bytes);
|
|
|
|
|
|
|
|
if ((uint)a != 0x1a2b3c4d)
|
|
|
|
throw new TestFailedException("a");
|
|
|
|
|
|
|
|
if ((uint)b != 0x1a2b3c4d)
|
|
|
|
throw new TestFailedException("b");
|
|
|
|
|
|
|
|
if ((uint)c != 0x1a2b3c4d)
|
|
|
|
throw new TestFailedException("c");
|
|
|
|
|
|
|
|
if (a.Equals(b) == false)
|
|
|
|
throw new TestFailedException("a Equals b");
|
|
|
|
|
|
|
|
if ((a == c) == false || (a != c))
|
|
|
|
throw new TestFailedException("a c == !=");
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
for (int i = 0; i < ipv4Bytes.Length; i++) {
|
2008-03-05 09:52:00 -05:00
|
|
|
if (ipv4Bytes[i] != c.GetAddressBytes()[i])
|
|
|
|
throw new TestFailedException("GetAddressBytes");
|
|
|
|
}
|
|
|
|
|
|
|
|
byte [] ipAddrBytes = ((IPAddress)c).GetAddressBytes();
|
2008-11-17 18:29:00 -05:00
|
|
|
for (int i = 0; i < ipv4Bytes.Length; i++) {
|
2008-03-05 09:52:00 -05:00
|
|
|
if (ipv4Bytes[i] != ipAddrBytes[i])
|
|
|
|
throw new TestFailedException("IPAddress");
|
|
|
|
}
|
|
|
|
|
|
|
|
IPAddress ipa = IPAddress.Parse(c.ToString());
|
2008-11-17 18:29:00 -05:00
|
|
|
if (new IPv4(ipa) != c) {
|
2008-03-05 09:52:00 -05:00
|
|
|
throw new TestFailedException("IPv4(IPAddress) Constructor");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void TestCompare()
|
|
|
|
{
|
|
|
|
IPv4 a = new IPv4(0x0a000001);
|
|
|
|
IPv4 b = new IPv4(0x0a000000);
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
IPv4 a = new IPv4(0x80808080);
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if ((uint)(a << 1) != 0x01010100) {
|
2008-03-05 09:52:00 -05:00
|
|
|
throw new TestFailedException("<< 1");
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if ((uint)(a << 2) != 0x02020200) {
|
2008-03-05 09:52:00 -05:00
|
|
|
throw new TestFailedException("<< 2");
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if ((uint)(a << 32) != 0) {
|
2008-03-05 09:52:00 -05:00
|
|
|
throw new TestFailedException("<< 32");
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if ((uint)(a >> 1) != 0x40404040) {
|
2008-03-05 09:52:00 -05:00
|
|
|
throw new TestFailedException(">> 1");
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if ((uint)(a >> 2) != 0x20202020) {
|
2008-03-05 09:52:00 -05:00
|
|
|
throw new TestFailedException(">> 2");
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if ((uint)(a >> 32) != 0) {
|
2008-03-05 09:52:00 -05:00
|
|
|
throw new TestFailedException(">> 32");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void TestBits()
|
|
|
|
{
|
|
|
|
IPv4 a = new IPv4(0x7f7f7f7f);
|
|
|
|
IPv4 b = new IPv4(0xc1c1c1c1);
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if ((uint)(a | b) != ~0U) {
|
2008-03-05 09:52:00 -05:00
|
|
|
throw new TestFailedException("OR");
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if ((uint)(a & b) != 0x41414141) {
|
2008-03-05 09:52:00 -05:00
|
|
|
throw new TestFailedException("AND");
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if ((uint)(a ^ b) != 0xbebebebe) {
|
2008-03-05 09:52:00 -05:00
|
|
|
throw new TestFailedException("XOR");
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if ((uint)(~a) != 0x80808080) {
|
2008-03-05 09:52:00 -05:00
|
|
|
throw new TestFailedException("NOT");
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if ((uint)IPv4.NetMask(0) != 0) {
|
2008-03-05 09:52:00 -05:00
|
|
|
throw new TestFailedException("NetMask(0)");
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if ((uint)IPv4.NetMask(17) != 0xffff8000) {
|
2008-03-05 09:52:00 -05:00
|
|
|
throw new TestFailedException("NetMask(17)");
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if ((uint)IPv4.NetMask(32) != 0xffffffffu) {
|
2008-03-05 09:52:00 -05:00
|
|
|
throw new TestFailedException("NetMask(32)");
|
|
|
|
}
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
try {
|
2008-03-05 09:52:00 -05:00
|
|
|
IPv4 n = IPv4.NetMask(66);
|
|
|
|
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
|
|
|
IPv4 n = IPv4.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 static void TestParseOne(string ipString,
|
|
|
|
IPv4 expectedIP,
|
|
|
|
bool expectedSuccess)
|
|
|
|
{
|
|
|
|
bool success = false;
|
|
|
|
IPv4 testIP = IPv4.Zero;
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
try {
|
2008-03-05 09:52:00 -05:00
|
|
|
testIP = IPv4.Parse(ipString);
|
|
|
|
success = (testIP == expectedIP);
|
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
catch (FormatException) {
|
2008-03-05 09:52:00 -05:00
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
if (success != expectedSuccess) {
|
2008-03-05 09:52:00 -05:00
|
|
|
throw new TestFailedException(ipString);
|
|
|
|
}
|
|
|
|
|
|
|
|
IPv4 dup = IPv4.Parse(testIP.ToString());
|
2008-11-17 18:29:00 -05:00
|
|
|
if (testIP != dup) {
|
2008-03-05 09:52:00 -05:00
|
|
|
throw new TestFailedException(
|
|
|
|
String.Format("ToString {0}", testIP)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void TestParse()
|
|
|
|
{
|
|
|
|
// Decimal Quads
|
|
|
|
TestParseOne("1000.0.0.1", IPv4.Zero, false);
|
|
|
|
TestParseOne("0.1000.0.1", IPv4.Zero, false);
|
|
|
|
TestParseOne("0.0.1000.1", IPv4.Zero, false);
|
|
|
|
TestParseOne("0.0.00.1000", IPv4.Zero, false);
|
|
|
|
TestParseOne("0.0.trailing.junk", IPv4.Zero, false);
|
|
|
|
TestParseOne("100.0.0.1", new IPv4(0x64000001), true);
|
|
|
|
TestParseOne("0.100.0.1", new IPv4(0x00640001), true);
|
|
|
|
TestParseOne("0.0.100.1", new IPv4(0x00006401), true);
|
|
|
|
TestParseOne("0.0.0.100", new IPv4(0x00000064), true);
|
|
|
|
TestParseOne("1", new IPv4(0x00000001), true);
|
|
|
|
TestParseOne("1.1", new IPv4(0x01000001), true);
|
|
|
|
TestParseOne("1.1.1", new IPv4(0x01010001), true);
|
|
|
|
TestParseOne("1.1.1.1", new IPv4(0x01010101), true);
|
|
|
|
|
|
|
|
// Hex Quads
|
|
|
|
TestParseOne("0xfff.0x0.0x0.0x1", IPv4.Zero, false);
|
|
|
|
TestParseOne("0x0.0xg0.0x0.0x1", IPv4.Zero, false);
|
|
|
|
TestParseOne("0x0.0.0x1000.1", IPv4.Zero, false);
|
|
|
|
TestParseOne("0x0.0x0.0x0.0x1000", IPv4.Zero, false);
|
|
|
|
TestParseOne("0x0.0x0.0xtrailing.0xjunk", IPv4.Zero, false);
|
|
|
|
TestParseOne("0xaA.0xbB.0xcC.0xdD", new IPv4(0xaabbccdd), true);
|
|
|
|
TestParseOne("0xeE.0xfF.0x01.0x02", new IPv4(0xeeff0102), true);
|
|
|
|
TestParseOne("0x1", new IPv4(0x00000001), true);
|
|
|
|
TestParseOne("0x1.0x1", new IPv4(0x01000001), true);
|
|
|
|
TestParseOne("0x1.0x1.0x1", new IPv4(0x01010001), true);
|
|
|
|
TestParseOne("0x1.0x1.0x1.0x1", new IPv4(0x01010101), true);
|
|
|
|
|
|
|
|
// Octal Quads
|
|
|
|
TestParseOne("0378.00.00.00", IPv4.Zero, false);
|
|
|
|
TestParseOne("0477.00.00.00", IPv4.Zero, false);
|
|
|
|
TestParseOne("0378.00.00.00", IPv4.Zero, false);
|
|
|
|
TestParseOne("0477.00.00.00", IPv4.Zero, false);
|
|
|
|
TestParseOne("01.02.0trailing.0junk", IPv4.Zero, false);
|
|
|
|
|
|
|
|
TestParseOne("0377.0376.0375.0374", new IPv4(0xfffefdfc), true);
|
|
|
|
TestParseOne("01", new IPv4(0x00000001), true);
|
|
|
|
TestParseOne("01.01", new IPv4(0x01000001), true);
|
|
|
|
TestParseOne("01.01.01", new IPv4(0x01010001), true);
|
|
|
|
TestParseOne("01.01.01.01", new IPv4(0x01010101), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void TestMath()
|
|
|
|
{
|
|
|
|
IPv4 a = new IPv4(0x0a000001);
|
|
|
|
|
|
|
|
if ((uint)++a != 0x0a000002)
|
|
|
|
throw new TestFailedException("Increment");
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if ((uint) --a != 0x0a000001)
|
2008-03-05 09:52:00 -05:00
|
|
|
throw new TestFailedException("Decrement 1");
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if ((uint) --a != 0x0a000000)
|
2008-03-05 09:52:00 -05:00
|
|
|
throw new TestFailedException("Decrement 2");
|
|
|
|
|
2008-11-17 18:29:00 -05:00
|
|
|
if ((uint) --a != 0x09ffffff)
|
2008-03-05 09:52:00 -05:00
|
|
|
throw new TestFailedException("Decrement 3");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void TestCount()
|
|
|
|
{
|
2008-11-17 18:29:00 -05:00
|
|
|
for (int i = IPv4.BitCount; i >= 0; i--) {
|
2008-03-05 09:52:00 -05:00
|
|
|
IPv4 addr = IPv4.NetMask(i);
|
2008-11-17 18:29:00 -05:00
|
|
|
if (IPv4.GetMaskLength(addr) != i) {
|
2008-03-05 09:52:00 -05:00
|
|
|
throw new TestFailedException(
|
|
|
|
String.Format("TestCount {0}", i)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
IPv4 addrDup = IPv4.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
|