Click here to Skip to main content
15,885,760 members
Articles / Web Development / HTML

SharpPcap - A Packet Capture Framework for .NET

,
Rate me:
Please Sign up or sign in to vote.
4.94/5 (192 votes)
5 May 2014MPL21 min read 2.1M   10.3K   518  
A framework for capturing, injecting and analyzing network packets for .NET applications based on the WinPcap packet capture driver
//// $Id: PacketEncodingTest.java,v 1.1 2002/07/10 23:12:09 pcharles Exp $
//
///// <summary>************************************************************************
///// Copyright (C) 2001, Patrick Charles and Jonas Lehmann                   *
///// Distributed under the Mozilla Public License                            *
///// http://www.mozilla.org/NPL/MPL-1.1.txt                                *
///// *************************************************************************
///// </summary>
//namespace Tamir.IPLib.Packets
//{
//	using System;
//	//using junit.framework;
//	/// <summary>*
//	/// </summary>
//	/// <author>  jguthrie
//	/// 
//	/// </author>
//	public class PacketEncodingTest:TestCase
//	{
//		private void  InitBlock()
//		{
//			goodSizedData2 = new byte[]{6, 7, 8, 9};
//			goodData2 = new byte[]{6, 7, 8, 9, 10};
//			goodHeader2 = new byte[]{3, 4, 5};
//			goodSizedData0 = new byte[]{4, 5, 6, 7};
//			goodData0 = new byte[]{4, 5, 6, 7, 8, 9, 10};
//			goodHeader0 = new byte[]{1, 2, 3};
//			testBytes = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
//		}
//		//UPGRADE_NOTE: The initialization of  'testBytes' was moved to method 'InitBlock'. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1005"'
//		internal byte[] testBytes;
//		internal int headerLen = 3;
//		//UPGRADE_NOTE: The initialization of  'goodHeader0' was moved to method 'InitBlock'. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1005"'
//		internal byte[] goodHeader0;
//		//UPGRADE_NOTE: The initialization of  'goodData0' was moved to method 'InitBlock'. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1005"'
//		internal byte[] goodData0;
//		//UPGRADE_NOTE: The initialization of  'goodSizedData0' was moved to method 'InitBlock'. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1005"'
//		internal byte[] goodSizedData0;
//		//UPGRADE_NOTE: The initialization of  'goodHeader2' was moved to method 'InitBlock'. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1005"'
//		internal byte[] goodHeader2;
//		//UPGRADE_NOTE: The initialization of  'goodData2' was moved to method 'InitBlock'. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1005"'
//		internal byte[] goodData2;
//		//UPGRADE_NOTE: The initialization of  'goodSizedData2' was moved to method 'InitBlock'. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1005"'
//		internal byte[] goodSizedData2;
//		
//		public PacketEncodingTest(System.String testName):base(testName)
//		{
//			InitBlock();
//		}
//		
//		[STAThread]
//		public static void  Main(System.String[] args)
//		{
//			junit.textui.TestRunner.run(suite());
//		}
//		
//		public static Test suite()
//		{
//			TestSuite suite = new TestSuite(typeof(PacketEncodingTest));
//			
//			return suite;
//		}
//		
//		private bool sameBytes(byte[] b1, byte[] b2)
//		{
//			if ((b1 == null) || (b2 == null))
//				return false;
//			// nulls are bad
//			if (b1.Length != b2.Length)
//				return false;
//			// different lengths are bad
//			for (int i = 0; i < b1.Length; i++)
//			{
//				if (b1[i] != b2[i])
//					return false;
//				// different values are bad
//			}
//			return true; // nothing bad, so that's good
//		}
//		
//		private System.String bytesAsString(byte[] bytes)
//		{
//			if (bytes == null)
//				return "null";
//			System.Text.StringBuilder buf = new System.Text.StringBuilder("[");
//			System.String sep = "";
//			for (int i = 0; i < bytes.Length; i++)
//			{
//				buf.Append(sep);
//				sep = ",";
//				buf.Append((byte) bytes[i]);
//			}
//			buf.Append("]");
//			return buf.ToString();
//		}
//		
//		/// <summary>Test of extractHeader method with header start at position 0 
//		/// </summary>
//		public virtual void  testExtractHeaderAtPosition0()
//		{
//			byte[] header = PacketEncoding.extractHeader(0, headerLen, testBytes);
//			assertTrue("byte array mismatch, " + bytesAsString(header) + ", should be " + bytesAsString(goodHeader0), sameBytes(header, goodHeader0));
//		}
//		
//		/// <summary>Test of extractHeader method with header start at position 2 
//		/// </summary>
//		public virtual void  testExtractHeaderAtPosition2()
//		{
//			byte[] header = PacketEncoding.extractHeader(2, headerLen, testBytes);
//			assertTrue("byte array mismatch, " + bytesAsString(header) + ", should be " + bytesAsString(goodHeader2), sameBytes(header, goodHeader2));
//		}
//		
//		/// <summary>Test of extractHeader method with offset out of bounds.
//		/// Should return 0 length byte array 
//		/// </summary>
//		public virtual void  testExtractHeaderTooLargeOffset()
//		{
//			byte[] header = PacketEncoding.extractHeader(20, headerLen, testBytes);
//			assertTrue("bad parameter did not return 0-length array", (header.Length == 0));
//		}
//		
//		/// <summary>Test of extractHeader method with offset out of bounds.
//		/// Should return whole byte array 
//		/// </summary>
//		public virtual void  testExtractHeaderTooLargeLength()
//		{
//			byte[] header = PacketEncoding.extractHeader(0, 20, testBytes);
//			assertTrue("byte array mismatch, " + bytesAsString(header) + ", should be " + bytesAsString(testBytes), sameBytes(header, testBytes));
//		}
//		
//		/// <summary>Test of extractHeader method with negative offset. 
//		/// </summary>
//		public virtual void  testExtractHeaderNegativeOffset()
//		{
//			byte[] header = PacketEncoding.extractHeader(- 20, headerLen, testBytes);
//			assertTrue("negative parameter did not return 0-length array", (header.Length == 0));
//		}
//		
//		/// <summary>Test of extractHeader method with negative header length.
//		/// Should return 0 length byte array 
//		/// </summary>
//		public virtual void  testExtractHeaderNegativeLength()
//		{
//			byte[] header = PacketEncoding.extractHeader(0, - 20, testBytes);
//			assertTrue("negative parameter did not return 0-length array", (header.Length == 0));
//		}
//		
//		/// <summary>Test of extractHeader method with null input array.
//		/// Should return null byte array 
//		/// </summary>
//		public virtual void  testExtractHeaderNullArray()
//		{
//			byte[] header = PacketEncoding.extractHeader(0, 20, null);
//			assertNull("null in did not return null", header);
//		}
//		
//		/// <summary>Test of extractData method with header start at position 0 
//		/// </summary>
//		public virtual void  testExtractDataAtPosition0()
//		{
//			byte[] data = PacketEncoding.extractData(0, headerLen, testBytes);
//			assertTrue("byte array mismatch, " + bytesAsString(data) + ", should be " + bytesAsString(goodData0), sameBytes(data, goodData0));
//		}
//		
//		/// <summary>Test of extractData method with header start at position 2 
//		/// </summary>
//		public virtual void  testExtractDataAtPosition2()
//		{
//			byte[] data = PacketEncoding.extractData(2, headerLen, testBytes);
//			assertTrue("byte array mismatch, " + bytesAsString(data) + ", should be " + bytesAsString(goodData2), sameBytes(data, goodData2));
//		}
//		
//		/// <summary>Test of extractData method with offset out of bounds.
//		/// Should return 0 length byte array 
//		/// </summary>
//		public virtual void  testExtractDataTooLargeOffset()
//		{
//			byte[] data = PacketEncoding.extractData(20, headerLen, testBytes);
//			assertTrue("bad parameter did not return 0-length array", (data.Length == 0));
//		}
//		
//		/// <summary>Test of extractData method with header length out of bounds.
//		/// Should return whole byte array 
//		/// </summary>
//		public virtual void  testExtractDataTooLargeLength()
//		{
//			byte[] data = PacketEncoding.extractData(0, 20, testBytes);
//			assertTrue("bad parameter did not return 0-length array", (data.Length == 0));
//		}
//		
//		/// <summary>Test of extractData method with negative offset.
//		/// Should return 0 length byte array 
//		/// </summary>
//		public virtual void  testExtractDataNegativeOffset()
//		{
//			byte[] data = PacketEncoding.extractData(- 20, headerLen, testBytes);
//			assertTrue("negative parameter did not return 0-length array", (data.Length == 0));
//		}
//		
//		/// <summary>Test of extractData method with negative header length.
//		/// Should return 0 length byte array 
//		/// </summary>
//		public virtual void  testExtractDataNegativeLength()
//		{
//			byte[] data = PacketEncoding.extractData(0, - 20, testBytes);
//			assertTrue("negative parameter did not return 0-length array", (data.Length == 0));
//		}
//		
//		/// <summary>Test of extractData method with null input array.
//		/// Should return null byte array 
//		/// </summary>
//		public virtual void  testExtractDataNullArray()
//		{
//			byte[] data = PacketEncoding.extractData(0, 20, null);
//			assertNull("null in did not return null", data);
//		}
//		
//		/// <summary>Test of extractData method with header start at position 0 
//		/// </summary>
//		public virtual void  testExtractSizedDataAtPosition0()
//		{
//			byte[] data = PacketEncoding.extractData(0, headerLen, testBytes, 4);
//			assertTrue("byte array mismatch, " + bytesAsString(data) + ", should be " + bytesAsString(goodSizedData0), sameBytes(data, goodSizedData0));
//		}
//		
//		/// <summary>Test of extractData method with header start at position 0 
//		/// </summary>
//		public virtual void  testExtractSizedDataAtPosition2()
//		{
//			byte[] data = PacketEncoding.extractData(2, headerLen, testBytes, 4);
//			assertTrue("byte array mismatch, " + bytesAsString(data) + ", should be " + bytesAsString(goodSizedData2), sameBytes(data, goodSizedData2));
//		}
//		
//		/// <summary>Test of extractData method with offset out of bounds.
//		/// Should return 0 length byte array 
//		/// </summary>
//		public virtual void  testExtractSizedDataTooLargeOffset()
//		{
//			byte[] data = PacketEncoding.extractData(20, headerLen, testBytes, 4);
//			assertTrue("bad parameter did not return 0-length array", (data.Length == 0));
//		}
//		
//		/// <summary>Test of extractData method with header length out of bounds.
//		/// Should return whole byte array 
//		/// </summary>
//		public virtual void  testExtractSizedDataTooLargeLength()
//		{
//			byte[] data = PacketEncoding.extractData(0, 20, testBytes, 4);
//			assertTrue("bad parameter did not return 0-length array", (data.Length == 0));
//		}
//		
//		/// <summary>Test of extractData method with negative size.
//		/// Should return 0 length byte array 
//		/// </summary>
//		public virtual void  testExtractSizedDataNegativeSize()
//		{
//			byte[] data = PacketEncoding.extractData(0, headerLen, testBytes, - 2);
//			assertTrue("bad parameter did not return 0-length array", (data.Length == 0));
//		}
//		
//		/// <summary>Test of extractData method with header length out of bounds.
//		/// Should return whole byte array 
//		/// </summary>
//		public virtual void  testExtractSizedDataTooLargeSize()
//		{
//			byte[] data = PacketEncoding.extractData(0, headerLen, testBytes, 20);
//			assertTrue("byte array mismatch, " + bytesAsString(data) + ", should be " + bytesAsString(goodData0), sameBytes(data, goodData0));
//		}
//		
//		/// <summary>Test of extractData method with null input array.
//		/// Should return null byte array 
//		/// </summary>
//		public virtual void  testExtractSizedDataNullArray()
//		{
//			byte[] data = PacketEncoding.extractData(0, 20, null, 4);
//			assertNull("null in did not return null", data);
//		}
//	}
//}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Mozilla Public License 1.1 (MPL 1.1)


Written By
Software Developer
Israel Israel
Works as a Network Engineer for a leading networking company.

Written By
United States United States
Entrepreneur and product developer with a wide range of technical and business experience.

Comments and Discussions