aboutsummaryrefslogtreecommitdiffstats
path: root/brillo/unittest_utils.cc
blob: 7cace5dfdefd3a0e8a29ef00da8c7668f588997e (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
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <brillo/unittest_utils.h>

#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

#include <base/logging.h>
#include <gtest/gtest.h>

namespace brillo {

const int ScopedPipe::kPipeSize = 4096;

ScopedPipe::ScopedPipe() {
  int fds[2];
  if (pipe(fds) != 0) {
    PLOG(FATAL) << "Creating a pipe()";
  }
  reader = fds[0];
  writer = fds[1];
  EXPECT_EQ(kPipeSize, fcntl(writer, F_SETPIPE_SZ, kPipeSize));
}

ScopedPipe::~ScopedPipe() {
  if (reader != -1)
    close(reader);
  if (writer != -1)
    close(writer);
}


ScopedSocketPair::ScopedSocketPair() {
  int fds[2];
  if (socketpair(PF_LOCAL, SOCK_STREAM, 0, fds) != 0) {
    PLOG(FATAL) << "Creating a socketpair()";
  }
  left = fds[0];
  right = fds[1];
}

ScopedSocketPair::~ScopedSocketPair() {
  if (left != -1)
    close(left);
  if (right != -1)
    close(right);
}

}  // namespace brillo