From 2fca53d388f22585d2404f9ef15fbcb714a7a3dc Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 21 Aug 2025 08:32:35 +0200 Subject: feat: simplify the unit test for the `modem` class Signed-off-by: Amlal El Mahrouss --- dev/lib/core/includes.hpp | 5 +- dev/lib/net/modem.hpp | 144 +++++++++++++++++++++++++++++ dev/lib/net/network.hpp | 136 --------------------------- dev/tests/fix_basic/CMakeLists.txt | 2 +- dev/tests/network_basic/CMakeLists.txt | 4 +- dev/tests/network_basic/net_test.cc | 18 +--- dev/tests/tracked_ptr_basic/CMakeLists.txt | 2 +- dev/tests/tracked_ptr_leak/CMakeLists.txt | 2 +- 8 files changed, 154 insertions(+), 159 deletions(-) create mode 100644 dev/lib/net/modem.hpp delete mode 100644 dev/lib/net/network.hpp (limited to 'dev') diff --git a/dev/lib/core/includes.hpp b/dev/lib/core/includes.hpp index e482a7c..6bddba9 100644 --- a/dev/lib/core/includes.hpp +++ b/dev/lib/core/includes.hpp @@ -11,6 +11,7 @@ #include #include #include -#include -#include + #include +#include +#include diff --git a/dev/lib/net/modem.hpp b/dev/lib/net/modem.hpp new file mode 100644 index 0000000..ba3be0c --- /dev/null +++ b/dev/lib/net/modem.hpp @@ -0,0 +1,144 @@ +/* + * File: net/modem.hpp + * Purpose: Modem concept in modern C++ + * Author: Amlal El Mahrouss (founder@snu.systems) + * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp. + */ + +#ifndef _SNU_NET_NETWORK_HPP +#define _SNU_NET_NETWORK_HPP + +#include +#include +#include +#include +#include +#include + +#define SNU_MODEM_INTERFACE : protected snu::net::basic_modem + +namespace snu::net +{ + template + class basic_modem; + + typedef int64_t socket_type; + + /// @brief Modem container concept, a container to read and write on a network stream. + template + class basic_modem + { + socket_type fd_{}; + bool server_fd_{}; + + public: + explicit basic_modem() = default; + virtual ~basic_modem() = default; + + basic_modem& operator=(const basic_modem&) = default; + basic_modem(const basic_modem&) = default; + + static constexpr auto local_address_ip4 = "127.0.0.1"; + static constexpr auto local_address_ip6 = "::1"; + static constexpr const auto backlog_count = 5U; + + bool is_valid() const noexcept + { + return this->fd_ != -1; + } + + template + bool receive(ptr_type& out, std::size_t len) noexcept + { + static_assert(std::is_pointer::value, "ptr_type is not a pointer!"); + + if (!out) + return false; + + if (!len) + return false; + + socket_type cl_{fd_}; + + if (this->server_fd_) + cl_ = ::accept(fd_, nullptr, nullptr); + + auto ret = ::recv(cl_, out, len, 0); + + return ret > 0L; + } + + template + bool transmit(ptr_type& out, std::size_t len) noexcept + { + static_assert(std::is_pointer::value, "char_type is not a pointer!"); + + if (!out) + return false; + + if (!len) + return false; + + auto ret = ::send(fd_, out, len, 0); + + return ret > 0L; + } + + template + bool transmit(std::basic_string out) noexcept + { + if (out.empty()) + return false; + + auto ret = ::send(fd_, out.data(), out.size(), 0); + + return ret > 0L; + } + + template + bool construct(const char* addr = basic_modem::local_address_ip4, const bool& is_server = false) noexcept + { + static_assert(af != 0, "Address family is zero"); + static_assert(kind != 0, "Kind is zero"); + + fd_ = ::socket(af, kind, 0); + server_fd_ = is_server; + + if (fd_ == -1) + return false; + + struct sockaddr_in addr_; + std::memset(&addr_, 0, sizeof(struct sockaddr_in)); + + addr_.sin_addr.s_addr = ::inet_addr(addr); + addr_.sin_port = htons(port); + addr_.sin_family = af; + + if (!is_server) + { + const auto ret = ::connect(fd_, reinterpret_cast(&addr_), sizeof(addr_)); + return ret == 0L; + } + + ::bind(fd_, (struct sockaddr*)&addr_, sizeof(addr_)); + ::listen(fd_, basic_modem::backlog_count); + + return true; + } + + bool destroy() noexcept + { + if (!fd_) + return false; + + ::shutdown(fd_, SHUT_RDWR); + ::close(fd_); + + fd_ = 0L; + + return true; + } + }; +} // namespace snu::net + +#endif // ifndef _SNU_NET_NETWORK_HPP diff --git a/dev/lib/net/network.hpp b/dev/lib/net/network.hpp deleted file mode 100644 index e078c93..0000000 --- a/dev/lib/net/network.hpp +++ /dev/null @@ -1,136 +0,0 @@ -/* - * File: net/network.hpp - * Purpose: Modem concept in modern C++ - * Author: Amlal El Mahrouss (founder@snu.systems) - * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp. - */ - -#ifndef _SNU_NET_NETWORK_HPP -#define _SNU_NET_NETWORK_HPP - -#include -#include -#include -#include -#include -#include - -#define SNU_MODEM_INTERFACE : protected snu::net::basic_modem - -namespace snu::net -{ - template - class basic_modem; - - typedef int64_t socket_type; - - /// @brief Delivery modem concept, a container to read and write on a network stream. - template - class basic_modem - { - public: - explicit basic_modem() = default; - virtual ~basic_modem() = default; - - basic_modem& operator=(const basic_modem&) = default; - basic_modem(const basic_modem&) = default; - - static constexpr auto local_address_ip6 = "127.0.0.1"; - static constexpr auto local_address_ip4 = "::1"; - static constexpr const auto backlog_count = 18U; - - socket_type fd_{}; - - bool is_valid() const noexcept - { - return this->fd_ != -1; - } - - template - bool receive(ptr_type& out, std::size_t len) noexcept - { - static_assert(std::is_pointer::value, "ptr_type is not a pointer!"); - - if (!out) - return false; - - if (!len) - return false; - - auto ret = ::recv(fd_, out, len, 0); - - return ret > 0L; - } - - template - bool transmit(ptr_type& out, std::size_t len) noexcept - { - static_assert(std::is_pointer::value, "char_type is not a pointer!"); - - if (!out) - return false; - - if (!len) - return false; - - auto ret = ::send(fd_, out, len, 0); - - return ret > 0L; - } - - template - bool transmit(std::basic_string out) noexcept - { - if (out.empty()) - return false; - - auto ret = ::send(fd_, out.data(), out.size(), 0); - - return ret > 0L; - } - - template - bool construct(const char* addr = basic_modem::local_address_ip4, const bool& is_server = false) noexcept - { - static_assert(af != 0, "Address family is zero"); - static_assert(kind != 0, "Type is zero"); - - fd_ = ::socket(af, kind, ip_proto); - - if (fd_ == -1) - return false; - - struct sockaddr_in addr_ - { - }; - - addr_.sin_addr.s_addr = ::inet_addr(addr); - addr_.sin_port = port; - - if (!is_server) - { - const auto ret = ::connect(fd_, reinterpret_cast(&addr_), sizeof(decltype(addr_))); - return ret == 0L; - } - - ::listen(fd_, basic_modem::backlog_count); - - return true; - } - - bool destroy() noexcept - { - if (!fd_) - return false; - - ::shutdown(fd_, SHUT_RDWR); - ::close(fd_); - - fd_ = 0L; - - return true; - } - }; -} // namespace snu::net - -#endif // ifndef _SNU_NET_NETWORK_HPP diff --git a/dev/tests/fix_basic/CMakeLists.txt b/dev/tests/fix_basic/CMakeLists.txt index f792277..7b06112 100644 --- a/dev/tests/fix_basic/CMakeLists.txt +++ b/dev/tests/fix_basic/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(SOCLTests) +project(FIXTestBasic) include(FetchContent) FetchContent_Declare( diff --git a/dev/tests/network_basic/CMakeLists.txt b/dev/tests/network_basic/CMakeLists.txt index a5704fc..4cfe0d1 100644 --- a/dev/tests/network_basic/CMakeLists.txt +++ b/dev/tests/network_basic/CMakeLists.txt @@ -1,5 +1,5 @@ -cmake_minimum_required(VERSION 3.28) -project(SOCLTests LANGUAGES CXX) +cmake_minimum_required(VERSION 3.27) +project(NetworkTestBasic LANGUAGES CXX) include(FetchContent) FetchContent_Declare( diff --git a/dev/tests/network_basic/net_test.cc b/dev/tests/network_basic/net_test.cc index 634048e..34f6ebe 100644 --- a/dev/tests/network_basic/net_test.cc +++ b/dev/tests/network_basic/net_test.cc @@ -13,24 +13,10 @@ TEST(NetworkTest, BasicNetworkUsage) { snu::net::basic_modem modem; - modem.construct(snu::net::basic_modem::local_address_ip4, true); + modem.construct(snu::net::basic_modem::local_address_ip4, true); - snu::net::basic_modem modem_cl; - modem_cl.construct(snu::net::basic_modem::local_address_ip4, false); - - EXPECT_TRUE(modem_cl.is_valid()); EXPECT_TRUE(modem.is_valid()); std::basic_string buf_dst = "HELLO, NET!"; - char* buf = new char[buf_dst.size()]; - - modem_cl.transmit(buf_dst); - modem.receive(buf, buf_dst.size()); - - snu::io::print(buf_dst); - snu::io::print(buf); - snu::io::print(); - - delete[] buf; - buf = nullptr; + modem.transmit(buf_dst); } diff --git a/dev/tests/tracked_ptr_basic/CMakeLists.txt b/dev/tests/tracked_ptr_basic/CMakeLists.txt index 9100e9a..1dd5095 100644 --- a/dev/tests/tracked_ptr_basic/CMakeLists.txt +++ b/dev/tests/tracked_ptr_basic/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(SOCLTests) +project(TrackedPtrTestBasic) include(FetchContent) FetchContent_Declare( diff --git a/dev/tests/tracked_ptr_leak/CMakeLists.txt b/dev/tests/tracked_ptr_leak/CMakeLists.txt index 625a1dc..30c91a1 100644 --- a/dev/tests/tracked_ptr_leak/CMakeLists.txt +++ b/dev/tests/tracked_ptr_leak/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(SOCLTests) +project(TrackedPtrTestLeak) include(FetchContent) FetchContent_Declare( -- cgit v1.2.3