Skip to content

Commit

Permalink
Lib: few tweaks for Packet class, added operators to Guid class, adde…
Browse files Browse the repository at this point in the history
…d GPL, added tests
  • Loading branch information
Arcidev committed Jan 8, 2016
1 parent d41c15f commit 25c30c0
Show file tree
Hide file tree
Showing 15 changed files with 1,487 additions and 211 deletions.
99 changes: 99 additions & 0 deletions Arci.Networking.Tests/Arci.Networking.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{D62482C6-AF88-4BD9-97CB-CC22BDF1EA12}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Arci.Networking.Tests</RootNamespace>
<AssemblyName>Arci.Networking.Tests</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
</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="System" />
</ItemGroup>
<Choose>
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
</ItemGroup>
</Otherwise>
</Choose>
<ItemGroup>
<Compile Include="DataTests\BaseTestClass.cs" />
<Compile Include="DataTests\ByteBufferTests.cs" />
<Compile Include="DataTests\GuidTests.cs" />
<Compile Include="DataTests\PacketTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Arci.Networking\Arci.Networking.csproj">
<Project>{ed123dc2-0a2c-4b87-b723-23aad065d518}</Project>
<Name>Arci.Networking</Name>
</ProjectReference>
<ProjectReference Include="..\Shared\Shared.csproj">
<Project>{dda1699f-c5dc-40b0-83cf-7343abd4df43}</Project>
<Name>Shared</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="EncryptionTests\" />
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
</ItemGroup>
</When>
</Choose>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\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>
24 changes: 24 additions & 0 deletions Arci.Networking.Tests/DataTests/BaseTestClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Shared;
using System;
using Guid = Arci.Networking.Data.Guid;

namespace Arci.Networking.Tests.DataTests
{
public class BaseTestClass
{
protected const UInt16 packetOpcode = 255;
protected const UInt64 uint64Value = 0xAABBCCDDEEFF1199;
protected const UInt32 uint32Value = 1;
protected const UInt16 uint16Value = 2;
protected const byte byteValue = 3;
protected const Int32 int32Value = 4;
protected const Int16 int16Value = 5;
protected const sbyte sbyteValue = 6;
protected const string stringValue = "Hello World";
protected const ClientPacketTypes clientEnumOpcode = ClientPacketTypes.CMSG_INIT_ENCRYPTED_AES;
protected const ServerPacketTypes serverEnumOpcode = ServerPacketTypes.SMSG_INIT_RESPONSE_ENCRYPTED_RSA;

protected static readonly Guid[] guidValues = new Guid[] { new Guid(uint64Value), new Guid(0xDDEEFF1199AABBCC), new Guid(0xAA1199BBCCDDEEFF) };
protected static readonly byte[] byteArrayValue = new byte[] { 1, 2, 3, 4 };
}
}
40 changes: 40 additions & 0 deletions Arci.Networking.Tests/DataTests/ByteBufferTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Arci.Networking.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Arci.Networking.Tests.DataTests
{
[TestClass]
public class ByteBufferTests : BaseTestClass
{
[TestMethod]
public void AppendByteBufferTest()
{
Packet writePacket = new Packet(packetOpcode);
ByteBuffer buffer1 = new ByteBuffer();
ByteBuffer buffer2 = new ByteBuffer();

for (int i = 0; i < byteArrayValue.Length; i++)
{
writePacket.Write(byteArrayValue[i]);
buffer1.Write(byteArrayValue[i]);
buffer2.Write(byteArrayValue[byteArrayValue.Length - (i + 1)]);
}

writePacket.Write(buffer1);
writePacket.Write(buffer2);

Packet readPacket = new Packet(writePacket.Data);
for (int i = 0; i < byteArrayValue.Length; i++)
Assert.AreEqual(byteArrayValue[i], readPacket.ReadByte());

for (int i = 0; i < byteArrayValue.Length; i++)
Assert.AreEqual(byteArrayValue[i], readPacket.ReadByte());

for (int i = byteArrayValue.Length - 1; i >= 0; i--)
Assert.AreEqual(byteArrayValue[i], readPacket.ReadByte());

readPacket.Dispose();
writePacket.Dispose();
}
}
}
91 changes: 91 additions & 0 deletions Arci.Networking.Tests/DataTests/GuidTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using Arci.Networking.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Guid = Arci.Networking.Data.Guid;

