Refactor to use jtest library
This commit is contained in:
@@ -33,15 +33,25 @@ endif()
|
||||
|
||||
set_target_properties(Sockets PROPERTIES LINKER_LANGUAGE CXX)
|
||||
|
||||
|
||||
CPMAddPackage(
|
||||
NAME Event
|
||||
URL https://git.redacted.cc/josh/Event/archive/Release-10.zip
|
||||
)
|
||||
CPMAddPackage(
|
||||
NAME jlog
|
||||
URL https://git.redacted.cc/josh/jlog/archive/Prerelease-16.zip
|
||||
)
|
||||
CPMAddPackage(
|
||||
NAME jtest
|
||||
URL https://git.redacted.cc/josh/jtest/archive/Prerelease-5.zip
|
||||
URL https://git.redacted.cc/josh/jtest/archive/Release-1.3.zip
|
||||
)
|
||||
|
||||
|
||||
target_include_directories(Sockets PUBLIC ${jtest_SOURCE_DIR}/include)
|
||||
target_include_directories(Event PUBLIC ${Event_SOURCE_DIR}/include)
|
||||
|
||||
target_link_libraries(Sockets PUBLIC jtest)
|
||||
target_link_libraries(Sockets PUBLIC jtest Event)
|
||||
|
||||
install(TARGETS ${PROJECT_NAME} DESTINATION lib/${PROJECT_NAME})
|
||||
install(FILES ${HEADERS} DESTINATION include/${PROJECT_NAME})
|
||||
|
@@ -15,6 +15,11 @@ namespace Socket
|
||||
constexpr bool IsLittleEndian() {
|
||||
return (std::endian::native == std::endian::little);
|
||||
}
|
||||
constexpr bool IsBigIndian()
|
||||
{
|
||||
/// Shoutout Umar Rajguru
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T ReverseByteOrder(T val)
|
||||
|
12
main.cpp
12
main.cpp
@@ -9,8 +9,10 @@
|
||||
|
||||
using namespace Socket;
|
||||
|
||||
|
||||
void TCP_Roundtrip()
|
||||
/// Spins up a TCP server, listens for connections, and echoes input directly back to the client.
|
||||
/// While running this function, use the terminal program netcat to connect and send messages:
|
||||
/// nc localhost 40269
|
||||
void TCP_ReceiveTest()
|
||||
{
|
||||
using Socket_p = std::shared_ptr<TcpSocket>;
|
||||
|
||||
@@ -43,7 +45,7 @@ void TCP_Roundtrip()
|
||||
client->Close();
|
||||
}
|
||||
|
||||
void UDP_Roundtrip()
|
||||
void UDP_ReceiveTest()
|
||||
{
|
||||
using UdpSocket_p = std::shared_ptr<UdpSocket>;
|
||||
|
||||
@@ -100,8 +102,8 @@ int main(int argc, char *argv[])
|
||||
std::cout << a << std::endl;
|
||||
|
||||
|
||||
TCP_Roundtrip();
|
||||
UDP_Roundtrip();
|
||||
TCP_ReceiveTest();
|
||||
UDP_ReceiveTest();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@@ -10,15 +10,10 @@
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <jlog/Logger.hpp>
|
||||
|
||||
#define DEBUG_ACT true
|
||||
|
||||
namespace Socket {
|
||||
void DEBUG(std::string message)
|
||||
{
|
||||
if (DEBUG_ACT)
|
||||
std::cout << "DEBUG: " << message << std::endl;
|
||||
}
|
||||
|
||||
TcpSocket::TcpSocket()
|
||||
{
|
||||
@@ -142,7 +137,7 @@ namespace Socket {
|
||||
if (::listen(mSock, maxQueue) != 0)
|
||||
CheckErrors_accept(errno);
|
||||
|
||||
DEBUG("Listening...");
|
||||
jlog::Debug("Listening...");
|
||||
}
|
||||
|
||||
|
||||
@@ -221,7 +216,7 @@ namespace Socket {
|
||||
}
|
||||
|
||||
std::shared_ptr<TcpSocket> TcpSocket::Accept() {
|
||||
DEBUG("Starting to accept");
|
||||
jlog::Debug("Starting to accept");
|
||||
union {
|
||||
sockaddr addr;
|
||||
sockaddr_in in;
|
||||
@@ -232,10 +227,10 @@ namespace Socket {
|
||||
int newSock;
|
||||
if ((newSock = ::accept(mSock, (struct sockaddr *) 0, (unsigned int *) 0)) == -1) {
|
||||
//if ((newSock = ::accept(mSock, (sockaddr*)&address.s, &addressSize)) == -1) {
|
||||
DEBUG(strerror(errno));
|
||||
jlog::Debug(strerror(errno));
|
||||
CheckErrors_accept(errno);
|
||||
}
|
||||
DEBUG("1 client accepted");
|
||||
jlog::Debug("1 client accepted");
|
||||
|
||||
addrinfo info;
|
||||
memset(&info, 0, sizeof info);
|
||||
|
@@ -2,7 +2,6 @@
|
||||
#include "Sockets/Sockets.hpp"
|
||||
|
||||
|
||||
|
||||
namespace Socket
|
||||
{
|
||||
UdpSocket::UdpSocket(int family, int flags)
|
||||
|
@@ -1,3 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <Sockets/Exceptions.hpp>
|
2
tests/ExceptionTests.hpp
Normal file
2
tests/ExceptionTests.hpp
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
#include <Sockets/Exceptions.hpp>
|
@@ -1,61 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <Sockets/IPAddress.hpp>
|
||||
#include <Sockets/Exceptions.hpp>
|
||||
|
||||
TEST(IPAddressTest, IPAddress_Constructor_Default)
|
||||
{
|
||||
Socket::IPAddress addr;
|
||||
ASSERT_TRUE(addr.addr_string() == "0.0.0.0");
|
||||
ASSERT_TRUE(addr.port == 0);
|
||||
}
|
||||
|
||||
TEST(IPAddressTest, IPAddress_Constructor_String)
|
||||
{
|
||||
Socket::IPAddress addr("127.0.0.1", 4444);
|
||||
ASSERT_TRUE(addr.addr_string() == "127.0.0.1");
|
||||
ASSERT_TRUE(addr.port == 4444);
|
||||
}
|
||||
|
||||
TEST(IPAddressTest, IPAddress_Constructor_Octets)
|
||||
{
|
||||
Socket::IPAddress addr(255, 0, 255, 0, 42069);
|
||||
ASSERT_TRUE(addr.octets[0]==255);
|
||||
ASSERT_TRUE(addr.octets[1]==0);
|
||||
ASSERT_TRUE(addr.octets[2]==255);
|
||||
ASSERT_TRUE(addr.octets[3]==0);
|
||||
ASSERT_TRUE(addr.port == 42069);
|
||||
}
|
||||
|
||||
TEST(IPAddressTest, IPAddress_Constructor_Invalid_Address)
|
||||
{
|
||||
ASSERT_THROW({
|
||||
Socket::IPAddress addr("127.0.0,", 4444);
|
||||
}, std::exception);
|
||||
ASSERT_THROW({
|
||||
Socket::IPAddress addr("www.google.com", 4444);
|
||||
}, std::exception);
|
||||
}
|
||||
|
||||
TEST(IPAddressTest, IPAddress_Resolve)
|
||||
{
|
||||
Socket::IPAddress addr = Socket::IPAddress::Resolve("www.google.com", 80);
|
||||
|
||||
}
|
||||
|
||||
TEST(IPAddressTest, IPAddress_Resolve_Invalid)
|
||||
{
|
||||
ASSERT_THROW({
|
||||
Socket::IPAddress addr = Socket::IPAddress::Resolve("GOOGLEPLS", 80);
|
||||
}, std::exception);
|
||||
}
|
||||
|
||||
TEST(IPAddressTest, IPAddress_Operator_Equality)
|
||||
{
|
||||
Socket::IPAddress a("127.0.0.1", 42069);
|
||||
Socket::IPAddress b("127.0.0.1", 42069);
|
||||
Socket::IPAddress c("255.255.255.255", 666);
|
||||
|
||||
ASSERT_TRUE(a == b);
|
||||
ASSERT_TRUE(a != c);
|
||||
}
|
||||
|
84
tests/IPAddressTests.hpp
Normal file
84
tests/IPAddressTests.hpp
Normal file
@@ -0,0 +1,84 @@
|
||||
#include <Sockets/IPAddress.hpp>
|
||||
#include <Sockets/Exceptions.hpp>
|
||||
#include <jtest/jtest.hpp>
|
||||
#include <jtest/Unit.hpp>
|
||||
|
||||
jtest::Unit IPAddrUnit {"IPAddress"};
|
||||
|
||||
namespace IPAddressTests
|
||||
{
|
||||
|
||||
inline void Run()
|
||||
{
|
||||
IPAddrUnit.RunAll();
|
||||
}
|
||||
|
||||
inline void Define()
|
||||
{
|
||||
using namespace Socket;
|
||||
using namespace jtest;
|
||||
|
||||
IPAddrUnit += Test("DefaultCtor", []
|
||||
{
|
||||
Socket::IPAddress addr;
|
||||
check(addr.addr_string() == "0.0.0.0");
|
||||
check(addr.port == 0);
|
||||
});
|
||||
|
||||
IPAddrUnit += Test("StringCtor", []
|
||||
{
|
||||
Socket::IPAddress addr("127.0.0.1", 4444);
|
||||
check(addr.addr_string() == "127.0.0.1");
|
||||
check(addr.port == 4444);
|
||||
});
|
||||
|
||||
IPAddrUnit += Test("OctetCtor", []
|
||||
{
|
||||
Socket::IPAddress addr(255, 0, 255, 0, 42069);
|
||||
check(addr.octets[0]==255);
|
||||
check(addr.octets[1]==0);
|
||||
check(addr.octets[2]==255);
|
||||
check(addr.octets[3]==0);
|
||||
check(addr.port == 42069);
|
||||
});
|
||||
|
||||
IPAddrUnit += Test("CtorWithInvalidAddress", []
|
||||
{
|
||||
check_death([]{
|
||||
Socket::IPAddress addr("127.0.0,", 4444);
|
||||
});
|
||||
check_death([]{
|
||||
Socket::IPAddress addr("www.google.com", 4444);
|
||||
});
|
||||
});
|
||||
|
||||
IPAddrUnit += Test("Resolve", []
|
||||
{
|
||||
Socket::IPAddress addr = Socket::IPAddress::Resolve("www.google.com", 80);
|
||||
|
||||
});
|
||||
|
||||
IPAddrUnit += Test("ResolveWithInvalidUrl", []
|
||||
{
|
||||
check_death([]{
|
||||
Socket::IPAddress addr = Socket::IPAddress::Resolve("GOOGLEPLS", 80);
|
||||
});
|
||||
});
|
||||
|
||||
IPAddrUnit += Test("operator == ()", []
|
||||
{
|
||||
Socket::IPAddress a("127.0.0.1", 42069);
|
||||
Socket::IPAddress b("127.0.0.1", 42069);
|
||||
Socket::IPAddress c("255.255.255.255", 666);
|
||||
|
||||
check(a == b);
|
||||
check(a != c);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@@ -1,4 +1,3 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <Sockets/Uri.hpp>
|
||||
|
||||
TEST(UriTest, Uri_Constructor_Default)
|
@@ -1,7 +1,21 @@
|
||||
|
||||
#include <iostream>
|
||||
#include "IPAddressTests.hpp"
|
||||
|
||||
void DefineTests()
|
||||
{
|
||||
IPAddressTests::Define();
|
||||
}
|
||||
|
||||
void RunTests()
|
||||
{
|
||||
IPAddressTests::Run();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << "Tests coming soon!!" << std::endl;
|
||||
DefineTests();
|
||||
RunTests();
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user