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