summaryrefslogtreecommitdiffhomepage
path: root/test/syscalls/linux/socket_test_util.h
blob: 75c0d47351b2b642b87f603c4d79da6f2221937e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef GVISOR_TEST_SYSCALLS_SOCKET_TEST_UTIL_H_
#define GVISOR_TEST_SYSCALLS_SOCKET_TEST_UTIL_H_

#include <errno.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/udp.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>

#include <functional>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "gtest/gtest.h"
#include "absl/strings/str_format.h"
#include "test/util/file_descriptor.h"
#include "test/util/posix_error.h"
#include "test/util/test_util.h"

namespace gvisor {
namespace testing {

// Wrapper for socket(2) that returns a FileDescriptor.
inline PosixErrorOr<FileDescriptor> Socket(int family, int type, int protocol) {
  int fd = socket(family, type, protocol);
  MaybeSave();
  if (fd < 0) {
    return PosixError(
        errno, absl::StrFormat("socket(%d, %d, %d)", family, type, protocol));
  }
  return FileDescriptor(fd);
}

// Wrapper for accept(2) that returns a FileDescriptor.
inline PosixErrorOr<FileDescriptor> Accept(int sockfd, sockaddr* addr,
                                           socklen_t* addrlen) {
  int fd = RetryEINTR(accept)(sockfd, addr, addrlen);
  MaybeSave();
  if (fd < 0) {
    return PosixError(
        errno, absl::StrFormat("accept(%d, %p, %p)", sockfd, addr, addrlen));
  }
  return FileDescriptor(fd);
}

// Wrapper for accept4(2) that returns a FileDescriptor.
inline PosixErrorOr<FileDescriptor> Accept4(int sockfd, sockaddr* addr,
                                            socklen_t* addrlen, int flags) {
  int fd = RetryEINTR(accept4)(sockfd, addr, addrlen, flags);
  MaybeSave();
  if (fd < 0) {
    return PosixError(errno, absl::StrFormat("accept4(%d, %p, %p, %#x)", sockfd,
                                             addr, addrlen, flags));
  }
  return FileDescriptor(fd);
}

inline ssize_t SendFd(int fd, void* buf, size_t count, int flags) {
  return internal::ApplyFileIoSyscall(
      [&](size_t completed) {
        return sendto(fd, static_cast<char*>(buf) + completed,
                      count - completed, flags, nullptr, 0);
      },
      count);
}

PosixErrorOr<struct sockaddr_un> UniqueUnixAddr(bool abstract, int domain);

// A Creator<T> is a function that attempts to create and return a new T. (This
// is copy/pasted from cloud/gvisor/api/sandbox_util.h and is just duplicated
// here for clarity.)
template <typename T>
using Creator = std::function<PosixErrorOr<std::unique_ptr<T>>()>;

// A SocketPair represents a pair of socket file descriptors owned by the
// SocketPair.
class SocketPair {
 public:
  virtual ~SocketPair() = default;

  virtual int first_fd() const = 0;
  virtual int second_fd() const = 0;
  virtual int release_first_fd() = 0;
  virtual int release_second_fd() = 0;
  virtual const struct sockaddr* first_addr() const = 0;
  virtual const struct sockaddr* second_addr() const = 0;
  virtual size_t first_addr_size() const = 0;
  virtual size_t second_addr_size() const = 0;
  virtual size_t first_addr_len() const = 0;
  virtual size_t second_addr_len() const = 0;
};

// A FDSocketPair is a SocketPair that consists of only a pair of file
// descriptors.
class FDSocketPair : public SocketPair {
 public:
  FDSocketPair(int first_fd, int second_fd)
      : first_(first_fd), second_(second_fd) {}
  FDSocketPair(std::unique_ptr<FileDescriptor> first_fd,
               std::unique_ptr<FileDescriptor> second_fd)
      : first_(first_fd->release()), second_(second_fd->release()) {}

