Skip to content

Commit

Permalink
[Serializable] Attribute added to the protocol classes
Browse files Browse the repository at this point in the history
Unit tests added to test serializing and deserializing the protocol classes
  • Loading branch information
dgreen81 authored and chmorgan committed Aug 3, 2014
1 parent 8c7f8e4 commit 2c7a7f5
Show file tree
Hide file tree
Showing 41 changed files with 737 additions and 2 deletions.
1 change: 1 addition & 0 deletions PacketDotNet/ARPPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace PacketDotNet
/// <summary>
/// An ARP protocol packet.
/// </summary>
[Serializable]
public class ARPPacket : InternetLinkLayerPacket
{
#if DEBUG
Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/EthernetPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace PacketDotNet
/// <summary>
/// See http://en.wikipedia.org/wiki/Ethernet#Ethernet_frame_types_and_the_EtherType_field
/// </summary>
[Serializable]
public class EthernetPacket : InternetLinkLayerPacket
{
#if DEBUG
Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/IPv4Packet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace PacketDotNet
/// IPv4 packet
/// See http://en.wikipedia.org/wiki/IPv4 for into
/// </summary>
[Serializable]
public class IPv4Packet : IpPacket
{
#if DEBUG
Expand Down
3 changes: 2 additions & 1 deletion PacketDotNet/IPv6Packet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ namespace PacketDotNet
/// ----------
/// http://tools.ietf.org/html/rfc2460
/// http://en.wikipedia.org/wiki/IPv6
/// </summary>
/// </summary>
[Serializable]
public class IPv6Packet : IpPacket
{
#if DEBUG
Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/InternetLinkLayerPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace PacketDotNet
/// Internet Link layer packet
/// See http://en.wikipedia.org/wiki/Link_Layer
/// </summary>
[Serializable]
public class InternetLinkLayerPacket : Packet
{
#if DEBUG
Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/InternetPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace PacketDotNet
/// Internet packets include IPv4, IPv6, IGMP etc, see
/// http://en.wikipedia.org/wiki/Internet_Layer
/// </summary>
[Serializable]
public class InternetPacket : Packet
{
/// <summary>
Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/IpPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace PacketDotNet
/// Base class for IPv4 and IPv6 packets that exports the common
/// functionality that both of these classes has in common
/// </summary>
[Serializable]
public abstract class IpPacket : InternetPacket
{
#if DEBUG
Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/LLDP/ChassisID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace PacketDotNet.LLDP
/// <summary>
/// A Chassis ID TLV
/// </summary>
[Serializable]
public class ChassisID : TLV
{
#if DEBUG
Expand Down
2 changes: 2 additions & 0 deletions PacketDotNet/LLDP/EndOfLLDPDU.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ You should have received a copy of the GNU Lesser General Public License
* Copyright 2010 Evan Plaice <evanplaice@gmail.com>
* Copyright 2010 Chris Morgan <chmorgan@gmail.com>
*/
using System;
namespace PacketDotNet.LLDP
{
/// <summary>
/// An End Of LLDPDU TLV
/// </summary>
[Serializable]
public class EndOfLLDPDU : TLV
{
#region Constructors
Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/LLDP/ManagementAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace PacketDotNet.LLDP
/// [Interface Subtype : 1][Interface number : 4][OID length : 1][OID : 0-128]
///
/// </summary>
[Serializable]
public class ManagementAddress : TLV
{
#if DEBUG
Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/LLDP/OrganizationSpecific.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace PacketDotNet.LLDP
/// [TLV Type Length : 2][Organizationally Unique Identifier OUI : 3]
/// [Organizationally Defined Subtype : 1][Organizationally Defined Information String : 0 - 507]
/// </summary>
[Serializable]
public class OrganizationSpecific : TLV
{
#if DEBUG
Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/LLDP/PortDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace PacketDotNet.LLDP
/// <summary>
/// A Port Description TLV
/// </summary>
[Serializable]
public class PortDescription : StringTLV
{
#if DEBUG
Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/LLDP/PortID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace PacketDotNet.LLDP
/// <summary>
/// A Port ID TLV
/// </summary>
[Serializable]
public class PortID : TLV
{
#if DEBUG
Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/LLDP/StringTLV.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace PacketDotNet.LLDP
/// <summary>
/// Base class for several TLV types that all contain strings
/// </summary>
[Serializable]
public class StringTLV : TLV
{
#region Constructors
Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/LLDP/SystemCapabilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace PacketDotNet.LLDP
///
/// [TLVTypeLength - 2 bytes][System Capabilities - 2 bytes][Enabled Capabilities - 2 bytes]
/// </summary>
[Serializable]
public class SystemCapabilities : TLV
{
private const int SystemCapabilitiesLength = 2;
Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/LLDP/SystemDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace PacketDotNet.LLDP
/// <summary>
/// A System Description TLV
/// </summary>
[Serializable]
public class SystemDescription : StringTLV
{
#if DEBUG
Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/LLDP/SystemName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace PacketDotNet.LLDP
/// <summary>
/// A System Name TLV
/// </summary>
[Serializable]
public class SystemName : StringTLV
{
#region Constructors
Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/LLDP/TLV.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace PacketDotNet.LLDP
/// <summary>
/// A Type-Length-Value object
/// </summary>
[Serializable]
public class TLV
{
#if DEBUG
Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/LLDP/TLVCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace PacketDotNet
/// - Preventing an EndOfLLDPDU tlv from being added out of place
/// - Checking and throwing exceptions if one-per-LLDP packet TLVs are added multiple times
/// </summary>
[Serializable]
public class TLVCollection : Collection<TLV>
{
#if DEBUG
Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/LLDP/TLVTypeLength.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace PacketDotNet.LLDP
/// Tlv type and length are 2 bytes
/// See http://en.wikipedia.org/wiki/Link_Layer_Discovery_Protocol#Frame_structure
/// </summary>
[Serializable]
public class TLVTypeLength
{
#if DEBUG
Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/LLDP/TimeToLive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace PacketDotNet.LLDP
/// <summary>
/// A Time to Live TLV
/// </summary>
[Serializable]
public class TimeToLive : TLV
{
#if DEBUG
Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/LLDPPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace PacketDotNet
/// See http://en.wikipedia.org/wiki/Link_Layer_Discovery_Protocol for general info
/// See IETF 802.1AB for the full specification
/// </remarks>
[Serializable]
public class LLDPPacket : InternetLinkLayerPacket, IEnumerable
{

Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/PPPPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace PacketDotNet
/// PPP packet
/// See http://en.wikipedia.org/wiki/Point-to-Point_Protocol
/// </summary>
[Serializable]
public class PPPPacket : Packet
{
#if DEBUG
Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/Packet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace PacketDotNet
/// Defines helper methods and accessors for the architecture that underlies how
/// packets interact and store their data.
/// </summary>
[Serializable]
public abstract class Packet
{
#if DEBUG
Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/PacketOrByteArraySegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace PacketDotNet
/// Encapsulates and ensures that we have either a Packet OR
/// a ByteArraySegment but not both
/// </summary>
[Serializable]
public class PacketOrByteArraySegment
{
private ByteArraySegment theByteArraySegment;
Expand Down
3 changes: 2 additions & 1 deletion PacketDotNet/TcpPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ namespace PacketDotNet
/// <summary>
/// TcpPacket
/// See: http://en.wikipedia.org/wiki/Transmission_Control_Protocol
/// </summary>
/// </summary>
[Serializable]
public class TcpPacket : TransportPacket
{
#if DEBUG
Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/TransportPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace PacketDotNet
/// <summary>
/// Transport layer packet
/// </summary>
[Serializable]
public abstract class TransportPacket : Packet
{
#if DEBUG
Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/UdpPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace PacketDotNet
/// User datagram protocol
/// See http://en.wikipedia.org/wiki/Udp
/// </summary>
[Serializable]
public class UdpPacket : TransportPacket
{
#if DEBUG
Expand Down
1 change: 1 addition & 0 deletions PacketDotNet/Utils/ByteArraySegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace PacketDotNet.Utils
/// Used to ensure high performance by allowing memory copies to
/// be avoided
/// </summary>
[Serializable]
public class ByteArraySegment
{
#if DEBUG
Expand Down
62 changes: 62 additions & 0 deletions Test/PacketType/ArpPacketTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ You should have received a copy of the GNU Lesser General Public License
using PacketDotNet;
using PacketDotNet.Utils;
using SharpPcap;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Test.PacketType
{
Expand Down Expand Up @@ -156,5 +158,65 @@ public void PrintVerboseString()
Console.WriteLine("Printing human readable string");
Console.WriteLine(arp.ToString(StringOutputType.Verbose));
}

[Test]
public void BinarySerialization()
{
var dev = new CaptureFileReaderDevice("../../CaptureFiles/arp_request_response.pcap");
dev.Open();

RawCapture rawCapture;
bool foundARP = false;
while ((rawCapture = dev.GetNextPacket()) != null)
{
var p = PacketDotNet.Packet.ParsePacket(rawCapture.LinkLayerType, rawCapture.Data);

var arpPacket = (ARPPacket)p.Extract(typeof(ARPPacket));
if (arpPacket == null)
{
continue;
}
foundARP = true;

var memoryStream = new MemoryStream();
BinaryFormatter serializer = new BinaryFormatter();
serializer.Serialize(memoryStream, arpPacket);

memoryStream.Seek (0, SeekOrigin.Begin);
BinaryFormatter deserializer = new BinaryFormatter();
ARPPacket fromFile = (ARPPacket)deserializer.Deserialize(memoryStream);

CollectionAssert.AreEqual(arpPacket.Bytes, fromFile.Bytes);
Assert.AreEqual(arpPacket.BytesHighPerformance.Bytes, fromFile.BytesHighPerformance.Bytes);
Assert.AreEqual(arpPacket.BytesHighPerformance.BytesLength, fromFile.BytesHighPerformance.BytesLength);
Assert.AreEqual(arpPacket.BytesHighPerformance.Length, fromFile.BytesHighPerformance.Length);
Assert.AreEqual(arpPacket.BytesHighPerformance.NeedsCopyForActualBytes, fromFile.BytesHighPerformance.NeedsCopyForActualBytes);
Assert.AreEqual(arpPacket.BytesHighPerformance.Offset, fromFile.BytesHighPerformance.Offset);
Assert.AreEqual(arpPacket.Color, fromFile.Color);
Assert.AreEqual(arpPacket.HardwareAddressLength, fromFile.HardwareAddressLength);
Assert.AreEqual(arpPacket.HardwareAddressType, fromFile.HardwareAddressType);
CollectionAssert.AreEqual(arpPacket.Header, fromFile.Header);
Assert.AreEqual(arpPacket.Operation, fromFile.Operation);
Assert.AreEqual(arpPacket.ParentPacket, fromFile.ParentPacket);
CollectionAssert.AreEqual(arpPacket.PayloadData, fromFile.PayloadData);
Assert.AreEqual(arpPacket.PayloadPacket, fromFile.PayloadPacket);
Assert.AreEqual(arpPacket.ProtocolAddressLength, fromFile.ProtocolAddressLength);
Assert.AreEqual(arpPacket.ProtocolAddressType, fromFile.ProtocolAddressType);
Assert.AreEqual(arpPacket.SenderHardwareAddress, fromFile.SenderHardwareAddress);
Assert.AreEqual(arpPacket.SenderProtocolAddress, fromFile.SenderProtocolAddress);
Assert.AreEqual(arpPacket.TargetHardwareAddress, fromFile.TargetHardwareAddress);
Assert.AreEqual(arpPacket.TargetProtocolAddress, fromFile.TargetProtocolAddress);

//Method Invocations to make sure that a deserialized packet does not cause
//additional errors.

arpPacket.PrintHex();
arpPacket.UpdateCalculatedValues();
}

dev.Close();

Assert.IsTrue(foundARP, "Capture file contained no ARP packets");
}
}
}
50 changes: 50 additions & 0 deletions Test/PacketType/EthernetPacketTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ You should have received a copy of the GNU Lesser General Public License
using SharpPcap.LibPcap;
using PacketDotNet;
using PacketDotNet.Utils;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Test.PacketType
{
Expand Down Expand Up @@ -293,5 +295,53 @@ public void RandomPacket()
{
EthernetPacket.RandomPacket();
}

[Test]
public void BinarySerialization()
{
var dev = new CaptureFileReaderDevice("../../CaptureFiles/tcp.pcap");
dev.Open();

RawCapture rawCapture;
bool foundEthernet = false;
while ((rawCapture = dev.GetNextPacket()) != null)
{
var ethernetPacket = new EthernetPacket(new ByteArraySegment(rawCapture.Data));
if (ethernetPacket == null)
{
continue;
}
foundEthernet = true;

var memoryStream = new MemoryStream();
BinaryFormatter serializer = new BinaryFormatter();
serializer.Serialize(memoryStream, ethernetPacket);

memoryStream.Seek (0, SeekOrigin.Begin);
BinaryFormatter deserializer = new BinaryFormatter();
EthernetPacket fromFile = (EthernetPacket)deserializer.Deserialize(memoryStream);

Assert.AreEqual(ethernetPacket.Bytes, fromFile.Bytes);
Assert.AreEqual(ethernetPacket.BytesHighPerformance.Bytes, fromFile.BytesHighPerformance.Bytes);
Assert.AreEqual(ethernetPacket.BytesHighPerformance.BytesLength, fromFile.BytesHighPerformance.BytesLength);
Assert.AreEqual(ethernetPacket.BytesHighPerformance.Length, fromFile.BytesHighPerformance.Length);
Assert.AreEqual(ethernetPacket.BytesHighPerformance.NeedsCopyForActualBytes, fromFile.BytesHighPerformance.NeedsCopyForActualBytes);
Assert.AreEqual(ethernetPacket.BytesHighPerformance.Offset, fromFile.BytesHighPerformance.Offset);
Assert.AreEqual(ethernetPacket.Color, fromFile.Color);
Assert.AreEqual(ethernetPacket.DestinationHwAddress, fromFile.DestinationHwAddress);
Assert.AreEqual(ethernetPacket.Header, fromFile.Header);
Assert.AreEqual(ethernetPacket.ParentPacket, fromFile.ParentPacket);
Assert.AreEqual(ethernetPacket.PayloadData, fromFile.PayloadData);
Assert.AreEqual(ethernetPacket.SourceHwAddress, fromFile.SourceHwAddress);
Assert.AreEqual(ethernetPacket.Type, fromFile.Type);

}

dev.Close();
Assert.IsTrue(foundEthernet, "Capture file contained no Ethernet packets");


}

}
}
Loading

0 comments on commit 2c7a7f5

Please sign in to comment.