Switch MtpFfsHandle to aio, add control functions.
MtpFfsHandle now uses kaio to handle usb data.
This achieves better performance without using
the endpoint alloc ioctl.
This also allows ep0 events to be handled without
race conditions. Events will also include control
requests, which will allow both host and device
initiated cancellation.
Bug: 37916658
Bug: 36802721
Test: Transfer various size files, run MtpFfsHandleTest
Test: Cancel transfer on Windows
Test: Allow device to cancel transfer on Windows
Change-Id: Ib3ce996f00782ce7f68f29b2510dbc17f09fdf14
diff --git a/media/mtp/tests/Android.bp b/media/mtp/tests/Android.bp
index 356406d..a0480b6 100644
--- a/media/mtp/tests/Android.bp
+++ b/media/mtp/tests/Android.bp
@@ -30,8 +30,9 @@
}
cc_test {
- name: "async_io_test",
- srcs: ["AsyncIO_test.cpp"],
+ name: "posix_async_io_test",
+ test_suites: ["device-tests"],
+ srcs: ["PosixAsyncIO_test.cpp"],
shared_libs: [
"libbase",
"libmtp",
diff --git a/media/mtp/tests/AsyncIO_test.cpp b/media/mtp/tests/AsyncIO_test.cpp
deleted file mode 100644
index b5f4538..0000000
--- a/media/mtp/tests/AsyncIO_test.cpp
+++ /dev/null
@@ -1,192 +0,0 @@
-/*
- * Copyright 2016 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#define LOG_TAG "AsyncIO_test.cpp"
-
-#include <android-base/test_utils.h>
-#include <fcntl.h>
-#include <gtest/gtest.h>
-#include <string>
-#include <unistd.h>
-#include <utils/Log.h>
-
-#include "AsyncIO.h"
-
-namespace android {
-
-constexpr int TEST_PACKET_SIZE = 512;
-constexpr int POOL_COUNT = 10;
-
-static const std::string dummyDataStr =
- "/*\n * Copyright 2015 The Android Open Source Project\n *\n * Licensed un"
- "der the Apache License, Version 2.0 (the \"License\");\n * you may not us"
- "e this file except in compliance with the License.\n * You may obtain a c"
- "opy of the License at\n *\n * http://www.apache.org/licenses/LICENSE"
- "-2.0\n *\n * Unless required by applicable law or agreed to in writing, s"
- "oftware\n * distributed under the License is distributed on an \"AS IS\" "
- "BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express o"
- "r implied.\n * Se";
-
-
-class AsyncIOTest : public ::testing::Test {
-protected:
- TemporaryFile dummy_file;
-
- AsyncIOTest() {}
- ~AsyncIOTest() {}
-};
-
-TEST_F(AsyncIOTest, testRead) {
- char buf[TEST_PACKET_SIZE + 1];
- buf[TEST_PACKET_SIZE] = '\0';
- EXPECT_EQ(write(dummy_file.fd, dummyDataStr.c_str(), TEST_PACKET_SIZE), TEST_PACKET_SIZE);
- struct aiocb aio;
- struct aiocb *aiol[] = {&aio};
- aio.aio_fildes = dummy_file.fd;
- aio.aio_buf = buf;
- aio.aio_offset = 0;
- aio.aio_nbytes = TEST_PACKET_SIZE;
-
- EXPECT_EQ(aio_read(&aio), 0);
- EXPECT_EQ(aio_suspend(aiol, 1, nullptr), 0);
- EXPECT_EQ(aio_return(&aio), TEST_PACKET_SIZE);
- EXPECT_STREQ(buf, dummyDataStr.c_str());
-}
-
-TEST_F(AsyncIOTest, testWrite) {
- char buf[TEST_PACKET_SIZE + 1];
- buf[TEST_PACKET_SIZE] = '\0';
- struct aiocb aio;
- struct aiocb *aiol[] = {&aio};
- aio.aio_fildes = dummy_file.fd;
- aio.aio_buf = const_cast<char*>(dummyDataStr.c_str());
- aio.aio_offset = 0;
- aio.aio_nbytes = TEST_PACKET_SIZE;
-
- EXPECT_EQ(aio_write(&aio), 0);
- EXPECT_EQ(aio_suspend(aiol, 1, nullptr), 0);
- EXPECT_EQ(aio_return(&aio), TEST_PACKET_SIZE);
- EXPECT_EQ(read(dummy_file.fd, buf, TEST_PACKET_SIZE), TEST_PACKET_SIZE);
- EXPECT_STREQ(buf, dummyDataStr.c_str());
-}
-
-TEST_F(AsyncIOTest, testError) {
- char buf[TEST_PACKET_SIZE + 1];
- buf[TEST_PACKET_SIZE] = '\0';
- struct aiocb aio;
- struct aiocb *aiol[] = {&aio};
- aio.aio_fildes = -1;
- aio.aio_buf = const_cast<char*>(dummyDataStr.c_str());
- aio.aio_offset = 0;
- aio.aio_nbytes = TEST_PACKET_SIZE;
-
- EXPECT_EQ(aio_write(&aio), 0);
- EXPECT_EQ(aio_suspend(aiol, 1, nullptr), 0);
- EXPECT_EQ(aio_return(&aio), -1);
- EXPECT_EQ(aio_error(&aio), EBADF);
-}
-
-TEST_F(AsyncIOTest, testSpliceRead) {
- char buf[TEST_PACKET_SIZE + 1];
- buf[TEST_PACKET_SIZE] = '\0';
- int pipeFd[2];
- EXPECT_EQ(pipe(pipeFd), 0);
- EXPECT_EQ(write(dummy_file.fd, dummyDataStr.c_str(), TEST_PACKET_SIZE), TEST_PACKET_SIZE);
- struct aiocb aio;
- struct aiocb *aiol[] = {&aio};
- aio.aio_fildes = dummy_file.fd;
- aio.aio_sink = pipeFd[1];
- aio.aio_offset = 0;
- aio.aio_nbytes = TEST_PACKET_SIZE;
-
- EXPECT_EQ(aio_splice_read(&aio), 0);
- EXPECT_EQ(aio_suspend(aiol, 1, nullptr), 0);
- EXPECT_EQ(aio_return(&aio), TEST_PACKET_SIZE);
-
- EXPECT_EQ(read(pipeFd[0], buf, TEST_PACKET_SIZE), TEST_PACKET_SIZE);
- EXPECT_STREQ(buf, dummyDataStr.c_str());
-}
-
-TEST_F(AsyncIOTest, testSpliceWrite) {
- char buf[TEST_PACKET_SIZE + 1];
- buf[TEST_PACKET_SIZE] = '\0';
- int pipeFd[2];
- EXPECT_EQ(pipe(pipeFd), 0);
- EXPECT_EQ(write(pipeFd[1], dummyDataStr.c_str(), TEST_PACKET_SIZE), TEST_PACKET_SIZE);
- struct aiocb aio;
- struct aiocb *aiol[] = {&aio};
- aio.aio_fildes = pipeFd[0];
- aio.aio_sink = dummy_file.fd;
- aio.aio_offset = 0;
- aio.aio_nbytes = TEST_PACKET_SIZE;
-
- EXPECT_EQ(aio_splice_write(&aio), 0);
- EXPECT_EQ(aio_suspend(aiol, 1, nullptr), 0);
- EXPECT_EQ(aio_return(&aio), TEST_PACKET_SIZE);
- EXPECT_EQ(read(dummy_file.fd, buf, TEST_PACKET_SIZE), TEST_PACKET_SIZE);
- EXPECT_STREQ(buf, dummyDataStr.c_str());
-}
-
-TEST_F(AsyncIOTest, testPoolWrite) {
- aio_pool_write_init();
- char buf[TEST_PACKET_SIZE * POOL_COUNT + 1];
- buf[TEST_PACKET_SIZE * POOL_COUNT] = '\0';
-
- for (int i = 0; i < POOL_COUNT; i++) {
- struct aiocb *aiop = new struct aiocb;
- aiop->aio_fildes = dummy_file.fd;
- aiop->aio_pool_buf = std::unique_ptr<char[]>(new char[TEST_PACKET_SIZE]);
- memcpy(aiop->aio_pool_buf.get(), dummyDataStr.c_str(), TEST_PACKET_SIZE);
- aiop->aio_offset = i * TEST_PACKET_SIZE;
- aiop->aio_nbytes = TEST_PACKET_SIZE;
- EXPECT_EQ(aio_pool_write(aiop), 0);
- }
- aio_pool_end();
- EXPECT_EQ(read(dummy_file.fd, buf, TEST_PACKET_SIZE * POOL_COUNT), TEST_PACKET_SIZE * POOL_COUNT);
-
- std::stringstream ss;
- for (int i = 0; i < POOL_COUNT; i++)
- ss << dummyDataStr;
-
- EXPECT_STREQ(buf, ss.str().c_str());
-}
-
-TEST_F(AsyncIOTest, testSplicePoolWrite) {
- aio_pool_splice_init();
- char buf[TEST_PACKET_SIZE * POOL_COUNT + 1];
- buf[TEST_PACKET_SIZE * POOL_COUNT] = '\0';
-
- for (int i = 0; i < POOL_COUNT; i++) {
- int pipeFd[2];
- EXPECT_EQ(pipe(pipeFd), 0);
- EXPECT_EQ(write(pipeFd[1], dummyDataStr.c_str(), TEST_PACKET_SIZE), TEST_PACKET_SIZE);
- struct aiocb *aiop = new struct aiocb;
- aiop->aio_fildes = pipeFd[0];
- aiop->aio_sink = dummy_file.fd;
- aiop->aio_offset = i * TEST_PACKET_SIZE;
- aiop->aio_nbytes = TEST_PACKET_SIZE;
- EXPECT_EQ(aio_pool_write(aiop), 0);
- }
- aio_pool_end();
- EXPECT_EQ(read(dummy_file.fd, buf, TEST_PACKET_SIZE * POOL_COUNT), TEST_PACKET_SIZE * POOL_COUNT);
-
- std::stringstream ss;
- for (int i = 0; i < POOL_COUNT; i++)
- ss << dummyDataStr;
-
- EXPECT_STREQ(buf, ss.str().c_str());
-}
-
-} // namespace android
diff --git a/media/mtp/tests/MtpFfsHandle_test.cpp b/media/mtp/tests/MtpFfsHandle_test.cpp
index 554f867..8d7301d 100644
--- a/media/mtp/tests/MtpFfsHandle_test.cpp
+++ b/media/mtp/tests/MtpFfsHandle_test.cpp
@@ -26,12 +26,11 @@
#include <utils/Log.h>
#include "MtpFfsHandle.h"
+#include "MtpFfsCompatHandle.h"
namespace android {
-constexpr int MAX_FILE_CHUNK_SIZE = 3 * 1024 * 1024;
-
-constexpr int TEST_PACKET_SIZE = 512;
+constexpr int TEST_PACKET_SIZE = 500;
constexpr int SMALL_MULT = 30;
constexpr int MED_MULT = 510;
@@ -43,17 +42,19 @@
"-2.0\n *\n * Unless required by applicable law or agreed to in writing, s"
"oftware\n * distributed under the License is distributed on an \"AS IS\" "
"BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express o"
- "r implied.\n * Se";
+ "r im";
/**
* Functional tests for the MtpFfsHandle class. Ensures header and data integrity
* by mocking ffs endpoints as pipes to capture input / output.
*/
+template <class T>
class MtpFfsHandleTest : public ::testing::Test {
protected:
- std::unique_ptr<IMtpHandle> handle;
+ std::unique_ptr<MtpFfsHandle> handle;
// Pipes for reading endpoint data
+ android::base::unique_fd control;
android::base::unique_fd bulk_in;
android::base::unique_fd bulk_out;
android::base::unique_fd intr;
@@ -62,88 +63,144 @@
MtpFfsHandleTest() {
int fd[2];
- handle = std::unique_ptr<IMtpHandle>(get_ffs_handle());
- MtpFfsHandle *ffs_handle = static_cast<MtpFfsHandle*>(handle.get());
- EXPECT_TRUE(ffs_handle != NULL);
+ handle = std::make_unique<T>();
+
+ EXPECT_EQ(pipe(fd), 0);
+ handle->mControl.reset(fd[0]);
+ control.reset(fd[1]);
EXPECT_EQ(pipe(fd), 0);
EXPECT_EQ(fcntl(fd[0], F_SETPIPE_SZ, 1048576), 1048576);
bulk_in.reset(fd[0]);
- ffs_handle->mBulkIn.reset(fd[1]);
+ handle->mBulkIn.reset(fd[1]);
EXPECT_EQ(pipe(fd), 0);
EXPECT_EQ(fcntl(fd[0], F_SETPIPE_SZ, 1048576), 1048576);
bulk_out.reset(fd[1]);
- ffs_handle->mBulkOut.reset(fd[0]);
+ handle->mBulkOut.reset(fd[0]);
EXPECT_EQ(pipe(fd), 0);
intr.reset(fd[0]);
- ffs_handle->mIntr.reset(fd[1]);
+ handle->mIntr.reset(fd[1]);
- ffs_handle->mBuffer1.resize(MAX_FILE_CHUNK_SIZE);
- ffs_handle->mBuffer2.resize(MAX_FILE_CHUNK_SIZE);
+ handle->start();
}
- ~MtpFfsHandleTest() {}
+ ~MtpFfsHandleTest() {
+ handle->close();
+ }
};
-TEST_F(MtpFfsHandleTest, testRead) {
- EXPECT_EQ(write(bulk_out, dummyDataStr.c_str(), TEST_PACKET_SIZE), TEST_PACKET_SIZE);
+typedef ::testing::Types<MtpFfsHandle, MtpFfsCompatHandle> mtpHandles;
+TYPED_TEST_CASE(MtpFfsHandleTest, mtpHandles);
+
+TYPED_TEST(MtpFfsHandleTest, testRead) {
+ EXPECT_EQ(write(this->bulk_out, dummyDataStr.c_str(), TEST_PACKET_SIZE), TEST_PACKET_SIZE);
char buf[TEST_PACKET_SIZE + 1];
buf[TEST_PACKET_SIZE] = '\0';
- EXPECT_EQ(handle->read(buf, TEST_PACKET_SIZE), TEST_PACKET_SIZE);
+ EXPECT_EQ(this->handle->read(buf, TEST_PACKET_SIZE), TEST_PACKET_SIZE);
EXPECT_STREQ(buf, dummyDataStr.c_str());
}
-TEST_F(MtpFfsHandleTest, testWrite) {
+TYPED_TEST(MtpFfsHandleTest, testWrite) {
char buf[TEST_PACKET_SIZE + 1];
buf[TEST_PACKET_SIZE] = '\0';
- EXPECT_EQ(handle->write(dummyDataStr.c_str(), TEST_PACKET_SIZE), TEST_PACKET_SIZE);
- EXPECT_EQ(read(bulk_in, buf, TEST_PACKET_SIZE), TEST_PACKET_SIZE);
+ EXPECT_EQ(this->handle->write(dummyDataStr.c_str(), TEST_PACKET_SIZE), TEST_PACKET_SIZE);
+ EXPECT_EQ(read(this->bulk_in, buf, TEST_PACKET_SIZE), TEST_PACKET_SIZE);
EXPECT_STREQ(buf, dummyDataStr.c_str());
}
-TEST_F(MtpFfsHandleTest, testReceiveFileSmall) {
+TYPED_TEST(MtpFfsHandleTest, testReceiveFileEmpty) {
+ std::stringstream ss;
+ mtp_file_range mfr;
+ int size = 0;
+ char buf[size + 1];
+ buf[size] = '\0';
+
+ mfr.offset = 0;
+ mfr.length = size;
+ mfr.fd = this->dummy_file.fd;
+
+ EXPECT_EQ(write(this->bulk_out, ss.str().c_str(), size), size);
+ EXPECT_EQ(this->handle->receiveFile(mfr, false), 0);
+
+ EXPECT_EQ(read(this->dummy_file.fd, buf, size), size);
+}
+
+TYPED_TEST(MtpFfsHandleTest, testReceiveFileSmall) {
std::stringstream ss;
mtp_file_range mfr;
int size = TEST_PACKET_SIZE * SMALL_MULT;
char buf[size + 1];
buf[size] = '\0';
+ mfr.offset = 0;
mfr.length = size;
- mfr.fd = dummy_file.fd;
+ mfr.fd = this->dummy_file.fd;
for (int i = 0; i < SMALL_MULT; i++)
ss << dummyDataStr;
- EXPECT_EQ(write(bulk_out, ss.str().c_str(), size), size);
- EXPECT_EQ(handle->receiveFile(mfr, false), 0);
+ EXPECT_EQ(write(this->bulk_out, ss.str().c_str(), size), size);
+ EXPECT_EQ(this->handle->receiveFile(mfr, false), 0);
- EXPECT_EQ(read(dummy_file.fd, buf, size), size);
+ EXPECT_EQ(read(this->dummy_file.fd, buf, size), size);
EXPECT_STREQ(buf, ss.str().c_str());
}
-TEST_F(MtpFfsHandleTest, testReceiveFileMed) {
+TYPED_TEST(MtpFfsHandleTest, testReceiveFileMed) {
std::stringstream ss;
mtp_file_range mfr;
int size = TEST_PACKET_SIZE * MED_MULT;
char buf[size + 1];
buf[size] = '\0';
+ mfr.offset = 0;
mfr.length = size;
- mfr.fd = dummy_file.fd;
+ mfr.fd = this->dummy_file.fd;
for (int i = 0; i < MED_MULT; i++)
ss << dummyDataStr;
- EXPECT_EQ(write(bulk_out, ss.str().c_str(), size), size);
- EXPECT_EQ(handle->receiveFile(mfr, false), 0);
+ EXPECT_EQ(write(this->bulk_out, ss.str().c_str(), size), size);
+ EXPECT_EQ(this->handle->receiveFile(mfr, false), 0);
- EXPECT_EQ(read(dummy_file.fd, buf, size), size);
+ EXPECT_EQ(read(this->dummy_file.fd, buf, size), size);
EXPECT_STREQ(buf, ss.str().c_str());
}
-TEST_F(MtpFfsHandleTest, testSendFileSmall) {
+TYPED_TEST(MtpFfsHandleTest, testReceiveFileMedPartial) {
+ std::stringstream ss;
+ mtp_file_range mfr;
+ int size = TEST_PACKET_SIZE * MED_MULT;
+ char buf[size + 1];
+ buf[size] = '\0';
+
+ mfr.fd = this->dummy_file.fd;
+ for (int i = 0; i < MED_MULT; i++)
+ ss << dummyDataStr;
+
+ EXPECT_EQ(write(this->bulk_out, ss.str().c_str(), size), size);
+
+ std::random_device rd;
+ std::mt19937 gen(rd());
+ std::uniform_int_distribution<> dis(1, TEST_PACKET_SIZE);
+ int offset = 0;
+ while (offset != size) {
+ mfr.offset = offset;
+ int length = std::min(size - offset, dis(gen));
+ mfr.length = length;
+
+ EXPECT_EQ(this->handle->receiveFile(mfr, false), 0);
+ offset += length;
+ }
+
+ EXPECT_EQ(read(this->dummy_file.fd, buf, size), size);
+
+ EXPECT_STREQ(buf, ss.str().c_str());
+}
+
+TYPED_TEST(MtpFfsHandleTest, testSendFileSmall) {
std::stringstream ss;
mtp_file_range mfr;
mfr.command = 42;
@@ -154,14 +211,14 @@
buf[size + sizeof(mtp_data_header)] = '\0';
mfr.length = size;
- mfr.fd = dummy_file.fd;
+ mfr.fd = this->dummy_file.fd;
for (int i = 0; i < SMALL_MULT; i++)
ss << dummyDataStr;
- EXPECT_EQ(write(dummy_file.fd, ss.str().c_str(), size), size);
- EXPECT_EQ(handle->sendFile(mfr), 0);
+ EXPECT_EQ(write(this->dummy_file.fd, ss.str().c_str(), size), size);
+ EXPECT_EQ(this->handle->sendFile(mfr), 0);
- EXPECT_EQ(read(bulk_in, buf, size + sizeof(mtp_data_header)),
+ EXPECT_EQ(read(this->bulk_in, buf, size + sizeof(mtp_data_header)),
static_cast<long>(size + sizeof(mtp_data_header)));
struct mtp_data_header *header = reinterpret_cast<struct mtp_data_header*>(buf);
@@ -172,7 +229,7 @@
EXPECT_EQ(header->transaction_id, static_cast<unsigned int>(1337));
}
-TEST_F(MtpFfsHandleTest, testSendFileMed) {
+TYPED_TEST(MtpFfsHandleTest, testSendFileMed) {
std::stringstream ss;
mtp_file_range mfr;
mfr.command = 42;
@@ -183,14 +240,14 @@
buf[size + sizeof(mtp_data_header)] = '\0';
mfr.length = size;
- mfr.fd = dummy_file.fd;
+ mfr.fd = this->dummy_file.fd;
for (int i = 0; i < MED_MULT; i++)
ss << dummyDataStr;
- EXPECT_EQ(write(dummy_file.fd, ss.str().c_str(), size), size);
- EXPECT_EQ(handle->sendFile(mfr), 0);
+ EXPECT_EQ(write(this->dummy_file.fd, ss.str().c_str(), size), size);
+ EXPECT_EQ(this->handle->sendFile(mfr), 0);
- EXPECT_EQ(read(bulk_in, buf, size + sizeof(mtp_data_header)),
+ EXPECT_EQ(read(this->bulk_in, buf, size + sizeof(mtp_data_header)),
static_cast<long>(size + sizeof(mtp_data_header)));
struct mtp_data_header *header = reinterpret_cast<struct mtp_data_header*>(buf);
@@ -201,10 +258,10 @@
EXPECT_EQ(header->transaction_id, static_cast<unsigned int>(1337));
}
-TEST_F(MtpFfsHandleTest, testSendFileMedPartial) {
+TYPED_TEST(MtpFfsHandleTest, testSendFileMedPartial) {
std::stringstream ss;
mtp_file_range mfr;
- mfr.fd = dummy_file.fd;
+ mfr.fd = this->dummy_file.fd;
mfr.command = 42;
mfr.transaction_id = 1337;
int size = TEST_PACKET_SIZE * MED_MULT;
@@ -214,7 +271,7 @@
for (int i = 0; i < MED_MULT; i++)
ss << dummyDataStr;
- EXPECT_EQ(write(dummy_file.fd, ss.str().c_str(), size), size);
+ EXPECT_EQ(write(this->dummy_file.fd, ss.str().c_str(), size), size);
std::random_device rd;
std::mt19937 gen(rd());
@@ -225,9 +282,9 @@
int length = std::min(size - offset, dis(gen));
mfr.length = length;
char temp_buf[length + sizeof(mtp_data_header)];
- EXPECT_EQ(handle->sendFile(mfr), 0);
+ EXPECT_EQ(this->handle->sendFile(mfr), 0);
- EXPECT_EQ(read(bulk_in, temp_buf, length + sizeof(mtp_data_header)),
+ EXPECT_EQ(read(this->bulk_in, temp_buf, length + sizeof(mtp_data_header)),
static_cast<long>(length + sizeof(mtp_data_header)));
struct mtp_data_header *header = reinterpret_cast<struct mtp_data_header*>(temp_buf);
@@ -241,7 +298,7 @@
EXPECT_STREQ(buf, ss.str().c_str());
}
-TEST_F(MtpFfsHandleTest, testSendFileEmpty) {
+TYPED_TEST(MtpFfsHandleTest, testSendFileEmpty) {
mtp_file_range mfr;
mfr.command = 42;
mfr.transaction_id = 1337;
@@ -251,11 +308,11 @@
buf[size + sizeof(mtp_data_header)] = '\0';
mfr.length = size;
- mfr.fd = dummy_file.fd;
+ mfr.fd = this->dummy_file.fd;
- EXPECT_EQ(handle->sendFile(mfr), 0);
+ EXPECT_EQ(this->handle->sendFile(mfr), 0);
- EXPECT_EQ(read(bulk_in, buf, size + sizeof(mtp_data_header)),
+ EXPECT_EQ(read(this->bulk_in, buf, size + sizeof(mtp_data_header)),
static_cast<long>(size + sizeof(mtp_data_header)));
struct mtp_data_header *header = reinterpret_cast<struct mtp_data_header*>(buf);
@@ -265,15 +322,15 @@
EXPECT_EQ(header->transaction_id, static_cast<unsigned int>(1337));
}
-TEST_F(MtpFfsHandleTest, testSendEvent) {
+TYPED_TEST(MtpFfsHandleTest, testSendEvent) {
struct mtp_event event;
event.length = TEST_PACKET_SIZE;
event.data = const_cast<char*>(dummyDataStr.c_str());
char buf[TEST_PACKET_SIZE + 1];
buf[TEST_PACKET_SIZE] = '\0';
- handle->sendEvent(event);
- read(intr, buf, TEST_PACKET_SIZE);
+ this->handle->sendEvent(event);
+ read(this->intr, buf, TEST_PACKET_SIZE);
EXPECT_STREQ(buf, dummyDataStr.c_str());
}
diff --git a/media/mtp/tests/PosixAsyncIO_test.cpp b/media/mtp/tests/PosixAsyncIO_test.cpp
new file mode 100644
index 0000000..63b9a35
--- /dev/null
+++ b/media/mtp/tests/PosixAsyncIO_test.cpp
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#define LOG_TAG "PosixAsyncIO_test.cpp"
+
+#include <android-base/test_utils.h>
+#include <fcntl.h>
+#include <gtest/gtest.h>
+#include <string>
+#include <unistd.h>
+#include <utils/Log.h>
+
+#include "PosixAsyncIO.h"
+
+namespace android {
+
+constexpr int TEST_PACKET_SIZE = 512;
+
+static const std::string dummyDataStr =
+ "/*\n * Copyright 2015 The Android Open Source Project\n *\n * Licensed un"
+ "der the Apache License, Version 2.0 (the \"License\");\n * you may not us"
+ "e this file except in compliance with the License.\n * You may obtain a c"
+ "opy of the License at\n *\n * http://www.apache.org/licenses/LICENSE"
+ "-2.0\n *\n * Unless required by applicable law or agreed to in writing, s"
+ "oftware\n * distributed under the License is distributed on an \"AS IS\" "
+ "BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express o"
+ "r implied.\n * Se";
+
+
+class PosixAsyncIOTest : public ::testing::Test {
+protected:
+ TemporaryFile dummy_file;
+
+ PosixAsyncIOTest() {}
+ ~PosixAsyncIOTest() {}
+};
+
+TEST_F(PosixAsyncIOTest, testRead) {
+ char buf[TEST_PACKET_SIZE + 1];
+ buf[TEST_PACKET_SIZE] = '\0';
+ EXPECT_EQ(write(dummy_file.fd, dummyDataStr.c_str(), TEST_PACKET_SIZE), TEST_PACKET_SIZE);
+ struct aiocb aio;
+ struct aiocb *aiol[] = {&aio};
+ aio.aio_fildes = dummy_file.fd;
+ aio.aio_buf = buf;
+ aio.aio_offset = 0;
+ aio.aio_nbytes = TEST_PACKET_SIZE;
+
+ EXPECT_EQ(aio_read(&aio), 0);
+ EXPECT_EQ(aio_suspend(aiol, 1, nullptr), 0);
+ EXPECT_EQ(aio_return(&aio), TEST_PACKET_SIZE);
+ EXPECT_STREQ(buf, dummyDataStr.c_str());
+}
+
+TEST_F(PosixAsyncIOTest, testWrite) {
+ char buf[TEST_PACKET_SIZE + 1];
+ buf[TEST_PACKET_SIZE] = '\0';
+ struct aiocb aio;
+ struct aiocb *aiol[] = {&aio};
+ aio.aio_fildes = dummy_file.fd;
+ aio.aio_buf = const_cast<char*>(dummyDataStr.c_str());
+ aio.aio_offset = 0;
+ aio.aio_nbytes = TEST_PACKET_SIZE;
+
+ EXPECT_EQ(aio_write(&aio), 0);
+ EXPECT_EQ(aio_suspend(aiol, 1, nullptr), 0);
+ EXPECT_EQ(aio_return(&aio), TEST_PACKET_SIZE);
+ EXPECT_EQ(read(dummy_file.fd, buf, TEST_PACKET_SIZE), TEST_PACKET_SIZE);
+ EXPECT_STREQ(buf, dummyDataStr.c_str());
+}
+
+TEST_F(PosixAsyncIOTest, testError) {
+ char buf[TEST_PACKET_SIZE + 1];
+ buf[TEST_PACKET_SIZE] = '\0';
+ struct aiocb aio;
+ struct aiocb *aiol[] = {&aio};
+ aio.aio_fildes = -1;
+ aio.aio_buf = const_cast<char*>(dummyDataStr.c_str());
+ aio.aio_offset = 0;
+ aio.aio_nbytes = TEST_PACKET_SIZE;
+
+ EXPECT_EQ(aio_write(&aio), 0);
+ EXPECT_EQ(aio_suspend(aiol, 1, nullptr), 0);
+ EXPECT_EQ(aio_return(&aio), -1);
+ EXPECT_EQ(aio_error(&aio), EBADF);
+}
+
+} // namespace android