  int first_fd() const override { return first_.get(); }
  int second_fd() const override { return second_.get(); }
  int release_first_fd() override { return first_.release(); }
  int release_second_fd() override { return second_.release(); }
  const struct sockaddr* first_addr() const override { return nullptr; }
  const struct sockaddr* second_addr() const override { return nullptr; }
  size_t first_addr_size() const override { return 0; }
  size_t second_addr_size() const override { return 0; }
  size_t first_addr_len() const override { return 0; }
  size_t second_addr_len() const override { return 0; }

 private:
  FileDescriptor first_;
  FileDescriptor second_;
};

// CalculateUnixSockAddrLen calculates the length returned by recvfrom(2) and
// recvmsg(2) for Unix sockets.
size_t CalculateUnixSockAddrLen(const char* sun_path);

// A AddrFDSocketPair is a SocketPair that consists of a pair of file
// descriptors in addition to a pair of socket addresses.
class AddrFDSocketPair : public SocketPair {
 public:
  AddrFDSocketPair(int first_fd, int second_fd,
                   const struct sockaddr_un& first_address,
                   const struct sockaddr_un& second_address)
      : first_(first_fd),
        second_(second_fd),
        first_addr_(to_storage(first_address)),
        second_addr_(to_storage(second_address)),
        first_len_(CalculateUnixSockAddrLen(first_address.sun_path)),
        second_len_(CalculateUnixSockAddrLen(second_address.sun_path)),
        first_size_(sizeof(first_address)),
        second_size_(sizeof(second_address)) {}

  AddrFDSocketPair(int first_fd, int second_fd,
                   const struct sockaddr_in& first_address,
                   const struct sockaddr_in& second_address)
      : first_(first_fd),
        second_(second_fd),
        first_addr_(to_storage(first_address)),
        second_addr_(to_storage(second_address)),
        first_len_(sizeof(first_address)),
        second_len_(sizeof(second_address)),
        first_size_(sizeof(first_address)),
        second_size_(sizeof(second_address)) {}

  AddrFDSocketPair(int first_fd, int second_fd,
                   const struct sockaddr_in6& first_address,
                   const struct sockaddr_in6& second_address)
      : first_(first_fd),
        second_(second_fd),
        first_addr_(to_storage(first_address)),
        second_addr_(to_storage(second_address)),
        first_len_(sizeof(first_address)),
        second_len_(sizeof(second_address)),
        first_size_(sizeof(first_address)),
        second_size_(sizeof(second_address)) {}

  int first_fd() const override { return first_.get(); }
  int second_fd() const override { return second_.get(); }
  int release_first_fd() override { return first_.release(); }
  int release_second_fd() override { return second_.release(); }
  const struct sockaddr* first_addr() const override {
    return reinterpret_cast<const struct sockaddr*>(&first_addr_);
  }
  const struct sockaddr* second_addr() const override {
    return reinterpret_cast<const struct sockaddr*>(&second_addr_);
  }
  size_t first_addr_size() const override { return first_size_; }
  size_t second_addr_size() const override { return second_size_; }
  size_t first_addr_len() const override { return first_len_; }
  size_t second_addr_len() const override { return second_len_; }

 private:
  // to_storage coverts a sockaddr_* to a sockaddr_storage.
  static struct sockaddr_storage to_storage(const sockaddr_un& addr);
  static struct sockaddr_storage to_storage(const sockaddr_in& addr);
  static struct sockaddr_storage to_storage(const sockaddr_in6& addr);

