text: // Copyright 2019 Proyectos y Sistemas de Mantenimiento SL (eProsima).
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an "AS IS" BASIS,
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  // See the License for the specific language governing permissions and
  // limitations under the License.
  
  #include "BlackboxTests.hpp"
  
  #include "TCPReqRepHelloWorldRequester.hpp"
  #include "TCPReqRepHelloWorldReplier.hpp"
  
  #include <gtest/gtest.h>
  
  using namespace eprosima::fastrtps;
  using namespace eprosima::fastrtps::rtps;
  
  #if TLS_FOUND
  static const char* certs_path = nullptr;
  #endif // if TLS_FOUND
  
  // TCP and Domain management with logical ports tests
  TEST(BlackBox, TCPDomainHelloWorld_P0_P1_D0_D0)
  {
      TCPReqRepHelloWorldRequester requester;
      TCPReqRepHelloWorldReplier replier;
      const uint16_t nmsgs = 5;
  
      requester.init(0, 0, global_port);
  
      ASSERT_TRUE(requester.isInitialized());
  
      replier.init(1, 0, global_port);
  
      ASSERT_TRUE(replier.isInitialized());
  
      // Wait for discovery.
      requester.wait_discovery();
      replier.wait_discovery();
  
      ASSERT_TRUE(requester.is_matched());
      ASSERT_TRUE(replier.is_matched());
  
      for (uint16_t count = 0; count < nmsgs; ++count)
      {
          requester.send(count);
          requester.block();
      }
  }
  
  TEST(BlackBox, TCPDomainHelloWorld_P0_P1_D0_D1)
  {
      TCPReqRepHelloWorldRequester requester;
      TCPReqRepHelloWorldReplier replier;
  
      requester.init(0, 0, global_port);
  
      ASSERT_TRUE(requester.isInitialized());
  
      replier.init(1, 1, global_port);
  
      ASSERT_TRUE(replier.isInitialized());
  
      // Wait for discovery. They must not discover each other.
      requester.wait_discovery(std::chrono::seconds(10));
  
      ASSERT_FALSE(requester.is_matched());
      ASSERT_FALSE(replier.is_matched());
  }
  
  TEST(BlackBox, TCPDomainHelloWorld_P0_P1_D1_D0)
  {
      TCPReqRepHelloWorldRequester requester;
      TCPReqRepHelloWorldReplier replier;
  
      requester.init(0, 1, global_port);
  
      ASSERT_TRUE(requester.isInitialized());
  
      replier.init(1, 0, global_port);
  
      ASSERT_TRUE(replier.isInitialized());
  
      // Wait for discovery. They must not discover each other.
      requester.wait_discovery(std::chrono::seconds(10));
  
      ASSERT_FALSE(requester.is_matched());
      ASSERT_FALSE(replier.is_matched());
  
  }
  
  TEST(BlackBox, TCPDomainHelloWorld_P0_P3_D0_D0)
  {
      TCPReqRepHelloWorldRequester requester;
      TCPReqRepHelloWorldReplier replier;
      const uint16_t nmsgs = 5;
  
      requester.init(0, 0, global_port);
  
      ASSERT_TRUE(requester.isInitialized());
  
      replier.init(3, 0, global_port);
  
      ASSERT_TRUE(replier.isInitialized());
  
      // Wait for discovery.
      requester.wait_discovery();
      replier.wait_discovery();
  
      for (uint16_t count = 0; count < nmsgs; ++count)
      {
          requester.send(count);
          requester.block();
      }
  
  }
  
  TEST(BlackBox, TCPDomainHelloWorld_P0_P3_D0_D1)
  {
      TCPReqRepHelloWorldRequester requester;
      TCPReqRepHelloWorldReplier replier;
  
      requester.init(0, 0, global_port);
  
      ASSERT_TRUE(requester.isInitialized());
  
      replier.init(3, 1, global_port);
  
      ASSERT_TRUE(replier.isInitialized());
  
      // Wait for discovery. They must not discover each other.
      requester.wait_discovery(std::chrono::seconds(10));
  
      ASSERT_FALSE(requester.is_matched());
      ASSERT_FALSE(replier.is_matched());
  }
  
  TEST(BlackBox, TCPDomainHelloWorld_P0_P3_D1_D0)
  {
      TCPReqRepHelloWorldRequester requester;
      TCPReqRepHelloWorldReplier replier;
  
      requester.init(0, 1, global_port);
  
      ASSERT_TRUE(requester.isInitialized());
  
      replier.init(3, 0, global_port);
  
      ASSERT_TRUE(replier.isInitialized());
  
      // Wait for discovery. They must not discover each other.
      requester.wait_discovery(std::chrono::seconds(10));
  
      ASSERT_FALSE(requester.is_matched());
      ASSERT_FALSE(replier.is_matched());
  
  }
  
  TEST(BlackBox, TCPDomainHelloWorld_P3_P0_D0_D0)
  {
      TCPReqRepHelloWorldRequester requester;
      TCPReqRepHelloWorldReplier replier;
      const uint16_t nmsgs = 5;
  
      requester.init(3, 0, global_port);
  
      ASSERT_TRUE(requester.isInitialized());
  
      replier.init(0, 0, global_port);
  
      ASSERT_TRUE(replier.isInitialized());
  
      // Wait for discovery.
      requester.wait_discovery();
      replier.wait_discovery();
  
      ASSERT_TRUE(requester.is_matched());
      ASSERT_TRUE(replier.is_matched());
  
      for (uint16_t count = 0; count < nmsgs; ++count)
      {
          requester.send(count);
          requester.block();
      }
  
  }
  
  TEST(BlackBox, TCPDomainHelloWorld_P3_P0_D0_D1)
  {
      TCPReqRepHelloWorldRequester requester;
      TCPReqRepHelloWorldReplier replier;
  
      requester.init(3, 0, global_port);
  
      ASSERT_TRUE(requester.isInitialized());
  
      replier.init(0, 1, global_port);
  
      ASSERT_TRUE(replier.isInitialized());
  
      // Wait for discovery. They must not discover each other.
      requester.wait_discovery(std::chrono::seconds(10));
  
      ASSERT_FALSE(requester.is_matched());
      ASSERT_FALSE(replier.is_matched());
  }
  
  TEST(BlackBox, TCPDomainHelloWorld_P3_P0_D1_D0)
  {
      TCPReqRepHelloWorldRequester requester;
      TCPReqRepHelloWorldReplier replier;
  
      requester.init(3, 1, global_port);
  
      ASSERT_TRUE(requester.isInitialized());
  
      replier.init(0, 0, global_port);
  
      ASSERT_TRUE(replier.isInitialized());
  
      // Wait for discovery. They must not discover each other.
      requester.wait_discovery(std::chrono::seconds(10));
  
      ASSERT_FALSE(requester.is_matched());
      ASSERT_FALSE(replier.is_matched());
  
  }
  
  TEST(BlackBox, TCPDomainHelloWorld_P2_P3_D0_D0)
  {
      TCPReqRepHelloWorldRequester requester;
      TCPReqRepHelloWorldReplier replier;
      const uint16_t nmsgs = 5;
  
      requester.init(2, 0, global_port);
  
      ASSERT_TRUE(requester.isInitialized());
  
      replier.init(3, 0, global_port);
  
      ASSERT_TRUE(replier.isInitialized());
  
      // Wait for discovery.
      requester.wait_discovery();
      replier.wait_discovery();
  
      for (uint16_t count = 0; count < nmsgs; ++count)
      {
          requester.send(count);
          requester.block();
      }
  
  }
  
  TEST(BlackBox, TCPDomainHelloWorld_P2_P3_D0_D1)
  {
      TCPReqRepHelloWorldRequester requester;
      TCPReqRepHelloWorldReplier replier;
  
      requester.init(2, 0, global_port);
  
      ASSERT_TRUE(requester.isInitialized());
  
      replier.init(3, 1, global_port);
  
      ASSERT_TRUE(replier.isInitialized());
  
      // Wait for discovery. They must not discover each other.
      requester.wait_discovery(std::chrono::seconds(10));
  
      ASSERT_FALSE(requester.is_matched());
      ASSERT_FALSE(replier.is_matched());
  }
  
  TEST(BlackBox, TCPDomainHelloWorld_P2_P3_D1_D0)
  {
      TCPReqRepHelloWorldRequester requester;
      TCPReqRepHelloWorldReplier replier;
  
      requester.init(2, 1, global_port);
  
      ASSERT_TRUE(requester.isInitialized());
  
      replier.init(3, 0, global_port);
  
      ASSERT_TRUE(replier.isInitialized());
  
      // Wait for discovery. They must not discover each other.
      requester.wait_discovery(std::chrono::seconds(10));
  
      ASSERT_FALSE(requester.is_matched());
      ASSERT_FALSE(replier.is_matched());
  }
  
  TEST(BlackBox, TCPMaxInitialPeer_P0_4_P3)
  {
      TCPReqRepHelloWorldRequester requester;
      TCPReqRepHelloWorldReplier replier;
  
      requester.init(0, 0, global_port, 4);
  
      ASSERT_TRUE(requester.isInitialized());
  
      replier.init(3, 0, global_port);
  
      ASSERT_TRUE(replier.isInitialized());
  
      // Wait for discovery.
      requester.wait_discovery();
      replier.wait_discovery();
  
      ASSERT_TRUE(requester.is_matched());
      ASSERT_TRUE(replier.is_matched());
  }
  
  TEST(BlackBox, TCPMaxInitialPeer_P0_4_P4)
  {
      TCPReqRepHelloWorldRequester requester;
      TCPReqRepHelloWorldReplier replier;
  
      requester.init(0, 0, global_port, 4);
  
      ASSERT_TRUE(requester.isInitialized());
  
      replier.init(4, 0, global_port);
  
      ASSERT_TRUE(replier.isInitialized());
  
      // Wait for discovery.
      requester.wait_discovery(std::chrono::seconds(10));
  
      ASSERT_FALSE(requester.is_matched());
      ASSERT_FALSE(replier.is_matched());
  }
  
  TEST(BlackBox, TCPMaxInitialPeer_P0_5_P4)
  {
      TCPReqRepHelloWorldRequester requester;
      TCPReqRepHelloWorldReplier replier;
  
      requester.init(0, 0, global_port, 5);
  
      ASSERT_TRUE(requester.isInitialized());
  
      replier.init(4, 0, global_port);
  
      ASSERT_TRUE(replier.isInitialized());
  
      // Wait for discovery.
      requester.wait_discovery();
      replier.wait_discovery();
  
      ASSERT_TRUE(requester.is_matched());
      ASSERT_TRUE(replier.is_matched());
  }
  
  #if TLS_FOUND
  TEST(BlackBox, TCP_TLS)
  {
      TCPReqRepHelloWorldRequester requester;
      TCPReqRepHelloWorldReplier replier;
  
      requester.init(0, 0, global_port, 5, certs_path);
  
      ASSERT_TRUE(requester.isInitialized());
  
      replier.init(4, 0, global_port, 5, certs_path);
  
      ASSERT_TRUE(replier.isInitialized());
  
      // Wait for discovery.
      requester.wait_discovery();
      replier.wait_discovery();
  
      ASSERT_TRUE(requester.is_matched());
      ASSERT_TRUE(replier.is_matched());
  }
  
  void tls_init()
  {
      certs_path = std::getenv("CERTS_PATH");
  
      if (certs_path == nullptr)
      {
          std::cout << "Cannot get enviroment variable CERTS_PATH" << std::endl;
          exit(-1);
      }
  }
  
  #endif // if TLS_FOUND
  
  // Regression test for ShrinkLocators/transform_remote_locators mechanism.
  TEST(BlackBox, TCPLocalhost)
  {
      TCPReqRepHelloWorldRequester requester;
      TCPReqRepHelloWorldReplier replier;
      const uint16_t nmsgs = 5;
  
      requester.init(0, 0, global_port, 0, nullptr, true);
  
      ASSERT_TRUE(requester.isInitialized());
  
      replier.init(1, 0, global_port);
  
      ASSERT_TRUE(replier.isInitialized());
  
      // Wait for discovery.
      requester.wait_discovery();
      replier.wait_discovery();
  
      ASSERT_TRUE(requester.is_matched());
      ASSERT_TRUE(replier.is_matched());
  
      for (uint16_t count = 0; count < nmsgs; ++count)
      {
          requester.send(count);
          requester.block();
      }
  }
  