Author: alanmc
Date: 2008-02-02 11:27:58 -0500 (Sat, 02 Feb 2008)
New Revision: 94638
Added:
trunk/bitsharp/src/MonoTorrent.Tests/AllowedFastAlgorithmTest.cs
trunk/bitsharp/src/MonoTorrent.Tests/BitFieldTest.cs
trunk/bitsharp/src/MonoTorrent.Tests/FileManager.cs
trunk/bitsharp/src/MonoTorrent.Tests/MonoTorrent.Tests.csproj
trunk/bitsharp/src/MonoTorrent.Tests/PeerMessagesTest.cs
Removed:
trunk/bitsharp/src/MonoTorrent.Tests/Client/
Log:
Updated the tests!
Copied: trunk/bitsharp/src/MonoTorrent.Tests/AllowedFastAlgorithmTest.cs (from
rev 94632,
trunk/bitsharp/src/MonoTorrent.Tests/Client/Tests/AllowedFastAlgorithmTest.cs)
===================================================================
---
trunk/bitsharp/src/MonoTorrent.Tests/Client/Tests/AllowedFastAlgorithmTest.cs
2008-02-02 15:34:30 UTC (rev 94632)
+++ trunk/bitsharp/src/MonoTorrent.Tests/AllowedFastAlgorithmTest.cs
2008-02-02 16:27:58 UTC (rev 94638)
@@ -0,0 +1,63 @@
+//
+// AllowedFastAlgorithm.cs
+//
+// Authors:
+// Alan McGovern [EMAIL PROTECTED]
+//
+// Copyright (C) 2006 Alan McGovern
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+
+using System;
+using System.Collections.Generic;
+using System.Text;
+using NUnit.Framework;
+using System.Net;
+
+
+namespace MonoTorrent.Client.Tests
+{
+ //[TestFixture]
+ //public class AllowedFastAlgorithmTest
+ //{
+ // [Test]
+ // public void CalculateTest()
+ // {
+ // byte[] infohash = new byte[20];
+ // IPEndPoint endpoint = new
IPEndPoint(IPAddress.Parse("80.4.4.200"), 24142);
+
+ // for (int i = 0; i < infohash.Length; i++)
+ // infohash[i] = (byte)170;
+
+ // List<UInt32> results =
AllowedFastAlgorithm.Calculate(endpoint.Address.GetAddressBytes(), infohash, 9,
(UInt32)1313);
+ // Assert.AreEqual(1059, results[0], "#1");
+ // Assert.AreEqual(431, results[1], "#2");
+ // Assert.AreEqual(808, results[2], "#3");
+ // Assert.AreEqual(1217, results[3], "#4");
+ // Assert.AreEqual(287, results[4], "#5");
+ // Assert.AreEqual(376, results[5], "#6");
+ // Assert.AreEqual(1188, results[6], "#7");
+ // Assert.AreEqual(353, results[7], "#8");
+ // Assert.AreEqual(508, results[8], "#9");
+ // }
+ //}
+}
Property changes on:
trunk/bitsharp/src/MonoTorrent.Tests/AllowedFastAlgorithmTest.cs
___________________________________________________________________
Name: svn:eol-style
+ native
Copied: trunk/bitsharp/src/MonoTorrent.Tests/BitFieldTest.cs (from rev 94632,
trunk/bitsharp/src/MonoTorrent.Tests/Client/Tests/BitFieldTest.cs)
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Tests/Client/Tests/BitFieldTest.cs
2008-02-02 15:34:30 UTC (rev 94632)
+++ trunk/bitsharp/src/MonoTorrent.Tests/BitFieldTest.cs 2008-02-02
16:27:58 UTC (rev 94638)
@@ -0,0 +1,183 @@
+//
+// BitFieldTest.cs
+//
+// Authors:
+// Alan McGovern [EMAIL PROTECTED]
+//
+// Copyright (C) 2006 Alan McGovern
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+
+
+using System;
+using NUnit.Framework;
+
+namespace MonoTorrent.Client.Tests
+{
+ /*
+ [TestFixture]
+ public class BitFieldTest
+ {
+
+ private BitField bf;
+ private int length;
+
+ [SetUp]
+ public void SetUp()
+ {
+ this.length = 10;
+ bf = new BitField(length);
+ }
+
+ [Test]
+ public void GetterSetter()
+ {
+ bf[0] = true;
+ bf[1] = true;
+ bf[3] = true;
+ bf[5] = true;
+
+ Assert.IsTrue(bf[0]);
+ Assert.IsTrue(bf[1]);
+ Assert.IsTrue(!bf[2]);
+ Assert.IsTrue(bf[3]);
+ Assert.IsTrue(!bf[4]);
+ Assert.IsTrue(bf[5]);
+ Assert.IsTrue(!bf[6]);
+ Assert.IsTrue(!bf[7]);
+ Assert.IsTrue(!bf[8]);
+ Assert.IsTrue(!bf[9]);
+ }
+
+ [Test]
+ public void Clone()
+ {
+ BitField bf2 = (BitField)bf.Clone();
+
+ Assert.AreEqual(bf.AllFalse(), bf2.AllFalse(), "AllFalse wrong");
+ Assert.AreEqual(bf.FirstTrue(), bf2.FirstTrue(), "FirstTrue
wrong");
+ Assert.AreEqual(bf.Length, bf2.Length, "Length wrong");
+ Assert.AreEqual(bf.LengthInBytes, bf2.LengthInBytes, "Length in
bytes wrong");
+
+ bf[4] = true;
+ bf[7] = true;
+
+ Assert.IsTrue(!bf2[4], "Clone changed1");
+ Assert.IsTrue(!bf2[7], "Clone changed");
+ }
+
+ [Test]
+ public void Length()
+ {
+ Assert.AreEqual(length, bf.Length);
+ }
+
+ [Test]
+ public void LengthInBytes()
+ {
+ Assert.AreEqual((int)Math.Ceiling(this.length / 8.0),
bf.LengthInBytes);
+ }
+
+ [Test]
+ public void And()
+ {
+ BitField bf1 = new BitField(5);
+ BitField bf2 = new BitField(5);
+
+ bf1[0] = true; bf2[0] = true;
+ bf1[4] = true; bf2[4] = true;
+
+ bf1[3] = true;
+ bf2[2] = true;
+
+ bf1.And(bf2);
+
+ Assert.IsTrue(bf1[0]);
+ Assert.IsTrue(!bf1[1]);
+ Assert.IsTrue(!bf1[2]);
+ Assert.IsTrue(!bf1[3]);
+ Assert.IsTrue(bf1[4]);
+
+ Assert.IsTrue(bf2[0]);
+ Assert.IsTrue(!bf2[1]);
+ Assert.IsTrue(bf2[2]);
+ Assert.IsTrue(!bf2[3]);
+ Assert.IsTrue(bf2[4]);
+ }
+
+ [Test]
+ public void Or()
+ {
+ BitField bf1 = new BitField(10);
+ BitField bf2 = new BitField(10);
+ bf1[0] = true;
+ bf1[4] = true;
+ bf2[3] = true;
+ bf2[5] = true;
+ bf2[7] = true;
+
+ bf1.Or(bf2);
+
+ Assert.IsTrue(bf1[0], "a");
+ Assert.IsTrue(!bf1[1], "b");
+ Assert.IsTrue(!bf1[2], "c");
+ Assert.IsTrue(bf1[3], "d");
+ Assert.IsTrue(bf1[4], "e");
+ Assert.IsTrue(bf1[5], "f");
+ Assert.IsTrue(!bf1[6], "g");
+ Assert.IsTrue(bf1[7], "h");
+ Assert.IsTrue(!bf1[8], "i");
+ Assert.IsTrue(!bf1[9], "j");
+
+
+ Assert.IsTrue(!bf2[0], "k");
+ Assert.IsTrue(!bf2[1], "l");
+ Assert.IsTrue(!bf2[2], "m");
+ Assert.IsTrue(bf2[3], "n");
+ Assert.IsTrue(!bf2[4], "o");
+ Assert.IsTrue(bf2[5], "p");
+ Assert.IsTrue(!bf2[6], "q");
+ Assert.IsTrue(bf2[7], "r");
+ Assert.IsTrue(!bf2[8], "s");
+ Assert.IsTrue(!bf2[9], "t");
+ }
+
+ [Test]
+ public void Not()
+ {
+ BitField bf1 = new BitField(5);
+
+ bf1[0] = true;
+ bf1[3] = true;
+ bf1[4] = true;
+
+ bf1.Not();
+
+ Assert.IsTrue(!bf1[0], "a");
+ Assert.IsTrue(bf1[1], "b");
+ Assert.IsTrue(bf1[2], "c");
+ Assert.IsTrue(!bf1[3], "d");
+ Assert.IsTrue(!bf1[4], "e");
+ }
+ }
+ */
+}
\ No newline at end of file
Property changes on: trunk/bitsharp/src/MonoTorrent.Tests/BitFieldTest.cs
___________________________________________________________________
Name: svn:eol-style
+ native
Copied: trunk/bitsharp/src/MonoTorrent.Tests/FileManager.cs (from rev 94632,
trunk/bitsharp/src/MonoTorrent.Tests/Client/Tests/FileManager.cs)
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Tests/Client/Tests/FileManager.cs
2008-02-02 15:34:30 UTC (rev 94632)
+++ trunk/bitsharp/src/MonoTorrent.Tests/FileManager.cs 2008-02-02 16:27:58 UTC
(rev 94638)
@@ -0,0 +1,98 @@
+//
+// FileManager.cs
+//
+// Authors:
+// Alan McGovern [EMAIL PROTECTED]
+//
+// Copyright (C) 2006 Alan McGovern
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+
+
+using System.IO;
+using NUnit.Framework;
+using System.Text;
+
+namespace MonoTorrent.Client.Tests
+{
+ /// <summary>
+ ///
+ /// </summary>
+ [TestFixture]
+ public class FileManagerTest
+ {
+ private string path = string.Empty;
+ private string directoryName = string.Empty;
+ private string fullPath;
+
+ /// <summary>
+ ///
+ /// </summary>
+ [SetUp]
+ public void Setup()
+ {
+ this.path = GetType().Assembly.Location;
+ for (int i = 0; i >= 0; i++)
+ if (!Directory.Exists("temp" + i.ToString()))
+ {
+ this.directoryName = "temp" + i.ToString();
+ this.fullPath = Path.Combine(this.path,
this.directoryName);
+ Directory.CreateDirectory(fullPath);
+ break;
+ }
+
+ GenerateTestFiles();
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ private void GenerateTestFiles()
+ {
+ FileStream file1 = File.OpenWrite(Path.Combine(this.fullPath,
"file1.txt"));
+ FileStream file2 = File.OpenWrite(Path.Combine(this.fullPath,
"file2.txt"));
+
+ string data = "this is my teststring. It's not really that long,
but i'll be writing a lot more where this come from\r\n";
+
+ for (int i = 0; i < 100; i++)
+ file1.Write(System.Text.Encoding.UTF8.GetBytes(data), 0,
System.Text.Encoding.UTF8.GetByteCount(data));
+
+ for (int i = 0; i < 5000; i++)
+ file2.Write(System.Text.Encoding.UTF8.GetBytes(data), 0,
System.Text.Encoding.UTF8.GetByteCount(data));
+
+ file1.Close();
+ file2.Close();
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ [TearDown]
+ public void RemoveTempFiles()
+ {
+ foreach (string str in Directory.GetFiles(Path.Combine(this.path,
this.directoryName)))
+ File.Delete(str);
+
+ Directory.Delete(Path.Combine(path, "temp"));
+ }
+ }
+}
\ No newline at end of file
Property changes on: trunk/bitsharp/src/MonoTorrent.Tests/FileManager.cs
___________________________________________________________________
Name: svn:eol-style
+ native
Added: trunk/bitsharp/src/MonoTorrent.Tests/MonoTorrent.Tests.csproj
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Tests/MonoTorrent.Tests.csproj
2008-02-02 16:26:28 UTC (rev 94637)
+++ trunk/bitsharp/src/MonoTorrent.Tests/MonoTorrent.Tests.csproj
2008-02-02 16:27:58 UTC (rev 94638)
@@ -0,0 +1,62 @@
+<Project DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>8.0.50727</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{526928D8-DBC8-4717-BCF0-A4FEDA80A0BD}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>MonoTorrent.Tests</RootNamespace>
+ <AssemblyName>MonoTorrent.Tests</AssemblyName>
+ <StartupObject>
+ </StartupObject>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU'
">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' ==
'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="nunit.framework, Version=2.4.6.0, Culture=neutral,
PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL" />
+ <Reference Include="System" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Web" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="AllowedFastAlgorithmTest.cs" />
+ <Compile Include="BitFieldTest.cs" />
+ <Compile Include="FileManager.cs" />
+ <Compile Include="PeerMessagesTest.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\MonoTorrent\MonoTorrent.csproj">
+ <Project>{F48B95BE-0C36-48A3-838F-83FB0E4D0936}</Project>
+ <Name>MonoTorrent</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <ItemGroup>
+ <Folder Include="Properties\" />
+ </ItemGroup>
+ <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets
below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
\ No newline at end of file
Property changes on:
trunk/bitsharp/src/MonoTorrent.Tests/MonoTorrent.Tests.csproj
___________________________________________________________________
Name: svn:eol-style
+ native
Copied: trunk/bitsharp/src/MonoTorrent.Tests/PeerMessagesTest.cs (from rev
94632, trunk/bitsharp/src/MonoTorrent.Tests/Client/Tests/PeerMessagesTest.cs)
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Tests/Client/Tests/PeerMessagesTest.cs
2008-02-02 15:34:30 UTC (rev 94632)
+++ trunk/bitsharp/src/MonoTorrent.Tests/PeerMessagesTest.cs 2008-02-02
16:27:58 UTC (rev 94638)
@@ -0,0 +1,362 @@
+//
+// PeerMessagesTest.cs
+//
+// Authors:
+// Alan McGovern [EMAIL PROTECTED]
+//
+// Copyright (C) 2006 Alan McGovern
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+
+using System;
+using System.Collections.Generic;
+using System.Text;
+using NUnit.Framework;
+using MonoTorrent.Common;
+using System.Net;
+using MonoTorrent.Client.Messages;
+using MonoTorrent.Client.Messages.PeerMessages;
+
+namespace MonoTorrent.Client.Tests
+{
+ [TestFixture]
+ public class PeerMessagesTest
+ {
+ byte[] buffer = new byte[100000];
+ int offset = 2362;
+
+ [SetUp]
+ public void Setup()
+ {
+ buffer = new byte[100000];
+ for (int i = 0; i < buffer.Length; i++)
+ buffer[i] = 0xff;
+ }
+ [Test]
+ [Ignore]
+ public void IPeerMessageDecoding()
+ {
+ PeerMessage msg;
+ PeerMessage result;
+ TorrentManager manager = null;
+ byte[] buffer = new byte[1000];
+ /*
+
+
+ BitField bf = new BitField(307);
+ for (int i = 0; i < bf.Length; i++)
+ if (i % 5 == 0 || i % 13 == 0)
+ bf[i] = true;
+ */
+ //msg = new BitfieldMessage(bf);
+ //msg.Encode(buffer, 0);
+ //result = PeerMessage.DecodeMessage(buffer, 4, msg.ByteLength,
manager);
+ //Assert.AreEqual(msg, result, msg.ToString() + " decoding failed.
1");
+
+
+ msg = new CancelMessage();
+ msg.Encode(buffer, 0);
+ result = PeerMessage.DecodeMessage(buffer, 4, msg.ByteLength,
manager);
+ Assert.AreEqual(msg, result, msg.ToString() + " decoding failed.
2");
+
+
+ msg = new ChokeMessage();
+ msg.Encode(buffer, 0);
+ result = PeerMessage.DecodeMessage(buffer, 4, msg.ByteLength,
manager);
+ Assert.AreEqual(msg, result, msg.ToString() + " decoding failed.
3");
+
+
+ //msg = new HandshakeMessage();
+ //msg.Encode(buffer, 0);
+ //result = PeerMessage.DecodeMessage(buffer, 4, msg.ByteLength,
manager);
+ //Assert.AreEqual(msg, result, msg.ToString() + " decoding
failed");
+
+
+ msg = new HaveMessage();
+ msg.Encode(buffer, 0);
+ result = PeerMessage.DecodeMessage(buffer, 4, msg.ByteLength,
manager);
+ Assert.AreEqual(msg, result, msg.ToString() + " decoding failed");
+
+
+ msg = new InterestedMessage();
+ msg.Encode(buffer, 0);
+ result = PeerMessage.DecodeMessage(buffer, 4, msg.ByteLength,
manager);
+ Assert.AreEqual(msg, result, msg.ToString() + " decoding failed");
+
+
+ msg = new KeepAliveMessage();
+ msg.Encode(buffer, 0);
+ Assert.IsTrue(buffer[0] == 0
+ && buffer[1] == 0
+ && buffer[2] == 0
+ && buffer[3] == 0);
+
+
+ msg = new NotInterestedMessage();
+ msg.Encode(buffer, 0);
+ result = PeerMessage.DecodeMessage(buffer, 4, msg.ByteLength,
manager);
+ Assert.AreEqual(msg, result, msg.ToString() + " decoding failed");
+
+
+ //msg = new PieceMessage();
+ //msg.Encode(buffer, 0);
+ //result = PeerMessage.DecodeMessage(buffer, 4, msg.ByteLength,
manager);
+ //Assert.AreEqual(msg, result, msg.ToString() + " decoding
failed");
+
+
+ msg = new PortMessage();
+ msg.Encode(buffer, 0);
+ result = PeerMessage.DecodeMessage(buffer, 4, msg.ByteLength,
manager);
+ Assert.AreEqual(msg, result, msg.ToString() + " decoding failed");
+
+
+ msg = new RequestMessage();
+ msg.Encode(buffer, 0);
+ result = PeerMessage.DecodeMessage(buffer, 4, msg.ByteLength,
manager);
+ Assert.AreEqual(msg, result, msg.ToString() + " decoding failed");
+
+
+ msg = new UnchokeMessage();
+ msg.Encode(buffer, 0);
+ result = PeerMessage.DecodeMessage(buffer, 4, msg.ByteLength,
manager);
+ Assert.AreEqual(msg, result, msg.ToString() + " decoding failed");
+ }
+
+ [Test]
+ [Ignore]
+ public void BitFieldEncoding()
+ {/*
+ byte[] buffer = new byte[0];
+ BitField bf = new BitField(100);
+ for (int i = 0; i < 100; i++)
+ if (i % 5 == 0)
+ bf[i] = true;
+
+ BitfieldMessage msg = new BitfieldMessage(bf);
+ byte[] result = new byte[msg.ByteLength];
+ msg.Encode(result, 0);
+
+ Assert.AreEqual(System.Web.HttpUtility.UrlEncode(buffer),
System.Web.HttpUtility.UrlEncode(result), "Encoded bitfield incorrectly");
+ throw new NotImplementedException();*/
+ }
+ [Test]
+ [Ignore]
+ public void BitFieldDecoding()
+ {/*
+ BitField bf = new BitField(100);
+ for (int i = 0; i < 100; i++)
+ if (i % 5 == 0)
+ bf[i] = true;
+
+ byte[] bfBytes = new byte[] { 5, 6, 7, 8, 9, 10, 0, 0, 0, 0, 0, 0,
0 };
+ bf.FromArray(bfBytes, 0, bfBytes.Length);
+
+ for (int i = 0; i < bf.Length; i++)
+ if (i % 5 == 0)
+ Assert.AreEqual(true, bf[i], "Bitfield " + i.ToString() +
" is incorrect");
+ else
+ Assert.AreEqual(false, bf[i], "Bitfield " + i.ToString() +
" is incorrect");
+
+ throw new NotImplementedException();*/
+ }
+
+
+
+ [Test]
+ public void CancelEncoding()
+ {
+ int length = new CancelMessage(15, 1024, 16384).Encode(buffer,
offset);
+
Assert.AreEqual("00-00-00-0D-08-00-00-00-0F-00-00-04-00-00-00-40-00",
BitConverter.ToString(buffer, offset, length));
+ }
+ [Test]
+ public void CancelDecoding()
+ {
+ EncodeDecode(new CancelMessage(563, 4737, 88888));
+ }
+
+
+
+ [Test]
+ public void ChokeEncoding()
+ {
+ int length = new ChokeMessage().Encode(buffer, offset);
+ Assert.AreEqual("00-00-00-01-00", BitConverter.ToString(buffer,
offset, length));
+ }
+ [Test]
+ public void ChokeDecoding()
+ {
+ EncodeDecode(new ChokeMessage());
+ }
+
+
+
+ [Test]
+ public void HandshakeEncoding()
+ {
+ byte[] infohash = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 0, 12, 15, 12, 52 };
+ int length = new HandshakeMessage(infohash,
"12312312345645645678", VersionInfo.ProtocolStringV100, false).Encode(buffer,
offset);
+
+ byte[] peerId = Encoding.ASCII.GetBytes("12312312345645645678");
+ byte[] protocolVersion =
Encoding.ASCII.GetBytes(VersionInfo.ProtocolStringV100);
+ Assert.AreEqual(19, buffer[offset], "1");
+ Assert.IsTrue(Toolbox.ByteMatch(protocolVersion, 0, buffer, offset
+ 1, 19), "2");
+ Assert.IsTrue(Toolbox.ByteMatch(new byte[8], 0, buffer, offset +
20, 8), "3");
+ Assert.IsTrue(Toolbox.ByteMatch(infohash, 0, buffer, offset + 28,
20), "4");
+ Assert.IsTrue(Toolbox.ByteMatch(peerId, 0, buffer, offset + 48,
20), "5");
+ Assert.AreEqual(length, 68, "6");
+ }
+ [Test]
+ public void HandshakeDecoding()
+ {
+ byte[] infohash = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 0, 12, 15, 12, 52 };
+ HandshakeMessage orig = new HandshakeMessage(infohash,
"12312312345645645678", VersionInfo.ProtocolStringV100);
+ orig.Encode(buffer, offset);
+ HandshakeMessage dec = new HandshakeMessage();
+ dec.Decode(buffer, offset, 68);
+ Assert.IsTrue(orig.Equals(dec));
+ }
+
+
+
+ [Test]
+ public void HaveEncoding()
+ {
+ int length = new HaveMessage(150).Encode(buffer, offset);
+ Assert.AreEqual("00-00-00-05-04-00-00-00-96",
BitConverter.ToString(buffer, offset, length));
+ }
+ [Test]
+ public void HaveDecoding()
+ {
+ EncodeDecode(new HaveMessage(34622));
+ }
+
+
+
+ [Test]
+ public void InterestedEncoding()
+ {
+ int length = new InterestedMessage().Encode(buffer, offset);
+ Assert.AreEqual("00-00-00-01-02", BitConverter.ToString(buffer,
offset, length));
+ }
+ [Test]
+ public void InterestedDecoding()
+ {
+ EncodeDecode(new InterestedMessage());
+ }
+
+
+
+ [Test]
+ public void KeepAliveEncoding()
+ {
+ new KeepAliveMessage().Encode(buffer, offset);
+ Assert.IsTrue(buffer[offset] == 0
+ && buffer[offset + 1] == 0
+ && buffer[offset + 2] == 0
+ && buffer[offset + 3] == 0);
+ }
+ [Test]
+ public void KeepAliveDecoding()
+ {
+ // Keep alives aren't "decoded" as they have 0 message id and 0
body length
+ }
+
+
+
+ [Test]
+ public void NotInterestedEncoding()
+ {
+ int length = new NotInterestedMessage().Encode(buffer, offset);
+ Assert.AreEqual("00-00-00-01-03", BitConverter.ToString(buffer,
offset, length));
+ }
+ [Test]
+ public void NotInterestedDecoding()
+ {
+ EncodeDecode(new NotInterestedMessage());
+ }
+
+
+
+ [Test]
+ [Ignore]
+ public void PieceEncoding()
+ {
+ int length = new PieceMessage(null, 15, 1024,
16384).Encode(buffer, offset);
+ throw new NotImplementedException();
+ }
+ [Test]
+ [Ignore]
+ public void PieceDecoding()
+ {
+ EncodeDecode(new PieceMessage(null, 123, 456, 789));
+ }
+
+
+
+ [Test]
+ public void PortEncoding()
+ {
+ int length = new PortMessage(2500).Encode(buffer, offset);
+ Assert.AreEqual("00-00-00-03-09-09-C4",
BitConverter.ToString(buffer, offset, length));
+ }
+ [Test]
+ public void PortDecoding()
+ {
+ EncodeDecode(new PortMessage(5452));
+ }
+
+
+
+ [Test]
+ public void RequestEncoding()
+ {
+ int length = new RequestMessage(5, 1024, 16384).Encode(buffer,
offset);
+
Assert.AreEqual("00-00-00-0D-06-00-00-00-05-00-00-04-00-00-00-40-00",
BitConverter.ToString(buffer, offset, length));
+ }
+ [Test]
+ public void RequestDecoding()
+ {
+ EncodeDecode(new RequestMessage(123, 789, 4235));
+ }
+
+
+
+ [Test]
+ public void UnchokeEncoding()
+ {
+ int length = new UnchokeMessage().Encode(buffer, offset);
+ Assert.AreEqual("00-00-00-01-01", BitConverter.ToString(buffer,
offset, length));
+ }
+ [Test]
+ public void UnchokeDecoding()
+ {
+ EncodeDecode(new UnchokeMessage());
+ }
+
+ private void EncodeDecode(Message orig)
+ {
+ orig.Encode(buffer, offset);
+ Message dec = PeerMessage.DecodeMessage(buffer, offset + 4, 1000,
null); // We need the +4 to skip past the message length bytes
+ Assert.IsTrue(orig.Equals(dec), string.Format("orig: {0}, new:
{1}", orig, dec));
+ }
+ }
+}
Property changes on: trunk/bitsharp/src/MonoTorrent.Tests/PeerMessagesTest.cs
___________________________________________________________________
Name: svn:eol-style
+ native
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches