<commit_before>/******************************************************************************
 *                       ____    _    _____                                   *
 *                      / ___|  / \  |  ___|    C++                           *
 *                     | |     / _ \ | |_       Actor                         *
 *                     | |___ / ___ \|  _|      Framework                     *
 *                      \____/_/   \_|_|                                      *
 *                                                                            *
 * Copyright 2011-2019 <NAME>                                     *
 *                                                                            *
 * Distributed under the terms and conditions of the BSD 3-Clause License or  *
 * (at your option) under the terms and conditions of the Boost Software      *
 * License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE.       *
 *                                                                            *
 * If you did not receive a copy of the license files, see                    *
 * http://opensource.org/licenses/BSD-3-Clause and                            *
 * http://www.boost.org/LICENSE_1_0.txt.                                      *
 ******************************************************************************/

#define CAF_SUITE udp_datagram_socket

#include "caf/net/udp_datagram_socket.hpp"

#include "caf/test/dsl.hpp"

#include "host_fixture.hpp"

#include "caf/detail/net_syscall.hpp"
#include "caf/detail/socket_sys_includes.hpp"
#include "caf/ip_address.hpp"
#include "caf/ipv4_address.hpp"
#include "caf/net/ip.hpp"

using namespace caf;
using namespace caf::net;

namespace {

constexpr string_view hello_test = "Hello test!";

struct fixture : host_fixture {
  fixture()
    : host_fixture(),
      v4_local{make_ipv4_address(127, 0, 0, 1)},
      v6_local{{0}, {0x1}},
      addrs{net::ip::resolve("localhost")} {
  }

  bool contains(ip_address x) {
    return std::count(addrs.begin(), addrs.end(), x) > 0;
  }

  void test_send_receive(ip_address addr) {
    std::vector<byte> buf(1024);
    ip_endpoint ep(addr, 0);
    auto sender = unbox(make_udp_datagram_socket(ep));
    ep.port(0);
    auto receiver = unbox(make_udp_datagram_socket(ep));
    CAF_MESSAGE("sending data to: " << to_string(ep));
    auto guard = detail::make_scope_guard([&] {
      close(socket_cast<net::socket>(sender));
      close(socket_cast<net::socket>(receiver));
    });
    if (auto err = nonblocking(socket_cast<net::socket>(receiver), true))
      CAF_FAIL("nonblocking returned an error" << err);
    auto test_read_res = read(receiver, make_span(buf));
    if (auto err = get_if<sec>(&test_read_res))
      CAF_CHECK_EQUAL(*err, sec::unavailable_or_would_block);
    else
      CAF_FAIL("read should have failed");
    auto write_ret = write(sender, as_bytes(make_span(hello_test)), ep);
    if (auto num_bytes = get_if<size_t>(&write_ret))
      CAF_CHECK_EQUAL(*num_bytes, hello_test.size());
    else
      CAF_FAIL("write returned an error: " << sys.render(get<sec>(write_ret)));
    auto read_ret = read(receiver, make_span(buf));
    if (auto read_res = get_if<std::pair<size_t, ip_endpoint>>(&read_ret)) {
      CAF_CHECK_EQUAL(read_res->first, hello_test.size());
      buf.resize(read_res->first);
    } else {
      CAF_FAIL("write returned an error: " << sys.render(get<sec>(read_ret)));
    }
    string_view buf_view{reinterpret_cast<const char*>(buf.data()), buf.size()};
    CAF_CHECK_EQUAL(buf_view, hello_test);
  }

  actor_system_config cfg;
  actor_system sys{cfg};
  ip_address v4_local;
  ip_address v6_local;
  std::vector<ip_address> addrs;
};

} // namespace

CAF_TEST_FIXTURE_SCOPE(udp_datagram_socket_test, fixture)

CAF_TEST(send and receive IPv4) {
  if (!contains(v4_local)) {
    CAF_MESSAGE("this host does not have av IPv4 address");
  } else {
    test_send_receive(v4_local);
  }
}

CAF_TEST(send and receive IPv6) {
  if (!contains(v6_local)) {
    CAF_MESSAGE("this host does not have av IPv6 address");
  } else {
    test_send_receive(v6_local);
  }
}