namespace Arci.Networking.Tests.DataTests
{
[TestClass]
public class GuidTests : BaseTestClass
{
[TestMethod]
public void WriteGuidTests()
{
Packet writePacket = new Packet(packetOpcode);
writePacket.WriteGuidBitStreamInOrder(guidValues[0], 0, 1, 2, 3, 4, 5, 6, 7);
writePacket.WriteGuidBitStreamInOrder(guidValues[1], 7, 6, 5);
writePacket.WriteGuidBitStreamInOrder(guidValues[2], 0, 1, 2, 3);
writePacket.WriteGuidBitStreamInOrder(guidValues[1], 4, 3, 2);
writePacket.WriteGuidBitStreamInOrder(guidValues[2], 7, 6, 5, 4);
writePacket.WriteGuidBitStreamInOrder(guidValues[1], 1, 0);
writePacket.FlushBits(); // Not required as we've written the whole bytes (3 * 8 bits == 3 bytes)

writePacket.WriteGuidByteStreamInOrder(guidValues[1], 0, 1, 2, 3, 4, 5, 6, 7);
writePacket.WriteGuidByteStreamInOrder(guidValues[2], 7, 6);
writePacket.WriteGuidByteStreamInOrder(guidValues[0], 0, 1, 2, 3, 7, 6, 5, 4);
writePacket.WriteGuidByteStreamInOrder(guidValues[2], 5, 4, 3, 2, 1, 0);

var guids = new Guid[3];
for (int i = 0; i < guids.Length; i++)
guids[i] = new Guid();

Packet readPacket = new Packet(writePacket.Data);
readPacket.ReadGuidBitStreamInOrder(guids[0], 0, 1, 2, 3, 4, 5, 6, 7);
readPacket.ReadGuidBitStreamInOrder(guids[1], 7, 6, 5);
readPacket.ReadGuidBitStreamInOrder(guids[2], 0, 1, 2, 3);
readPacket.ReadGuidBitStreamInOrder(guids[1], 4, 3, 2);
readPacket.ReadGuidBitStreamInOrder(guids[2], 7, 6, 5, 4);
readPacket.ReadGuidBitStreamInOrder(guids[1], 1, 0);

readPacket.ReadGuidByteStreamInOrder(guids[1], 0, 1, 2, 3, 4, 5, 6, 7);
readPacket.ReadGuidByteStreamInOrder(guids[2], 7, 6);
readPacket.ReadGuidByteStreamInOrder(guids[0], 0, 1, 2, 3, 7, 6, 5, 4);
readPacket.ReadGuidByteStreamInOrder(guids[2], 5, 4, 3, 2, 1, 0);

Assert.AreEqual(guidValues[0], guids[0]);
Assert.AreEqual(guidValues[1], guids[1]);
Assert.AreEqual(guidValues[2], guids[2]);

readPacket.Dispose();
writePacket.Dispose();
}

[TestMethod]
public void MatchTests()
{
var guid1 = guidValues[0];
var guid2 = guidValues[1];
Assert.IsTrue(guidValues[0] == guid1); // Testing against different adress
Assert.IsTrue(guid2 != guid1);
Assert.IsFalse(guid1 == null);
Assert.IsFalse(null == guid2);
Assert.IsTrue(guid1 != null);
Assert.IsTrue(null != guid2);
Assert.IsFalse(guid1.Equals(guid2));
Assert.AreNotEqual(guid1.GetHashCode(), guid2.GetHashCode());
}

[TestMethod]
public void ConversionOperatorsTests()
{
var guid1 = new Guid(uint64Value);
var guid2 = (Guid)uint64Value;
var uint64Val = (UInt64)guid1;

Assert.AreEqual(guid1, guid2);
Assert.AreEqual(uint64Value, uint64Val);
}

[TestMethod]
public void IndexTests()
{
var guid1 = guidValues[0];
var guid2 = new Guid();

for (int i = 0; i < 8; i++)
guid2[i] = guid1[i];

Assert.AreEqual(guid1, guid2);
}
}
}
93 changes: 93 additions & 0 deletions Arci.Networking.Tests/DataTests/PacketTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using Arci.Networking.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shared;
using System;

namespace Arci.Networking.Tests.DataTests
{
[TestClass]
public class PacketTests : BaseTestClass
{
[TestMethod]
public void WriteReadByteDataTest()
{
Packet writePacket = new Packet(packetOpcode);
writePacket.Write(uint32Value);
writePacket.Write(uint16Value);
writePacket.Write(byteValue);

writePacket.Write(int32Value);
writePacket.Write(int16Value);
writePacket.Write(sbyteValue);

writePacket.Write(stringValue);
writePacket.Write(byteArrayValue);

Packet readPacket = new Packet(writePacket.Data);

Assert.AreEqual(writePacket.OpcodeNumber, readPacket.OpcodeNumber);
Assert.AreEqual(uint32Value, readPacket.ReadUInt32());
Assert.AreEqual(uint16Value, readPacket.ReadUInt16());
Assert.AreEqual(byteValue, readPacket.ReadByte());
Assert.AreEqual(int32Value, readPacket.ReadInt32());
Assert.AreEqual(int16Value, readPacket.ReadInt16());
Assert.AreEqual(sbyteValue, readPacket.ReadSByte());
Assert.AreEqual(stringValue, readPacket.ReadString());
var packetBytes = readPacket.ReadBytes();
for (int i = 0; i < byteArrayValue.Length; i++)
Assert.AreEqual(byteArrayValue[i], packetBytes[i]);

readPacket.Dispose();
writePacket.Dispose();
}

[TestMethod]
public void WriteReadBitDataTest()
{
Packet writePacket = new Packet(packetOpcode);
writePacket.WriteBit(byteValue & 0x1);
writePacket.WriteBit(byteValue & 0x2);
writePacket.WriteBit(byteValue & 0x4);
writePacket.WriteBit(byteValue & 0x8);
writePacket.WriteBit(byteValue & 0x10);
writePacket.WriteBit(byteValue & 0x20);
writePacket.WriteBit(byteValue & 0x40);
writePacket.WriteBit(byteValue & 0x80);
writePacket.WriteBit(true);
writePacket.WriteBit(false);
writePacket.FlushBits();

int bVal = 0;
Packet readPacket = new Packet(writePacket.Data);
for (int i = 0; i < 8; i++)
if (readPacket.ReadBit())
bVal |= 1 << i;

Assert.AreEqual(byteValue, bVal);
Assert.IsTrue(readPacket.ReadBit());
Assert.IsFalse(readPacket.ReadBit());

readPacket.Dispose();
writePacket.Dispose();
}

[TestMethod]
public void InitPacketTests()
{
Packet packet1 = new Packet(packetOpcode);
Packet packet2 = new Packet(clientEnumOpcode);

Assert.AreEqual(packetOpcode, packet1.OpcodeNumber);
Assert.AreEqual(clientEnumOpcode, (ClientPacketTypes)packet2.OpcodeNumber);

packet1.Initialize(packetOpcode & 0xF);
packet2.Initialize(serverEnumOpcode);

Assert.AreEqual(packetOpcode & 0xF, packet1.OpcodeNumber);
Assert.AreEqual(serverEnumOpcode, (ServerPacketTypes)packet2.OpcodeNumber);

packet1.Dispose();
packet2.Dispose();
}
}
}
36 changes: 36 additions & 0 deletions Arci.Networking.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Arci.Networking.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Arci.Networking.Tests")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("d62482c6-af88-4bd9-97cb-cc22bdf1ea12")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Loading

0 comments on commit 25c30c0

Please sign in to comment.