PosixAsyncIO_test.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright 2016 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #define LOG_TAG "PosixAsyncIO_test.cpp"
  17. #include <android-base/test_utils.h>
  18. #include <fcntl.h>
  19. #include <gtest/gtest.h>
  20. #include <string>
  21. #include <unistd.h>
  22. #include <log/log.h>
  23. #include "PosixAsyncIO.h"
  24. namespace android {
  25. constexpr int TEST_PACKET_SIZE = 512;
  26. static const std::string dummyDataStr =
  27. "/*\n * Copyright 2015 The Android Open Source Project\n *\n * Licensed un"
  28. "der the Apache License, Version 2.0 (the \"License\");\n * you may not us"
  29. "e this file except in compliance with the License.\n * You may obtain a c"
  30. "opy of the License at\n *\n * http://www.apache.org/licenses/LICENSE"
  31. "-2.0\n *\n * Unless required by applicable law or agreed to in writing, s"
  32. "oftware\n * distributed under the License is distributed on an \"AS IS\" "
  33. "BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express o"
  34. "r implied.\n * Se";
  35. class PosixAsyncIOTest : public ::testing::Test {
  36. protected:
  37. TemporaryFile dummy_file;
  38. PosixAsyncIOTest() {}
  39. ~PosixAsyncIOTest() {}
  40. };
  41. TEST_F(PosixAsyncIOTest, testRead) {
  42. char buf[TEST_PACKET_SIZE + 1];
  43. buf[TEST_PACKET_SIZE] = '\0';
  44. EXPECT_EQ(write(dummy_file.fd, dummyDataStr.c_str(), TEST_PACKET_SIZE), TEST_PACKET_SIZE);
  45. struct aiocb aio;
  46. struct aiocb *aiol[] = {&aio};
  47. aio.aio_fildes = dummy_file.fd;
  48. aio.aio_buf = buf;
  49. aio.aio_offset = 0;
  50. aio.aio_nbytes = TEST_PACKET_SIZE;
  51. EXPECT_EQ(aio_read(&aio), 0);
  52. EXPECT_EQ(aio_suspend(aiol, 1, nullptr), 0);
  53. EXPECT_EQ(aio_return(&aio), TEST_PACKET_SIZE);
  54. EXPECT_STREQ(buf, dummyDataStr.c_str());
  55. }
  56. TEST_F(PosixAsyncIOTest, testWrite) {
  57. char buf[TEST_PACKET_SIZE + 1];
  58. buf[TEST_PACKET_SIZE] = '\0';
  59. struct aiocb aio;
  60. struct aiocb *aiol[] = {&aio};
  61. aio.aio_fildes = dummy_file.fd;
  62. aio.aio_buf = const_cast<char*>(dummyDataStr.c_str());
  63. aio.aio_offset = 0;
  64. aio.aio_nbytes = TEST_PACKET_SIZE;
  65. EXPECT_EQ(aio_write(&aio), 0);
  66. EXPECT_EQ(aio_suspend(aiol, 1, nullptr), 0);
  67. EXPECT_EQ(aio_return(&aio), TEST_PACKET_SIZE);
  68. EXPECT_EQ(read(dummy_file.fd, buf, TEST_PACKET_SIZE), TEST_PACKET_SIZE);
  69. EXPECT_STREQ(buf, dummyDataStr.c_str());
  70. }
  71. TEST_F(PosixAsyncIOTest, testError) {
  72. char buf[TEST_PACKET_SIZE + 1];
  73. buf[TEST_PACKET_SIZE] = '\0';
  74. struct aiocb aio;
  75. struct aiocb *aiol[] = {&aio};
  76. aio.aio_fildes = -1;
  77. aio.aio_buf = const_cast<char*>(dummyDataStr.c_str());
  78. aio.aio_offset = 0;
  79. aio.aio_nbytes = TEST_PACKET_SIZE;
  80. EXPECT_EQ(aio_write(&aio), 0);
  81. EXPECT_EQ(aio_suspend(aiol, 1, nullptr), 0);
  82. EXPECT_EQ(aio_return(&aio), -1);
  83. EXPECT_EQ(aio_error(&aio), EBADF);
  84. }
  85. } // namespace android