CAF_TEST_FIXTURE_SCOPE_END()<commit_msg>prefer to test with ipv4<commit_after>/******************************************************************************
 *                       ____    _    _____                                   *
 *                      / ___|  / \  |  ___|    C++                           *
 *                     | |     / _ \ | |_       Actor                         *
 *                     | |___ / ___ \|  _|      Framework                     *
 *                      \____/_/   \_|_|                                      *
 *                                                                            *
 * Copyright 2011-2019 <NAME>                                     *
 *                                                                            *
 * Distributed under the terms and conditions of the BSD 3-Clause License or  *
 * (at your option) under the terms and conditions of the Boost Software      *
 * License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE.       *
 *                                                                            *
 * If you did not receive a copy of the license files, see                    *
 * http://opensource.org/licenses/BSD-3-Clause and                            *
 * http://www.boost.org/LICENSE_1_0.txt.                                      *
 ******************************************************************************/

#define CAF_SUITE udp_datagram_socket

#include "caf/net/udp_datagram_socket.hpp"

#include "caf/test/dsl.hpp"

#include "host_fixture.hpp"

#include "caf/detail/net_syscall.hpp"
#include "caf/detail/socket_sys_includes.hpp"
#include "caf/ip_address.hpp"
#include "caf/ipv4_address.hpp"
#include "caf/net/ip.hpp"
#include "caf/net/socket_guard.hpp"

using namespace caf;
using namespace caf::net;

namespace {

constexpr string_view hello_test = "Hello test!";

struct fixture : host_fixture {
  fixture()
    : host_fixture(),
      v4_local{make_ipv4_address(127, 0, 0, 1)},
      v6_local{{0}, {0x1}},
      addrs{net::ip::resolve("localhost")} {
  }

  bool contains(ip_address x) {
    return std::count(addrs.begin(), addrs.end(), x) > 0;
  }

  void test_send_receive(ip_address addr) {
    std::vector<byte> buf(1024);
    ip_endpoint ep(addr, 0);
    auto sender = unbox(make_udp_datagram_socket(ep));
    ep.port(0);
    auto receiver = unbox(make_udp_datagram_socket(ep));
    CAF_MESSAGE("sending data to: " << to_string(ep));
    auto send_guard = make_socket_guard(sender);
    auto receive_guard = make_socket_guard(receiver);
    if (auto err = nonblocking(socket_cast<net::socket>(receiver), true))
      CAF_FAIL("nonblocking returned an error" << err);
    auto test_read_res = read(receiver, make_span(buf));
    if (auto err = get_if<sec>(&test_read_res))
      CAF_CHECK_EQUAL(*err, sec::unavailable_or_would_block);
    else
      CAF_FAIL("read should have failed");
    auto write_ret = write(sender, as_bytes(make_span(hello_test)), ep);
    if (auto num_bytes = get_if<size_t>(&write_ret))
      CAF_CHECK_EQUAL(*num_bytes, hello_test.size());
    else
      CAF_FAIL("write returned an error: " << sys.render(get<sec>(write_ret)));
    auto read_ret = read(receiver, make_span(buf));
    if (auto read_res = get_if<std::pair<size_t, ip_endpoint>>(&read_ret)) {
      CAF_CHECK_EQUAL(read_res->first, hello_test.size());
      buf.resize(read_res->first);
    } else {
      CAF_FAIL("write returned an error: " << sys.render(get<sec>(read_ret)));
    }
    string_view buf_view{reinterpret_cast<const char*>(buf.data()), buf.size()};
    CAF_CHECK_EQUAL(buf_view, hello_test);
  }

  actor_system_config cfg;
  actor_system sys{cfg};
  ip_address v4_local;
  ip_address v6_local;
  std::vector<ip_address> addrs;
};

} // namespace

CAF_TEST_FIXTURE_SCOPE(udp_datagram_socket_test, fixture)

CAF_TEST(send and receive) {
  if (contains(v4_local)) {
    test_send_receive(v4_local);
  } else if (contains(v6_local)) {
    test_send_receive(v6_local);
  } else {
    CAF_FAIL("could not resolve 'localhost'");
  }
}

CAF_TEST_FIXTURE_SCOPE_END()
{'fraction_non_alphanumeric': 0.15330503335354762, 'fraction_numerical': 0.010309278350515464, 'mean_word_length': 2.223612197028929, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6069227', 'n_tokens_mistral': 2628, 'n_tokens_neox': 2509, 'n_words': 471}