1// Copyright 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <brillo/unittest_utils.h>
6
7#include <fcntl.h>
8#include <sys/socket.h>
9#include <sys/types.h>
10#include <unistd.h>
11
12#include <base/logging.h>
13#include <gtest/gtest.h>
14
15namespace brillo {
16
17const int ScopedPipe::kPipeSize = 4096;
18
19ScopedPipe::ScopedPipe() {
20  int fds[2];
21  if (pipe(fds) != 0) {
22    PLOG(FATAL) << "Creating a pipe()";
23  }
24  reader = fds[0];
25  writer = fds[1];
26  EXPECT_EQ(kPipeSize, fcntl(writer, F_SETPIPE_SZ, kPipeSize));
27}
28
29ScopedPipe::~ScopedPipe() {
30  if (reader != -1)
31    close(reader);
32  if (writer != -1)
33    close(writer);
34}
35
36
37ScopedSocketPair::ScopedSocketPair() {
38  int fds[2];
39  if (socketpair(PF_LOCAL, SOCK_STREAM, 0, fds) != 0) {
40    PLOG(FATAL) << "Creating a socketpair()";
41  }
42  left = fds[0];
43  right = fds[1];
44}
45
46ScopedSocketPair::~ScopedSocketPair() {
47  if (left != -1)
48    close(left);
49  if (right != -1)
50    close(right);
51}
52
53}  // namespace brillo
54