fd_tests.cc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <util/io/fd.h>
  15. #include <util/unittests/fork.h>
  16. #include <gtest/gtest.h>
  17. using namespace isc::util::io;
  18. using namespace isc::util::unittests;
  19. namespace {
  20. // Make sure the test is large enough and does not fit into one
  21. // read or write request
  22. const size_t TEST_DATA_SIZE = 8 * 1024 * 1024;
  23. class FDTest : public ::testing::Test {
  24. public:
  25. unsigned char *data, *buffer;
  26. FDTest() :
  27. // We do not care what is inside, we just need it to be the same
  28. data(new unsigned char[TEST_DATA_SIZE]),
  29. buffer(NULL)
  30. { }
  31. ~ FDTest() {
  32. delete[] data;
  33. delete[] buffer;
  34. }
  35. };
  36. // Test we read what was sent
  37. TEST_F(FDTest, read) {
  38. int read_pipe(0);
  39. buffer = new unsigned char[TEST_DATA_SIZE];
  40. pid_t feeder(provide_input(&read_pipe, data, TEST_DATA_SIZE));
  41. ASSERT_GE(feeder, 0);
  42. ssize_t received(read_data(read_pipe, buffer, TEST_DATA_SIZE));
  43. EXPECT_TRUE(process_ok(feeder));
  44. EXPECT_EQ(TEST_DATA_SIZE, received);
  45. EXPECT_EQ(0, memcmp(data, buffer, received));
  46. }
  47. // Test we write the correct thing
  48. TEST_F(FDTest, write) {
  49. int write_pipe(0);
  50. pid_t checker(check_output(&write_pipe, data, TEST_DATA_SIZE));
  51. ASSERT_GE(checker, 0);
  52. EXPECT_TRUE(write_data(write_pipe, data, TEST_DATA_SIZE));
  53. EXPECT_EQ(0, close(write_pipe));
  54. EXPECT_TRUE(process_ok(checker));
  55. }
  56. }