  FileDescriptor first_;
  FileDescriptor second_;
  const struct sockaddr_storage first_addr_;
  const struct sockaddr_storage second_addr_;
  const size_t first_len_;
  const size_t second_len_;
  const size_t first_size_;
  const size_t second_size_;
};

// SyscallSocketPairCreator returns a Creator<SocketPair> that obtains file
// descriptors by invoking the socketpair() syscall.
Creator<SocketPair> SyscallSocketPairCreator(int domain, int type,
                                             int protocol);

// SyscallSocketCreator returns a Creator<FileDescriptor> that obtains a file
// descriptor by invoking the socket() syscall.
Creator<FileDescriptor> SyscallSocketCreator(int domain, int type,
                                             int protocol);

// FilesystemBidirectionalBindSocketPairCreator returns a Creator<SocketPair>
// that obtains file descriptors by invoking the bind() and connect() syscalls
// on filesystem paths. Only works for DGRAM sockets.
Creator<SocketPair> FilesystemBidirectionalBindSocketPairCreator(int domain,
                                                                 int type,
                                                                 int protocol);

// AbstractBidirectionalBindSocketPairCreator returns a Creator<SocketPair> that
// obtains file descriptors by invoking the bind() and connect() syscalls on
// abstract namespace paths. Only works for DGRAM sockets.
Creator<SocketPair> AbstractBidirectionalBindSocketPairCreator(int domain,
                                                               int type,
                                                               int protocol);

// SocketpairGoferSocketPairCreator returns a Creator<SocketPair> that
// obtains file descriptors by connect() syscalls on two sockets with socketpair
// gofer paths.
Creator<SocketPair> SocketpairGoferSocketPairCreator(int domain, int type,
                                                     int protocol);

// SocketpairGoferFileSocketPairCreator returns a Creator<SocketPair> that
// obtains file descriptors by open() syscalls on socketpair gofer paths.
Creator<SocketPair> SocketpairGoferFileSocketPairCreator(int flags);

// FilesystemAcceptBindSocketPairCreator returns a Creator<SocketPair> that
// obtains file descriptors by invoking the accept() and bind() syscalls on
// a filesystem path. Only works for STREAM and SEQPACKET sockets.
Creator<SocketPair> FilesystemAcceptBindSocketPairCreator(int domain, int type,
                                                          int protocol);

// AbstractAcceptBindSocketPairCreator returns a Creator<SocketPair> that
// obtains file descriptors by invoking the accept() and bind() syscalls on a
// abstract namespace path. Only works for STREAM and SEQPACKET sockets.
Creator<SocketPair> AbstractAcceptBindSocketPairCreator(int domain, int type,
                                                        int protocol);

// FilesystemUnboundSocketPairCreator returns a Creator<SocketPair> that obtains
// file descriptors by invoking the socket() syscall and generates a filesystem
// path for binding.
Creator<SocketPair> FilesystemUnboundSocketPairCreator(int domain, int type,
                                                       int protocol);

// AbstractUnboundSocketPairCreator returns a Creator<SocketPair> that obtains
// file descriptors by invoking the socket() syscall and generates an abstract
// path for binding.
Creator<SocketPair> AbstractUnboundSocketPairCreator(int domain, int type,
                                                     int protocol);

// TCPAcceptBindSocketPairCreator returns a Creator<SocketPair> that obtains
// file descriptors by invoking the accept() and bind() syscalls on TCP sockets.
Creator<SocketPair> TCPAcceptBindSocketPairCreator(int domain, int type,
                                                   int protocol,
                                                   bool dual_stack);

// TCPAcceptBindPersistentListenerSocketPairCreator is like
// TCPAcceptBindSocketPairCreator, except it uses the same listening socket to
// create all SocketPairs.
Creator<SocketPair> TCPAcceptBindPersistentListenerSocketPairCreator(
    int domain, int type, int protocol, bool dual_stack);

// UDPBidirectionalBindSocketPairCreator returns a Creator<SocketPair> that
// obtains file descriptors by invoking the bind() and connect() syscalls on UDP
// sockets.
Creator<SocketPair> UDPBidirectionalBindSocketPairCreator(int domain, int type,
                                                          int protocol,
                                                          bool dual_stack);

// UDPUnboundSocketPairCreator returns a Creator<SocketPair> that obtains file
// descriptors by creating UDP sockets.
Creator<SocketPair> UDPUnboundSocketPairCreator(int domain, int type,
                                                int protocol, bool dual_stack);

// UnboundSocketCreator returns a Creator<FileDescriptor> that obtains a file
// descriptor by creating a socket.
Creator<FileDescriptor> UnboundSocketCreator(int domain, int type,
                                             int protocol);

// A SocketPairKind couples a human-readable description of a socket pair with
// a function that creates such a socket pair.
struct SocketPairKind {
  std::string description;
  int domain;
  int type;
  int protocol;
  Creator<SocketPair> creator;

