Commit 8daded36 authored by Brickner_cp's avatar Brickner_cp

Improve Maintainability

parent 16995cfa
...@@ -68,35 +68,40 @@ namespace PcapDotNet.Packets.TestUtils ...@@ -68,35 +68,40 @@ namespace PcapDotNet.Packets.TestUtils
return new IpV4Address(random.NextUInt()); return new IpV4Address(random.NextUInt());
} }
public static IpV4Options NextIpV4Options(this Random random) public static IpV4Option NextIpV4Option(this Random random, int maximumOptionLength)
{ {
int optionsLength = random.Next(IpV4Options.MaximumBytesLength) / 4 * 4; if (maximumOptionLength == 0)
List<IpV4Option> options = new List<IpV4Option>(); throw new ArgumentOutOfRangeException("maximumOptionLength", maximumOptionLength, "option length must be positive");
while (optionsLength > 0)
List<IpV4OptionType> impossibleOptionTypes = new List<IpV4OptionType>();
if (maximumOptionLength < IpV4OptionSecurity.OptionLength)
impossibleOptionTypes.Add(IpV4OptionType.Security);
if (maximumOptionLength < IpV4OptionRoute.OptionMinimumLength)
{ {
IpV4Option option = null; impossibleOptionTypes.Add(IpV4OptionType.LooseSourceRouting);
IpV4OptionType optionType = random.NextEnum<IpV4OptionType>(); impossibleOptionTypes.Add(IpV4OptionType.StrictSourceRouting);
impossibleOptionTypes.Add(IpV4OptionType.RecordRoute);
}
if (maximumOptionLength < IpV4OptionStreamIdentifier.OptionLength)
impossibleOptionTypes.Add(IpV4OptionType.StreamIdentifier);
if (maximumOptionLength < IpV4OptionTimestamp.OptionMinimumLength)
impossibleOptionTypes.Add(IpV4OptionType.InternetTimestamp);
IpV4OptionType optionType = random.NextEnum<IpV4OptionType>(impossibleOptionTypes);
switch (optionType) switch (optionType)
{ {
case IpV4OptionType.EndOfOptionList: case IpV4OptionType.EndOfOptionList:
case IpV4OptionType.NoOperation: case IpV4OptionType.NoOperation:
option = new IpV4OptionSimple(optionType); return new IpV4OptionSimple(optionType);
break;
case IpV4OptionType.Security: case IpV4OptionType.Security:
if (optionsLength < IpV4OptionSecurity.OptionLength) return new IpV4OptionSecurity(random.NextEnum<IpV4OptionSecurityLevel>(), random.NextUShort(), random.NextUShort(),
break;
option = new IpV4OptionSecurity(random.NextEnum<IpV4OptionSecurityLevel>(), random.NextUShort(), random.NextUShort(),
random.NextUInt24()); random.NextUInt24());
break;
case IpV4OptionType.LooseSourceRouting: case IpV4OptionType.LooseSourceRouting:
case IpV4OptionType.StrictSourceRouting: case IpV4OptionType.StrictSourceRouting:
case IpV4OptionType.RecordRoute: case IpV4OptionType.RecordRoute:
if (optionsLength < IpV4OptionRoute.OptionMinimumLength) int numAddresses = random.Next((maximumOptionLength - IpV4OptionRoute.OptionMinimumLength) / 4 + 1);
break;
int numAddresses = random.Next((optionsLength - IpV4OptionRoute.OptionMinimumLength) / 4 + 1);
IpV4Address[] addresses = new IpV4Address[numAddresses]; IpV4Address[] addresses = new IpV4Address[numAddresses];
for (int addressIndex = 0; addressIndex != numAddresses; ++addressIndex) for (int addressIndex = 0; addressIndex != numAddresses; ++addressIndex)
addresses[addressIndex] = random.NextIpV4Address(); addresses[addressIndex] = random.NextIpV4Address();
...@@ -110,30 +115,22 @@ namespace PcapDotNet.Packets.TestUtils ...@@ -110,30 +115,22 @@ namespace PcapDotNet.Packets.TestUtils
switch (optionType) switch (optionType)
{ {
case IpV4OptionType.LooseSourceRouting: case IpV4OptionType.LooseSourceRouting:
option = new IpV4OptionLooseSourceRouting(addresses, pointedAddressIndex); return new IpV4OptionLooseSourceRouting(addresses, pointedAddressIndex);
break;
case IpV4OptionType.StrictSourceRouting: case IpV4OptionType.StrictSourceRouting:
option = new IpV4OptionStrictSourceRouting(addresses, pointedAddressIndex); return new IpV4OptionStrictSourceRouting(addresses, pointedAddressIndex);
break;
case IpV4OptionType.RecordRoute: case IpV4OptionType.RecordRoute:
option = new IpV4OptionRecordRoute(pointedAddressIndex, addresses); return new IpV4OptionRecordRoute(pointedAddressIndex, addresses);
break;
default:
throw new InvalidOperationException("optionType = " + optionType);
} }
break;
case IpV4OptionType.StreamIdentifier: case IpV4OptionType.StreamIdentifier:
if (optionsLength < IpV4OptionStreamIdentifier.OptionLength) return new IpV4OptionStreamIdentifier(random.NextUShort());
break;
option = new IpV4OptionStreamIdentifier(random.NextUShort());
break;
case IpV4OptionType.InternetTimestamp: case IpV4OptionType.InternetTimestamp:
if (optionsLength < IpV4OptionTimestamp.OptionMinimumLength)
break;
IpV4OptionTimestampType timestampType = random.NextEnum<IpV4OptionTimestampType>(); IpV4OptionTimestampType timestampType = random.NextEnum<IpV4OptionTimestampType>();
byte overflow = random.NextByte(IpV4OptionTimestamp.OverflowMaxValue + 1); byte overflow = random.NextByte(IpV4OptionTimestamp.OverflowMaxValue + 1);
byte pointedIndex; byte pointedIndex;
...@@ -145,25 +142,38 @@ namespace PcapDotNet.Packets.TestUtils ...@@ -145,25 +142,38 @@ namespace PcapDotNet.Packets.TestUtils
switch (timestampType) switch (timestampType)
{ {
case IpV4OptionTimestampType.TimestampOnly: case IpV4OptionTimestampType.TimestampOnly:
int numTimestamps = random.Next((optionsLength - IpV4OptionTimestamp.OptionMinimumLength) / 4 + 1); int numTimestamps = random.Next((maximumOptionLength - IpV4OptionTimestamp.OptionMinimumLength) / 4 + 1);
uint[] timestamps = new uint[numTimestamps]; uint[] timestamps = new uint[numTimestamps];
for (int i = 0; i != numTimestamps; ++i) for (int i = 0; i != numTimestamps; ++i)
timestamps[i] = random.NextUInt(); timestamps[i] = random.NextUInt();
option = new IpV4OptionTimestampOnly(overflow, pointedIndex, timestamps); return new IpV4OptionTimestampOnly(overflow, pointedIndex, timestamps);
break;
case IpV4OptionTimestampType.AddressAndTimestamp: case IpV4OptionTimestampType.AddressAndTimestamp:
int numPairs = random.Next((optionsLength - IpV4OptionTimestamp.OptionMinimumLength) / 8 + 1); case IpV4OptionTimestampType.AddressPrespecified:
int numPairs = random.Next((maximumOptionLength - IpV4OptionTimestamp.OptionMinimumLength) / 8 + 1);
IpV4OptionTimedAddress[] pairs = new IpV4OptionTimedAddress[numPairs]; IpV4OptionTimedAddress[] pairs = new IpV4OptionTimedAddress[numPairs];
for (int i = 0; i != numPairs; ++i) for (int i = 0; i != numPairs; ++i)
pairs[i] = new IpV4OptionTimedAddress(random.NextIpV4Address(), random.NextUInt()); pairs[i] = new IpV4OptionTimedAddress(random.NextIpV4Address(), random.NextUInt());
option = new IpV4OptionTimestampAndAddress(timestampType, overflow, pointedIndex, pairs); return new IpV4OptionTimestampAndAddress(timestampType, overflow, pointedIndex, pairs);
break;
default:
throw new InvalidOperationException("timestampType = " + timestampType);
}
default:
throw new InvalidOperationException("optionType = " + optionType);
} }
break;
} }
public static IpV4Options NextIpV4Options(this Random random)
{
int optionsLength = random.Next(IpV4Options.MaximumBytesLength) / 4 * 4;
List<IpV4Option> options = new List<IpV4Option>();
while (optionsLength > 0)
{
IpV4Option option = random.NextIpV4Option(optionsLength);
if (option == null) if (option == null)
continue; continue;
......
...@@ -83,7 +83,7 @@ namespace PcapDotNet.TestUtils ...@@ -83,7 +83,7 @@ namespace PcapDotNet.TestUtils
return new DateTime(random.NextLong(DateTime.MinValue.Ticks, DateTime.MaxValue.Ticks + 1)); return new DateTime(random.NextLong(DateTime.MinValue.Ticks, DateTime.MaxValue.Ticks + 1));
} }
public static T NextEnum<T>(this Random random, params T[] valuesToIgnore) public static T NextEnum<T>(this Random random, IEnumerable<T> valuesToIgnore)
{ {
Type type = typeof(T); Type type = typeof(T);
if (!type.IsEnum) if (!type.IsEnum)
...@@ -96,6 +96,11 @@ namespace PcapDotNet.TestUtils ...@@ -96,6 +96,11 @@ namespace PcapDotNet.TestUtils
return (T)random.NextValue(enumValues); return (T)random.NextValue(enumValues);
} }
public static T NextEnum<T>(this Random random, params T[] valuesToIgnore)
{
return random.NextEnum((IEnumerable<T>)valuesToIgnore);
}
public static object NextValue<T>(this Random random, IList<T> values) public static object NextValue<T>(this Random random, IList<T> values)
{ {
return values[random.Next(values.Count)]; return values[random.Next(values.Count)];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment