Skip to content
This repository was archived by the owner on Dec 23, 2022. It is now read-only.

Commit 5da24e5

Browse files
committed
Added UnitTests
Signed-off-by: Subtixx <subtixx@gmail.com>
1 parent 58b5f6d commit 5da24e5

File tree

5 files changed

+119
-7
lines changed

5 files changed

+119
-7
lines changed

RCON.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RCONServer", "RCONServer\RC
44
EndProject
55
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RCONServerLib", "RCONServerLib\RCONServerLib.csproj", "{2EFA0311-14CE-414B-9867-FC0D1FF6F6C4}"
66
EndProject
7+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RCONServerLib.Tests", "RCONServerLib.Tests\RCONServerLib.Tests.csproj", "{5C204D9A-D264-43E3-9270-0D0E7F21C87E}"
8+
EndProject
79
Global
810
GlobalSection(SolutionConfigurationPlatforms) = preSolution
911
Debug|Any CPU = Debug|Any CPU
@@ -18,5 +20,9 @@ Global
1820
{2EFA0311-14CE-414B-9867-FC0D1FF6F6C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
1921
{2EFA0311-14CE-414B-9867-FC0D1FF6F6C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
2022
{2EFA0311-14CE-414B-9867-FC0D1FF6F6C4}.Release|Any CPU.Build.0 = Release|Any CPU
23+
{5C204D9A-D264-43E3-9270-0D0E7F21C87E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{5C204D9A-D264-43E3-9270-0D0E7F21C87E}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{5C204D9A-D264-43E3-9270-0D0E7F21C87E}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{5C204D9A-D264-43E3-9270-0D0E7F21C87E}.Release|Any CPU.Build.0 = Release|Any CPU
2127
EndGlobalSection
2228
EndGlobal
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>netcoreapp2.0</TargetFramework>
4+
<IsPackable>false</IsPackable>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
8+
<PackageReference Include="xunit" Version="2.3.1" />
9+
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
10+
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
11+
</ItemGroup>
12+
<ItemGroup>
13+
<ProjectReference Include="..\RCONServerLib\RCONServerLib.csproj" />
14+
</ItemGroup>
15+
</Project>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Net.Http.Headers;
3+
using Xunit;
4+
5+
namespace RCONServerLib.Tests
6+
{
7+
public class RconPacketTests
8+
{
9+
[Fact]
10+
public void RconPacketLengthTest()
11+
{
12+
Assert.Throws<LengthMismatchException>(() => new RemoteConPacket(new byte[] {0xFF, 0xFF, 0x00, 0x00}));
13+
}
14+
15+
[Fact]
16+
public void RconPacketTypeTest()
17+
{
18+
Assert.Throws<InvalidPacketTypeException>(() => new RemoteConPacket(new byte[]
19+
{
20+
0x08, 0x00, 0x00, 0x00, // Size
21+
0x00, 0x00, 0x00, 0x00, // Id
22+
0xFF, 0xFF, 0x00, 0x00, // Type
23+
}));
24+
}
25+
26+
[Fact]
27+
public void RconPacketNullTerminatorEndTest()
28+
{
29+
Assert.Throws<NullTerminatorMissingException>(() => new RemoteConPacket(new byte[]
30+
{
31+
0x0A, 0x00, 0x00, 0x00, // Size
32+
0x00, 0x00, 0x00, 0x00, // Id
33+
0x00, 0x00, 0x00, 0x00, // Type
34+
0x00, // Payload
35+
0x01, // Null-terminator end
36+
}));
37+
}
38+
39+
[Fact]
40+
public void RconPacketTooLongTest()
41+
{
42+
byte[] bytes = new byte[4096];
43+
Array.Copy(new byte[]
44+
{
45+
0xFC, 0x0F, 0x00, 0x00, // Size - 4092
46+
0x00, 0x00, 0x00, 0x00,
47+
0x00, 0x00, 0x00, 0x00,
48+
}, 0, bytes, 0, 12);
49+
bytes[4094] = 0x00;
50+
bytes[4095] = 0x00;
51+
for (var i = 13; i < 4094; i++)
52+
{
53+
bytes[i] = 0xFF;
54+
}
55+
Assert.Throws<NullTerminatorMissingException>(() => new RemoteConPacket(bytes));
56+
}
57+
}
58+
}

RCONServerLib/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Reflection;
2+
using System.Runtime.CompilerServices;
23
using System.Runtime.InteropServices;
34

45
// General Information about an assembly is controlled through the following
@@ -21,6 +22,8 @@
2122
// The following GUID is for the ID of the typelib if this project is exposed to COM
2223
[assembly: Guid("2EFA0311-14CE-414B-9867-FC0D1FF6F6C4")]
2324

25+
[assembly:InternalsVisibleTo("RCONServerLib.Tests")]
26+
2427
// Version information for an assembly consists of the following four values:
2528
//
2629
// Major Version

RCONServerLib/RemoteConPacket.cs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,24 @@ public RemoteConPacket(byte[] packetBytes)
5151

5252
// The size field (4-Bytes Little Endian Int) is, according to specification, not included.
5353
if (Size + 4 != packetBytes.Length)
54-
throw new Exception("packet length mismatch");
54+
throw new LengthMismatchException("packet length mismatch");
5555

5656
Id = reader.ReadInt32LittleEndian();
5757
var packetType = reader.ReadInt32LittleEndian();
5858
if (!Enum.IsDefined(typeof(PacketType), packetType))
59-
throw new Exception("Invalid packet type");
59+
throw new InvalidPacketTypeException("Invalid packet type");
6060
Type = (PacketType) Enum.ToObject(typeof(PacketType), packetType);
6161
Payload = reader.ReadAscii();
6262

6363
// Get payload length by subtracting 9 bytes (ID 4-Bytes, Type 4-Bytes, Null-terminator 1-Byte)
6464
if (Encoding.ASCII.GetByteCount(Payload) > Size - 9)
65-
throw new Exception("Payload length mismatch");
65+
throw new LengthMismatchException("Payload length mismatch");
6666

6767
var nullTerminator = reader.ReadByte();
68-
if(nullTerminator != 0x00)
69-
throw new Exception("Missing last null-terminator");
70-
71-
if(reader.BaseStream.Position != reader.BaseStream.Length)
68+
if (nullTerminator != 0x00)
69+
throw new NullTerminatorMissingException("Missing last null-terminator");
70+
71+
if (reader.BaseStream.Position != reader.BaseStream.Length)
7272
throw new Exception("More data to read");
7373
}
7474
}
@@ -79,6 +79,8 @@ public RemoteConPacket(int id, PacketType type, string payload)
7979
Payload = payload;
8080
Id = id;
8181
Type = type;
82+
if (Length > 4096)
83+
throw new PacketTooLongException();
8284
}
8385

8486
/// <summary>
@@ -104,4 +106,32 @@ public byte[] GetBytes()
104106
}
105107
}
106108
}
109+
110+
public class PacketTooLongException : Exception
111+
{
112+
public PacketTooLongException() : base()
113+
{
114+
}
115+
}
116+
117+
public class NullTerminatorMissingException : Exception
118+
{
119+
public NullTerminatorMissingException(string message) : base(message)
120+
{
121+
}
122+
}
123+
124+
public class LengthMismatchException : Exception
125+
{
126+
public LengthMismatchException(string message) : base(message)
127+
{
128+
}
129+
}
130+
131+
public class InvalidPacketTypeException : Exception
132+
{
133+
public InvalidPacketTypeException(string message) : base(message)
134+
{
135+
}
136+
}
107137
}

0 commit comments

Comments
 (0)