  // Create creates a socket pair of this kind.
  PosixErrorOr<std::unique_ptr<SocketPair>> Create() const { return creator(); }
};

// A SocketKind couples a human-readable description of a socket with
// a function that creates such a socket.
struct SocketKind {
  std::string description;
  int domain;
  int type;
  int protocol;
  Creator<FileDescriptor> creator;

  // Create creates a socket pair of this kind.
  PosixErrorOr<std::unique_ptr<FileDescriptor>> Create() const {
    return creator();
  }
};

// A ReversedSocketPair wraps another SocketPair but flips the first and second
// file descriptors. ReversedSocketPair is used to test socket pairs that
// should be symmetric.
class ReversedSocketPair : public SocketPair {
 public:
  explicit ReversedSocketPair(std::unique_ptr<SocketPair> base)
      : base_(std::move(base)) {}

  int first_fd() const override { return base_->second_fd(); }
  int second_fd() const override { return base_->first_fd(); }
  int release_first_fd() override { return base_->release_second_fd(); }
  int release_second_fd() override { return base_->release_first_fd(); }
  const struct sockaddr* first_addr() const override {
    return base_->second_addr();
  }
  const struct sockaddr* second_addr() const override {
    return base_->first_addr();
  }
  size_t first_addr_size() const override { return base_->second_addr_size(); }
  size_t second_addr_size() const override { return base_->first_addr_size(); }
  size_t first_addr_len() const override { return base_->second_addr_len(); }
  size_t second_addr_len() const override { return base_->first_addr_len(); }

 private:
  std::unique_ptr<SocketPair> base_;
};

// Reversed returns a SocketPairKind that represents SocketPairs created by
// flipping the file descriptors provided by another SocketPair.
SocketPairKind Reversed(SocketPairKind const& base);

// IncludeReversals returns a vector<SocketPairKind> that returns all
// SocketPairKinds in `vec` as well as all SocketPairKinds obtained by flipping
// the file descriptors provided by the kinds in `vec`.
std::vector<SocketPairKind> IncludeReversals(std::vector<SocketPairKind> vec);

// A Middleware is a function wraps a SocketPairKind.
using Middleware = std::function<SocketPairKind(SocketPairKind)>;

// Reversed returns a SocketPairKind that represents SocketPairs created by
// flipping the file descriptors provided by another SocketPair.
template <typename T>
Middleware SetSockOpt(int level, int optname, T* value) {
  return [=](SocketPairKind const& base) {
    auto const& creator = base.creator;
    return SocketPairKind{
        absl::StrCat("setsockopt(", level, ", ", optname, ", ", *value, ") ",
                     base.description),
        base.domain, base.type, base.protocol,
        [creator, level, optname,
         value]() -> PosixErrorOr<std::unique_ptr<SocketPair>> {
          ASSIGN_OR_RETURN_ERRNO(auto creator_value, creator());
          if (creator_value->first_fd() >= 0) {
            RETURN_ERROR_IF_SYSCALL_FAIL(setsockopt(
                creator_value->first_fd(), level, optname, value, sizeof(T)));
          }
          if (creator_value->second_fd() >= 0) {
            RETURN_ERROR_IF_SYSCALL_FAIL(setsockopt(
                creator_value->second_fd(), level, optname, value, sizeof(T)));
          }
          return creator_value;
        }};
  };
}

constexpr int kSockOptOn = 1;
constexpr int kSockOptOff = 0;

// NoOp returns the same SocketPairKind that it is passed.
SocketPairKind NoOp(SocketPairKind const& base);

// TransferTest tests that data can be send back and fourth between two
// specified FDs. Note that calls to this function should be wrapped in
// ASSERT_NO_FATAL_FAILURE().
void TransferTest(int fd1, int fd2);

// Fills [buf, buf+len) with random bytes.
void RandomizeBuffer(char* buf, size_t len);

// Base test fixture for tests that operate on pairs of connected sockets.
class SocketPairTest : public ::testing::TestWithParam<SocketPairKind> {
 protected:
  SocketPairTest() {
    // gUnit uses printf, so so will we.
    printf("Testing with %s\n", GetParam().description.c_str());
    fflush(stdout);
  }

