brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.2 KiB · 8377260 Raw
67 lines · cpp
1//===-- Unittests for sendto/recvfrom -------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/sys/socket/recvfrom.h"10#include "src/sys/socket/sendto.h"11#include "src/sys/socket/socketpair.h"12 13#include "src/unistd/close.h"14 15#include "test/UnitTest/ErrnoCheckingTest.h"16#include "test/UnitTest/ErrnoSetterMatcher.h"17#include "test/UnitTest/Test.h"18 19#include <sys/socket.h> // For AF_UNIX and SOCK_DGRAM20 21using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;22using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;23using LlvmLibcSendToRecvFromTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;24 25TEST_F(LlvmLibcSendToRecvFromTest, SucceedsWithSocketPair) {26  const char TEST_MESSAGE[] = "connection successful";27  const size_t MESSAGE_LEN = sizeof(TEST_MESSAGE);28 29  int sockpair[2] = {0, 0};30 31  ASSERT_THAT(LIBC_NAMESPACE::socketpair(AF_UNIX, SOCK_STREAM, 0, sockpair),32              Succeeds(0));33 34  ASSERT_THAT(LIBC_NAMESPACE::sendto(sockpair[0], TEST_MESSAGE, MESSAGE_LEN, 0,35                                     nullptr, 0),36              Succeeds(static_cast<ssize_t>(MESSAGE_LEN)));37 38  char buffer[256];39 40  ASSERT_THAT(LIBC_NAMESPACE::recvfrom(sockpair[1], buffer, sizeof(buffer), 0,41                                       nullptr, 0),42              Succeeds(static_cast<ssize_t>(MESSAGE_LEN)));43 44  ASSERT_STREQ(buffer, TEST_MESSAGE);45 46  // close both ends of the socket47  ASSERT_THAT(LIBC_NAMESPACE::close(sockpair[0]), Succeeds(0));48  ASSERT_THAT(LIBC_NAMESPACE::close(sockpair[1]), Succeeds(0));49}50 51TEST_F(LlvmLibcSendToRecvFromTest, SendToFails) {52  const char TEST_MESSAGE[] = "connection terminated";53  const size_t MESSAGE_LEN = sizeof(TEST_MESSAGE);54 55  ASSERT_THAT(56      LIBC_NAMESPACE::sendto(-1, TEST_MESSAGE, MESSAGE_LEN, 0, nullptr, 0),57      Fails(EBADF));58}59 60TEST_F(LlvmLibcSendToRecvFromTest, RecvFromFails) {61  char buffer[256];62 63  ASSERT_THAT(64      LIBC_NAMESPACE::recvfrom(-1, buffer, sizeof(buffer), 0, nullptr, 0),65      Fails(EBADF));66}67