Prechádzať zdrojové kódy

Add listen method to Socket class

- adjust posix socket mock class
- extend posix socket class implementation
- add test for listen method of Socket class
Patrick-Christopher Mattulat 1 rok pred
rodič
commit
048d768a62

+ 1 - 0
CMakeLists.txt

@@ -219,6 +219,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/exception/IllegalArithmeticOperationExceptionTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/exception/IncompleteJsonExceptionTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/exception/NullPointerExceptionTest.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/exception/WrongProtocolExceptionTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/utils/RegexUtilsTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/utils/STLUtilsTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/core/ClassTest.cpp

+ 30 - 0
include/ls_std/core/exception/WrongProtocolException.hpp

@@ -0,0 +1,30 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2022-12-12
+ * Changed:         2022-12-12
+ *
+ * */
+
+#ifndef LS_STD_WRONG_PROTOCOL_EXCEPTION_HPP
+#define LS_STD_WRONG_PROTOCOL_EXCEPTION_HPP
+
+#include <exception>
+
+namespace ls::std::core
+{
+  class WrongProtocolException : public ::std::exception
+  {
+    public:
+
+      explicit WrongProtocolException() = default;
+
+      [[nodiscard]] const char *what() const noexcept override
+      {
+        return "WrongProtocolException thrown - the available protocol is not valid for this operation!";
+      }
+  };
+}
+
+#endif

+ 2 - 1
include/ls_std/core/interface/IPosixSocket.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-12-09
- * Changed:         2022-12-10
+ * Changed:         2022-12-12
  *
  * */
 
@@ -23,6 +23,7 @@ namespace ls::std::core::interface_type
       virtual int bind(int _socketFileDescriptor, const struct sockaddr* _address, socklen_t _addressLength) = 0;
       virtual int connect(int _socketFileDescriptor, const struct sockaddr* _address, socklen_t _addressLength) = 0;
       virtual int create(int _domain, int _type, int _protocol) = 0; // constructor
+      virtual int listen(int _socketFileDescriptor, int _backlog) = 0;
   };
 }
 

+ 2 - 1
include/ls_std/ls_std_core.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-05-13
- * Changed:         2022-12-09
+ * Changed:         2022-12-12
  *
  * */
 
@@ -18,6 +18,7 @@
 #include <ls_std/core/exception/IllegalArithmeticOperationException.hpp>
 #include <ls_std/core/exception/IncompleteJsonException.hpp>
 #include <ls_std/core/exception/NullPointerException.hpp>
+#include <ls_std/core/exception/WrongProtocolException.hpp>
 
 #include <ls_std/core/interface/IBoxing.hpp>
 #include <ls_std/core/interface/IEncoding.hpp>

+ 2 - 1
include/ls_std/network/core/ProtocolType.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-11-16
- * Changed:         2022-11-16
+ * Changed:         2022-12-12
  *
  * */
 
@@ -15,6 +15,7 @@ namespace ls::std::network
   enum ProtocolType
   {
     PROTOCOL_TYPE_NOT_INITIALIZED = 0,
+    PROTOCOL_TYPE_UDP,
     PROTOCOL_TYPE_TCP
   };
 }

+ 2 - 1
include/ls_std/network/socket/MockPosixSocket.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-12-09
- * Changed:         2022-12-10
+ * Changed:         2022-12-12
  *
  * */
 
@@ -25,6 +25,7 @@ namespace ls_std_network_test
       MOCK_METHOD(int, bind, (int _socketFileDescriptor, const struct sockaddr *_address, socklen_t _addressLength), (override));
       MOCK_METHOD(int, connect, (int _socketFileDescriptor, const struct sockaddr *_address, socklen_t _addressLength), (override));
       MOCK_METHOD(int, create, (int _domain, int _type, int _protocol), (override));
+      MOCK_METHOD(int, listen, (int _socketFileDescriptor, int _backlog), (override));
   };
 }
 

