Skip to content

Commit

Permalink
Merge pull request #56 from exomia/development
Browse files Browse the repository at this point in the history
- added unit tests for payload encoding
  • Loading branch information
baetz-daniel authored Sep 23, 2020
2 parents 3ed318a + d65b851 commit 967873a
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 42 deletions.
30 changes: 5 additions & 25 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ on:
pull_request:
branches:
- master
- development
- development
env:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
DOTNET_NOLOGO: 1
DOTNET_CLI_TELEMETRY_OPTOUT: 1
jobs:
build-windows:
name: build release and debug for windows
Expand All @@ -17,17 +21,11 @@ jobs:
with:
dotnet-version: 3.1.300
- name: build debug
env:
DOTNET_NOLOGO: 1
DOTNET_CLI_TELEMETRY_OPTOUT: 1
run: |
dotnet build src/Exomia.Network --configuration Windows-Debug --force --nologo -p:Platform=AnyCPU
dotnet build src/Exomia.Network --configuration Windows-Debug --force --nologo -p:Platform=x86
dotnet build src/Exomia.Network --configuration Windows-Debug --force --nologo -p:Platform=x64
- name: build release
env:
DOTNET_NOLOGO: 1
DOTNET_CLI_TELEMETRY_OPTOUT: 1
run: |
dotnet build src/Exomia.Network --configuration Windows-Release --force --nologo -p:Platform=AnyCPU
dotnet build src/Exomia.Network --configuration Windows-Release --force --nologo -p:Platform=x86
Expand All @@ -42,17 +40,11 @@ jobs:
with:
dotnet-version: 3.1.300
- name: test debug
env:
DOTNET_NOLOGO: 1
DOTNET_CLI_TELEMETRY_OPTOUT: 1
run: |
dotnet test tests/L0/Exomia.Network.Tests --configuration Windows-Debug --nologo -p:Platform=AnyCPU
dotnet test tests/L0/Exomia.Network.Tests --configuration Windows-Debug --nologo -p:Platform=x86
dotnet test tests/L0/Exomia.Network.Tests --configuration Windows-Debug --nologo -p:Platform=x64
- name: test release
env:
DOTNET_NOLOGO: 1
DOTNET_CLI_TELEMETRY_OPTOUT: 1
run: |
dotnet test tests/L0/Exomia.Network.Tests --configuration Windows-Release --nologo -p:Platform=AnyCPU
dotnet test tests/L0/Exomia.Network.Tests --configuration Windows-Release --nologo -p:Platform=x86
Expand All @@ -66,17 +58,11 @@ jobs:
with:
dotnet-version: 3.1.300
- name: build debug
env:
DOTNET_NOLOGO: 1
DOTNET_CLI_TELEMETRY_OPTOUT: 1
run: |
dotnet build src/Exomia.Network --configuration Linux-Debug --force --nologo -p:Platform=AnyCPU
dotnet build src/Exomia.Network --configuration Linux-Debug --force --nologo -p:Platform=x86
dotnet build src/Exomia.Network --configuration Linux-Debug --force --nologo -p:Platform=x64
- name: build release
env:
DOTNET_NOLOGO: 1
DOTNET_CLI_TELEMETRY_OPTOUT: 1
run: |
dotnet build src/Exomia.Network --configuration Linux-Release --force --nologo -p:Platform=AnyCPU
dotnet build src/Exomia.Network --configuration Linux-Release --force --nologo -p:Platform=x86
Expand All @@ -91,17 +77,11 @@ jobs:
with:
dotnet-version: 3.1.300
- name: test debug
env:
DOTNET_NOLOGO: 1
DOTNET_CLI_TELEMETRY_OPTOUT: 1
run: |
dotnet test tests/L0/Exomia.Network.Tests --configuration Linux-Debug --nologo -p:Platform=AnyCPU
dotnet test tests/L0/Exomia.Network.Tests --configuration Linux-Debug --nologo -p:Platform=x86
dotnet test tests/L0/Exomia.Network.Tests --configuration Linux-Debug --nologo -p:Platform=x64
- name: test release
env:
DOTNET_NOLOGO: 1
DOTNET_CLI_TELEMETRY_OPTOUT: 1
run: |
dotnet test tests/L0/Exomia.Network.Tests --configuration Linux-Release --nologo -p:Platform=AnyCPU
dotnet test tests/L0/Exomia.Network.Tests --configuration Linux-Release --nologo -p:Platform=x86
Expand Down
4 changes: 2 additions & 2 deletions src/Exomia.Network/Encoding/PayloadEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal static ushort Encode(byte* data, int length, byte* buffer, out int buff
data += 7;
length -= 7;
}

