src/connectivity/network/tests/bsdsocket_test.cc
// DatagramSendtoRecvfrom tests if UDP send automatically binds an ephemeral
// port where the receiver can responds to.
DatagramSendtoRecvfrom tests if UDP send automatically binds an ephemeral
port where the receiver can responds to.
DatagramSendtoRecvfrom tests if UDP send automatically binds an ephemeral port where the receiver can responds to .
TEST(NetDatagramTest, DatagramSendtoRecvfrom) {
  int recvfd;
  ASSERT_GE(recvfd = socket(AF_INET, SOCK_DGRAM, 0), 0) << strerror(errno);
  struct sockaddr_in addr = {};
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  ASSERT_EQ(bind(recvfd, reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)), 0)
      << strerror(errno);
  socklen_t addrlen = sizeof(addr);
  ASSERT_EQ(getsockname(recvfd, reinterpret_cast<struct sockaddr*>(&addr), &addrlen), 0)
      << strerror(errno);
  ASSERT_EQ(addrlen, sizeof(addr));
  const char msg[] = "hello";
  int sendfd;
  ASSERT_GE(sendfd = socket(AF_INET, SOCK_DGRAM, 0), 0) << strerror(errno);
  ASSERT_EQ(sendto(sendfd, msg, sizeof(msg), 0, reinterpret_cast<struct sockaddr*>(&addr), addrlen),
            (ssize_t)sizeof(msg))
      << strerror(errno);
  char buf[sizeof(msg) + 1] = {};
  struct sockaddr_in peer;
  socklen_t peerlen = sizeof(peer);
  ASSERT_EQ(
      recvfrom(recvfd, buf, sizeof(buf), 0, reinterpret_cast<struct sockaddr*>(&peer), &peerlen),
      (ssize_t)sizeof(msg))
      << strerror(errno);
  ASSERT_EQ(peerlen, sizeof(peer));
  ASSERT_STREQ(msg, buf);
  ASSERT_EQ(sendto(recvfd, buf, sizeof(msg), 0, reinterpret_cast<struct sockaddr*>(&peer), peerlen),
            (ssize_t)sizeof(msg))
      << strerror(errno);
  ASSERT_EQ(
      recvfrom(sendfd, buf, sizeof(buf), 0, reinterpret_cast<struct sockaddr*>(&peer), &peerlen),
      (ssize_t)sizeof(msg))
      << strerror(errno);
  ASSERT_EQ(peerlen, sizeof(peer));
  ASSERT_STREQ(msg, buf);
  char addrbuf[INET_ADDRSTRLEN], peerbuf[INET_ADDRSTRLEN];
  const char* addrstr = inet_ntop(addr.sin_family, &addr.sin_addr, addrbuf, sizeof(addrbuf));
  ASSERT_NE(addrstr, nullptr);
  const char* peerstr = inet_ntop(peer.sin_family, &peer.sin_addr, peerbuf, sizeof(peerbuf));
  ASSERT_NE(peerstr, nullptr);
  ASSERT_STREQ(peerstr, addrstr);
  ASSERT_EQ(close(sendfd), 0) << strerror(errno);
  EXPECT_EQ(close(recvfd), 0) << strerror(errno);
}
TEST ( NetDatagramTest , DatagramSendtoRecvfrom ) { int recvfd ; ASSERT_GE ( recvfd = socket ( AF_INET , SOCK_DGRAM , 0 ) , 0 ) << strerror ( errno ) ; struct sockaddr_in addr = { } ; addr . sin_family = AF_INET ; addr . sin_addr . s_addr = htonl ( INADDR_LOOPBACK ) ; ASSERT_EQ ( bind ( recvfd , reinterpret_cast < const struct sockaddr * > ( & addr ) , sizeof ( addr ) ) , 0 ) << strerror ( errno ) ; socklen_t addrlen = sizeof ( addr ) ; ASSERT_EQ ( getsockname ( recvfd , reinterpret_cast < struct sockaddr * > ( & addr ) , & addrlen ) , 0 ) << strerror ( errno ) ; ASSERT_EQ ( addrlen , sizeof ( addr ) ) ; const char msg [ ] = " " ; int sendfd ; ASSERT_GE ( sendfd = socket ( AF_INET , SOCK_DGRAM , 0 ) , 0 ) << strerror ( errno ) ; ASSERT_EQ ( sendto ( sendfd , msg , sizeof ( msg ) , 0 , reinterpret_cast < struct sockaddr * > ( & addr ) , addrlen ) , ( ssize_t ) sizeof ( msg ) ) << strerror ( errno ) ; char buf [ sizeof ( msg ) + 1 ] = { } ; struct sockaddr_in peer ; socklen_t peerlen = sizeof ( peer ) ; ASSERT_EQ ( recvfrom ( recvfd , buf , sizeof ( buf ) , 0 , reinterpret_cast < struct sockaddr * > ( & peer ) , & peerlen ) , ( ssize_t ) sizeof ( msg ) ) << strerror ( errno ) ; ASSERT_EQ ( peerlen , sizeof ( peer ) ) ; ASSERT_STREQ ( msg , buf ) ; ASSERT_EQ ( sendto ( recvfd , buf , sizeof ( msg ) , 0 , reinterpret_cast < struct sockaddr * > ( & peer ) , peerlen ) , ( ssize_t ) sizeof ( msg ) ) << strerror ( errno ) ; ASSERT_EQ ( recvfrom ( sendfd , buf , sizeof ( buf ) , 0 , reinterpret_cast < struct sockaddr * > ( & peer ) , & peerlen ) , ( ssize_t ) sizeof ( msg ) ) << strerror ( errno ) ; ASSERT_EQ ( peerlen , sizeof ( peer ) ) ; ASSERT_STREQ ( msg , buf ) ; char addrbuf [ INET_ADDRSTRLEN ] , peerbuf [ INET_ADDRSTRLEN ] ; const char * addrstr = inet_ntop ( addr . sin_family , & addr . sin_addr , addrbuf , sizeof ( addrbuf ) ) ; ASSERT_NE ( addrstr , nullptr ) ; const char * peerstr = inet_ntop ( peer . sin_family , & peer . sin_addr , peerbuf , sizeof ( peerbuf ) ) ; ASSERT_NE ( peerstr , nullptr ) ; ASSERT_STREQ ( peerstr , addrstr ) ; ASSERT_EQ ( close ( sendfd ) , 0 ) << strerror ( errno ) ; EXPECT_EQ ( close ( recvfd ) , 0 ) << strerror ( errno ) ; }
DatagramSendtoRecvfrom tests if UDP send automatically binds an ephemeral
port where the receiver can responds to.
DatagramSendtoRecvfrom tests if UDP send automatically binds an ephemeral port where the receiver can responds to .