1 // Copyright (C) 2011-2021 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 
9 #include <util/unittests/check_valgrind.h>
10 
11 #include <util/io/fd.h>
12 
13 #include <util/unittests/fork.h>
14 
15 #include <gtest/gtest.h>
16 
17 using namespace isc::util::io;
18 using namespace isc::util::unittests;
19 
20 namespace {
21 
22 // Make sure the test is large enough and does not fit into one
23 // read or write request
24 const size_t TEST_DATA_SIZE = 8 * 1024 * 1024;
25 
26 class FDTest : public ::testing::Test {
27 public:
28     unsigned char *data, *buffer;
29 
30     /// @brief Constructor
FDTest()31     FDTest() :
32         // We do not care what is inside, we just need it to be the same
33         data(new unsigned char[TEST_DATA_SIZE]),
34         buffer(NULL) {
35         memset(data, 0, TEST_DATA_SIZE);
36     }
37 
38     /// @brief Destructor
~FDTest()39     ~FDTest() {
40         delete[] data;
41         delete[] buffer;
42     }
43 };
44 
45 // Test we read what was sent
TEST_F(FDTest,read)46 TEST_F(FDTest, read) {
47     if (!isc::util::unittests::runningOnValgrind()) {
48         int read_pipe(0);
49         buffer = new unsigned char[TEST_DATA_SIZE];
50         pid_t feeder(provide_input(&read_pipe, data, TEST_DATA_SIZE));
51         ASSERT_GE(feeder, 0);
52         ssize_t received(read_data(read_pipe, buffer, TEST_DATA_SIZE));
53         EXPECT_TRUE(process_ok(feeder));
54         EXPECT_EQ(TEST_DATA_SIZE, received);
55         EXPECT_EQ(0, memcmp(data, buffer, received));
56     }
57 }
58 
59 // Test we write the correct thing
TEST_F(FDTest,write)60 TEST_F(FDTest, write) {
61     if (!isc::util::unittests::runningOnValgrind()) {
62         int write_pipe(0);
63         pid_t checker(check_output(&write_pipe, data, TEST_DATA_SIZE));
64         ASSERT_GE(checker, 0);
65         EXPECT_TRUE(write_data(write_pipe, data, TEST_DATA_SIZE));
66         EXPECT_EQ(0, close(write_pipe));
67         EXPECT_TRUE(process_ok(checker));
68     }
69 }
70 
71 }
72