MtpFfsHandle_test.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 "MtpFfsHandle_test.cpp"
  17. #include <android-base/unique_fd.h>
  18. #include <android-base/test_utils.h>
  19. #include <fcntl.h>
  20. #include <gtest/gtest.h>
  21. #include <memory>
  22. #include <random>
  23. #include <string>
  24. #include <unistd.h>
  25. #include <log/log.h>
  26. #include "MtpDescriptors.h"
  27. #include "MtpFfsHandle.h"
  28. #include "MtpFfsCompatHandle.h"
  29. namespace android {
  30. constexpr int TEST_PACKET_SIZE = 500;
  31. constexpr int SMALL_MULT = 30;
  32. constexpr int MED_MULT = 510;
  33. static const std::string dummyDataStr =
  34. "/*\n * Copyright 2015 The Android Open Source Project\n *\n * Licensed un"
  35. "der the Apache License, Version 2.0 (the \"License\");\n * you may not us"
  36. "e this file except in compliance with the License.\n * You may obtain a c"
  37. "opy of the License at\n *\n * http://www.apache.org/licenses/LICENSE"
  38. "-2.0\n *\n * Unless required by applicable law or agreed to in writing, s"
  39. "oftware\n * distributed under the License is distributed on an \"AS IS\" "
  40. "BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express o"
  41. "r im";
  42. /**
  43. * Functional tests for the MtpFfsHandle class. Ensures header and data integrity
  44. * by mocking ffs endpoints as pipes to capture input / output.
  45. */
  46. template <class T>
  47. class MtpFfsHandleTest : public ::testing::Test {
  48. protected:
  49. std::unique_ptr<MtpFfsHandle> handle;
  50. // Pipes for reading endpoint data
  51. android::base::unique_fd control;
  52. android::base::unique_fd bulk_in;
  53. android::base::unique_fd bulk_out;
  54. android::base::unique_fd intr;
  55. TemporaryFile dummy_file;
  56. MtpFfsHandleTest() {
  57. int fd[2];
  58. handle = std::make_unique<T>(-1);
  59. EXPECT_EQ(pipe(fd), 0);
  60. control.reset(fd[0]);
  61. handle->mControl.reset(fd[1]);
  62. EXPECT_EQ(pipe(fd), 0);
  63. EXPECT_EQ(fcntl(fd[0], F_SETPIPE_SZ, 1048576), 1048576);
  64. bulk_in.reset(fd[0]);
  65. handle->mBulkIn.reset(fd[1]);
  66. EXPECT_EQ(pipe(fd), 0);
  67. EXPECT_EQ(fcntl(fd[0], F_SETPIPE_SZ, 1048576), 1048576);
  68. bulk_out.reset(fd[1]);
  69. handle->mBulkOut.reset(fd[0]);
  70. EXPECT_EQ(pipe(fd), 0);
  71. intr.reset(fd[0]);
  72. handle->mIntr.reset(fd[1]);
  73. EXPECT_EQ(handle->start(false), 0);
  74. }
  75. ~MtpFfsHandleTest() {
  76. handle->close();
  77. }
  78. };
  79. typedef ::testing::Types<MtpFfsHandle, MtpFfsCompatHandle> mtpHandles;
  80. TYPED_TEST_CASE(MtpFfsHandleTest, mtpHandles);
  81. TYPED_TEST(MtpFfsHandleTest, testMtpControl) {
  82. EXPECT_TRUE(this->handle->writeDescriptors(false));
  83. struct desc_v2 desc;
  84. struct functionfs_strings strings;
  85. EXPECT_EQ(read(this->control, &desc, sizeof(desc)), (long)sizeof(desc));
  86. EXPECT_EQ(read(this->control, &strings, sizeof(strings)), (long)sizeof(strings));
  87. EXPECT_TRUE(std::memcmp(&desc, &mtp_desc_v2, sizeof(desc)) == 0);
  88. EXPECT_TRUE(std::memcmp(&strings, &mtp_strings, sizeof(strings)) == 0);
  89. }
  90. TYPED_TEST(MtpFfsHandleTest, testPtpControl) {
  91. EXPECT_TRUE(this->handle->writeDescriptors(true));
  92. struct desc_v2 desc;
  93. struct functionfs_strings strings;
  94. EXPECT_EQ(read(this->control, &desc, sizeof(desc)), (long)sizeof(desc));
  95. EXPECT_EQ(read(this->control, &strings, sizeof(strings)), (long)sizeof(strings));
  96. EXPECT_TRUE(std::memcmp(&desc, &ptp_desc_v2, sizeof(desc)) == 0);
  97. EXPECT_TRUE(std::memcmp(&strings, &mtp_strings, sizeof(strings)) == 0);
  98. }
  99. TYPED_TEST(MtpFfsHandleTest, testRead) {
  100. EXPECT_EQ(write(this->bulk_out, dummyDataStr.c_str(), TEST_PACKET_SIZE), TEST_PACKET_SIZE);
  101. char buf[TEST_PACKET_SIZE + 1];
  102. buf[TEST_PACKET_SIZE] = '\0';
  103. EXPECT_EQ(this->handle->read(buf, TEST_PACKET_SIZE), TEST_PACKET_SIZE);
  104. EXPECT_STREQ(buf, dummyDataStr.c_str());
  105. }
  106. TYPED_TEST(MtpFfsHandleTest, testReadLarge) {
  107. std::stringstream ss;
  108. int size = TEST_PACKET_SIZE * MED_MULT;
  109. char buf[size + 1];
  110. buf[size] = '\0';
  111. for (int i = 0; i < MED_MULT; i++)
  112. ss << dummyDataStr;
  113. EXPECT_EQ(write(this->bulk_out, ss.str().c_str(), size), size);
  114. EXPECT_EQ(this->handle->read(buf, size), size);
  115. EXPECT_STREQ(buf, ss.str().c_str());
  116. }
  117. TYPED_TEST(MtpFfsHandleTest, testWrite) {
  118. char buf[TEST_PACKET_SIZE + 1];
  119. buf[TEST_PACKET_SIZE] = '\0';
  120. EXPECT_EQ(this->handle->write(dummyDataStr.c_str(), TEST_PACKET_SIZE), TEST_PACKET_SIZE);
  121. EXPECT_EQ(read(this->bulk_in, buf, TEST_PACKET_SIZE), TEST_PACKET_SIZE);
  122. EXPECT_STREQ(buf, dummyDataStr.c_str());
  123. }
  124. TYPED_TEST(MtpFfsHandleTest, testWriteLarge) {
  125. std::stringstream ss;
  126. int size = TEST_PACKET_SIZE * MED_MULT;
  127. char buf[size + 1];
  128. buf[size] = '\0';
  129. for (int i = 0; i < MED_MULT; i++)
  130. ss << dummyDataStr;
  131. EXPECT_EQ(this->handle->write(ss.str().c_str(), size), size);
  132. EXPECT_EQ(read(this->bulk_in, buf, size), size);
  133. EXPECT_STREQ(buf, ss.str().c_str());
  134. }
  135. TYPED_TEST(MtpFfsHandleTest, testReceiveFileEmpty) {
  136. std::stringstream ss;
  137. mtp_file_range mfr;
  138. int size = 0;
  139. char buf[size + 1];
  140. buf[size] = '\0';
  141. mfr.offset = 0;
  142. mfr.length = size;
  143. mfr.fd = this->dummy_file.fd;
  144. EXPECT_EQ(write(this->bulk_out, ss.str().c_str(), size), size);
  145. EXPECT_EQ(this->handle->receiveFile(mfr, false), 0);
  146. EXPECT_EQ(read(this->dummy_file.fd, buf, size), size);
  147. }
  148. TYPED_TEST(MtpFfsHandleTest, testReceiveFileSmall) {
  149. std::stringstream ss;
  150. mtp_file_range mfr;
  151. int size = TEST_PACKET_SIZE * SMALL_MULT;
  152. char buf[size + 1];
  153. buf[size] = '\0';
  154. mfr.offset = 0;
  155. mfr.length = size;
  156. mfr.fd = this->dummy_file.fd;
  157. for (int i = 0; i < SMALL_MULT; i++)
  158. ss << dummyDataStr;
  159. EXPECT_EQ(write(this->bulk_out, ss.str().c_str(), size), size);
  160. EXPECT_EQ(this->handle->receiveFile(mfr, false), 0);
  161. EXPECT_EQ(read(this->dummy_file.fd, buf, size), size);
  162. EXPECT_STREQ(buf, ss.str().c_str());
  163. }
  164. TYPED_TEST(MtpFfsHandleTest, testReceiveFileMed) {
  165. std::stringstream ss;
  166. mtp_file_range mfr;
  167. int size = TEST_PACKET_SIZE * MED_MULT;
  168. char buf[size + 1];
  169. buf[size] = '\0';
  170. mfr.offset = 0;
  171. mfr.length = size;
  172. mfr.fd = this->dummy_file.fd;
  173. for (int i = 0; i < MED_MULT; i++)
  174. ss << dummyDataStr;
  175. EXPECT_EQ(write(this->bulk_out, ss.str().c_str(), size), size);
  176. EXPECT_EQ(this->handle->receiveFile(mfr, false), 0);
  177. EXPECT_EQ(read(this->dummy_file.fd, buf, size), size);
  178. EXPECT_STREQ(buf, ss.str().c_str());
  179. }
  180. TYPED_TEST(MtpFfsHandleTest, testReceiveFileMedPartial) {
  181. std::stringstream ss;
  182. mtp_file_range mfr;
  183. int size = TEST_PACKET_SIZE * MED_MULT;
  184. char buf[size + 1];
  185. buf[size] = '\0';
  186. mfr.fd = this->dummy_file.fd;
  187. for (int i = 0; i < MED_MULT; i++)
  188. ss << dummyDataStr;
  189. EXPECT_EQ(write(this->bulk_out, ss.str().c_str(), size), size);
  190. std::random_device rd;
  191. std::mt19937 gen(rd());
  192. std::uniform_int_distribution<> dis(1, TEST_PACKET_SIZE);
  193. int offset = 0;
  194. while (offset != size) {
  195. mfr.offset = offset;
  196. int length = std::min(size - offset, dis(gen));
  197. mfr.length = length;
  198. EXPECT_EQ(this->handle->receiveFile(mfr, false), 0);
  199. offset += length;
  200. }
  201. EXPECT_EQ(read(this->dummy_file.fd, buf, size), size);
  202. EXPECT_STREQ(buf, ss.str().c_str());
  203. }
  204. TYPED_TEST(MtpFfsHandleTest, testSendFileSmall) {
  205. std::stringstream ss;
  206. mtp_file_range mfr;
  207. mfr.command = 42;
  208. mfr.transaction_id = 1337;
  209. mfr.offset = 0;
  210. int size = TEST_PACKET_SIZE * SMALL_MULT;
  211. char buf[size + sizeof(mtp_data_header) + 1];
  212. buf[size + sizeof(mtp_data_header)] = '\0';
  213. mfr.length = size;
  214. mfr.fd = this->dummy_file.fd;
  215. for (int i = 0; i < SMALL_MULT; i++)
  216. ss << dummyDataStr;
  217. EXPECT_EQ(write(this->dummy_file.fd, ss.str().c_str(), size), size);
  218. EXPECT_EQ(this->handle->sendFile(mfr), 0);
  219. EXPECT_EQ(read(this->bulk_in, buf, size + sizeof(mtp_data_header)),
  220. static_cast<long>(size + sizeof(mtp_data_header)));
  221. struct mtp_data_header *header = reinterpret_cast<struct mtp_data_header*>(buf);
  222. EXPECT_STREQ(buf + sizeof(mtp_data_header), ss.str().c_str());
  223. EXPECT_EQ(header->length, static_cast<unsigned int>(size + sizeof(mtp_data_header)));
  224. EXPECT_EQ(header->type, static_cast<unsigned int>(2));
  225. EXPECT_EQ(header->command, static_cast<unsigned int>(42));
  226. EXPECT_EQ(header->transaction_id, static_cast<unsigned int>(1337));
  227. }
  228. TYPED_TEST(MtpFfsHandleTest, testSendFileMed) {
  229. std::stringstream ss;
  230. mtp_file_range mfr;
  231. mfr.command = 42;
  232. mfr.transaction_id = 1337;
  233. mfr.offset = 0;
  234. int size = TEST_PACKET_SIZE * MED_MULT;
  235. char buf[size + sizeof(mtp_data_header) + 1];
  236. buf[size + sizeof(mtp_data_header)] = '\0';
  237. mfr.length = size;
  238. mfr.fd = this->dummy_file.fd;
  239. for (int i = 0; i < MED_MULT; i++)
  240. ss << dummyDataStr;
  241. EXPECT_EQ(write(this->dummy_file.fd, ss.str().c_str(), size), size);
  242. EXPECT_EQ(this->handle->sendFile(mfr), 0);
  243. EXPECT_EQ(read(this->bulk_in, buf, size + sizeof(mtp_data_header)),
  244. static_cast<long>(size + sizeof(mtp_data_header)));
  245. struct mtp_data_header *header = reinterpret_cast<struct mtp_data_header*>(buf);
  246. EXPECT_STREQ(buf + sizeof(mtp_data_header), ss.str().c_str());
  247. EXPECT_EQ(header->length, static_cast<unsigned int>(size + sizeof(mtp_data_header)));
  248. EXPECT_EQ(header->type, static_cast<unsigned int>(2));
  249. EXPECT_EQ(header->command, static_cast<unsigned int>(42));
  250. EXPECT_EQ(header->transaction_id, static_cast<unsigned int>(1337));
  251. }
  252. TYPED_TEST(MtpFfsHandleTest, testSendFileMedPartial) {
  253. std::stringstream ss;
  254. mtp_file_range mfr;
  255. mfr.fd = this->dummy_file.fd;
  256. mfr.command = 42;
  257. mfr.transaction_id = 1337;
  258. int size = TEST_PACKET_SIZE * MED_MULT;
  259. char buf[size + 1];
  260. buf[size] = '\0';
  261. for (int i = 0; i < MED_MULT; i++)
  262. ss << dummyDataStr;
  263. EXPECT_EQ(write(this->dummy_file.fd, ss.str().c_str(), size), size);
  264. std::random_device rd;
  265. std::mt19937 gen(rd());
  266. std::uniform_int_distribution<> dis(1, TEST_PACKET_SIZE);
  267. int offset = 0;
  268. while (offset != size) {
  269. mfr.offset = offset;
  270. int length = std::min(size - offset, dis(gen));
  271. mfr.length = length;
  272. char temp_buf[length + sizeof(mtp_data_header)];
  273. EXPECT_EQ(this->handle->sendFile(mfr), 0);
  274. EXPECT_EQ(read(this->bulk_in, temp_buf, length + sizeof(mtp_data_header)),
  275. static_cast<long>(length + sizeof(mtp_data_header)));
  276. struct mtp_data_header *header = reinterpret_cast<struct mtp_data_header*>(temp_buf);
  277. EXPECT_EQ(header->length, static_cast<unsigned int>(length + sizeof(mtp_data_header)));
  278. EXPECT_EQ(header->type, static_cast<unsigned int>(2));
  279. EXPECT_EQ(header->command, static_cast<unsigned int>(42));
  280. EXPECT_EQ(header->transaction_id, static_cast<unsigned int>(1337));
  281. memcpy(buf + offset, temp_buf + sizeof(mtp_data_header), length);
  282. offset += length;
  283. }
  284. EXPECT_STREQ(buf, ss.str().c_str());
  285. }
  286. TYPED_TEST(MtpFfsHandleTest, testSendFileEmpty) {
  287. mtp_file_range mfr;
  288. mfr.command = 42;
  289. mfr.transaction_id = 1337;
  290. mfr.offset = 0;
  291. int size = 0;
  292. char buf[size + sizeof(mtp_data_header) + 1];
  293. buf[size + sizeof(mtp_data_header)] = '\0';
  294. mfr.length = size;
  295. mfr.fd = this->dummy_file.fd;
  296. EXPECT_EQ(this->handle->sendFile(mfr), 0);
  297. EXPECT_EQ(read(this->bulk_in, buf, size + sizeof(mtp_data_header)),
  298. static_cast<long>(size + sizeof(mtp_data_header)));
  299. struct mtp_data_header *header = reinterpret_cast<struct mtp_data_header*>(buf);
  300. EXPECT_EQ(header->length, static_cast<unsigned int>(size + sizeof(mtp_data_header)));
  301. EXPECT_EQ(header->type, static_cast<unsigned int>(2));
  302. EXPECT_EQ(header->command, static_cast<unsigned int>(42));
  303. EXPECT_EQ(header->transaction_id, static_cast<unsigned int>(1337));
  304. }
  305. TYPED_TEST(MtpFfsHandleTest, testSendEvent) {
  306. struct mtp_event event;
  307. event.length = TEST_PACKET_SIZE;
  308. event.data = const_cast<char*>(dummyDataStr.c_str());
  309. char buf[TEST_PACKET_SIZE + 1];
  310. buf[TEST_PACKET_SIZE] = '\0';
  311. this->handle->sendEvent(event);
  312. read(this->intr, buf, TEST_PACKET_SIZE);
  313. EXPECT_STREQ(buf, dummyDataStr.c_str());
  314. }
  315. } // namespace android