SocketAddressMapperTest.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-18
  6. * Changed: 2022-12-31
  7. *
  8. * */
  9. #include <ls_std/ls_std_os.hpp>
  10. #include <gtest/gtest.h>
  11. #include <ls_std/ls_std_network.hpp>
  12. #if LS_STD_UNIX_PLATFORM
  13. #include <sys/socket.h>
  14. #include "ls_std/network/socket/Socket.hpp"
  15. #endif
  16. using namespace ls::std::network;
  17. namespace
  18. {
  19. class SocketAddressMapperTest : public ::testing::Test
  20. {
  21. protected:
  22. SocketAddressMapperTest() = default;
  23. ~SocketAddressMapperTest() override = default;
  24. void SetUp() override
  25. {}
  26. void TearDown() override
  27. {}
  28. static SocketAddressMapperParameter createSocketAddressMapperParameter()
  29. {
  30. SocketAddressMapperParameter parameter{};
  31. parameter.protocolFamilyType = ProtocolFamilyType::LS_STD_PROTOCOL_FAMILY_TYPE_IPV4;
  32. SocketAddress socketAddress{};
  33. socketAddress.port = 8080;
  34. socketAddress.protocolType = LS_STD_PROTOCOL_TYPE_TCP;
  35. socketAddress.ipAddress = "127.0.0.1";
  36. parameter.socketAddress = socketAddress;
  37. return parameter;
  38. }
  39. };
  40. TEST_F(SocketAddressMapperTest, getClassName)
  41. {
  42. ASSERT_STREQ("SocketAddressMapper", SocketAddressMapper{}.getClassName().c_str());
  43. }
  44. TEST_F(SocketAddressMapperTest, from)
  45. {
  46. ConvertedSocketAddress convertedSocketAddress = SocketAddressMapper::from(createSocketAddressMapperParameter());
  47. #if LS_STD_UNIX_PLATFORM
  48. ASSERT_EQ(36895, convertedSocketAddress.socketAddressUnix.sin_port); // expected: return value of htons()
  49. ASSERT_EQ(AF_INET, convertedSocketAddress.socketAddressUnix.sin_family);
  50. ASSERT_EQ(16777343, convertedSocketAddress.socketAddressUnix.sin_addr.s_addr); // expected: return value of inet_aton()
  51. ASSERT_EQ(16, convertedSocketAddress.addressLength);
  52. #endif
  53. }
  54. TEST_F(SocketAddressMapperTest, from_with_empty_address)
  55. {
  56. SocketAddressMapperParameter parameter = createSocketAddressMapperParameter();
  57. parameter.socketAddress.ipAddress = "";
  58. ConvertedSocketAddress convertedSocketAddress = SocketAddressMapper::from(parameter);
  59. #if LS_STD_UNIX_PLATFORM
  60. ASSERT_EQ(36895, convertedSocketAddress.socketAddressUnix.sin_port); // expected: return value of htons()
  61. ASSERT_EQ(AF_INET, convertedSocketAddress.socketAddressUnix.sin_family);
  62. ASSERT_EQ(INADDR_ANY, convertedSocketAddress.socketAddressUnix.sin_addr.s_addr); // expected: return value of inet_aton()
  63. ASSERT_EQ(16, convertedSocketAddress.addressLength);
  64. #endif
  65. }
  66. }