+ 2 - 1
include/ls_std/network/socket/PosixSocket.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-12-09
- * Changed:         2022-12-10
+ * Changed:         2022-12-12
  *
  * */
 
@@ -24,6 +24,7 @@ namespace ls::std::network
       int bind(int _socketFileDescriptor, const struct sockaddr *_address, socklen_t _addressLength) override;
       int connect(int _socketFileDescriptor, const struct sockaddr *_address, socklen_t _addressLength) override;
       int create(int _domain, int _type, int _protocol) override;
+      int listen(int _socketFileDescriptor, int _backlog) override;
   };
 }
 

+ 2 - 0
include/ls_std/network/socket/Socket.hpp

@@ -31,6 +31,7 @@ namespace ls::std::network
       [[nodiscard]] bool bind();
       [[nodiscard]] bool connect();
       [[nodiscard]] bool isInitialized() const;
+      [[nodiscard]] bool listen();
 
     private:
 
@@ -48,6 +49,7 @@ namespace ls::std::network
       [[nodiscard]] bool _init();
       #if defined(unix) || defined(__APPLE__)
       [[nodiscard]] bool _initUnix();
+      [[nodiscard]] bool _listenUnix();
       void _setUnixSocketApi();
       #endif
   };

+ 2 - 1
include/ls_std/network/socket/SocketParameter.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-11-16
- * Changed:         2022-12-11
+ * Changed:         2022-12-12
  *
  * */
 
@@ -21,6 +21,7 @@ namespace ls::std::network
     ::std::shared_ptr<ls::std::core::interface_type::IPosixSocket> posixSocket{};
     ls::std::network::ProtocolFamilyType protocolFamilyType{};
     ls::std::network::SocketAddress socketAddress{};
+    int queueSize{};
   };
 }
 

+ 5 - 1
source/ls_std/network/core/ProtocolMapper.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-11-17
- * Changed:         2022-11-17
+ * Changed:         2022-12-12
  *
  * */
 
