blob: b5110412b6d7445240cb5edadf666e71c460f9ed [file] [log] [blame]
Jerry Zhang487be612016-10-24 12:10:41 -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 "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 <string>
24#include <unistd.h>
25#include <utils/Log.h>
26
27#include "MtpFfsHandle.h"
28
29namespace android {
30
31constexpr int TEST_PACKET_SIZE = 512;
32constexpr int SMALL_MULT = 30;
33constexpr int MED_MULT = 510;
34
35static const std::string dummyDataStr =
36 "/*\n * Copyright 2015 The Android Open Source Project\n *\n * Licensed un"
37 "der the Apache License, Version 2.0 (the \"License\");\n * you may not us"
38 "e this file except in compliance with the License.\n * You may obtain a c"
39 "opy of the License at\n *\n * http://www.apache.org/licenses/LICENSE"
40 "-2.0\n *\n * Unless required by applicable law or agreed to in writing, s"
41 "oftware\n * distributed under the License is distributed on an \"AS IS\" "
42 "BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express o"
43 "r implied.\n * Se";
44
45class MtpFfsHandleTest : public ::testing::Test {
46protected:
47 std::unique_ptr<IMtpHandle> handle;
48
49 // Pipes for reading endpoint data
50 android::base::unique_fd bulk_in;
51 android::base::unique_fd bulk_out;
52 android::base::unique_fd intr;
53
54 TemporaryFile dummy_file;
55
56 MtpFfsHandleTest() {
57 int fd[2];
58 handle = std::unique_ptr<IMtpHandle>(get_ffs_handle());
59 MtpFfsHandle *ffs_handle = static_cast<MtpFfsHandle*>(handle.get());
60 EXPECT_TRUE(ffs_handle != NULL);
61
62 EXPECT_EQ(pipe(fd), 0);
63 EXPECT_EQ(fcntl(fd[0], F_SETPIPE_SZ, 1048576), 1048576);
64 bulk_in.reset(fd[0]);
65 ffs_handle->mBulkIn.reset(fd[1]);
66
67 EXPECT_EQ(pipe(fd), 0);
68 EXPECT_EQ(fcntl(fd[0], F_SETPIPE_SZ, 1048576), 1048576);
69 bulk_out.reset(fd[1]);
70 ffs_handle->mBulkOut.reset(fd[0]);
71
72 EXPECT_EQ(pipe(fd), 0);
73 intr.reset(fd[0]);
74 ffs_handle->mIntr.reset(fd[1]);
75 }
76
77 ~MtpFfsHandleTest() {}
78};
79
80TEST_F(MtpFfsHandleTest, testRead) {
81 EXPECT_EQ(write(bulk_out, dummyDataStr.c_str(), TEST_PACKET_SIZE), TEST_PACKET_SIZE);
82 char buf[TEST_PACKET_SIZE + 1];
83 buf[TEST_PACKET_SIZE] = '\0';
84 EXPECT_EQ(handle->read(buf, TEST_PACKET_SIZE), TEST_PACKET_SIZE);
85 EXPECT_STREQ(buf, dummyDataStr.c_str());
86}
87
88TEST_F(MtpFfsHandleTest, testWrite) {
89 char buf[TEST_PACKET_SIZE + 1];
90 buf[TEST_PACKET_SIZE] = '\0';
91 EXPECT_EQ(handle->write(dummyDataStr.c_str(), TEST_PACKET_SIZE), TEST_PACKET_SIZE);
92 EXPECT_EQ(read(bulk_in, buf, TEST_PACKET_SIZE), TEST_PACKET_SIZE);
93 EXPECT_STREQ(buf, dummyDataStr.c_str());
94}
95
96TEST_F(MtpFfsHandleTest, testReceiveFileSmall) {
97 std::stringstream ss;
98 mtp_file_range mfr;
99 int size = TEST_PACKET_SIZE * SMALL_MULT;
100 char buf[size + 1];
101 buf[size] = '\0';
102
103 mfr.length = size;
104 mfr.fd = dummy_file.fd;
105 for (int i = 0; i < SMALL_MULT; i++)
106 ss << dummyDataStr;
107
108 EXPECT_EQ(write(bulk_out, ss.str().c_str(), size), size);
109 EXPECT_EQ(handle->receiveFile(mfr), 0);
110
111 EXPECT_EQ(read(dummy_file.fd, buf, size), size);
112
113 EXPECT_STREQ(buf, ss.str().c_str());
114}
115
116TEST_F(MtpFfsHandleTest, testReceiveFileMed) {
117 std::stringstream ss;
118 mtp_file_range mfr;
119 int size = TEST_PACKET_SIZE * MED_MULT;
120 char buf[size + 1];
121 buf[size] = '\0';
122
123 mfr.length = size;
124 mfr.fd = dummy_file.fd;
125 for (int i = 0; i < MED_MULT; i++)
126 ss << dummyDataStr;
127
128 EXPECT_EQ(write(bulk_out, ss.str().c_str(), size), size);
129 EXPECT_EQ(handle->receiveFile(mfr), 0);
130
131 EXPECT_EQ(read(dummy_file.fd, buf, size), size);
132
133 EXPECT_STREQ(buf, ss.str().c_str());
134}
135
136TEST_F(MtpFfsHandleTest, testSendFileSmall) {
137 std::stringstream ss;
138 mtp_file_range mfr;
139 mfr.command = 42;
140 mfr.transaction_id = 1337;
141 int size = TEST_PACKET_SIZE * SMALL_MULT;
142 char buf[size + sizeof(mtp_data_header) + 1];
143 buf[size + sizeof(mtp_data_header)] = '\0';
144
145 mfr.length = size;
146 mfr.fd = dummy_file.fd;
147 for (int i = 0; i < SMALL_MULT; i++)
148 ss << dummyDataStr;
149
150 EXPECT_EQ(write(dummy_file.fd, ss.str().c_str(), size), size);
151 EXPECT_EQ(handle->sendFile(mfr), 0);
152
153 EXPECT_EQ(read(bulk_in, buf, size + sizeof(mtp_data_header)),
154 static_cast<long>(size + sizeof(mtp_data_header)));
155
156 struct mtp_data_header *header = reinterpret_cast<struct mtp_data_header*>(buf);
157 EXPECT_STREQ(buf + sizeof(mtp_data_header), ss.str().c_str());
158 EXPECT_EQ(header->length, static_cast<unsigned int>(size + sizeof(mtp_data_header)));
159 EXPECT_EQ(header->type, static_cast<unsigned int>(2));
160 EXPECT_EQ(header->command, static_cast<unsigned int>(42));
161 EXPECT_EQ(header->transaction_id, static_cast<unsigned int>(1337));
162}
163
164TEST_F(MtpFfsHandleTest, testSendFileMed) {
165 std::stringstream ss;
166 mtp_file_range mfr;
167 mfr.command = 42;
168 mfr.transaction_id = 1337;
169 int size = TEST_PACKET_SIZE * MED_MULT;
170 char buf[size + sizeof(mtp_data_header) + 1];
171 buf[size + sizeof(mtp_data_header)] = '\0';
172
173 mfr.length = size;
174 mfr.fd = dummy_file.fd;
175 for (int i = 0; i < MED_MULT; i++)
176 ss << dummyDataStr;
177
178 EXPECT_EQ(write(dummy_file.fd, ss.str().c_str(), size), size);
179 EXPECT_EQ(handle->sendFile(mfr), 0);
180
181 EXPECT_EQ(read(bulk_in, buf, size + sizeof(mtp_data_header)),
182 static_cast<long>(size + sizeof(mtp_data_header)));
183
184 struct mtp_data_header *header = reinterpret_cast<struct mtp_data_header*>(buf);
185 EXPECT_STREQ(buf + sizeof(mtp_data_header), ss.str().c_str());
186 EXPECT_EQ(header->length, static_cast<unsigned int>(size + sizeof(mtp_data_header)));
187 EXPECT_EQ(header->type, static_cast<unsigned int>(2));
188 EXPECT_EQ(header->command, static_cast<unsigned int>(42));
189 EXPECT_EQ(header->transaction_id, static_cast<unsigned int>(1337));
190}
191
192TEST_F(MtpFfsHandleTest, testSendEvent) {
193 struct mtp_event event;
194 event.length = TEST_PACKET_SIZE;
195 event.data = const_cast<char*>(dummyDataStr.c_str());
196 char buf[TEST_PACKET_SIZE + 1];
197 buf[TEST_PACKET_SIZE] = '\0';
198
199 handle->sendEvent(event);
200 read(intr, buf, TEST_PACKET_SIZE);
201 EXPECT_STREQ(buf, dummyDataStr.c_str());
202}
203
204} // namespace android