Initial commit

This commit is contained in:
alexpete
2021-03-05 11:26:34 -08:00
commit a10351f38d
27091 changed files with 5521199 additions and 0 deletions
@@ -0,0 +1,18 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AzNetworking/ConnectionLayer/ConnectionMetrics.h>
#include <AzCore/UnitTest/TestTypes.h>
namespace UnitTest
{
}
@@ -0,0 +1,49 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AzNetworking/ConnectionLayer/SequenceGenerator.h>
#include <AzCore/UnitTest/TestTypes.h>
namespace UnitTest
{
TEST(SequenceGenerator, Basic8BitSequenceTest)
{
EXPECT_FALSE(AzNetworking::SequenceMoreRecent(uint8_t(0), uint8_t(1)));
EXPECT_FALSE(AzNetworking::SequenceMoreRecent(uint8_t(1), uint8_t(2)));
EXPECT_TRUE (AzNetworking::SequenceMoreRecent(uint8_t(1), uint8_t(0)));
EXPECT_TRUE (AzNetworking::SequenceMoreRecent(uint8_t(2), uint8_t(1)));
}
TEST(SequenceGenerator, Wraparound8BitSequenceTest)
{
EXPECT_FALSE(AzNetworking::SequenceMoreRecent(uint8_t(254), uint8_t(255)));
EXPECT_FALSE(AzNetworking::SequenceMoreRecent(uint8_t(255), uint8_t( 0)));
EXPECT_TRUE (AzNetworking::SequenceMoreRecent(uint8_t(255), uint8_t(254)));
EXPECT_TRUE (AzNetworking::SequenceMoreRecent(uint8_t( 0), uint8_t(255)));
}
TEST(SequenceGenerator, Basic16BitSequenceTest)
{
EXPECT_FALSE(AzNetworking::SequenceMoreRecent(uint16_t(0), uint16_t(1)));
EXPECT_FALSE(AzNetworking::SequenceMoreRecent(uint16_t(1), uint16_t(2)));
EXPECT_TRUE (AzNetworking::SequenceMoreRecent(uint16_t(1), uint16_t(0)));
EXPECT_TRUE (AzNetworking::SequenceMoreRecent(uint16_t(2), uint16_t(1)));
}
TEST(SequenceGenerator, Wraparound16BitSequenceTest)
{
EXPECT_FALSE(AzNetworking::SequenceMoreRecent(uint16_t(65534), uint16_t(65535)));
EXPECT_FALSE(AzNetworking::SequenceMoreRecent(uint16_t(65535), uint16_t( 0)));
EXPECT_TRUE (AzNetworking::SequenceMoreRecent(uint16_t(65535), uint16_t(65534)));
EXPECT_TRUE (AzNetworking::SequenceMoreRecent(uint16_t( 0), uint16_t(65535)));
}
}
@@ -0,0 +1,63 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AzNetworking/DataStructures/FixedSizeBitset.h>
#include <AzCore/UnitTest/TestTypes.h>
namespace UnitTest
{
TEST(FixedSizeBitset, TestSetBit)
{
AzNetworking::FixedSizeBitset<128> test;
test.SetBit(0, true);
test.SetBit(2, true);
// 0000 0101
EXPECT_EQ(test.GetContainer()[0], 0x05);
}
TEST(FixedSizeBitset, TestAppend)
{
AzNetworking::FixedSizeBitset<63> appendTest1;
appendTest1.SetBit(1, true);
AzNetworking::FixedSizeBitset<63> appendTest2;
appendTest2.SetBit(2, true);
appendTest2.SetBit(32, true);
appendTest1 |= appendTest2;
EXPECT_EQ(appendTest1.GetBit(1), true);
EXPECT_EQ(appendTest1.GetBit(2), true);
EXPECT_EQ(appendTest1.GetBit(32), true);
}
TEST(FixedSizeBitset, TestAllSet)
{
AzNetworking::FixedSizeBitset<63> unusedBitTest(true);
bool allSet = true;
for (uint32_t i = 0; i < 63; ++i)
{
allSet &= unusedBitTest.GetBit(i);
}
EXPECT_EQ(allSet, true);
}
TEST(FixedSizeBitset, TestAnySet)
{
AzNetworking::FixedSizeBitset<9> unusedBitTest(true);
for (uint32_t i = 0; i < 9; ++i)
{
unusedBitTest.SetBit(i, false);
}
EXPECT_FALSE(unusedBitTest.AnySet());
}
}
@@ -0,0 +1,49 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AzNetworking/DataStructures/FixedSizeBitsetView.h>
#include <AzNetworking/DataStructures/FixedSizeBitset.h>
#include <AzCore/UnitTest/TestTypes.h>
namespace UnitTest
{
TEST(FixedSizeBitsetView, BasicTests)
{
AzNetworking::FixedSizeBitset<100> bitset;
{
AzNetworking::FixedSizeBitsetView view(bitset, 10, 1);
EXPECT_FALSE(bitset.GetBit(10));
EXPECT_FALSE(view.GetBit(0));
view.SetBit(0, true);
EXPECT_TRUE(bitset.GetBit(10));
EXPECT_TRUE(view.GetBit(0));
bitset.SetBit(10, false);
EXPECT_FALSE(view.GetBit(0));
}
{
AzNetworking::FixedSizeBitsetView view(bitset, 20, 1);
EXPECT_FALSE(view.GetBit(0));
EXPECT_FALSE(bitset.GetBit(20));
view.SetBit(0, true);
EXPECT_TRUE(bitset.GetBit(20));
EXPECT_TRUE(view.GetBit(0));
bitset.SetBit(20, false);
EXPECT_FALSE(view.GetBit(0));
}
}
}
@@ -0,0 +1,18 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AzNetworking/DataStructures/FixedSizeVectorBitset.h>
#include <AzCore/UnitTest/TestTypes.h>
namespace UnitTest
{
}
@@ -0,0 +1,83 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AzNetworking/DataStructures/RingBufferBitset.h>
#include <AzCore/UnitTest/TestTypes.h>
namespace UnitTest
{
AzNetworking::RingbufferBitset<128> test;
TEST(RingbufferBitset, Push2Bits)
{
test.PushBackBits(2);
test.SetBit(0, true);
test.SetBit(1, true);
EXPECT_TRUE(test.GetBit(0));
EXPECT_TRUE(test.GetBit(1));
}
TEST(RingbufferBitset, Push4Bits)
{
test.PushBackBits(2);
EXPECT_FALSE(test.GetBit(0));
EXPECT_FALSE(test.GetBit(1));
EXPECT_TRUE (test.GetBit(2));
EXPECT_TRUE (test.GetBit(3));
}
TEST(RingbufferBitset, Push44Bits)
{
test.PushBackBits(40);
test.SetBit(41, true);
EXPECT_FALSE(test.GetBit(40));
EXPECT_TRUE (test.GetBit(41));
EXPECT_TRUE (test.GetBit(42));
EXPECT_TRUE (test.GetBit(43));
}
TEST(RingbufferBitset, Push84Bits)
{
test.PushBackBits(40);
test.SetBit(80, true);
EXPECT_TRUE(test.GetBit(80));
EXPECT_TRUE(test.GetBit(81));
EXPECT_TRUE(test.GetBit(82));
EXPECT_TRUE(test.GetBit(83));
}
TEST(RingbufferBitset, Push124Bits)
{
test.PushBackBits(40);
EXPECT_TRUE(test.GetBit(120));
EXPECT_TRUE(test.GetBit(121));
EXPECT_TRUE(test.GetBit(122));
EXPECT_TRUE(test.GetBit(123));
}
TEST(RingbufferBitset, TestWrap)
{
// Bits should reset to false after wrapping and falling out of our window
test.PushBackBits(40);
EXPECT_FALSE(test.GetBit(160 - 128));
EXPECT_FALSE(test.GetBit(161 - 128));
EXPECT_FALSE(test.GetBit(162 - 128));
EXPECT_FALSE(test.GetBit(163 - 128));
}
}
@@ -0,0 +1,18 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AzNetworking/DataStructures/TimeoutQueue.h>
#include <AzCore/UnitTest/TestTypes.h>
namespace UnitTest
{
}
@@ -0,0 +1,17 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AzCore/UnitTest/UnitTest.h>
#include <AzTest/AzTest.h>
AZ_UNIT_TEST_HOOK(DEFAULT_UNIT_TEST_ENV);
@@ -0,0 +1,18 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AzNetworking/Serialization/DeltaSerializer.h>
#include <AzCore/UnitTest/TestTypes.h>
namespace UnitTest
{
}
@@ -0,0 +1,18 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AzNetworking/Serialization/HashSerializer.h>
#include <AzCore/UnitTest/TestTypes.h>
namespace UnitTest
{
}
@@ -0,0 +1,18 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AzNetworking/Serialization/NetworkInputSerializer.h>
#include <AzCore/UnitTest/TestTypes.h>
namespace UnitTest
{
}
@@ -0,0 +1,18 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AzNetworking/Serialization/NetworkOutputSerializer.h>
#include <AzCore/UnitTest/TestTypes.h>
namespace UnitTest
{
}
@@ -0,0 +1,18 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AzNetworking/Serialization/TrackChangedSerializer.h>
#include <AzCore/UnitTest/TestTypes.h>
namespace UnitTest
{
}
@@ -0,0 +1,192 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AzNetworking/TcpTransport/TcpNetworkInterface.h>
#include <AzNetworking/Framework/NetworkingSystemComponent.h>
#include <AzNetworking/AutoGen/CorePackets.AutoPackets.h>
#include <AzCore/Interface/Interface.h>
#include <AzCore/Console/LoggerSystemComponent.h>
#include <AzCore/Time/TimeSystemComponent.h>
#include <AzCore/Name/NameDictionary.h>
#include <AzCore/UnitTest/TestTypes.h>
namespace UnitTest
{
using namespace AzNetworking;
class TestTcpConnectionListener
: public IConnectionListener
{
public:
ConnectResult ValidateConnect([[maybe_unused]] const IpAddress& remoteAddress, [[maybe_unused]] const IPacketHeader& packetHeader, [[maybe_unused]] ISerializer& serializer)
{
return ConnectResult::Accepted;
}
void OnConnect([[maybe_unused]] IConnection* connection)
{
;
}
bool OnPacketReceived([[maybe_unused]] IConnection* connection, const IPacketHeader& packetHeader, [[maybe_unused]] ISerializer& serializer)
{
EXPECT_TRUE((packetHeader.GetPacketType() == static_cast<PacketType>(CorePackets::PacketType::InitiateConnectionPacket))
|| (packetHeader.GetPacketType() == static_cast<PacketType>(CorePackets::PacketType::HeartbeatPacket)));
return false;
}
void OnPacketLost([[maybe_unused]] IConnection* connection, [[maybe_unused]] PacketId packetId)
{
}
void OnDisconnect([[maybe_unused]] IConnection* connection, [[maybe_unused]] DisconnectReason reason, [[maybe_unused]] TerminationEndpoint endpoint)
{
}
};
class TestTcpClient
{
public:
TestTcpClient()
{
AZStd::string name = AZStd::string::format("TcpClient%d", ++s_numClients);
m_name = name;
m_clientNetworkInterface = AZ::Interface<INetworking>::Get()->CreateNetworkInterface(m_name, ProtocolType::Tcp, TrustZone::ExternalClientToServer, m_connectionListener);
m_clientNetworkInterface->Connect(IpAddress(127, 0, 0, 1, 12345));
}
~TestTcpClient()
{
AZ::Interface<INetworking>::Get()->DestroyNetworkInterface(m_name);
}
AZ::Name m_name;
TestTcpConnectionListener m_connectionListener;
INetworkInterface* m_clientNetworkInterface;
static inline int32_t s_numClients = 0;
};
class TestTcpServer
{
public:
TestTcpServer()
{
m_serverNetworkInterface = AZ::Interface<INetworking>::Get()->CreateNetworkInterface(m_name, ProtocolType::Tcp, TrustZone::ExternalClientToServer, m_connectionListener);
m_serverNetworkInterface->Listen(12345);
}
~TestTcpServer()
{
AZ::Interface<INetworking>::Get()->DestroyNetworkInterface(m_name);
}
AZ::Name m_name = AZ::Name(AZStd::string_view("TcpServer"));
TestTcpConnectionListener m_connectionListener;
INetworkInterface* m_serverNetworkInterface;
};
class TcpTransportTests
: public AllocatorsFixture
{
public:
void SetUp() override
{
SetupAllocator();
AZ::NameDictionary::Create();
m_loggerComponent = new AZ::LoggerSystemComponent;
m_timeComponent = new AZ::TimeSystemComponent;
m_networkingSystemComponent = new AzNetworking::NetworkingSystemComponent;
}
void TearDown() override
{
delete m_networkingSystemComponent;
delete m_timeComponent;
delete m_loggerComponent;
AZ::NameDictionary::Destroy();
TeardownAllocator();
}
AZ::LoggerSystemComponent* m_loggerComponent;
AZ::TimeSystemComponent* m_timeComponent;
AzNetworking::NetworkingSystemComponent* m_networkingSystemComponent;
};
#if AZ_TRAIT_DISABLE_FAILED_NETWORKING_TESTS
TEST_F(TcpTransportTests, DISABLED_TestSingleClient)
#else
TEST_F(TcpTransportTests, TestSingleClient)
#endif // AZ_TRAIT_DISABLE_FAILED_NETWORKING_TESTS
{
TestTcpServer testServer;
TestTcpClient testClient;
constexpr AZ::TimeMs TotalIterationTimeMs = AZ::TimeMs{ 5000 };
const AZ::TimeMs startTimeMs = AZ::GetElapsedTimeMs();
for (;;)
{
AZStd::this_thread::sleep_for(AZStd::chrono::milliseconds(25));
m_networkingSystemComponent->OnTick(0.0f, AZ::ScriptTimePoint());
bool timeExpired = (AZ::GetElapsedTimeMs() - startTimeMs > TotalIterationTimeMs);
bool canTerminate = (testServer.m_serverNetworkInterface->GetConnectionSet().GetConnectionCount() == 1)
&& (testClient.m_clientNetworkInterface->GetConnectionSet().GetConnectionCount() == 1);
if (canTerminate || timeExpired)
{
break;
}
}
EXPECT_EQ(testServer.m_serverNetworkInterface->GetConnectionSet().GetConnectionCount(), 1);
EXPECT_EQ(testClient.m_clientNetworkInterface->GetConnectionSet().GetConnectionCount(), 1);
}
#if AZ_TRAIT_DISABLE_FAILED_NETWORKING_TESTS
TEST_F(TcpTransportTests, DISABLED_TestMultipleClients)
#else
TEST_F(TcpTransportTests, TestMultipleClients)
#endif // AZ_TRAIT_DISABLE_FAILED_NETWORKING_TESTS
{
constexpr uint32_t NumTestClients = 50;
TestTcpServer testServer;
TestTcpClient testClient[NumTestClients];
constexpr AZ::TimeMs TotalIterationTimeMs = AZ::TimeMs{ 5000 };
const AZ::TimeMs startTimeMs = AZ::GetElapsedTimeMs();
for (;;)
{
AZStd::this_thread::sleep_for(AZStd::chrono::milliseconds(25));
m_networkingSystemComponent->OnTick(0.0f, AZ::ScriptTimePoint());
bool timeExpired = (AZ::GetElapsedTimeMs() - startTimeMs > TotalIterationTimeMs);
bool canTerminate = testServer.m_serverNetworkInterface->GetConnectionSet().GetConnectionCount() == NumTestClients;
for (uint32_t i = 0; i < NumTestClients; ++i)
{
canTerminate &= testClient[i].m_clientNetworkInterface->GetConnectionSet().GetConnectionCount() == 1;
}
if (canTerminate || timeExpired)
{
break;
}
}
EXPECT_EQ(testServer.m_serverNetworkInterface->GetConnectionSet().GetConnectionCount(), NumTestClients);
for (uint32_t i = 0; i < NumTestClients; ++i)
{
EXPECT_EQ(testClient[i].m_clientNetworkInterface->GetConnectionSet().GetConnectionCount(), 1);
}
}
}
@@ -0,0 +1,306 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AzNetworking/UdpTransport/UdpNetworkInterface.h>
#include <AzNetworking/UdpTransport/UdpPacketTracker.h>
#include <AzNetworking/UdpTransport/UdpPacketIdWindow.h>
#include <AzNetworking/ConnectionLayer/IConnectionListener.h>
#include <AzNetworking/Framework/NetworkingSystemComponent.h>
#include <AzNetworking/AutoGen/CorePackets.AutoPackets.h>
#include <AzCore/Interface/Interface.h>
#include <AzCore/Console/LoggerSystemComponent.h>
#include <AzCore/Time/TimeSystemComponent.h>
#include <AzCore/Name/NameDictionary.h>
#include <AzCore/UnitTest/TestTypes.h>
namespace UnitTest
{
using namespace AzNetworking;
class TestUdpConnectionListener
: public IConnectionListener
{
public:
ConnectResult ValidateConnect([[maybe_unused]] const IpAddress& remoteAddress, [[maybe_unused]] const IPacketHeader& packetHeader, [[maybe_unused]] ISerializer& serializer)
{
return ConnectResult::Accepted;
}
void OnConnect([[maybe_unused]] IConnection* connection)
{
;
}
bool OnPacketReceived([[maybe_unused]] IConnection* connection, const IPacketHeader& packetHeader, [[maybe_unused]] ISerializer& serializer)
{
EXPECT_TRUE((packetHeader.GetPacketType() == static_cast<PacketType>(CorePackets::PacketType::InitiateConnectionPacket))
|| (packetHeader.GetPacketType() == static_cast<PacketType>(CorePackets::PacketType::HeartbeatPacket)));
return false;
}
void OnPacketLost([[maybe_unused]] IConnection* connection, [[maybe_unused]] PacketId packetId)
{
}
void OnDisconnect([[maybe_unused]] IConnection* connection, [[maybe_unused]] DisconnectReason reason, [[maybe_unused]] TerminationEndpoint endpoint)
{
}
};
class TestUdpClient
{
public:
TestUdpClient()
{
AZStd::string name = AZStd::string::format("UdpClient%d", ++s_numClients);
m_name = name;
m_clientNetworkInterface = AZ::Interface<INetworking>::Get()->CreateNetworkInterface(m_name, ProtocolType::Udp, TrustZone::ExternalClientToServer, m_connectionListener);
m_clientNetworkInterface->Connect(IpAddress(127, 0, 0, 1, 12345));
}
~TestUdpClient()
{
AZ::Interface<INetworking>::Get()->DestroyNetworkInterface(m_name);
}
AZ::Name m_name;
TestUdpConnectionListener m_connectionListener;
INetworkInterface* m_clientNetworkInterface;
static inline int32_t s_numClients = 0;
};
class TestUdpServer
{
public:
TestUdpServer()
{
m_serverNetworkInterface = AZ::Interface<INetworking>::Get()->CreateNetworkInterface(m_name, ProtocolType::Udp, TrustZone::ExternalClientToServer, m_connectionListener);
m_serverNetworkInterface->Listen(12345);
}
~TestUdpServer()
{
AZ::Interface<INetworking>::Get()->DestroyNetworkInterface(m_name);
}
AZ::Name m_name = AZ::Name(AZStd::string_view("UdpServer"));
TestUdpConnectionListener m_connectionListener;
INetworkInterface* m_serverNetworkInterface;
};
class UdpTransportTests
: public AllocatorsFixture
{
public:
void SetUp() override
{
SetupAllocator();
AZ::NameDictionary::Create();
m_loggerComponent = new AZ::LoggerSystemComponent;
m_timeComponent = new AZ::TimeSystemComponent;
m_networkingSystemComponent = new AzNetworking::NetworkingSystemComponent;
}
void TearDown() override
{
delete m_networkingSystemComponent;
delete m_timeComponent;
delete m_loggerComponent;
AZ::NameDictionary::Destroy();
TeardownAllocator();
}
AZ::LoggerSystemComponent* m_loggerComponent;
AZ::TimeSystemComponent* m_timeComponent;
AzNetworking::NetworkingSystemComponent* m_networkingSystemComponent;
};
TEST_F(UdpTransportTests, AckReplication)
{
static const SequenceId TestReliableSequenceId = InvalidSequenceId;
static const PacketType TestPacketId = PacketType{ 0 };
UdpPacketTracker send;
UdpPacketTracker recv;
for (uint32_t i = 0; i < 128; i++)
{
UdpPacketHeader sendHeader1(send, TestPacketId, TestReliableSequenceId);
UdpPacketHeader sendHeader2(send, TestPacketId, TestReliableSequenceId);
UdpPacketHeader sendHeader3(send, TestPacketId, TestReliableSequenceId);
UdpPacketHeader sendHeader4(send, TestPacketId, TestReliableSequenceId);
UdpPacketHeader sendHeader5(send, TestPacketId, TestReliableSequenceId);
UdpPacketHeader sendHeader6(send, TestPacketId, TestReliableSequenceId);
UdpPacketHeader sendHeader7(send, TestPacketId, TestReliableSequenceId);
UdpPacketHeader sendHeader8(send, TestPacketId, TestReliableSequenceId);
UdpPacketHeader recvHeader1(recv, TestPacketId, TestReliableSequenceId);
UdpPacketHeader recvHeader2(recv, TestPacketId, TestReliableSequenceId);
UdpPacketHeader recvHeader3(recv, TestPacketId, TestReliableSequenceId);
UdpPacketHeader recvHeader4(recv, TestPacketId, TestReliableSequenceId);
UdpPacketHeader recvHeader5(recv, TestPacketId, TestReliableSequenceId);
UdpPacketHeader recvHeader6(recv, TestPacketId, TestReliableSequenceId);
UdpPacketHeader recvHeader7(recv, TestPacketId, TestReliableSequenceId);
UdpPacketHeader recvHeader8(recv, TestPacketId, TestReliableSequenceId);
send.ProcessReceived(nullptr, recvHeader3);
recv.ProcessReceived(nullptr, sendHeader3);
recv.ProcessReceived(nullptr, sendHeader2);
send.ProcessReceived(nullptr, recvHeader3);
recv.ProcessReceived(nullptr, sendHeader1);
recv.ProcessReceived(nullptr, sendHeader5);
recv.ProcessReceived(nullptr, sendHeader8);
send.ProcessReceived(nullptr, recvHeader2);
UdpPacketHeader recvHeaderTmp(recv, TestPacketId, TestReliableSequenceId);
send.ProcessReceived(nullptr, recvHeaderTmp);
{
BitsetChunk sendChunk;
BitsetChunk recvChunk;
send.GetAcknowledgedWindow().GetMostRecentAckState(sendChunk);
recv.GetReceivedWindow().GetMostRecentAckState(recvChunk);
BitsetChunk testResult = 0;
for (uint32_t bit = 0; bit < UdpPacketIdWindow::PacketAckContainer::NumBitsetChunkedBits; ++bit)
{
if (send.GetAcknowledgedWindow().GetPacketAckContainer().GetBit(bit))
{
SetBitHelper(testResult, bit, true);
}
}
EXPECT_EQ(sendChunk, recvChunk); // PacketTracker: Replication of acked bits
EXPECT_EQ(sendChunk, testResult); // Optimized ack window generation failed brute force check
}
UdpPacketHeader sendHeaderTmp(send, TestPacketId, TestReliableSequenceId);
recv.ProcessReceived(nullptr, sendHeaderTmp);
{
BitsetChunk sendChunk;
BitsetChunk recvChunk;
recv.GetAcknowledgedWindow().GetMostRecentAckState(sendChunk);
send.GetReceivedWindow().GetMostRecentAckState(recvChunk);
BitsetChunk testResult = 0;
for (uint32_t bit = 0; bit < UdpPacketIdWindow::PacketAckContainer::NumBitsetChunkedBits; ++bit)
{
if (recv.GetAcknowledgedWindow().GetPacketAckContainer().GetBit(bit))
{
SetBitHelper(testResult, bit, true);
}
}
EXPECT_EQ(sendChunk, recvChunk); // PacketTracker: Replication of acked bits
EXPECT_EQ(sendChunk, testResult); // Optimized ack window generation failed brute force check
}
}
}
TEST_F(UdpTransportTests, PacketIdWindow)
{
const PacketType TestPacketType{ 12212 };
UdpPacketIdWindow packetWindow;
UdpPacketHeader header1(TestPacketType, InvalidSequenceId, SequenceId{ 985 }, InvalidSequenceId, 0xF8000FFF, SequenceRolloverCount{ 0 });
packetWindow.UpdateForRemoteAckStatus(nullptr, header1);
UdpPacketHeader header2(TestPacketType, InvalidSequenceId, SequenceId{ 995 }, InvalidSequenceId, 0x3FFFFF, SequenceRolloverCount{ 0 });
packetWindow.UpdateForRemoteAckStatus(nullptr, header2);
UdpPacketHeader header3(TestPacketType, InvalidSequenceId, SequenceId{ 999 }, InvalidSequenceId, 0x3FFFFFF, SequenceRolloverCount{ 0 });
packetWindow.UpdateForRemoteAckStatus(nullptr, header3);
UdpPacketHeader header4(TestPacketType, InvalidSequenceId, SequenceId{ 1080 }, InvalidSequenceId, 0x3FF, SequenceRolloverCount{ 0 });
packetWindow.UpdateForRemoteAckStatus(nullptr, header4);
UdpPacketHeader header5(TestPacketType, InvalidSequenceId, SequenceId{ 1090 }, InvalidSequenceId, 0xFFFFF, SequenceRolloverCount{ 0 });
packetWindow.UpdateForRemoteAckStatus(nullptr, header5);
UdpPacketHeader header6(TestPacketType, InvalidSequenceId, SequenceId{ 1100 }, InvalidSequenceId, 0x3FFFFFFF, SequenceRolloverCount{ 0 });
packetWindow.UpdateForRemoteAckStatus(nullptr, header6);
UdpPacketHeader header7(TestPacketType, InvalidSequenceId, SequenceId{ 1102 }, InvalidSequenceId, 0xFFFFFFFF, SequenceRolloverCount{ 0 });
packetWindow.UpdateForRemoteAckStatus(nullptr, header7);
UdpPacketHeader header8(TestPacketType, InvalidSequenceId, SequenceId{ 1134 }, InvalidSequenceId, 0x1, SequenceRolloverCount{ 0 });
packetWindow.UpdateForRemoteAckStatus(nullptr, header8);
PacketAckState ackState = packetWindow.GetPacketAckStatus(PacketId(1007));
EXPECT_EQ(ackState, PacketAckState::Nacked); // Testing that PacketId is not flagged as acked
}
TEST_F(UdpTransportTests, TestSingleClient)
{
TestUdpServer testServer;
TestUdpClient testClient;
constexpr AZ::TimeMs TotalIterationTimeMs = AZ::TimeMs{ 5000 };
const AZ::TimeMs startTimeMs = AZ::GetElapsedTimeMs();
for (;;)
{
AZStd::this_thread::sleep_for(AZStd::chrono::milliseconds(25));
m_networkingSystemComponent->OnTick(0.0f, AZ::ScriptTimePoint());
bool timeExpired = (AZ::GetElapsedTimeMs() - startTimeMs > TotalIterationTimeMs);
bool canTerminate = (testServer.m_serverNetworkInterface->GetConnectionSet().GetConnectionCount() == 1)
&& (testClient.m_clientNetworkInterface->GetConnectionSet().GetConnectionCount() == 1);
if (canTerminate || timeExpired)
{
break;
}
}
EXPECT_EQ(testServer.m_serverNetworkInterface->GetConnectionSet().GetConnectionCount(), 1);
EXPECT_EQ(testClient.m_clientNetworkInterface->GetConnectionSet().GetConnectionCount(), 1);
}
TEST_F(UdpTransportTests, TestMultipleClients)
{
constexpr uint32_t NumTestClients = 50;
TestUdpServer testServer;
TestUdpClient testClient[NumTestClients];
constexpr AZ::TimeMs TotalIterationTimeMs = AZ::TimeMs{ 5000 };
const AZ::TimeMs startTimeMs = AZ::GetElapsedTimeMs();
for (;;)
{
AZStd::this_thread::sleep_for(AZStd::chrono::milliseconds(25));
m_networkingSystemComponent->OnTick(0.0f, AZ::ScriptTimePoint());
bool timeExpired = (AZ::GetElapsedTimeMs() - startTimeMs > TotalIterationTimeMs);
bool canTerminate = testServer.m_serverNetworkInterface->GetConnectionSet().GetConnectionCount() == NumTestClients;
for (uint32_t i = 0; i < NumTestClients; ++i)
{
canTerminate &= testClient[i].m_clientNetworkInterface->GetConnectionSet().GetConnectionCount() == 1;
}
if (canTerminate || timeExpired)
{
break;
}
}
EXPECT_EQ(testServer.m_serverNetworkInterface->GetConnectionSet().GetConnectionCount(), NumTestClients);
for (uint32_t i = 0; i < NumTestClients; ++i)
{
EXPECT_EQ(testClient[i].m_clientNetworkInterface->GetConnectionSet().GetConnectionCount(), 1);
}
}
}
@@ -0,0 +1,97 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AzNetworking/Utilities/CidrAddress.h>
#include <AzNetworking/Utilities/IpAddress.h>
#include <AzCore/UnitTest/TestTypes.h>
namespace UnitTest
{
class CidrAddressTests
: public AllocatorsFixture
{
public:
void SetUp() override
{
AllocatorsFixture::SetUp();
}
void TearDown() override
{
AllocatorsFixture::TearDown();
}
};
static const AzNetworking::IpAddress testIp1 = AzNetworking::IpAddress(127, 0, 0, 1, 0);
static const AzNetworking::IpAddress testIp2 = AzNetworking::IpAddress(127, 0, 0, 255, 0);
static const AzNetworking::IpAddress testIp3 = AzNetworking::IpAddress(127, 0, 255, 255, 0);
static const AzNetworking::IpAddress testIp4 = AzNetworking::IpAddress(127, 255, 255, 255, 0);
static const AzNetworking::IpAddress testIp5 = AzNetworking::IpAddress(255, 255, 255, 255, 0);
TEST_F(CidrAddressTests, Test32BitMask)
{
AzNetworking::CidrAddress testCidr1("127.0.0.1/32");
EXPECT_TRUE (testCidr1.IsMatch(testIp1));
EXPECT_FALSE(testCidr1.IsMatch(testIp2));
EXPECT_FALSE(testCidr1.IsMatch(testIp3));
EXPECT_FALSE(testCidr1.IsMatch(testIp4));
EXPECT_FALSE(testCidr1.IsMatch(testIp5));
}
TEST_F(CidrAddressTests, Test24BitMask)
{
AzNetworking::CidrAddress testCidr2("127.0.0.1/24");
EXPECT_TRUE (testCidr2.IsMatch(testIp1));
EXPECT_TRUE (testCidr2.IsMatch(testIp2));
EXPECT_FALSE(testCidr2.IsMatch(testIp3));
EXPECT_FALSE(testCidr2.IsMatch(testIp4));
EXPECT_FALSE(testCidr2.IsMatch(testIp5));
}
TEST_F(CidrAddressTests, Test16BitMask)
{
AzNetworking::CidrAddress testCidr3("127.0.0.1/16");
EXPECT_TRUE (testCidr3.IsMatch(testIp1));
EXPECT_TRUE (testCidr3.IsMatch(testIp2));
EXPECT_TRUE (testCidr3.IsMatch(testIp3));
EXPECT_FALSE(testCidr3.IsMatch(testIp4));
EXPECT_FALSE(testCidr3.IsMatch(testIp5));
}
TEST_F(CidrAddressTests, Test8BitMask)
{
AzNetworking::CidrAddress testCidr4("127.0.0.1/8");
EXPECT_TRUE (testCidr4.IsMatch(testIp1));
EXPECT_TRUE (testCidr4.IsMatch(testIp2));
EXPECT_TRUE (testCidr4.IsMatch(testIp3));
EXPECT_TRUE (testCidr4.IsMatch(testIp4));
EXPECT_FALSE(testCidr4.IsMatch(testIp5));
}
TEST_F(CidrAddressTests, TestAnyMatch)
{
AzNetworking::CidrAddress testCidr5("127.0.0.1/0");
EXPECT_TRUE (testCidr5.IsMatch(testIp1));
EXPECT_TRUE (testCidr5.IsMatch(testIp2));
EXPECT_TRUE (testCidr5.IsMatch(testIp3));
EXPECT_TRUE (testCidr5.IsMatch(testIp4));
EXPECT_TRUE (testCidr5.IsMatch(testIp5));
}
TEST_F(CidrAddressTests, TestOutOfRange)
{
AzNetworking::CidrAddress testCidr6("127.0.0.1/33");
EXPECT_EQ(testCidr6.GetMask(), 0xFFFFFFFF);
AzNetworking::CidrAddress testCidr7("127.0.0.1/-1");
EXPECT_EQ(testCidr7.GetMask(), 0);
}
}
@@ -0,0 +1,18 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AzNetworking/Utilities/IpAddress.h>
#include <AzCore/UnitTest/TestTypes.h>
namespace UnitTest
{
}
@@ -0,0 +1,38 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AzNetworking/Utilities/NetworkCommon.h>
#include <AzCore/UnitTest/TestTypes.h>
namespace UnitTest
{
TEST(NetworkCommon, GenerateSerializerIndexLabelByte)
{
EXPECT_STREQ(AzNetworking::GenerateIndexLabel<255>(0).c_str(), "00");
EXPECT_STREQ(AzNetworking::GenerateIndexLabel<255>(1).c_str(), "01");
EXPECT_STREQ(AzNetworking::GenerateIndexLabel<255>(10).c_str(), "0A");
EXPECT_STREQ(AzNetworking::GenerateIndexLabel<255>(100).c_str(), "64");
}
TEST(NetworkCommon, GenerateSerializerIndexLabelShort)
{
EXPECT_STREQ(AzNetworking::GenerateIndexLabel<65535>(0).c_str(), "0000");
EXPECT_STREQ(AzNetworking::GenerateIndexLabel<65535>(1).c_str(), "0001");
EXPECT_STREQ(AzNetworking::GenerateIndexLabel<65535>(10).c_str(), "000A");
EXPECT_STREQ(AzNetworking::GenerateIndexLabel<65535>(100).c_str(), "0064");
}
TEST(NetworkCommon, GenerateSerializerIndexLabelMax)
{
EXPECT_STREQ(AzNetworking::GenerateIndexLabel<UINT_MAX>(UINT_MAX).c_str(), "FFFFFFFF");
}
}
@@ -0,0 +1,363 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AzNetworking/Utilities/QuantizedValues.h>
#include <AzNetworking/Serialization/NetworkInputSerializer.h>
#include <AzNetworking/Serialization/NetworkOutputSerializer.h>
#include <AzCore/UnitTest/TestTypes.h>
namespace UnitTest
{
template <uint32_t NUM_ELEMENTS>
struct ValueFromFloat {};
template <>
struct ValueFromFloat<1>
{
using ValueType = float;
static float Construct(float value)
{
return value;
}
static void CheckEqual(ValueType a, ValueType b)
{
EXPECT_FLOAT_EQ(a, b);
}
};
template <>
struct ValueFromFloat<2>
{
using ValueType = AZ::Vector2;
static AZ::Vector2 Construct(float value)
{
return AZ::Vector2(value, value);
}
static void CheckEqual(ValueType a, ValueType b)
{
EXPECT_TRUE(a.IsClose(b, 0.01f));
}
};
template <>
struct ValueFromFloat<3>
{
using ValueType = AZ::Vector3;
static AZ::Vector3 Construct(float value)
{
return AZ::Vector3(value, value, value);
}
static void CheckEqual(ValueType a, ValueType b)
{
EXPECT_TRUE(a.IsClose(b, 0.01f));
}
};
template <>
struct ValueFromFloat<4>
{
using ValueType = AZ::Quaternion;
static AZ::Quaternion Construct(float value)
{
return AZ::Quaternion(value, value, value, value);
}
static void CheckEqual(ValueType a, ValueType b)
{
EXPECT_TRUE(a.IsClose(b, 0.01f));
}
};
template <uint32_t NUM_ELEMENTS, uint32_t NUM_BYTES>
void TestQuantizedValuesHelper01()
{
AzNetworking::QuantizedValues<NUM_ELEMENTS, NUM_BYTES, 0, 1> testIn, testOut; // Transmits float values between 0 and 1 using NUM_BYTES
AZStd::array<uint8_t, 1024> buffer;
AzNetworking::NetworkInputSerializer inputSerializer(buffer.data(), static_cast<uint32_t>(buffer.size()));
AzNetworking::NetworkOutputSerializer outputSerializer(buffer.data(), static_cast<uint32_t>(buffer.size()));
testIn = ValueFromFloat<NUM_ELEMENTS>::Construct(0.0f);
EXPECT_EQ(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(0.0f));
testIn.Serialize(inputSerializer);
EXPECT_EQ(inputSerializer.GetSize(), NUM_BYTES * NUM_ELEMENTS);
testOut.Serialize(outputSerializer);
EXPECT_EQ(testIn, testOut);
testIn = ValueFromFloat<NUM_ELEMENTS>::Construct(1.0f);
EXPECT_EQ(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(1.0f));
testIn.Serialize(inputSerializer);
testOut.Serialize(outputSerializer);
EXPECT_EQ(testIn, testOut);
testIn = ValueFromFloat<NUM_ELEMENTS>::Construct(0.5f);
EXPECT_EQ(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(0.5f));
testIn.Serialize(inputSerializer);
testOut.Serialize(outputSerializer);
EXPECT_EQ(testIn, testOut);
testIn = ValueFromFloat<NUM_ELEMENTS>::Construct(-1.0f);
EXPECT_NE(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(-1.0f));
EXPECT_EQ(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(0.0f));
testIn.Serialize(inputSerializer);
testOut.Serialize(outputSerializer);
EXPECT_EQ(testIn, testOut);
testIn = ValueFromFloat<NUM_ELEMENTS>::Construct(2.0f);
EXPECT_NE(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(2.0f));
EXPECT_EQ(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(1.0f));
testIn.Serialize(inputSerializer);
testOut.Serialize(outputSerializer);
EXPECT_EQ(testIn, testOut);
}
template <uint32_t NUM_ELEMENTS, uint32_t NUM_BYTES>
void TestQuantizedValuesHelper11()
{
AzNetworking::QuantizedValues<NUM_ELEMENTS, NUM_BYTES, -1, 1> testIn, testOut; // Transmits float values between -1.0 and 1.0 using NUM_BYTES
AZStd::array<uint8_t, 1024> buffer;
AzNetworking::NetworkInputSerializer inputSerializer(buffer.data(), static_cast<uint32_t>(buffer.size()));
AzNetworking::NetworkOutputSerializer outputSerializer(buffer.data(), static_cast<uint32_t>(buffer.size()));
testIn = ValueFromFloat<NUM_ELEMENTS>::Construct(-1.0f);
ValueFromFloat<NUM_ELEMENTS>::CheckEqual(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(-1.0f));
testIn.Serialize(inputSerializer);
EXPECT_EQ(inputSerializer.GetSize(), NUM_BYTES * NUM_ELEMENTS);
testOut.Serialize(outputSerializer);
EXPECT_EQ(testIn, testOut);
testIn = ValueFromFloat<NUM_ELEMENTS>::Construct(1.0f);
ValueFromFloat<NUM_ELEMENTS>::CheckEqual(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(1.0f));
testIn.Serialize(inputSerializer);
testOut.Serialize(outputSerializer);
EXPECT_EQ(testIn, testOut);
testIn = ValueFromFloat<NUM_ELEMENTS>::Construct(0.0f);
ValueFromFloat<NUM_ELEMENTS>::CheckEqual(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(0.0f));
testIn.Serialize(inputSerializer);
testOut.Serialize(outputSerializer);
EXPECT_EQ(testIn, testOut);
testIn = ValueFromFloat<NUM_ELEMENTS>::Construct(-2.0f);
EXPECT_NE(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(-2.0f));
ValueFromFloat<NUM_ELEMENTS>::CheckEqual(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(-1.0f));
testIn.Serialize(inputSerializer);
testOut.Serialize(outputSerializer);
EXPECT_EQ(testIn, testOut);
testIn = ValueFromFloat<NUM_ELEMENTS>::Construct(2.0f);
EXPECT_NE(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(2.0f));
ValueFromFloat<NUM_ELEMENTS>::CheckEqual(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(1.0f));
testIn.Serialize(inputSerializer);
testOut.Serialize(outputSerializer);
EXPECT_EQ(testIn, testOut);
}
template <uint32_t NUM_ELEMENTS, uint32_t NUM_BYTES>
void TestQuantizedValuesHelper16k()
{
AzNetworking::QuantizedValues<NUM_ELEMENTS, NUM_BYTES, -16384, 16384> testIn, testOut; // Transmits float values between -1.0 and 1.0 using NUM_BYTES
AZStd::array<uint8_t, 1024> buffer;
AzNetworking::NetworkInputSerializer inputSerializer(buffer.data(), static_cast<uint32_t>(buffer.size()));
AzNetworking::NetworkOutputSerializer outputSerializer(buffer.data(), static_cast<uint32_t>(buffer.size()));
testIn = ValueFromFloat<NUM_ELEMENTS>::Construct(-2000.0f);
ValueFromFloat<NUM_ELEMENTS>::CheckEqual(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(-2000.0f));
testIn.Serialize(inputSerializer);
EXPECT_EQ(inputSerializer.GetSize(), NUM_BYTES * NUM_ELEMENTS);
testOut.Serialize(outputSerializer);
EXPECT_EQ(testIn, testOut);
testIn = ValueFromFloat<NUM_ELEMENTS>::Construct(2000.0f);
ValueFromFloat<NUM_ELEMENTS>::CheckEqual(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(2000.0f));
testIn.Serialize(inputSerializer);
testOut.Serialize(outputSerializer);
EXPECT_EQ(testIn, testOut);
testIn = ValueFromFloat<NUM_ELEMENTS>::Construct(0.0f);
ValueFromFloat<NUM_ELEMENTS>::CheckEqual(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(0.0f));
testIn.Serialize(inputSerializer);
testOut.Serialize(outputSerializer);
EXPECT_EQ(testIn, testOut);
testIn = ValueFromFloat<NUM_ELEMENTS>::Construct(-16385.0f);
EXPECT_NE(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(-16385.0f));
ValueFromFloat<NUM_ELEMENTS>::CheckEqual(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(-16384.0f));
testIn.Serialize(inputSerializer);
testOut.Serialize(outputSerializer);
EXPECT_EQ(testIn, testOut);
testIn = ValueFromFloat<NUM_ELEMENTS>::Construct(16385.0f);
EXPECT_NE(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(16385.0f));
EXPECT_EQ(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(16384.0f));
testIn.Serialize(inputSerializer);
testOut.Serialize(outputSerializer);
EXPECT_EQ(testIn, testOut);
}
template <uint32_t NUM_ELEMENTS, uint32_t NUM_BYTES>
void TestQuantizedValuesHelper24bitRange()
{
// This gives us a hash sensitivity of around 1/128th of a unit, and will detect errors within a range of -16,777,216 to +16,777,216
static const int32_t FloatHashMinValue = (INT_MIN >> 7);
static const int32_t FloatHashMaxValue = (INT_MAX >> 7);
AzNetworking::QuantizedValues<NUM_ELEMENTS, NUM_BYTES, FloatHashMinValue, FloatHashMaxValue> testIn, testOut;
AZStd::array<uint8_t, 1024> buffer;
AzNetworking::NetworkInputSerializer inputSerializer(buffer.data(), static_cast<uint32_t>(buffer.size()));
AzNetworking::NetworkOutputSerializer outputSerializer(buffer.data(), static_cast<uint32_t>(buffer.size()));
testIn = ValueFromFloat<NUM_ELEMENTS>::Construct(-20000.0f);
ValueFromFloat<NUM_ELEMENTS>::CheckEqual(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(-20000.0f));
testIn.Serialize(inputSerializer);
EXPECT_EQ(inputSerializer.GetSize(), NUM_BYTES * NUM_ELEMENTS);
testOut.Serialize(outputSerializer);
EXPECT_EQ(testIn, testOut);
testIn = ValueFromFloat<NUM_ELEMENTS>::Construct(20000.0f);
ValueFromFloat<NUM_ELEMENTS>::CheckEqual(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(20000.0f));
testIn.Serialize(inputSerializer);
testOut.Serialize(outputSerializer);
EXPECT_EQ(testIn, testOut);
testIn = ValueFromFloat<NUM_ELEMENTS>::Construct(0.0f);
ValueFromFloat<NUM_ELEMENTS>::CheckEqual(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(0.0f));
testIn.Serialize(inputSerializer);
testOut.Serialize(outputSerializer);
EXPECT_EQ(testIn, testOut);
testIn = ValueFromFloat<NUM_ELEMENTS>::Construct(-163850.0f);
ValueFromFloat<NUM_ELEMENTS>::CheckEqual(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(-163850.0f));
testIn.Serialize(inputSerializer);
testOut.Serialize(outputSerializer);
EXPECT_EQ(testIn, testOut);
testIn = ValueFromFloat<NUM_ELEMENTS>::Construct(163850.0f);
ValueFromFloat<NUM_ELEMENTS>::CheckEqual(static_cast<typename ValueFromFloat<NUM_ELEMENTS>::ValueType>(testIn), ValueFromFloat<NUM_ELEMENTS>::Construct(163850.0f));
testIn.Serialize(inputSerializer);
testOut.Serialize(outputSerializer);
EXPECT_EQ(testIn, testOut);
}
TEST(QuantizedValues, Test1Elements1Bytes)
{
TestQuantizedValuesHelper01<1, 1>();
TestQuantizedValuesHelper11<1, 1>();
}
TEST(QuantizedValues, Test2Elements1Bytes)
{
TestQuantizedValuesHelper01<2, 1>();
TestQuantizedValuesHelper11<2, 1>();
}
TEST(QuantizedValues, Test3Elements1Bytes)
{
TestQuantizedValuesHelper01<3, 1>();
TestQuantizedValuesHelper11<3, 1>();
}
TEST(QuantizedValues, Test4Elements1Bytes)
{
TestQuantizedValuesHelper01<4, 1>();
TestQuantizedValuesHelper11<4, 1>();
}
TEST(QuantizedValues, Test1Elements2Bytes)
{
TestQuantizedValuesHelper01<1, 2>();
TestQuantizedValuesHelper11<1, 2>();
}
TEST(QuantizedValues, Test2Elements2Bytes)
{
TestQuantizedValuesHelper01<2, 2>();
TestQuantizedValuesHelper11<2, 2>();
}
TEST(QuantizedValues, Test3Elements2Bytes)
{
TestQuantizedValuesHelper01<3, 2>();
TestQuantizedValuesHelper11<3, 2>();
}
TEST(QuantizedValues, Test4Elements2Bytes)
{
TestQuantizedValuesHelper01<4, 2>();
TestQuantizedValuesHelper11<4, 2>();
}
TEST(QuantizedValues, Test1Elements3Bytes)
{
TestQuantizedValuesHelper01 <1, 3>();
TestQuantizedValuesHelper11 <1, 3>();
TestQuantizedValuesHelper16k<1, 3>();
}
TEST(QuantizedValues, Test2Elements3Bytes)
{
TestQuantizedValuesHelper01 <2, 3>();
TestQuantizedValuesHelper11 <2, 3>();
TestQuantizedValuesHelper16k<2, 3>();
}
TEST(QuantizedValues, Test3Elements3Bytes)
{
TestQuantizedValuesHelper01 <3, 3>();
TestQuantizedValuesHelper11 <3, 3>();
TestQuantizedValuesHelper16k<3, 3>();
}
TEST(QuantizedValues, Test4Elements3Bytes)
{
TestQuantizedValuesHelper01 <4, 3>();
TestQuantizedValuesHelper11 <4, 3>();
TestQuantizedValuesHelper16k<4, 3>();
}
TEST(QuantizedValues, Test1Elements4Bytes)
{
TestQuantizedValuesHelper01 <1, 4>();
TestQuantizedValuesHelper11 <1, 4>();
TestQuantizedValuesHelper16k<1, 4>();
}
TEST(QuantizedValues, Test2Elements4Bytes)
{
TestQuantizedValuesHelper01 <2, 4>();
TestQuantizedValuesHelper11 <2, 4>();
TestQuantizedValuesHelper16k<2, 4>();
TestQuantizedValuesHelper24bitRange<2, 4>();
}
TEST(QuantizedValues, Test3Elements4Bytes)
{
TestQuantizedValuesHelper01 <3, 4>();
TestQuantizedValuesHelper11 <3, 4>();
TestQuantizedValuesHelper16k<3, 4>();
TestQuantizedValuesHelper24bitRange<3, 4>();
}
TEST(QuantizedValues, Test4Elements4Bytes)
{
TestQuantizedValuesHelper01 <4, 4>();
TestQuantizedValuesHelper11 <4, 4>();
TestQuantizedValuesHelper16k<4, 4>();
TestQuantizedValuesHelper24bitRange<4, 4>();
}
}
@@ -0,0 +1,32 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
set(FILES
Main.cpp
ConnectionLayer/ConnectionMetricsTests.cpp
ConnectionLayer/SequenceGeneratorTests.cpp
DataStructures/FixedSizeBitsetTests.cpp
DataStructures/FixedSizeBitsetViewTests.cpp
DataStructures/FixedSizeVectorBitsetTests.cpp
DataStructures/RingBufferBitsetTests.cpp
DataStructures/TimeoutQueueTests.cpp
Serialization/DeltaSerializerTests.cpp
Serialization/HashSerializerTests.cpp
Serialization/NetworkInputSerializerTests.cpp
Serialization/NetworkOutputSerializerTests.cpp
Serialization/TrackChangedSerializerTests.cpp
TcpTransport/TcpTransportTests.cpp
UdpTransport/UdpTransportTests.cpp
Utilities/CidrAddressTests.cpp
Utilities/IpAddressTests.cpp
Utilities/NetworkCommonTests.cpp
Utilities/QuantizedValuesTests.cpp
)