summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/pipe_reader_unittest.cc
blob: e74565545a050a9bb7cf0bc99a71f0c5f5c00910 (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
// Copyright (c) 2009 The Chromium 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 "chrome/browser/chromeos/pipe_reader.h"

#include <errno.h>

#include "base/file_path.h"
#include "base/safe_strerror_posix.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace chromeos {

typedef testing::Test PipeReaderTest;

TEST_F(PipeReaderTest, SuccessfulReadTest) {
  FilePath pipe_name("/tmp/MYFIFO");
  /* Create the FIFO if it does not exist */
  umask(0);
  mknod(pipe_name.value().c_str(), S_IFIFO|0666, 0);
  const char line[] = "foo";

  pid_t pID = fork();
  if (pID == 0) {
    int pipe = open(pipe_name.value().c_str(), O_WRONLY);
    EXPECT_NE(pipe, -1) << safe_strerror(errno);
    int num_bytes = write(pipe, line, strlen(line));
    EXPECT_NE(num_bytes, -1) << safe_strerror(errno);
    close(pipe);
    exit(1);
  } else {
    PipeReader reader(pipe_name);
    // asking for more should still just return the amount that was written.
    EXPECT_EQ(line, reader.Read(5 * strlen(line)));
  }
}

TEST_F(PipeReaderTest, SuccessfulMultiLineReadTest) {
  FilePath pipe_name("/tmp/TESTFIFO");
  /* Create the FIFO if it does not exist */
  umask(0);
  mknod(pipe_name.value().c_str(), S_IFIFO|0666, 0);
  const char foo[] = "foo";
  const char boo[] = "boo";
  std::string line(foo);
  line.append("\n");
  line.append(boo);
  line.append("\n");

  pid_t pID = fork();
  if (pID == 0) {
    int pipe = open(pipe_name.value().c_str(), O_WRONLY);
    EXPECT_NE(pipe, -1) << safe_strerror(errno);
    int num_bytes = write(pipe, line.c_str(), line.length());
    EXPECT_NE(num_bytes, -1) << safe_strerror(errno);
    close(pipe);
    exit(1);
  } else {
    PipeReader reader(pipe_name);
    // asking for more should still just return the amount that was written.
    std::string my_foo = reader.Read(5 * line.length());
    EXPECT_EQ(my_foo[my_foo.length() - 1], '\n');
    my_foo.resize(my_foo.length() - 1);
    EXPECT_EQ(my_foo, foo);

    std::string my_boo = reader.Read(5 * line.length());
    EXPECT_EQ(my_boo[my_boo.length() - 1], '\n');
    my_boo.resize(my_boo.length() - 1);
    EXPECT_EQ(my_boo, boo);
  }
}

TEST_F(PipeReaderTest, SuccessfulMultiLineReadNoEndingNewlineTest) {
  FilePath pipe_name("/tmp/TESTFIFO");
  /* Create the FIFO if it does not exist */
  umask(0);
  mknod(pipe_name.value().c_str(), S_IFIFO|0666, 0);
  const char foo[] = "foo";
  const char boo[] = "boo";
  std::string line(foo);
  line.append("\n");
  line.append(boo);

  pid_t pID = fork();
  if (pID == 0) {
    int pipe = open(pipe_name.value().c_str(), O_WRONLY);
    EXPECT_NE(pipe, -1) << safe_strerror(errno);
    int num_bytes = write(pipe, line.c_str(), line.length());
    EXPECT_NE(num_bytes, -1) << safe_strerror(errno);
    close(pipe);
    exit(1);
  } else {
    PipeReader reader(pipe_name);
    // asking for more should still just return the amount that was written.
    std::string my_foo = reader.Read(5 * line.length());
    EXPECT_EQ(my_foo[my_foo.length() - 1], '\n');
    my_foo.resize(my_foo.length() - 1);
    EXPECT_EQ(my_foo, foo);

    std::string my_boo = reader.Read(5 * line.length());
    EXPECT_EQ(my_boo, boo);
  }
}

}  // namespace chromeos