Commit f7b62ff8 authored by Brickner_cp's avatar Brickner_cp

HTTP

parent cc5bf2f4
using System; using System;
using System.CodeDom;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Globalization; using System.Globalization;
...@@ -8,7 +7,6 @@ using System.Linq; ...@@ -8,7 +7,6 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Xml.Linq; using System.Xml.Linq;
using Microsoft.CSharp;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using PcapDotNet.Base; using PcapDotNet.Base;
using PcapDotNet.Packets; using PcapDotNet.Packets;
...@@ -37,6 +35,7 @@ namespace PcapDotNet.Core.Test ...@@ -37,6 +35,7 @@ namespace PcapDotNet.Core.Test
private const bool IsRetry private const bool IsRetry
// = true; // = true;
= false; = false;
private const byte RetryNumber = 101;
/// <summary> /// <summary>
/// Gets or sets the test context which provides /// Gets or sets the test context which provides
...@@ -69,7 +68,7 @@ namespace PcapDotNet.Core.Test ...@@ -69,7 +68,7 @@ namespace PcapDotNet.Core.Test
[TestMethod] [TestMethod]
public void ComparePacketsToWiresharkTest() public void ComparePacketsToWiresharkTest()
{ {
for (int i = 0; i != 10; ++i) for (int i = 0; i != 100; ++i)
{ {
// Create packets // Create packets
List<Packet> packets = new List<Packet>(CreateRandomPackets(200)); List<Packet> packets = new List<Packet>(CreateRandomPackets(200));
...@@ -229,7 +228,6 @@ namespace PcapDotNet.Core.Test ...@@ -229,7 +228,6 @@ namespace PcapDotNet.Core.Test
} }
else else
{ {
const byte RetryNumber = 89;
pcapFilename = Path.GetTempPath() + "temp." + RetryNumber + ".pcap"; pcapFilename = Path.GetTempPath() + "temp." + RetryNumber + ".pcap";
List<Packet> packetsList = new List<Packet>(); List<Packet> packetsList = new List<Packet>();
using (PacketCommunicator communicator = new OfflinePacketDevice(pcapFilename).Open()) using (PacketCommunicator communicator = new OfflinePacketDevice(pcapFilename).Open())
...@@ -248,6 +246,7 @@ namespace PcapDotNet.Core.Test ...@@ -248,6 +246,7 @@ namespace PcapDotNet.Core.Test
{ {
process.StartInfo = new ProcessStartInfo process.StartInfo = new ProcessStartInfo
{ {
// Wireshark's preferences file is %APPDATA%\Wireshark\preferences
FileName = WiresharkTsharkPath, FileName = WiresharkTsharkPath,
Arguments = "-o udp.check_checksum:TRUE " + Arguments = "-o udp.check_checksum:TRUE " +
"-o tcp.relative_sequence_numbers:FALSE " + "-o tcp.relative_sequence_numbers:FALSE " +
...@@ -255,6 +254,7 @@ namespace PcapDotNet.Core.Test ...@@ -255,6 +254,7 @@ namespace PcapDotNet.Core.Test
"-o tcp.track_bytes_in_flight:FALSE " + "-o tcp.track_bytes_in_flight:FALSE " +
"-o tcp.desegment_tcp_streams:FALSE " + "-o tcp.desegment_tcp_streams:FALSE " +
"-o tcp.check_checksum:TRUE " + "-o tcp.check_checksum:TRUE " +
"-o http.dechunk_body:FALSE " +
"-t r -n -r \"" + pcapFilename + "\" -T pdml", "-t r -n -r \"" + pcapFilename + "\" -T pdml",
WorkingDirectory = WiresharkDiretory, WorkingDirectory = WiresharkDiretory,
UseShellExecute = false, UseShellExecute = false,
...@@ -478,7 +478,7 @@ namespace PcapDotNet.Core.Test ...@@ -478,7 +478,7 @@ namespace PcapDotNet.Core.Test
switch (field.Name()) switch (field.Name())
{ {
case "arp.hw.type": case "arp.hw.type":
field.AssertShowHex((ushort)arpDatagram.HardwareType); field.AssertShowDecimal((ushort)arpDatagram.HardwareType);
break; break;
case "arp.proto.type": case "arp.proto.type":
...@@ -494,7 +494,7 @@ namespace PcapDotNet.Core.Test ...@@ -494,7 +494,7 @@ namespace PcapDotNet.Core.Test
break; break;
case "arp.opcode": case "arp.opcode":
field.AssertShowHex((ushort)arpDatagram.Operation); field.AssertShowDecimal((ushort)arpDatagram.Operation);
break; break;
case "arp.src.hw": case "arp.src.hw":
...@@ -1431,10 +1431,16 @@ namespace PcapDotNet.Core.Test ...@@ -1431,10 +1431,16 @@ namespace PcapDotNet.Core.Test
break; break;
case "": case "":
if (fieldShow == "HTTP chunked response")
{
throw new InvalidOperationException("HTTP chunked response");
}
data.Append(field.Value()); data.Append(field.Value());
if (fieldShow == @"\r\n") if (fieldShow == @"\r\n")
break; break;
else if (isFirstEmptyName)
if (isFirstEmptyName)
{ {
CompareHttpFirstLine(field, httpDatagram); CompareHttpFirstLine(field, httpDatagram);
isFirstEmptyName = false; isFirstEmptyName = false;
...@@ -1443,10 +1449,6 @@ namespace PcapDotNet.Core.Test ...@@ -1443,10 +1449,6 @@ namespace PcapDotNet.Core.Test
{ {
break; break;
} }
else if (fieldShow == "HTTP chunked response")
{
field.AssertValue(httpDatagram.Body);
}
else else
{ {
fieldShow = Encoding.GetEncoding(28591).GetString(HexEncoding.Instance.GetBytes(field.Value())); fieldShow = Encoding.GetEncoding(28591).GetString(HexEncoding.Instance.GetBytes(field.Value()));
...@@ -1546,7 +1548,10 @@ namespace PcapDotNet.Core.Test ...@@ -1546,7 +1548,10 @@ namespace PcapDotNet.Core.Test
break; break;
case "http.request.version": case "http.request.version":
field.AssertShow(httpDatagram.Version.ToString()); if (httpDatagram.Version == null)
field.AssertShow(string.Empty);
else
field.AssertShow(httpDatagram.Version.ToString());
break; break;
case "http.response.code": case "http.response.code":
......
...@@ -31,7 +31,7 @@ namespace PcapDotNet.Core.Test ...@@ -31,7 +31,7 @@ namespace PcapDotNet.Core.Test
if (currentChar >= 0x7F || currentChar < 0x20) if (currentChar >= 0x7F || currentChar < 0x20)
{ {
result.Append(@"\x"); result.Append(@"\x");
result.Append(((int)currentChar).ToString("x")); result.Append(((int)currentChar).ToString("x2"));
break; break;
} }
......
...@@ -50,7 +50,7 @@ namespace PcapDotNet.Packets.Test ...@@ -50,7 +50,7 @@ namespace PcapDotNet.Packets.Test
[TestMethod] [TestMethod]
public void RandomHttpTest() public void RandomHttpTest()
{ {
Random random = new Random(1); Random random = new Random();
for (int i = 0; i != 1000; ++i) for (int i = 0; i != 1000; ++i)
{ {
...@@ -76,6 +76,16 @@ namespace PcapDotNet.Packets.Test ...@@ -76,6 +76,16 @@ namespace PcapDotNet.Packets.Test
Assert.AreEqual(httpRequestLayer.Method, httpRequestDatagram.Method); Assert.AreEqual(httpRequestLayer.Method, httpRequestDatagram.Method);
Assert.AreEqual(httpRequestLayer.Uri, httpRequestDatagram.Uri); Assert.AreEqual(httpRequestLayer.Uri, httpRequestDatagram.Uri);
} }
else
{
Assert.IsFalse(httpDatagram.IsRequest);
Assert.IsTrue(httpDatagram.IsResponse);
HttpResponseLayer httpResponseLayer = (HttpResponseLayer)httpLayer;
HttpResponseDatagram httpResponseDatagram = (HttpResponseDatagram)httpDatagram;
Assert.AreEqual(httpResponseLayer.StatusCode, httpResponseDatagram.StatusCode);
Assert.AreEqual(httpResponseLayer.ReasonPhrase, httpResponseDatagram.ReasonPhrase);
}
Assert.AreEqual(httpLayer.Header, httpDatagram.Header); Assert.AreEqual(httpLayer.Header, httpDatagram.Header);
Assert.AreEqual(httpLayer.Body, httpDatagram.Body); Assert.AreEqual(httpLayer.Body, httpDatagram.Body);
Assert.AreEqual(httpLayer, httpDatagram.ExtractLayer(), "HTTP Layer"); Assert.AreEqual(httpLayer, httpDatagram.ExtractLayer(), "HTTP Layer");
......
...@@ -958,27 +958,47 @@ namespace PcapDotNet.Packets.TestUtils ...@@ -958,27 +958,47 @@ namespace PcapDotNet.Packets.TestUtils
// HTTP // HTTP
public static HttpLayer NextHttpLayer(this Random random) public static HttpLayer NextHttpLayer(this Random random)
{ {
// if (random.NextBool()) if (random.NextBool())
// { {
HttpRequestLayer httpRequestLayer = new HttpRequestLayer(); HttpRequestLayer httpRequestLayer = new HttpRequestLayer();
if (random.NextBool()) if (random.NextBool(10))
{ {
httpRequestLayer.Method = random.NextHttpRequestMethod(); httpRequestLayer.Method = random.NextHttpRequestMethod();
httpRequestLayer.Uri = httpRequestLayer.Method == null ? null : random.NextHttpUri(); httpRequestLayer.Uri = httpRequestLayer.Method == null ? null : random.NextHttpUri();
httpRequestLayer.Version = httpRequestLayer.Uri == null ? null : random.NextHttpVersion(); httpRequestLayer.Version = httpRequestLayer.Uri == null || random.NextBool(10) ? null : random.NextHttpVersion();
httpRequestLayer.Header = httpRequestLayer.Version == null ? null : random.NextHttpHeader(); httpRequestLayer.Header = httpRequestLayer.Version == null ? null : random.NextHttpHeader();
httpRequestLayer.Body = httpRequestLayer.Header == null ? null : random.NextHttpBody(true, null, httpRequestLayer.Header); httpRequestLayer.Body = httpRequestLayer.Header == null ? null : random.NextHttpBody(true, null, httpRequestLayer.Header);
} }
return httpRequestLayer; return httpRequestLayer;
// } }
// else
// { HttpResponseLayer httpResponseLayer = new HttpResponseLayer
// httpLayer = new HttpResponseLayer {
// { Version = random.NextHttpVersion(),
// StatusCode = random.NextBool(10) ? null : (uint?)random.NextUInt(100, 999),
// }; };
// } httpResponseLayer.ReasonPhrase = httpResponseLayer.StatusCode == null ? null : random.NextHttpReasonPhrase();
httpResponseLayer.Header = httpResponseLayer.ReasonPhrase == null ? null : random.NextHttpHeader();
httpResponseLayer.Body = httpResponseLayer.Header == null ? null : random.NextHttpBody(false , httpResponseLayer.StatusCode, httpResponseLayer.Header);
return httpResponseLayer;
}
public static Datagram NextHttpReasonPhrase(this Random random)
{
int reasonPhraseLength = random.Next(100);
StringBuilder reasonPhrase = new StringBuilder(reasonPhraseLength);
for (int i = 0; i != reasonPhraseLength; ++i)
{
if (random.NextBool())
reasonPhrase.Append(random.NextHttpTextChar());
else if (random.NextBool())
reasonPhrase.Append(' ');
else
reasonPhrase.Append('\t');
}
return new Datagram(Encoding.GetEncoding(28591).GetBytes(reasonPhrase.ToString()));
} }
public static HttpRequestMethod NextHttpRequestMethod(this Random random) public static HttpRequestMethod NextHttpRequestMethod(this Random random)
...@@ -1127,7 +1147,13 @@ namespace PcapDotNet.Packets.TestUtils ...@@ -1127,7 +1147,13 @@ namespace PcapDotNet.Packets.TestUtils
int numParameters = random.Next(10); int numParameters = random.Next(10);
List<KeyValuePair<string, string>> parameters = new List<KeyValuePair<string, string>>(numParameters); List<KeyValuePair<string, string>> parameters = new List<KeyValuePair<string, string>>(numParameters);
for (int i = 0; i != numParameters; ++i) for (int i = 0; i != numParameters; ++i)
parameters.Add(new KeyValuePair<string, string>(random.NextHttpToken(), random.NextBool() ? random.NextHttpToken() : random.NextHttpQuotedString())); {
string parameterName = random.NextHttpToken();
while (parameters.Any(pair => pair.Key == parameterName))
parameterName = random.NextHttpToken();
parameters.Add(new KeyValuePair<string, string>(parameterName,
random.NextBool() ? random.NextHttpToken() : random.NextHttpQuotedString()));
}
return new HttpFieldParameters(parameters); return new HttpFieldParameters(parameters);
} }
......
...@@ -17,7 +17,7 @@ namespace PcapDotNet.Packets.Http ...@@ -17,7 +17,7 @@ namespace PcapDotNet.Packets.Http
{ {
return base.Equals(other) && return base.Equals(other) &&
StatusCode == other.StatusCode && StatusCode == other.StatusCode &&
ReasonPhrase == other.ReasonPhrase; (ReferenceEquals(ReasonPhrase, other.ReasonPhrase) || ReasonPhrase.Equals(other.ReasonPhrase));
} }
protected override int FirstLineLength protected override int FirstLineLength
......
...@@ -7,9 +7,11 @@ namespace PcapDotNet.TestUtils ...@@ -7,9 +7,11 @@ namespace PcapDotNet.TestUtils
{ {
public static class RandomExtensions public static class RandomExtensions
{ {
public static bool NextBool(this Random random) public static bool NextBool(this Random random, int chance = 2)
{ {
return random.Next() % 2 == 0; if (chance < 1)
throw new ArgumentOutOfRangeException("chance", chance, "must be at least 1");
return random.Next() % chance == 0;
} }
public static byte NextByte(this Random random, int minValue, int maxValue) public static byte NextByte(this Random random, int minValue, int maxValue)
...@@ -64,6 +66,11 @@ namespace PcapDotNet.TestUtils ...@@ -64,6 +66,11 @@ namespace PcapDotNet.TestUtils
return random.NextUInt() % maxValue; return random.NextUInt() % maxValue;
} }
public static uint NextUInt(this Random random, uint minvalue, uint maxValue)
{
return random.NextUInt() % (maxValue - minvalue + 1) + minvalue;
}
public static UInt48 NextUInt48(this Random random) public static UInt48 NextUInt48(this Random random)
{ {
return (UInt48)random.NextLong(UInt48.MaxValue + 1); return (UInt48)random.NextLong(UInt48.MaxValue + 1);
......
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