  PosixErrorOr<std::unique_ptr<SocketPair>> NewSocketPair() const {
    return GetParam().Create();
  }
};

// Base test fixture for tests that operate on simple Sockets.
class SimpleSocketTest : public ::testing::TestWithParam<SocketKind> {
 protected:
  SimpleSocketTest() {
    // gUnit uses printf, so so will we.
    printf("Testing with %s\n", GetParam().description.c_str());
  }

  PosixErrorOr<std::unique_ptr<FileDescriptor>> NewSocket() const {
    return GetParam().Create();
  }
};

SocketKind SimpleSocket(int fam, int type, int proto);

// Send a buffer of size 'size' to sockets->first_fd(), returning the result of
// sendmsg.
//
// If reader, read from second_fd() until size bytes have been read.
ssize_t SendLargeSendMsg(const std::unique_ptr<SocketPair>& sockets,
                         size_t size, bool reader);

// Initializes the given buffer with random data.
void RandomizeBuffer(char* ptr, size_t len);

enum class AddressFamily { kIpv4 = 1, kIpv6 = 2, kDualStack = 3 };
enum class SocketType { kUdp = 1, kTcp = 2 };

// Returns a PosixError or a port that is available. If 0 is specified as the
// port it will bind port 0 (and allow the kernel to select any free port).
// Otherwise, it will try to bind the specified port and validate that it can be
// used for the requested family and socket type. The final option is
// reuse_addr. This specifies whether SO_REUSEADDR should be applied before a
// bind(2) attempt. SO_REUSEADDR means that sockets in TIME_WAIT states or other
// bound UDP sockets would not cause an error on bind(2). This option should be
// set if subsequent calls to bind on the returned port will also use
// SO_REUSEADDR.
//
// Note: That this test will attempt to bind the ANY address for the respective
// protocol.
PosixErrorOr<int> PortAvailable(int port, AddressFamily family, SocketType type,
                                bool reuse_addr);

// FreeAvailablePort is used to return a port that was obtained by using
// the PortAvailable helper with port 0.
PosixError FreeAvailablePort(int port);

// SendMsg converts a buffer to an iovec and adds it to msg before sending it.
PosixErrorOr<int> SendMsg(int sock, msghdr* msg, char buf[], int buf_size);

// RecvTimeout calls select on sock with timeout and then calls recv on sock.
PosixErrorOr<int> RecvTimeout(int sock, char buf[], int buf_size, int timeout);

// RecvMsgTimeout calls select on sock with timeout and then calls recvmsg on
// sock.
PosixErrorOr<int> RecvMsgTimeout(int sock, msghdr* msg, int timeout);

// RecvNoData checks that no data is receivable on sock.
void RecvNoData(int sock);

// Base test fixture for tests that apply to all kinds of pairs of connected
// sockets.
using AllSocketPairTest = SocketPairTest;

struct TestAddress {
  std::string description;
  sockaddr_storage addr;
  socklen_t addr_len;

  int family() const { return addr.ss_family; }
  explicit TestAddress(std::string description = "")
      : description(std::move(description)), addr(), addr_len() {}
};

constexpr char kMulticastAddress[] = "224.0.2.1";
constexpr char kBroadcastAddress[] = "255.255.255.255";

TestAddress V4Any();
TestAddress V4Broadcast();
TestAddress V4Loopback();
TestAddress V4MappedAny();
TestAddress V4MappedLoopback();
TestAddress V4Multicast();
TestAddress V6Any();
TestAddress V6Loopback();
TestAddress V6Multicast();

// Compute the internet checksum of an IP header.
uint16_t IPChecksum(struct iphdr ip);

// Compute the internet checksum of a UDP header.
uint16_t UDPChecksum(struct iphdr iphdr, struct udphdr udphdr,
                     const char* payload, ssize_t payload_len);

// Compute the internet checksum of an ICMP header.
uint16_t ICMPChecksum(struct icmphdr icmphdr, const char* payload,
                      ssize_t payload_len);

namespace internal {
PosixErrorOr<int> TryPortAvailable(int port, AddressFamily family,
                                   SocketType type, bool reuse_addr);
}  // namespace internal

}  // namespace testing
}  // namespace gvisor

#endif  // GVISOR_TEST_SYSCALLS_SOCKET_TEST_UTIL_H_