@@ -30,6 +30,10 @@ ls::std::network::Protocol ls::std::network::ProtocolMapper::_toUnixProtocol(con
 
   switch (_protocolType)
   {
+    case ls::std::network::ProtocolType::PROTOCOL_TYPE_UDP:
+    {
+      protocol.unixProtocol = SOCK_DGRAM;
+    } break;
     case ls::std::network::ProtocolType::PROTOCOL_TYPE_TCP:
     {
       protocol.unixProtocol = SOCK_STREAM;

+ 6 - 1
source/ls_std/network/socket/PosixSocket.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-12-09
- * Changed:         2022-12-10
+ * Changed:         2022-12-12
  *
  * */
 
@@ -24,3 +24,8 @@ int ls::std::network::PosixSocket::create(int _domain, int _type, int _protocol)
 {
   return ::socket(_domain, _type, _protocol);
 }
+
+int ls::std::network::PosixSocket::listen(int _socketFileDescriptor, int _backlog)
+{
+  return ::listen(_socketFileDescriptor, _backlog);
+}

+ 19 - 2
source/ls_std/network/socket/Socket.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-11-16
- * Changed:         2022-12-11
+ * Changed:         2022-12-12
  *
  * */
 
@@ -14,6 +14,7 @@
 #include <ls_std/network/socket/SocketAddressMapper.hpp>
 #include <ls_std/network/socket/MockPosixSocket.hpp>
 #include <ls_std/network/socket/PosixSocket.hpp>
+#include <ls_std/core/exception/WrongProtocolException.hpp>
 #include <memory>
 #include <utility>
 
@@ -45,8 +46,19 @@ bool ls::std::network::Socket::isInitialized() const
   return this->initialized;
 }
 
-#if defined(unix) || defined(__APPLE__)
+bool ls::std::network::Socket::listen()
+{
+  if (this->parameter.socketAddress.protocolType != PROTOCOL_TYPE_TCP)
+  {
+    throw ls::std::core::WrongProtocolException{};
+  }
+
+  #if defined(unix) || defined(__APPLE__)
+  return ls::std::network::Socket::_listenUnix();
+  #endif
+}
 
+#if defined(unix) || defined(__APPLE__)
 bool ls::std::network::Socket::_bindUnix()
 {
   ls::std::network::ConvertedSocketAddress convertedSocketAddress = ls::std::network::SocketAddressMapper::from(ls::std::network::Socket::_createSocketAddressMapperParameter());
@@ -86,6 +98,11 @@ bool ls::std::network::Socket::_initUnix()
   return this->parameter.posixSocket->create(convertedProtocolFamily.unixDomain, protocol.unixProtocol, 0) != -1;
 }
 
+bool ls::std::network::Socket::_listenUnix()
+{
+  return this->parameter.posixSocket->listen(this->unixDescriptor, this->parameter.queueSize) == 0;
+}
+
 void ls::std::network::Socket::_setUnixSocketApi()
 {
   if (this->parameter.posixSocket == nullptr)

+ 45 - 0
test/cases/core/exception/WrongProtocolExceptionTest.cpp

@@ -0,0 +1,45 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2022-12-12
+ * Changed:         2022-12-12
+ *
+ * */
+
+#include <gtest/gtest.h>
+#include <ls_std/ls_std_core.hpp>
+
+using namespace ls::std::core;
+
+namespace
+{
+  class WrongProtocolExceptionTest : public ::testing::Test
+  {
+    protected:
+
+      WrongProtocolExceptionTest() = default;
+      ~WrongProtocolExceptionTest() override = default;
+
+      void SetUp() override
+      {}
+
+      void TearDown() override
+      {}
+  };
+
+  TEST_F(WrongProtocolExceptionTest, constructor)
+  {
+    EXPECT_THROW({
+                   try
+                   {
+                     throw WrongProtocolException{};
+                   }
+                   catch (const WrongProtocolException &_exception)
+                   {
+                     EXPECT_STREQ("WrongProtocolException thrown - the available protocol is not valid for this operation!", _exception.what());
+                     throw;
+                   }
+                 }, WrongProtocolException);
+  }
+}

+ 46 - 0
test/cases/network/socket/SocketTest.cpp

@@ -14,6 +14,7 @@ using namespace ls::std::network;
 using namespace ::testing;
 using namespace ls_std_network_test;
 using namespace ::std;
+using namespace ls::std::core;
 
 namespace
 {
@@ -91,4 +92,49 @@ namespace
     Socket socket{generateSocketParameter()};
     ASSERT_TRUE(socket.isInitialized());
   }
+
+  TEST_F(SocketTest, listen)
+  {
+    SocketParameter parameter = generateSocketParameter();
+
+    #if defined(unix) || defined(__APPLE__)
+    shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
+    parameter.posixSocket = mockSocket;
+
+    EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
+    ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
+    EXPECT_CALL(*mockSocket, listen(_, _)).Times(AtLeast(1));
+    ON_CALL(*mockSocket, listen(_, _)).WillByDefault(Return(0));
+    #endif
+
+    Socket socket{parameter};
+    ASSERT_TRUE(socket.listen());
+  }
+
+  TEST_F(SocketTest, listen_wrong_protocol)
+  {
+    SocketParameter parameter = generateSocketParameter();
+    parameter.socketAddress.protocolType = PROTOCOL_TYPE_UDP;
+
+    #if defined(unix) || defined(__APPLE__)
+    shared_ptr<MockPosixSocket> mockSocket = make_shared<MockPosixSocket>();
+    parameter.posixSocket = mockSocket;
+
+    EXPECT_CALL(*mockSocket, create(_, _, _)).Times(AtLeast(1));
+    ON_CALL(*mockSocket, create(_, _, _)).WillByDefault(Return(0));
+    #endif
+
+    Socket socket{parameter};
+
+    EXPECT_THROW({
+                   try
+                   {
+                     bool listened = socket.listen();
+                   }
+                   catch (const WrongProtocolException &_exception)
+                   {
+                     throw;
+                   }
+                 }, WrongProtocolException);
+  }
 }