blob: 9e337aa7dbba830a06807983b32f2ddd85436207 [file] [log] [blame]
Jerry Zhangdf69dd32017-05-03 17:17:49 -07001/*
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
18#include <android-base/test_utils.h>
19#include <fcntl.h>
20#include <gtest/gtest.h>
21#include <string>
22#include <unistd.h>
Jerry Zhangbc1d4b42018-03-27 15:25:03 -070023#include <log/log.h>
Jerry Zhangdf69dd32017-05-03 17:17:49 -070024
25#include "PosixAsyncIO.h"
26
27namespace android {
28
29constexpr int TEST_PACKET_SIZE = 512;
30
31static const std::string dummyDataStr =
32 "/*\n * Copyright 2015 The Android Open Source Project\n *\n * Licensed un"
33 "der the Apache License, Version 2.0 (the \"License\");\n * you may not us"
34 "e this file except in compliance with the License.\n * You may obtain a c"
35 "opy of the License at\n *\n * http://www.apache.org/licenses/LICENSE"
36 "-2.0\n *\n * Unless required by applicable law or agreed to in writing, s"
37 "oftware\n * distributed under the License is distributed on an \"AS IS\" "
38 "BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express o"
39 "r implied.\n * Se";
40
41
42class PosixAsyncIOTest : public ::testing::Test {
43protected:
44 TemporaryFile dummy_file;
45
46 PosixAsyncIOTest() {}
47 ~PosixAsyncIOTest() {}
48};
49
50TEST_F(PosixAsyncIOTest, testRead) {
51 char buf[TEST_PACKET_SIZE + 1];
52 buf[TEST_PACKET_SIZE] = '\0';
53 EXPECT_EQ(write(dummy_file.fd, dummyDataStr.c_str(), TEST_PACKET_SIZE), TEST_PACKET_SIZE);
54 struct aiocb aio;
55 struct aiocb *aiol[] = {&aio};
56 aio.aio_fildes = dummy_file.fd;
57 aio.aio_buf = buf;
58 aio.aio_offset = 0;
59 aio.aio_nbytes = TEST_PACKET_SIZE;
60
61 EXPECT_EQ(aio_read(&aio), 0);
62 EXPECT_EQ(aio_suspend(aiol, 1, nullptr), 0);
63 EXPECT_EQ(aio_return(&aio), TEST_PACKET_SIZE);
64 EXPECT_STREQ(buf, dummyDataStr.c_str());
65}
66
67TEST_F(PosixAsyncIOTest, testWrite) {
68 char buf[TEST_PACKET_SIZE + 1];
69 buf[TEST_PACKET_SIZE] = '\0';
70 struct aiocb aio;
71 struct aiocb *aiol[] = {&aio};
72 aio.aio_fildes = dummy_file.fd;
73 aio.aio_buf = const_cast<char*>(dummyDataStr.c_str());
74 aio.aio_offset = 0;
75 aio.aio_nbytes = TEST_PACKET_SIZE;
76
77 EXPECT_EQ(aio_write(&aio), 0);
78 EXPECT_EQ(aio_suspend(aiol, 1, nullptr), 0);
79 EXPECT_EQ(aio_return(&aio), TEST_PACKET_SIZE);
80 EXPECT_EQ(read(dummy_file.fd, buf, TEST_PACKET_SIZE), TEST_PACKET_SIZE);
81 EXPECT_STREQ(buf, dummyDataStr.c_str());
82}
83
84TEST_F(PosixAsyncIOTest, testError) {
85 char buf[TEST_PACKET_SIZE + 1];
86 buf[TEST_PACKET_SIZE] = '\0';
87 struct aiocb aio;
88 struct aiocb *aiol[] = {&aio};
89 aio.aio_fildes = -1;
90 aio.aio_buf = const_cast<char*>(dummyDataStr.c_str());
91 aio.aio_offset = 0;
92 aio.aio_nbytes = TEST_PACKET_SIZE;
93
94 EXPECT_EQ(aio_write(&aio), 0);
95 EXPECT_EQ(aio_suspend(aiol, 1, nullptr), 0);
96 EXPECT_EQ(aio_return(&aio), -1);
97 EXPECT_EQ(aio_error(&aio), EBADF);
98}
99
100} // namespace android