public static IPv4Address GetIPv4NetworkAddress(this Random random, IPv4SubnetMask mask) { IPv4Address start = random.GetIPv4Address(); Byte[] rawResult = ByteHelper.AndArray(start.GetBytes(), mask.GetBytes()); IPv4Address result = IPv4Address.FromByteArray(rawResult); return(result); }
public void IPv4SubnetMask_FromByteArray(Byte[] input, Boolean parsable) { if (parsable == false) { Assert.ThrowsAny <Exception>(() => IPv4SubnetMask.FromByteArray(input)); return; } IPv4SubnetMask mask = IPv4SubnetMask.FromByteArray(input); Byte[] actual = mask.GetBytes(); Assert.Equal(input, actual); }
public void IPv4SubnetMask_FromIdentifier() { Dictionary <Int32, Byte> mapper = new Dictionary <int, byte> { { 0, 0 }, { 1, 128 }, { 2, 192 }, { 3, 224 }, { 4, 240 }, { 5, 248 }, { 6, 252 }, { 7, 254 }, { 8, 255 }, }; for (int maskIdentifier = 0; maskIdentifier <= 32; maskIdentifier++) { IPv4SubnetMask mask = new IPv4SubnetMask(new IPv4SubnetMaskIdentifier(maskIdentifier)); Byte[] result = mask.GetBytes(); Int32 index = maskIdentifier % 8; Int32 arrayIndex = maskIdentifier / 8; if (maskIdentifier == 32) { for (int i = 0; i < 4; i++) { Assert.Equal(255, result[i]); } continue; } for (int j = 0; j < arrayIndex; j++) { Assert.Equal(255, result[j]); } Assert.Equal(mapper[index], result[arrayIndex]); } }