diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b368cf..7403bcd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) diff --git a/include/Sockets/Endianness.hpp b/include/Sockets/Endianness.hpp index c9de8a1..b7c86b8 100644 --- a/include/Sockets/Endianness.hpp +++ b/include/Sockets/Endianness.hpp @@ -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 T ReverseByteOrder(T val) diff --git a/main.cpp b/main.cpp index 802a34b..f6d7a42 100644 --- a/main.cpp +++ b/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; @@ -43,7 +45,7 @@ void TCP_Roundtrip() client->Close(); } -void UDP_Roundtrip() +void UDP_ReceiveTest() { using UdpSocket_p = std::shared_ptr; @@ -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; } diff --git a/src/Sockets/TcpSocket.cpp b/src/Sockets/TcpSocket.cpp index 14a7492..54c6fc1 100644 --- a/src/Sockets/TcpSocket.cpp +++ b/src/Sockets/TcpSocket.cpp @@ -10,15 +10,10 @@ #include #include #include +#include -#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::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); diff --git a/src/Sockets/UdpSocket.cpp b/src/Sockets/UdpSocket.cpp index 8b495d9..e13bd6f 100644 --- a/src/Sockets/UdpSocket.cpp +++ b/src/Sockets/UdpSocket.cpp @@ -2,7 +2,6 @@ #include "Sockets/Sockets.hpp" - namespace Socket { UdpSocket::UdpSocket(int family, int flags) diff --git a/tests/ExceptionTests.cpp b/tests/ExceptionTests.cpp deleted file mode 100644 index f721a43..0000000 --- a/tests/ExceptionTests.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#include - -#include \ No newline at end of file diff --git a/tests/ExceptionTests.hpp b/tests/ExceptionTests.hpp new file mode 100644 index 0000000..1da5456 --- /dev/null +++ b/tests/ExceptionTests.hpp @@ -0,0 +1,2 @@ + +#include \ No newline at end of file diff --git a/tests/IPAddressTests.cpp b/tests/IPAddressTests.cpp deleted file mode 100644 index c41b6c8..0000000 --- a/tests/IPAddressTests.cpp +++ /dev/null @@ -1,61 +0,0 @@ -#include -#include -#include - -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); -} - diff --git a/tests/IPAddressTests.hpp b/tests/IPAddressTests.hpp new file mode 100644 index 0000000..3a84d46 --- /dev/null +++ b/tests/IPAddressTests.hpp @@ -0,0 +1,84 @@ +#include +#include +#include +#include + +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); + }); + } +} + + + + + + diff --git a/tests/UriTests.cpp b/tests/UriTests.hpp similarity index 72% rename from tests/UriTests.cpp rename to tests/UriTests.hpp index 5fed2b1..1b63f5a 100644 --- a/tests/UriTests.cpp +++ b/tests/UriTests.hpp @@ -1,4 +1,3 @@ -#include #include TEST(UriTest, Uri_Constructor_Default) diff --git a/tests/tests.cpp b/tests/tests.cpp index ed37f84..b62ba9e 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -1,7 +1,21 @@ #include +#include "IPAddressTests.hpp" + +void DefineTests() +{ + IPAddressTests::Define(); +} + +void RunTests() +{ + IPAddressTests::Run(); +} int main() { - std::cout << "Tests coming soon!!" << std::endl; + DefineTests(); + RunTests(); + + return 0; } \ No newline at end of file