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

     1  // Copyright 2022 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  #include <asm-generic/errno-base.h>
    16  #include <errno.h>
    17  #include <stdlib.h>
    18  #include <sys/socket.h>
    19  #include <sys/types.h>
    20  #include <sys/un.h>
    21  
    22  #include <string>
    23  #include <tuple>
    24  
    25  #include "gtest/gtest.h"
    26  #include "test/util/file_descriptor.h"
    27  #include "test/util/fs_util.h"
    28  #include "test/util/test_util.h"
    29  
    30  // This file contains tests specific to connecting to host UDS managed outside
    31  // the sandbox / test.
    32  //
    33  // A set of ultity sockets will be created externally in $TEST_UDS_TREE and
    34  // $TEST_UDS_ATTACH_TREE for these tests to interact with.
    35  
    36  namespace gvisor {
    37  namespace testing {
    38  
    39  namespace {
    40  
    41  struct ProtocolSocket {
    42    int protocol;
    43    std::string name;
    44  };
    45  
    46  // Parameter is pipe/UDS root dir.
    47  using HostPipeTest = ::testing::TestWithParam<std::string>;
    48  
    49  TEST_P(HostPipeTest, Read) {
    50    const std::string env = GetParam();
    51  
    52    const char* val = getenv(env.c_str());
    53    ASSERT_NE(val, nullptr);
    54    const std::string root(val);
    55  
    56    const std::string path = JoinPath(root, "in");
    57    FileDescriptor reader = ASSERT_NO_ERRNO_AND_VALUE(Open(path, O_RDONLY));
    58  
    59    char lastValue = 0;
    60    ssize_t length = 0;
    61    while (length < 1024 * 1024) {
    62      char buf[1024];
    63  
    64      ssize_t read = ReadFd(reader.get(), buf, sizeof(buf));
    65      ASSERT_THAT(read, SyscallSucceeds());
    66      for (uint i = 0; i < read; ++i) {
    67        ASSERT_EQ(static_cast<char>(lastValue + i), buf[i]);
    68      }
    69      lastValue += read;
    70      length += read;
    71    }
    72  }
    73  
    74  TEST_P(HostPipeTest, Write) {
    75    const std::string env = GetParam();
    76  
    77    const char* val = getenv(env.c_str());
    78    ASSERT_NE(val, nullptr);
    79    const std::string root(val);
    80  
    81    const std::string path = JoinPath(root, "out");
    82    FileDescriptor writer = ASSERT_NO_ERRNO_AND_VALUE(Open(path, O_WRONLY));
    83  
    84    char lastValue = 0;
    85    ssize_t length = 0;
    86    while (length < 1024 * 1024) {
    87      char buf[1024];
    88      for (unsigned int i = 0; i < sizeof(buf); ++i) {
    89        buf[i] = i + lastValue;
    90      }
    91  
    92      ASSERT_THAT(WriteFd(writer.get(), buf, sizeof(buf)),
    93                  SyscallSucceedsWithValue(sizeof(buf)));
    94      lastValue += sizeof(buf);
    95      length += sizeof(buf);
    96    }
    97  }
    98  
    99  INSTANTIATE_TEST_SUITE_P(Paths, HostPipeTest,
   100                           // Test access via standard path and attach point.
   101                           ::testing::Values("TEST_FIFO_TREE",
   102                                             "TEST_FIFO_ATTACH_TREE"));
   103  
   104  }  // namespace
   105  
   106  }  // namespace testing
   107  }  // namespace gvisor