gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/syscalls/linux/unix_domain_socket_test_util.h (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  #ifndef GVISOR_TEST_SYSCALLS_UNIX_DOMAIN_SOCKET_TEST_UTIL_H_
    16  #define GVISOR_TEST_SYSCALLS_UNIX_DOMAIN_SOCKET_TEST_UTIL_H_
    17  
    18  #include <string>
    19  
    20  #include "test/util/socket_util.h"
    21  
    22  namespace gvisor {
    23  namespace testing {
    24  
    25  // DescribeUnixDomainSocketType returns a human-readable string explaining the
    26  // given Unix domain socket type.
    27  std::string DescribeUnixDomainSocketType(int type);
    28  
    29  // UnixDomainSocketPair returns a SocketPairKind that represents SocketPairs
    30  // created by invoking the socketpair() syscall with AF_UNIX and the given type.
    31  SocketPairKind UnixDomainSocketPair(int type);
    32  
    33  // FilesystemBoundUnixDomainSocketPair returns a SocketPairKind that represents
    34  // SocketPairs created with bind() and accept() syscalls with a temp file path,
    35  // AF_UNIX and the given type.
    36  SocketPairKind FilesystemBoundUnixDomainSocketPair(int type);
    37  
    38  // AbstractBoundUnixDomainSocketPair returns a SocketPairKind that represents
    39  // SocketPairs created with bind() and accept() syscalls with a temp abstract
    40  // path, AF_UNIX and the given type.
    41  SocketPairKind AbstractBoundUnixDomainSocketPair(int type);
    42  
    43  // SocketpairGoferUnixDomainSocketPair returns a SocketPairKind that was created
    44  // with two sockets connected to the socketpair gofer.
    45  SocketPairKind SocketpairGoferUnixDomainSocketPair(int type);
    46  
    47  // SocketpairGoferFileSocketPair returns a SocketPairKind that was created with
    48  // two open() calls on paths backed by the socketpair gofer.
    49  SocketPairKind SocketpairGoferFileSocketPair(int type);
    50  
    51  // FilesystemUnboundUnixDomainSocketPair returns a SocketPairKind that
    52  // represents two unbound sockets and a filesystem path for binding.
    53  SocketPairKind FilesystemUnboundUnixDomainSocketPair(int type);
    54  
    55  // AbstractUnboundUnixDomainSocketPair returns a SocketPairKind that represents
    56  // two unbound sockets and an abstract namespace path for binding.
    57  SocketPairKind AbstractUnboundUnixDomainSocketPair(int type);
    58  
    59  // SendSingleFD sends both a single FD and some data over a unix domain socket
    60  // specified by an FD. Note that calls to this function must be wrapped in
    61  // ASSERT_NO_FATAL_FAILURE for internal assertions to halt the test.
    62  void SendSingleFD(int sock, int fd, char buf[], int buf_size);
    63  
    64  // SendFDs sends an arbitrary number of FDs and some data over a unix domain
    65  // socket specified by an FD. Note that calls to this function must be wrapped
    66  // in ASSERT_NO_FATAL_FAILURE for internal assertions to halt the test.
    67  void SendFDs(int sock, int fds[], int fds_size, char buf[], int buf_size);
    68  
    69  // RecvSingleFD receives both a single FD and some data over a unix domain
    70  // socket specified by an FD. Note that calls to this function must be wrapped
    71  // in ASSERT_NO_FATAL_FAILURE for internal assertions to halt the test.
    72  void RecvSingleFD(int sock, int* fd, char buf[], int buf_size);
    73  
    74  // RecvSingleFD receives both a single FD and some data over a unix domain
    75  // socket specified by an FD. This version allows the expected amount of data
    76  // received to be different than the buffer size. Note that calls to this
    77  // function must be wrapped in ASSERT_NO_FATAL_FAILURE for internal assertions
    78  // to halt the test.
    79  void RecvSingleFD(int sock, int* fd, char buf[], int buf_size,
    80                    int expected_size);
    81  
    82  // PeekSingleFD peeks at both a single FD and some data over a unix domain
    83  // socket specified by an FD. Note that calls to this function must be wrapped
    84  // in ASSERT_NO_FATAL_FAILURE for internal assertions to halt the test.
    85  void PeekSingleFD(int sock, int* fd, char buf[], int buf_size);
    86  
    87  // RecvFDs receives both an arbitrary number of FDs and some data over a unix
    88  // domain socket specified by an FD. Note that calls to this function must be
    89  // wrapped in ASSERT_NO_FATAL_FAILURE for internal assertions to halt the test.
    90  void RecvFDs(int sock, int fds[], int fds_size, char buf[], int buf_size);
    91  
    92  // RecvFDs receives both an arbitrary number of FDs and some data over a unix
    93  // domain socket specified by an FD. This version allows the expected amount of
    94  // data received to be different than the buffer size. Note that calls to this
    95  // function must be wrapped in ASSERT_NO_FATAL_FAILURE for internal assertions
    96  // to halt the test.
    97  void RecvFDs(int sock, int fds[], int fds_size, char buf[], int buf_size,
    98               int expected_size);
    99  
   100  // RecvNoCmsg receives some data over a unix domain socket specified by an FD
   101  // and asserts that no control messages are available for receiving. Note that
   102  // calls to this function must be wrapped in ASSERT_NO_FATAL_FAILURE for
   103  // internal assertions to halt the test.
   104  void RecvNoCmsg(int sock, char buf[], int buf_size, int expected_size);
   105  
   106  inline void RecvNoCmsg(int sock, char buf[], int buf_size) {
   107    RecvNoCmsg(sock, buf, buf_size, buf_size);
   108  }
   109  
   110  // SendCreds sends the credentials of the current process and some data over a
   111  // unix domain socket specified by an FD. Note that calls to this function must
   112  // be wrapped in ASSERT_NO_FATAL_FAILURE for internal assertions to halt the
   113  // test.
   114  void SendCreds(int sock, ucred creds, char buf[], int buf_size);
   115  
   116  // SendCredsAndFD sends the credentials of the current process, a single FD, and
   117  // some data over a unix domain socket specified by an FD. Note that calls to
   118  // this function must be wrapped in ASSERT_NO_FATAL_FAILURE for internal
   119  // assertions to halt the test.
   120  void SendCredsAndFD(int sock, ucred creds, int fd, char buf[], int buf_size);
   121  
   122  // RecvCreds receives some credentials and some data over a unix domain socket
   123  // specified by an FD. Note that calls to this function must be wrapped in
   124  // ASSERT_NO_FATAL_FAILURE for internal assertions to halt the test.
   125  void RecvCreds(int sock, ucred* creds, char buf[], int buf_size);
   126  
   127  // RecvCreds receives some credentials and some data over a unix domain socket
   128  // specified by an FD. This version allows the expected amount of data received
   129  // to be different than the buffer size. Note that calls to this function must
   130  // be wrapped in ASSERT_NO_FATAL_FAILURE for internal assertions to halt the
   131  // test.
   132  void RecvCreds(int sock, ucred* creds, char buf[], int buf_size,
   133                 int expected_size);
   134  
   135  // RecvCredsAndFD receives some credentials, a single FD, and some data over a
   136  // unix domain socket specified by an FD. Note that calls to this function must
   137  // be wrapped in ASSERT_NO_FATAL_FAILURE for internal assertions to halt the
   138  // test.
   139  void RecvCredsAndFD(int sock, ucred* creds, int* fd, char buf[], int buf_size);
   140  
   141  // SendNullCmsg sends a null control message and some data over a unix domain
   142  // socket specified by an FD. Note that calls to this function must be wrapped
   143  // in ASSERT_NO_FATAL_FAILURE for internal assertions to halt the test.
   144  void SendNullCmsg(int sock, char buf[], int buf_size);
   145  
   146  // RecvSingleFDUnaligned sends both a single FD and some data over a unix domain
   147  // socket specified by an FD. This function does not obey the spec, but Linux
   148  // allows it and the apphosting code depends on this quirk. Note that calls to
   149  // this function must be wrapped in ASSERT_NO_FATAL_FAILURE for internal
   150  // assertions to halt the test.
   151  void RecvSingleFDUnaligned(int sock, int* fd, char buf[], int buf_size);
   152  
   153  // SetSoPassCred sets the SO_PASSCRED option on the specified socket.
   154  void SetSoPassCred(int sock);
   155  
   156  // UnsetSoPassCred clears the SO_PASSCRED option on the specified socket.
   157  void UnsetSoPassCred(int sock);
   158  
   159  }  // namespace testing
   160  }  // namespace gvisor
   161  
   162  #endif  // GVISOR_TEST_SYSCALLS_UNIX_DOMAIN_SOCKET_TEST_UTIL_H_