if (length > 0)
{
Encode(&checksum, buffer, data, length);
Expand All @@ -69,7 +69,7 @@ internal static ushort Decode(byte* src, int length, byte* dst, out int dstLengt
src += 8;
length -= 8;
}

if (length > 0)
{
Decode(&checksum, dst, src, length);
Expand Down
1 change: 0 additions & 1 deletion src/Exomia.Network/Exomia.Network.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
</PropertyGroup>

<PropertyGroup>
<Version>1.7.0</Version>
<Company>exomia</Company>
<Authors>exomia;saika01</Authors>
<Description>a tcp- / udp- client and server</Description>
Expand Down
7 changes: 1 addition & 6 deletions src/Exomia.Network/Native/CircularBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,7 @@ public CircularBuffer(int capacity = 1024)
throw new ArgumentOutOfRangeException();
}

uint value = (uint)capacity;
if (value > 0x80000000)
{
throw new ArgumentOutOfRangeException();
}
value--;
uint value = (uint)capacity - 1;
value |= value >> 1;
value |= value >> 2;
value |= value >> 4;
Expand Down
2 changes: 1 addition & 1 deletion src/Exomia.Network/TCP/TcpClientApm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private protected override void ReceiveAsync()
private protected override unsafe SendError BeginSend(in PacketInfo packetInfo)
{
int size;
byte[] buffer = ByteArrayPool.Rent(Constants.TCP_HEADER_OFFSET + packetInfo.ChunkLength + 1);
byte[] buffer = ByteArrayPool.Rent(_payloadSize + Constants.TCP_HEADER_OFFSET);
fixed (byte* dst = buffer)
{
size = Serialization.Serialization.SerializeTcp(in packetInfo, dst, _encryptionMode);
Expand Down
4 changes: 1 addition & 3 deletions src/Exomia.Network/TCP/TcpClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ private protected TcpClientBase(ushort expectedMaxPayloadSize = Constants.TCP_PA
: Constants.TCP_PAYLOAD_SIZE_MAX;
_payloadSize = (ushort)(PayloadEncoding.EncodedPayloadLength(_maxPayloadSize) + 1);

_bufferRead =
new byte[PayloadEncoding.EncodedPayloadLength(_payloadSize + Constants.TCP_HEADER_OFFSET)];
_bufferRead = new byte[_payloadSize + Constants.TCP_HEADER_OFFSET];
_circularBuffer = new CircularBuffer(_bufferRead.Length * 2);

_bigDataHandler = new BigDataHandler<int>.Default();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Exomia.Network/TCP/TcpServerApmBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private protected override unsafe SendError BeginSendTo(Socket arg0,
in PacketInfo packetInfo)
{
SendStateObject state;
state.Buffer = ByteArrayPool.Rent(Constants.TCP_HEADER_OFFSET + packetInfo.ChunkLength + 1);
state.Buffer = ByteArrayPool.Rent(_payloadSize + Constants.TCP_HEADER_OFFSET);
state.Socket = arg0;
int size;
fixed (byte* dst = state.Buffer)
Expand Down
2 changes: 1 addition & 1 deletion src/Exomia.Network/UDP/UdpClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private protected UdpClientBase(ushort expectedMaxPayloadSize = Constants.UDP_PA
_maxPayloadSize =
expectedMaxPayloadSize > 0 && expectedMaxPayloadSize < Constants.UDP_PAYLOAD_SIZE_MAX
? expectedMaxPayloadSize
: Constants.TCP_PAYLOAD_SIZE_MAX;
: Constants.UDP_PAYLOAD_SIZE_MAX;

_bigDataHandler = new BigDataHandler<int>.Timed();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exomia.Network/UDP/UdpServerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private protected UdpServerBase(ushort expectedMaxPayloadSize = Constants.UDP_PA
_maxPayloadSize =
expectedMaxPayloadSize > 0 && expectedMaxPayloadSize < Constants.UDP_PAYLOAD_SIZE_MAX
? expectedMaxPayloadSize
: Constants.TCP_PAYLOAD_SIZE_MAX;
: Constants.UDP_PAYLOAD_SIZE_MAX;

_bigDataHandler = new BigDataHandler<(EndPoint, int)>.Timed();
}
Expand Down
115 changes: 115 additions & 0 deletions tests/L0/Exomia.Network.Tests/Encoding/PayloadEncodingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#region License

// Copyright (c) 2018-2020, exomia
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

#endregion

using System;
using System.Linq;
using Exomia.Network.Encoding;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Exomia.Network.Tests.Encoding
{
[TestClass]
public unsafe class PayloadEncodingTests
{
[TestMethod]
[DataRow(0, 0)]
[DataRow(1, 2)]
[DataRow(2, 3)]
[DataRow(3, 4)]
[DataRow(4, 5)]
[DataRow(5, 6)]
[DataRow(6, 7)]
[DataRow(7, 8)]
[DataRow(8, 10)]
[DataRow(9, 11)]
[DataRow(100, 115)]
[DataRow(1024, 1171)]
public void EncodedPayloadLength_ShouldReturnExpectedValue(int length, int expected)
{
Assert.AreEqual(expected, PayloadEncoding.EncodedPayloadLength(length));
}

[TestMethod]
[DataRow(0, 0)]
[DataRow(2, 1)]
[DataRow(3, 2)]
[DataRow(4, 3)]
[DataRow(5, 4)]
[DataRow(6, 5)]
[DataRow(7, 6)]
[DataRow(8, 7)]
[DataRow(10, 8)]
[DataRow(11, 9)]
[DataRow(115, 100)]
[DataRow(1171, 1024)]
public void DecodedPayloadLength_ShouldReturnExpectedValue(int length, int expected)
{
Assert.AreEqual(expected, PayloadEncoding.DecodedPayloadLength(length));
}

[TestMethod]
[DataRow(0)]
[DataRow(16)]
[DataRow(128)]
[DataRow(166)]
[DataRow(1024)]
[DataRow(4096)]
[DataRow(48574)]
[DataRow(Constants.TCP_PAYLOAD_SIZE_MAX)]
[DataRow(ushort.MaxValue)]
public void Encode_WithRandomData_ShouldNotFail(int length)
{
Random r = new Random((int)DateTime.Now.Ticks);
byte[] buffer = new byte[length];
byte[] buffer2 = new byte[PayloadEncoding.EncodedPayloadLength(length)];
r.NextBytes(buffer);
fixed (byte* src = buffer)
fixed (byte* dst = buffer2)
{
PayloadEncoding.Encode(src, length, dst, out int bufferLength);
Assert.AreEqual(buffer2.Length, bufferLength);
Assert.IsTrue(buffer2.All(b => b != 0));
}
}

[TestMethod]
[DataRow(0)]
[DataRow(16)]
[DataRow(128)]
[DataRow(166)]
[DataRow(1024)]
[DataRow(4096)]
[DataRow(48574)]
[DataRow(Constants.TCP_PAYLOAD_SIZE_MAX)]
[DataRow(ushort.MaxValue)]
public void Decode_WithEncodedRandomData_ShouldNotFail(int length)
{
Random r = new Random((int)DateTime.Now.Ticks);
byte[] buffer = new byte[length];
byte[] buffer2 = new byte[PayloadEncoding.EncodedPayloadLength(length)];

r.NextBytes(buffer);
fixed (byte* src = buffer)
fixed (byte* dst = buffer2)
{
ushort checksum1 = PayloadEncoding.Encode(src, length, dst, out int bufferLength);

byte[] buffer3 = new byte[bufferLength];
fixed (byte* dcp = buffer3)
{
ushort checksum2 = PayloadEncoding.Decode(dst, bufferLength, dcp, out int dstLength);
Assert.AreEqual(length, dstLength);
Assert.AreEqual(checksum1, checksum2);
Assert.IsTrue(buffer3.Take(dstLength).SequenceEqual(buffer));
}
}
}
}
}
37 changes: 36 additions & 1 deletion tests/L0/Exomia.Network.Tests/Native/CircularBufferTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void InitTest_CircularBuffer_Initialize_With_InvalidNumbers_ShouldFail(in
Assert.ThrowsException<ArgumentOutOfRangeException>(
() =>
{
CircularBuffer t1 = new CircularBuffer(count);
return new CircularBuffer(count);
});
}

Expand Down Expand Up @@ -697,5 +697,40 @@ public void PeekHeaderTest()
Assert.AreEqual(dataLength, (buffer[2] << 8) | buffer[1]);
Assert.AreEqual(checksum, (ushort)((buffer[6] << 8) | buffer[5]));
}

[TestMethod]
[DataRow(0)]
[DataRow(1)]
[DataRow(2)]
[DataRow(3)]
[DataRow(4)]
[DataRow(5)]
[DataRow(6)]
public void SkipTest(int skip)
{
CircularBuffer cb = new CircularBuffer(16);

byte[] buffer = { 12, 200, 4, 45, 177, 78, 147 };
cb.Write(buffer, 0, buffer.Length); // 7
cb.Skip(skip);
Assert.AreEqual(buffer.Length - skip, cb.Count);
}

[TestMethod]
[DataRow(15)]
[DataRow(101)]
[DataRow(102)]
[DataRow(103)]
[DataRow(1024)]
[DataRow(int.MaxValue)]
public void SkipTest_WithOverflow_ShouldBeEmpty(int skip)
{
CircularBuffer cb = new CircularBuffer(16);

byte[] buffer = { 12, 200, 4, 45, 177, 78, 147 };
cb.Write(buffer, 0, buffer.Length); // 7
cb.Skip(skip);
Assert.IsTrue(cb.IsEmpty);
}
}
}

0 comments on commit 967873a

Please sign in to comment.