1 /*
2 * Copyright (c) 2023-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "usbfn_mtp_test.h"
17
18 #include <cinttypes>
19 #include <cstdio>
20 #include <iostream>
21 #include <sstream>
22 #include <vector>
23 #include <fcntl.h>
24
25 #include "directory_ex.h"
26 #include "file_ex.h"
27 #include "hdf_log.h"
28 #include "securec.h"
29 #include "v1_0/iusb_interface.h"
30 #include "v1_0/iusbfn_mtp_interface.h"
31 #include "v1_0/usb_types.h"
32 #include "v1_0/usbfn_mtp_types.h"
33
34 #define HDF_LOG_TAG usbfn_mtp_ut
35
36 using namespace testing::ext;
37 using namespace OHOS;
38 using namespace OHOS::HDI::Usb::V1_0;
39 using namespace std;
40 using namespace OHOS::HDI::Usb::Gadget::Mtp::V1_0;
41
42 namespace {
43 constexpr int32_t SLEEP_TIME = 3;
44 constexpr int32_t MTP_EVENT_PACKET_MAX_BYTES = 28;
45 constexpr int32_t MTP_EVENT_PACKET_VALID_LEN = 20;
46 constexpr int32_t MTP_EVENT_PACKET_INVALID_LEN = 29;
47 constexpr uint16_t CMD_CODE_GET_DEVICE_INFO = 0x1001;
48 constexpr uint32_t TRANSACTION_ID_RANDOM = 0xF00D;
49 /* mtp packet head defined as [struct UsbMtpDataHeader] in usbfn_mtp_impl.h */
50 constexpr uint32_t MTP_PACKET_HEADER_SIZE = 12;
51 constexpr uint32_t BULK_OUT_ONCE_MAX_SIZE = 1024;
52 constexpr uint32_t BULK_OUT_LESS_THEN_ONCE = 23;
53 constexpr uint32_t BULK_OUT_MORE_THEN_ONCE = 1025;
54 constexpr uint32_t BULK_IN_ONCE_MAX_SIZE = 1024;
55 constexpr uint32_t BULK_IN_LESS_THEN_ONCE = 45;
56 constexpr uint32_t BULK_IN_MORE_THEN_ONCE = 2023;
57 constexpr uint32_t MTP_FILE_SIZE_ONE_REQ = 1024;
58 constexpr uint32_t MTP_FILE_SIZE_REUSE_REQ = 12 * 1024;
59 /* 0xFFFFFFFFLL is 4 * 1024 * 1024 * 1024 - 1 = 4GB - 1 */
60 constexpr int64_t MTP_MAX_FILE_SIZE = 0xFFFFFFFFLL;
61 constexpr int64_t GEN_FILE_BUF_SIZE = 1024;
62 constexpr int64_t GEN_FILE_LIMIT_512MB = 512 * 1024 * 1024;
63 constexpr int32_t PRINT_VECTOR_MAX_LENGTH = 30;
64 constexpr const char *WORKED_UT_PATH = "/data/local/tmp/";
65 constexpr const char *MTP_TEST_SEND_FILE = "/data/local/tmp/sampleFile.mtp";
66 constexpr const char *MTP_TEST_RECV_FILE = "/data/local/tmp/sampleFile.mtp";
67
68 sptr<IUsbfnMtpInterface> g_usbfnMtpInterface = nullptr;
69 sptr<IUsbInterface> g_usbInterface = nullptr;
70 int32_t g_currentFunc = USB_FUNCTION_NONE;
71 int32_t g_fileTestCount = 0;
72
73 struct UsbFnMtpFileSlice g_mfs = {
74 .offset = 0,
75 .length = 0,
76 .command = 0,
77 .transactionId = 0,
78 };
79
PrintVector(const std::string & msg,std::vector<uint8_t> & data,bool hexFormat)80 void PrintVector(const std::string &msg, std::vector<uint8_t> &data, bool hexFormat)
81 {
82 size_t printLen = data.size();
83 bool ignore = false;
84 if (printLen > static_cast<size_t>(PRINT_VECTOR_MAX_LENGTH)) {
85 printLen = static_cast<size_t>(PRINT_VECTOR_MAX_LENGTH);
86 ignore = true;
87 }
88 std::stringstream ss;
89 for (size_t i = 0; i < printLen; i++) {
90 if (hexFormat) {
91 ss << std::hex << "0x" << (0xFF & data.at(i)) << " ";
92 } else {
93 ss << data.at(i);
94 }
95 }
96 std::string output = msg + std::string("(") + std::to_string(printLen) + std::string("):") + ss.str();
97 if (ignore) {
98 output += "......";
99 }
100 HDF_LOGV("UsbfnMtpTest::PrintVector %{public}s", output.c_str());
101 }
102
GetFileSize(const std::string & pathName)103 uint64_t GetFileSize(const std::string &pathName)
104 {
105 struct stat statbuf;
106 uint64_t ret = stat(pathName.c_str(), &statbuf);
107 if (ret != 0) {
108 return 0;
109 }
110 return static_cast<uint64_t>(statbuf.st_size);
111 }
112
WriteRandomDataToFile(const std::string & pathName,uint64_t fileSize)113 bool WriteRandomDataToFile(const std::string &pathName, uint64_t fileSize)
114 {
115 int32_t random = open("/dev/urandom", O_RDONLY);
116 if (random < 0) {
117 HDF_LOGE("UsbfnMtpTest::WriteRandomDataToFile get random data failed");
118 return false;
119 }
120 FILE *opFile = std::fopen(pathName.c_str(), "w");
121 if (opFile == nullptr) {
122 HDF_LOGE("UsbfnMtpTest::WriteRandomDataToFile create file failed: %{public}s", pathName.c_str());
123 return false;
124 }
125 char buffer[GEN_FILE_BUF_SIZE];
126 int64_t count = static_cast<int64_t>(fileSize);
127 while (count > 0) {
128 (void)memset_s(buffer, sizeof(buffer), 0, sizeof(buffer));
129 int64_t readSize = count > GEN_FILE_BUF_SIZE ? GEN_FILE_BUF_SIZE : count;
130 ssize_t readActual = read(random, static_cast<void *>(buffer), static_cast<size_t>(readSize));
131 if (readActual != static_cast<ssize_t>(readSize)) {
132 HDF_LOGW("UsbfnMtpTest::WriteRandomDataToFile read random failed");
133 break;
134 }
135 size_t writeActual = std::fwrite(static_cast<void *>(buffer), 1, static_cast<size_t>(readSize), opFile);
136 if (writeActual != static_cast<size_t>(readSize)) {
137 HDF_LOGW("UsbfnMtpTest::WriteRandomDataToFile write failed");
138 break;
139 }
140 count -= readSize;
141 }
142 std::fflush(opFile);
143 std::fclose(opFile);
144 close(random);
145 HDF_LOGV("UsbfnMtpTest::WriteRandomDataToFile file %{public}s: %{public}" PRIu64 "/%{public}" PRIu64 "",
146 pathName.c_str(), GetFileSize(pathName), fileSize);
147 return count > 0 ? false : true;
148 }
149
GenerateFile(const std::string & pathName,int64_t fileSize)150 bool GenerateFile(const std::string &pathName, int64_t fileSize)
151 {
152 if (GetFileSize(pathName) == static_cast<uint64_t>(fileSize)) {
153 HDF_LOGW("UsbfnMtpTest::GenerateFile file already exist");
154 return true;
155 }
156 if (fileSize > GEN_FILE_LIMIT_512MB) {
157 int32_t ret = truncate(pathName.c_str(), static_cast<off_t>(fileSize));
158 if (ret != 0) {
159 HDF_LOGE("UsbfnMtpTest::GenerateFile fail to truncate file to size: %{public}" PRId64 "", fileSize);
160 return false;
161 }
162 HDF_LOGV("UsbfnMtpTest::GenerateFile truncate %{public}s %{public}" PRId64 "", pathName.c_str(), fileSize);
163 return true;
164 }
165 return WriteRandomDataToFile(pathName, static_cast<uint64_t>(fileSize));
166 }
167
SetUpTestCase(void)168 void UsbfnMtpTest::SetUpTestCase(void)
169 {
170 // Selinux config this UT only works in directory WORKED_UT_PATH for open/read/write file for case send/recvfile.
171 ASSERT_TRUE(GetCurrentProcPath() == std::string(WORKED_UT_PATH));
172 std::cout << "===>please connect to PC use USB 3.0 interface, press enter to continue set function to mtp"
173 << std::endl;
174 int32_t c;
175 while ((c = getchar()) != '\n' && c != EOF) {}
176
177 g_usbInterface = IUsbInterface::Get();
178 ASSERT_TRUE(g_usbInterface != nullptr);
179 auto ret = g_usbInterface->SetPortRole(DEFAULT_PORT_ID, POWER_ROLE_SINK, DATA_ROLE_DEVICE);
180 sleep(SLEEP_TIME);
181 if (ret != 0) {
182 ASSERT_EQ(HDF_ERR_NOT_SUPPORT, ret);
183 } else {
184 ASSERT_EQ(0, ret);
185 }
186 ret = g_usbInterface->GetCurrentFunctions(g_currentFunc);
187 ASSERT_EQ(0, ret);
188 std::cout << "===>current function=" << g_currentFunc << ", set function to mtp, please wait" << std::endl;
189 ret = g_usbInterface->SetCurrentFunctions(USB_FUNCTION_MTP);
190 ASSERT_EQ(0, ret);
191
192 g_usbfnMtpInterface = IUsbfnMtpInterface::Get();
193 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
194 ret = g_usbfnMtpInterface->Start();
195 ASSERT_EQ(0, ret);
196 }
197
TearDownTestCase(void)198 void UsbfnMtpTest::TearDownTestCase(void)
199 {
200 HDF_LOGV("UsbfnMtpTest::TearDownTestCase");
201 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
202 auto ret = g_usbfnMtpInterface->Stop();
203 ASSERT_EQ(0, ret);
204 ASSERT_TRUE(g_usbInterface != nullptr);
205 ret = g_usbInterface->SetCurrentFunctions(g_currentFunc);
206 ASSERT_EQ(0, ret);
207 if (g_fileTestCount == 0) {
208 return;
209 }
210 /* 1 means single test, run with '--gtest_filter=' option */
211 if (g_fileTestCount == 1) {
212 std::cout << "===>please delete temporary test file if needed: sendfile=" << MTP_TEST_SEND_FILE
213 << " recvfile=" << MTP_TEST_RECV_FILE << std::endl;
214 return;
215 }
216 if (FileExists(std::string(MTP_TEST_SEND_FILE))) {
217 if (remove(MTP_TEST_SEND_FILE) != 0) {
218 std::cout << "[-] remove send file failed: " << MTP_TEST_SEND_FILE << std::endl;
219 }
220 }
221 if (FileExists(std::string(MTP_TEST_RECV_FILE))) {
222 if (remove(MTP_TEST_RECV_FILE) != 0) {
223 std::cout << "[-] remove recv file failed: " << MTP_TEST_RECV_FILE << std::endl;
224 }
225 }
226 }
227
SetUp(void)228 void UsbfnMtpTest::SetUp(void) {}
229
TearDown(void)230 void UsbfnMtpTest::TearDown(void) {}
231
232 /**
233 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_0100
234 * @tc.desc: Test functions to Read
235 * @tc.desc: int32_t Read(std::vector<uint8_t>& data);
236 * @tc.desc: Positive test: parameters correctly, read length less then one packet size
237 * @tc.type: FUNC
238 */
239 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_0100, Function | MediumTest | Level1)
240 {
241 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
242 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_0100 Case Start");
243 std::vector<uint8_t> devData;
244
245 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_0100===>use libusb in PC launch bulk-out transfer(size="
246 << BULK_OUT_LESS_THEN_ONCE << "), press enter to continue" << std::endl;
247 int32_t c;
248 while ((c = getchar()) != '\n' && c != EOF) {}
249
250 int32_t ret = g_usbfnMtpInterface->Read(devData);
251 ASSERT_EQ(ret, 0);
252 ASSERT_EQ(devData.size(), static_cast<size_t>(BULK_OUT_LESS_THEN_ONCE));
253 }
254
255 /**
256 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_0200
257 * @tc.desc: Test functions to Read
258 * @tc.desc: int32_t Read(std::vector<uint8_t>& data);
259 * @tc.desc: Positive test: parameters correctly, read length exactly one packet size
260 * @tc.type: FUNC
261 */
262 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_0200, Function | MediumTest | Level1)
263 {
264 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
265 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_0200 Case Start");
266 std::vector<uint8_t> devData;
267
268 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_0200===>use libusb in PC launch bulk-out transfer(size="
269 << BULK_OUT_ONCE_MAX_SIZE << "), press enter to continue" << std::endl;
270 int32_t c;
271 while ((c = getchar()) != '\n' && c != EOF) {}
272
273 int32_t ret = g_usbfnMtpInterface->Read(devData);
274 ASSERT_EQ(ret, 0);
275 ASSERT_EQ(devData.size(), static_cast<size_t>(BULK_OUT_ONCE_MAX_SIZE));
276 }
277
278 /**
279 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_0300
280 * @tc.desc: Test functions to Read
281 * @tc.desc: int32_t Read(std::vector<uint8_t>& data);
282 * @tc.desc: Positive test: parameters correctly, read length more then one packet size, please read again
283 * @tc.type: FUNC
284 */
285 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_0300, Function | MediumTest | Level1)
286 {
287 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
288 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_0300 Case Start");
289 std::vector<uint8_t> devData;
290
291 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_0300===>use libusb in PC launch bulk-out transfer(size="
292 << BULK_OUT_MORE_THEN_ONCE << "), press enter to continue" << std::endl;
293 int32_t c;
294 while ((c = getchar()) != '\n' && c != EOF) {}
295
296 int32_t ret = g_usbfnMtpInterface->Read(devData);
297 ASSERT_EQ(ret, 0);
298 ASSERT_EQ(devData.size(), static_cast<size_t>(BULK_OUT_ONCE_MAX_SIZE));
299 devData.clear();
300 ret = g_usbfnMtpInterface->Read(devData);
301 ASSERT_EQ(ret, 0);
302 ASSERT_EQ(devData.size(), static_cast<size_t>(BULK_OUT_MORE_THEN_ONCE - BULK_OUT_ONCE_MAX_SIZE));
303 }
304
305 /**
306 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_0400
307 * @tc.desc: Test functions to Read
308 * @tc.desc: int32_t Read(std::vector<uint8_t>& data)
309 * @tc.desc: Positive test: parameters correctly, no specific read size
310 * @tc.type: FUNC
311 */
312 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_0400, Function | MediumTest | Level1)
313 {
314 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
315 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_0400 Case Start");
316 std::vector<uint8_t> devData;
317
318 std::cout
319 << "SUB_USB_DeviceManager_HDI_MTPPTP_0400===>use libusb in PC launch bulk-out transfer(size in [0, 1024]), "
320 << "press enter to continue" << std::endl;
321 int32_t c;
322 while ((c = getchar()) != '\n' && c != EOF) {}
323
324 auto ret = g_usbfnMtpInterface->Read(devData);
325 ASSERT_EQ(ret, 0);
326 ASSERT_GE(devData.size(), 0);
327 }
328
329 /**
330 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_0500
331 * @tc.desc: Test functions to Read
332 * @tc.desc: int32_t Read(std::vector<uint8_t>& data)
333 * @tc.desc: Positive test: parameters correctly, check read content
334 * @tc.type: FUNC
335 */
336 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_0500, Function | MediumTest | Level1)
337 {
338 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
339 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_0500 Case Start");
340 std::vector<uint8_t> devData;
341 // hex value of string "read005"
342 std::vector<uint8_t> expectData = {0x72, 0x65, 0x61, 0x64, 0x30, 0x30, 0x35};
343
344 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_0500===>"
345 << "use libusb in PC launch bulk-out transfer(string=read005), "
346 << "press enter to continue" << std::endl;
347 int32_t c;
348 while ((c = getchar()) != '\n' && c != EOF) {}
349
350 auto ret = g_usbfnMtpInterface->Read(devData);
351 ASSERT_EQ(ret, 0);
352 ASSERT_EQ(devData, expectData);
353 PrintVector("read005", devData, true);
354 }
355
356 /**
357 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_0600
358 * @tc.desc: Test functions to Write
359 * @tc.desc: int32_t Write(const std::vector<uint8_t>& data)
360 * @tc.desc: Positive test: parameters correctly
361 * @tc.type: FUNC
362 */
363 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_0600, Function | MediumTest | Level1)
364 {
365 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
366 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_0600 Case Start");
367 uint32_t length = BULK_IN_LESS_THEN_ONCE;
368 std::vector<uint8_t> devData;
369 devData.assign(length, 'w');
370
371 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_0600===>use libusb in PC launch bulk-in transfer(expect=" << length
372 << "), press enter to continue" << std::endl;
373 int32_t c;
374 while ((c = getchar()) != '\n' && c != EOF) {}
375
376 auto ret = g_usbfnMtpInterface->Write(devData);
377 ASSERT_EQ(ret, 0);
378 }
379
380 /**
381 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_0700
382 * @tc.desc: Test functions to Write
383 * @tc.desc: int32_t Write(const std::vector<uint8_t>& data)
384 * @tc.desc: Positive test: parameters correctly
385 * @tc.type: FUNC
386 */
387 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_0700, Function | MediumTest | Level1)
388 {
389 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
390 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_0700 Case Start");
391 uint32_t length = BULK_IN_ONCE_MAX_SIZE;
392 std::vector<uint8_t> devData;
393 devData.assign(length, 'w');
394 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_0700===>use libusb in PC launch bulk-in transfer(expect=" << length
395 << "), press enter to continue" << std::endl;
396 int32_t c;
397 while ((c = getchar()) != '\n' && c != EOF) {}
398
399 auto ret = g_usbfnMtpInterface->Write(devData);
400 ASSERT_EQ(ret, 0);
401 }
402
403 /**
404 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_0800
405 * @tc.desc: Test functions to Write
406 * @tc.desc: int32_t Write(const std::vector<uint8_t>& data)
407 * @tc.desc: Positive test: parameters correctly
408 * @tc.type: FUNC
409 */
410 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_0800, Function | MediumTest | Level1)
411 {
412 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
413 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_0800 Case Start");
414 uint32_t length = BULK_IN_MORE_THEN_ONCE;
415 std::vector<uint8_t> devData;
416 devData.assign(length, 'w');
417 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_0800===>use libusb in PC launch bulk-in transfer(expect=" << length
418 << "), press enter to continue" << std::endl;
419 int32_t c;
420 while ((c = getchar()) != '\n' && c != EOF) {}
421
422 auto ret = g_usbfnMtpInterface->Write(devData);
423 ASSERT_EQ(ret, 0);
424 }
425
426 /**
427 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_0900
428 * @tc.desc: Test functions to Write
429 * @tc.desc: int32_t Write(const std::vector<uint8_t>& data)
430 * @tc.desc: Positive test: parameters correctly, write empty data
431 * @tc.type: FUNC
432 */
433 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_0900, Function | MediumTest | Level1)
434 {
435 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
436 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_0900 Case Start");
437 std::vector<uint8_t> devData;
438 auto ret = g_usbfnMtpInterface->Write(devData);
439 ASSERT_EQ(ret, 0);
440 }
441
442 /**
443 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_1000
444 * @tc.desc: Test functions to Write
445 * @tc.desc: int32_t Write(const std::vector<uint8_t>& data)
446 * @tc.desc: Positive test: parameters correctly, write specific data
447 * @tc.type: FUNC
448 */
449 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_1000, Function | MediumTest | Level1)
450 {
451 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
452 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_1000 Case Start");
453 // hex value of string "write005"
454 std::vector<uint8_t> devData = {0x77, 0x72, 0x69, 0x74, 0x65, 0x30, 0x30, 0x35};
455 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_1000===>"
456 << "use libusb in PC launch bulk-in transfer(expect string=write005), "
457 << "press enter to continue" << std::endl;
458 int32_t c;
459 while ((c = getchar()) != '\n' && c != EOF) {}
460
461 auto ret = g_usbfnMtpInterface->Write(devData);
462 ASSERT_EQ(ret, 0);
463 PrintVector("write005", devData, true);
464 }
465
466 /**
467 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_1100
468 * @tc.desc: Test functions to SendEvent
469 * @tc.desc: int32_t SendEvent(const std::vector<uint8_t> &eventData);
470 * @tc.desc: Positive test: parameters correctly, valid length
471 * @tc.type: FUNC
472 */
473 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_1100, Function | MediumTest | Level1)
474 {
475 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
476 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_1100 Case Start");
477 std::vector<uint8_t> devData;
478 devData.assign(MTP_EVENT_PACKET_VALID_LEN, 'e');
479 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_1100===>use libusb in PC launch intr-in transfer(expect="
480 << devData.size() << "), press enter to continue" << std::endl;
481 int32_t c;
482 while ((c = getchar()) != '\n' && c != EOF) {}
483
484 auto ret = g_usbfnMtpInterface->SendEvent(devData);
485 ASSERT_EQ(0, ret);
486 }
487
488 /**
489 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_1200
490 * @tc.desc: Test functions to SendEvent
491 * @tc.desc: int32_t SendEvent(const std::vector<uint8_t> &eventData);
492 * @tc.desc: Positive test: parameters correctly, max length
493 * @tc.type: FUNC
494 */
495 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_1200, Function | MediumTest | Level1)
496 {
497 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
498 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_1200 Case Start");
499 std::vector<uint8_t> devData;
500 devData.assign(MTP_EVENT_PACKET_MAX_BYTES, 'e');
501 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_1200===>use libusb in PC launch intr-in transfer(expect="
502 << devData.size() << "), press enter to continue" << std::endl;
503 int32_t c;
504 while ((c = getchar()) != '\n' && c != EOF) {}
505 auto ret = g_usbfnMtpInterface->SendEvent(devData);
506 ASSERT_EQ(0, ret);
507 }
508
509 /**
510 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_1300
511 * @tc.desc: Test functions to SendEvent
512 * @tc.desc: int32_t SendEvent(const std::vector<uint8_t> &eventData);
513 * @tc.desc: Negative test: parameters exception, size overflow
514 * @tc.type: FUNC
515 */
516 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_1300, Function | MediumTest | Level1)
517 {
518 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
519 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_1300 Case Start");
520 std::vector<uint8_t> devData;
521 devData.assign(MTP_EVENT_PACKET_INVALID_LEN, 'e');
522 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_1300===>use libusb in PC launch intr-in transfer(expect=no data, "
523 << "or error), press enter to continue" << std::endl;
524 int32_t c;
525 while ((c = getchar()) != '\n' && c != EOF) {}
526
527 auto ret = g_usbfnMtpInterface->SendEvent(devData);
528 ASSERT_NE(0, ret);
529 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_1300===>make sure transfer timeout in PC, then start next test "
530 << std::endl;
531 }
532
533 /**
534 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_1400
535 * @tc.desc: Test functions to SendEvent
536 * @tc.desc: int32_t SendEvent(const std::vector<uint8_t> &eventData);
537 * @tc.desc: Positive test: parameters correctly, max length, check content
538 * @tc.type: FUNC
539 */
540 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_1400, Function | MediumTest | Level1)
541 {
542 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
543 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_1400 Case Start");
544 // hex value of string "event004"
545 std::vector<uint8_t> devData = {0x65, 0x76, 0x65, 0x6E, 0x74, 0x30, 0x30, 0x34};
546 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_1400===>"
547 << "use libusb in PC launch intr-in transfer(expect string=event004), "
548 << "press enter to continue" << std::endl;
549 int32_t c;
550 while ((c = getchar()) != '\n' && c != EOF) {}
551 auto ret = g_usbfnMtpInterface->SendEvent(devData);
552 ASSERT_EQ(0, ret);
553 PrintVector("event004", devData, true);
554 }
555
556 /**
557 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_1500
558 * @tc.desc: Test functions to ReceiveFile
559 * @tc.desc: int32_t ReceiveFile(const UsbFnMtpFileSlice &mfs);
560 * @tc.desc: Positive test: parameters correctly, one packet enough
561 * @tc.type: FUNC
562 */
563 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_1500, Function | MediumTest | Level1)
564 {
565 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
566 ASSERT_TRUE(GetCurrentProcPath() == std::string(WORKED_UT_PATH));
567 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_1500 Case Start");
568 g_fileTestCount++;
569 struct UsbFnMtpFileSlice mfs = g_mfs;
570 mfs.length = BULK_OUT_LESS_THEN_ONCE;
571 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_1500===>use libusb in PC launch bulk-out transfer(size = "
572 << mfs.length << "), press enter to continue" << std::endl;
573 int32_t c;
574 while ((c = getchar()) != '\n' && c != EOF) {}
575
576 std::string filePathName = MTP_TEST_RECV_FILE;
577 mfs.fd = open(filePathName.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0777);
578 ASSERT_GT(mfs.fd, 0);
579 auto ret = g_usbfnMtpInterface->ReceiveFile(mfs);
580 close(mfs.fd);
581 ASSERT_EQ(ret, 0);
582 ASSERT_EQ(GetFileSize(filePathName), static_cast<uint64_t>(mfs.length));
583 }
584
585 /**
586 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_1600
587 * @tc.desc: Test functions to ReceiveFile
588 * @tc.desc: int32_t ReceiveFile(const UsbFnMtpFileSlice &mfs);
589 * @tc.desc: Positive test: parameters correctly, zero length
590 * @tc.type: FUNC
591 */
592 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_1600, Function | MediumTest | Level1)
593 {
594 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
595 ASSERT_TRUE(GetCurrentProcPath() == std::string(WORKED_UT_PATH));
596 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_1600 Case Start");
597 g_fileTestCount++;
598 struct UsbFnMtpFileSlice mfs = g_mfs;
599 mfs.length = 0;
600 std::string filePathName = MTP_TEST_RECV_FILE;
601 mfs.fd = open(filePathName.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0777);
602 ASSERT_GT(mfs.fd, 0);
603 auto ret = g_usbfnMtpInterface->ReceiveFile(mfs);
604 close(mfs.fd);
605 ASSERT_EQ(ret, 0);
606 ASSERT_EQ(GetFileSize(filePathName), static_cast<uint64_t>(mfs.length));
607 }
608
609 /**
610 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_1700
611 * @tc.desc: Test functions to ReceiveFile
612 * @tc.desc: int32_t ReceiveFile(const UsbFnMtpFileSlice &mfs);
613 * @tc.desc: Positive test: parameters correctly, one normal packet + short packet
614 * @tc.type: FUNC
615 */
616 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_1700, Function | MediumTest | Level1)
617 {
618 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
619 ASSERT_TRUE(GetCurrentProcPath() == std::string(WORKED_UT_PATH));
620 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_1700 Case Start");
621 g_fileTestCount++;
622 struct UsbFnMtpFileSlice mfs = g_mfs;
623 mfs.length = BULK_OUT_MORE_THEN_ONCE;
624 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_1700===>use libusb in PC launch bulk-out transfer(size = "
625 << mfs.length << "), press enter to continue" << std::endl;
626 int32_t c;
627 while ((c = getchar()) != '\n' && c != EOF) {}
628
629 std::string filePathName = MTP_TEST_RECV_FILE;
630 mfs.fd = open(filePathName.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0777);
631 ASSERT_GT(mfs.fd, 0);
632 auto ret = g_usbfnMtpInterface->ReceiveFile(mfs);
633 close(mfs.fd);
634 ASSERT_EQ(ret, 0);
635 ASSERT_EQ(GetFileSize(filePathName), static_cast<uint64_t>(mfs.length));
636 }
637
638 /**
639 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_1800
640 * @tc.desc: Test functions to ReceiveFile
641 * @tc.desc: int32_t ReceiveFile(const UsbFnMtpFileSlice &mfs);
642 * @tc.desc: Positive test: mfs.length set to max, 12 packet + ZLP
643 * @tc.type: FUNC
644 */
645 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_1800, Function | MediumTest | Level1)
646 {
647 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
648 ASSERT_TRUE(GetCurrentProcPath() == std::string(WORKED_UT_PATH));
649 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_1800 Case Start");
650 g_fileTestCount++;
651 struct UsbFnMtpFileSlice mfs = g_mfs;
652 mfs.length = MTP_FILE_SIZE_REUSE_REQ;
653 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_1800===>use libusb in PC launch bulk-out transfer(size = "
654 << mfs.length << "), press enter to continue" << std::endl;
655 int32_t c;
656 while ((c = getchar()) != '\n' && c != EOF) {}
657
658 std::string filePathName = MTP_TEST_RECV_FILE;
659 mfs.fd = open(filePathName.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0777);
660 ASSERT_GT(mfs.fd, 0);
661 auto ret = g_usbfnMtpInterface->ReceiveFile(mfs);
662 close(mfs.fd);
663 ASSERT_EQ(ret, 0);
664 ASSERT_EQ(GetFileSize(filePathName), static_cast<uint64_t>(mfs.length));
665 }
666
667 /**
668 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_1900
669 * @tc.desc: Test functions to ReceiveFile
670 * @tc.desc: int32_t ReceiveFile(const UsbFnMtpFileSlice &mfs);
671 * @tc.desc: Positive test: parameters correctly, command and transactionId ignored
672 * @tc.type: FUNC
673 */
674 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_1900, Function | MediumTest | Level1)
675 {
676 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
677 ASSERT_TRUE(GetCurrentProcPath() == std::string(WORKED_UT_PATH));
678 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_1900 Case Start");
679 g_fileTestCount++;
680 struct UsbFnMtpFileSlice mfs = g_mfs;
681 mfs.length = BULK_OUT_LESS_THEN_ONCE;
682 mfs.command = CMD_CODE_GET_DEVICE_INFO;
683 mfs.transactionId = TRANSACTION_ID_RANDOM;
684 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_1900===>use libusb in PC launch bulk-out transfer(size = "
685 << mfs.length << "), press enter to continue" << std::endl;
686 int32_t c;
687 while ((c = getchar()) != '\n' && c != EOF) {}
688
689 std::string filePathName = MTP_TEST_RECV_FILE;
690 mfs.fd = open(filePathName.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0777);
691 ASSERT_GT(mfs.fd, 0);
692 auto ret = g_usbfnMtpInterface->ReceiveFile(mfs);
693 close(mfs.fd);
694 ASSERT_EQ(ret, 0);
695 ASSERT_EQ(GetFileSize(filePathName), static_cast<uint64_t>(mfs.length));
696 }
697
698 /**
699 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_2000
700 * @tc.desc: Test functions to ReceiveFile
701 * @tc.desc: int32_t ReceiveFile(const UsbFnMtpFileSlice &mfs);
702 * @tc.desc: Positive test: mfs.length set to max, recv actual file size depend on xfer count
703 * @tc.type: FUNC
704 */
705 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_2000, Function | MediumTest | Level1)
706 {
707 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
708 ASSERT_TRUE(GetCurrentProcPath() == std::string(WORKED_UT_PATH));
709 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_2000 Case Start");
710 g_fileTestCount++;
711 struct UsbFnMtpFileSlice mfs = g_mfs;
712 mfs.length = MTP_MAX_FILE_SIZE;
713 std::cout
714 << "SUB_USB_DeviceManager_HDI_MTPPTP_2000===>use libusb in PC launch bulk-out transfer(size = any), "
715 << "press enter to continue" << std::endl;
716 int32_t c;
717 while ((c = getchar()) != '\n' && c != EOF) {}
718
719 std::string filePathName = MTP_TEST_RECV_FILE;
720 mfs.fd = open(filePathName.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0777);
721 ASSERT_GT(mfs.fd, 0);
722 auto ret = g_usbfnMtpInterface->ReceiveFile(mfs);
723 close(mfs.fd);
724 ASSERT_EQ(ret, 0);
725 ASSERT_GE(GetFileSize(filePathName), 0);
726 }
727
728 /**
729 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_2100
730 * @tc.desc: Test functions to ReceiveFile
731 * @tc.desc: int32_t ReceiveFile(const UsbFnMtpFileSlice &mfs);
732 * @tc.desc: Positive test: mfs.length set to max - 1: 4GB - 2
733 * @tc.type: FUNC
734 */
735 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_2100, Function | MediumTest | Level1)
736 {
737 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
738 ASSERT_TRUE(GetCurrentProcPath() == std::string(WORKED_UT_PATH));
739 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_2100 Case Start");
740 g_fileTestCount++;
741 struct UsbFnMtpFileSlice mfs = g_mfs;
742 mfs.length = MTP_MAX_FILE_SIZE - 1;
743 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_2100===>use libusb in PC launch bulk-out transfer(size = "
744 << mfs.length << "), press enter to continue" << std::endl;
745 int32_t c;
746 while ((c = getchar()) != '\n' && c != EOF) {}
747
748 std::string filePathName = MTP_TEST_RECV_FILE;
749 mfs.fd = open(filePathName.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0777);
750 ASSERT_GT(mfs.fd, 0);
751 auto ret = g_usbfnMtpInterface->ReceiveFile(mfs);
752 close(mfs.fd);
753 ASSERT_EQ(ret, 0);
754 ASSERT_EQ(GetFileSize(filePathName), static_cast<uint64_t>(mfs.length));
755 }
756
757 /**
758 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_2200
759 * @tc.desc: Test functions to ReceiveFile
760 * @tc.desc: int32_t ReceiveFile(const UsbFnMtpFileSlice &mfs);
761 * @tc.desc: Positive test: mfs.length set to max + 1: 4GB
762 * @tc.type: FUNC
763 */
764 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_2200, Function | MediumTest | Level1)
765 {
766 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
767 ASSERT_TRUE(GetCurrentProcPath() == std::string(WORKED_UT_PATH));
768 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_2200 Case Start");
769 g_fileTestCount++;
770 struct UsbFnMtpFileSlice mfs = g_mfs;
771 mfs.length = MTP_MAX_FILE_SIZE + 1;
772 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_2200===>use libusb in PC launch bulk-out transfer(size = "
773 << mfs.length << "), press enter to continue" << std::endl;
774 int32_t c;
775 while ((c = getchar()) != '\n' && c != EOF) {}
776
777 std::string filePathName = MTP_TEST_RECV_FILE;
778 mfs.fd = open(filePathName.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0777);
779 ASSERT_GT(mfs.fd, 0);
780 auto ret = g_usbfnMtpInterface->ReceiveFile(mfs);
781 close(mfs.fd);
782 ASSERT_EQ(ret, 0);
783 ASSERT_EQ(GetFileSize(filePathName), static_cast<uint64_t>(mfs.length));
784 }
785
786 /**
787 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_2300
788 * @tc.desc: Test functions to ReceiveFile
789 * @tc.desc: int32_t ReceiveFile(const UsbFnMtpFileSlice &mfs);
790 * @tc.desc: Positive test: mfs.length set to max + 2: 4GB + 1
791 * @tc.type: FUNC
792 */
793 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_2300, Function | MediumTest | Level1)
794 {
795 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
796 ASSERT_TRUE(GetCurrentProcPath() == std::string(WORKED_UT_PATH));
797 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_2300 Case Start");
798 g_fileTestCount++;
799 struct UsbFnMtpFileSlice mfs = g_mfs;
800 mfs.length = MTP_MAX_FILE_SIZE + 2;
801 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_2300===>use libusb in PC launch bulk-out transfer(size = "
802 << mfs.length << "), press enter to continue" << std::endl;
803 int32_t c;
804 while ((c = getchar()) != '\n' && c != EOF) {}
805
806 std::string filePathName = MTP_TEST_RECV_FILE;
807 mfs.fd = open(filePathName.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0777);
808 ASSERT_GT(mfs.fd, 0);
809 auto ret = g_usbfnMtpInterface->ReceiveFile(mfs);
810 close(mfs.fd);
811 ASSERT_EQ(ret, 0);
812 ASSERT_EQ(GetFileSize(filePathName), static_cast<uint64_t>(mfs.length));
813 }
814
815 /**
816 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_2400
817 * @tc.desc: Test functions to SendFile
818 * @tc.desc: int32_t SendFile(const UsbFnMtpFileSlice &mfs);
819 * @tc.desc: Positive test: parameters correctly, length in one packet
820 * @tc.type: FUNC
821 */
822 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_2400, Function | MediumTest | Level1)
823 {
824 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
825 ASSERT_TRUE(GetCurrentProcPath() == std::string(WORKED_UT_PATH));
826 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_2400 Case Start");
827 g_fileTestCount++;
828 struct UsbFnMtpFileSlice mfs = g_mfs;
829 mfs.length = BULK_IN_LESS_THEN_ONCE;
830 std::string filePathName = MTP_TEST_SEND_FILE;
831 EXPECT_TRUE(GenerateFile(filePathName, mfs.length));
832 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_2400===>"
833 << "use libusb in PC launch bulk-in transfer(expect " << mfs.length
834 << "), press enter to continue" << std::endl;
835 int32_t c;
836 while ((c = getchar()) != '\n' && c != EOF) {}
837
838 mfs.fd = open(filePathName.c_str(), O_CREAT | O_RDONLY);
839 ASSERT_GT(mfs.fd, 0);
840 auto ret = g_usbfnMtpInterface->SendFile(mfs);
841 close(mfs.fd);
842 ASSERT_EQ(ret, 0);
843 }
844
845 /**
846 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_2500
847 * @tc.desc: Test functions to SendFile
848 * @tc.desc: int32_t SendFile(const UsbFnMtpFileSlice &mfs);
849 * @tc.desc: Positive test: parameters correctly, send header + data in one packet
850 * @tc.type: FUNC
851 */
852 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_2500, Function | MediumTest | Level1)
853 {
854 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
855 ASSERT_TRUE(GetCurrentProcPath() == std::string(WORKED_UT_PATH));
856 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_2500 Case Start");
857 g_fileTestCount++;
858 struct UsbFnMtpFileSlice mfs = g_mfs;
859 mfs.length = BULK_IN_LESS_THEN_ONCE;
860 mfs.command = CMD_CODE_GET_DEVICE_INFO;
861 std::string filePathName = MTP_TEST_SEND_FILE;
862 EXPECT_TRUE(GenerateFile(filePathName, mfs.length));
863 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_2500===>use libusb in PC launch bulk-in transfer(expect "
864 << mfs.length + MTP_PACKET_HEADER_SIZE << "), press enter to continue" << std::endl;
865 int32_t c;
866 while ((c = getchar()) != '\n' && c != EOF) {}
867
868 mfs.fd = open(filePathName.c_str(), O_CREAT | O_RDONLY);
869 ASSERT_GT(mfs.fd, 0);
870 auto ret = g_usbfnMtpInterface->SendFile(mfs);
871 close(mfs.fd);
872 ASSERT_EQ(0, ret);
873 }
874
875 /**
876 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_2600
877 * @tc.desc: Test functions to SendFile
878 * @tc.desc: int32_t SendFile(const UsbFnMtpFileSlice &mfs);
879 * @tc.desc: Positive test: parameters correctly, zero length
880 * @tc.type: FUNC
881 */
882 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_2600, Function | MediumTest | Level1)
883 {
884 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
885 ASSERT_TRUE(GetCurrentProcPath() == std::string(WORKED_UT_PATH));
886 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_2600 Case Start");
887 g_fileTestCount++;
888 struct UsbFnMtpFileSlice mfs = g_mfs;
889 mfs.length = 0;
890 std::string filePathName = MTP_TEST_SEND_FILE;
891 EXPECT_TRUE(GenerateFile(filePathName, mfs.length));
892 mfs.fd = open(filePathName.c_str(), O_CREAT | O_RDONLY);
893 ASSERT_GT(mfs.fd, 0);
894 auto ret = g_usbfnMtpInterface->SendFile(mfs);
895 close(mfs.fd);
896 ASSERT_EQ(0, ret);
897 }
898
899 /**
900 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_2700
901 * @tc.desc: Test functions to SendFile
902 * @tc.desc: int32_t SendFile(const UsbFnMtpFileSlice &mfs);
903 * @tc.desc: Positive test: parameters correctly, send header + data in two packet: normal + short
904 * @tc.type: FUNC
905 */
906 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_2700, Function | MediumTest | Level1)
907 {
908 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
909 ASSERT_TRUE(GetCurrentProcPath() == std::string(WORKED_UT_PATH));
910 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_2700 Case Start");
911 g_fileTestCount++;
912 struct UsbFnMtpFileSlice mfs = g_mfs;
913 mfs.length = MTP_FILE_SIZE_ONE_REQ;
914 mfs.command = CMD_CODE_GET_DEVICE_INFO;
915 std::string filePathName = MTP_TEST_SEND_FILE;
916 EXPECT_TRUE(GenerateFile(filePathName, mfs.length));
917 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_2700===>use libusb in PC launch bulk-in transfer(expect "
918 << mfs.length + MTP_PACKET_HEADER_SIZE << "), press enter to continue" << std::endl;
919 int32_t c;
920 while ((c = getchar()) != '\n' && c != EOF) {}
921
922 mfs.fd = open(filePathName.c_str(), O_CREAT | O_RDONLY);
923 ASSERT_GT(mfs.fd, 0);
924 auto ret = g_usbfnMtpInterface->SendFile(mfs);
925 close(mfs.fd);
926 ASSERT_EQ(0, ret);
927 }
928
929 /**
930 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_2800
931 * @tc.desc: Test functions to SendFile
932 * @tc.desc: int32_t SendFile(const UsbFnMtpFileSlice &mfs);
933 * @tc.desc: Positive test: parameters correctly, mfs.length set to max
934 * @tc.type: FUNC
935 */
936 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_2800, Function | MediumTest | Level1)
937 {
938 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
939 ASSERT_TRUE(GetCurrentProcPath() == std::string(WORKED_UT_PATH));
940 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_2800 Case Start");
941 g_fileTestCount++;
942 struct UsbFnMtpFileSlice mfs = g_mfs;
943 mfs.length = MTP_FILE_SIZE_REUSE_REQ;
944 std::string filePathName = MTP_TEST_SEND_FILE;
945 EXPECT_TRUE(GenerateFile(filePathName, mfs.length));
946 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_2800===>use libusb in PC launch bulk-in transfer(speed, expect "
947 << mfs.length << "), press enter to continue" << std::endl;
948 int32_t c;
949 while ((c = getchar()) != '\n' && c != EOF) {}
950
951 mfs.fd = open(filePathName.c_str(), O_CREAT | O_RDONLY);
952 ASSERT_GT(mfs.fd, 0);
953 auto ret = g_usbfnMtpInterface->SendFile(mfs);
954 close(mfs.fd);
955 ASSERT_EQ(0, ret);
956 }
957
958 /**
959 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_2900
960 * @tc.desc: Test functions to SendFile
961 * @tc.desc: int32_t SendFile(const UsbFnMtpFileSlice &mfs);
962 * @tc.desc: Positive test: parameters correctly, mfs.length set to max: 4GB - 1
963 * @tc.type: FUNC
964 */
965 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_2900, Function | MediumTest | Level1)
966 {
967 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
968 ASSERT_TRUE(GetCurrentProcPath() == std::string(WORKED_UT_PATH));
969 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_2900 Case Start");
970 g_fileTestCount++;
971 struct UsbFnMtpFileSlice mfs = g_mfs;
972 mfs.length = MTP_MAX_FILE_SIZE;
973 std::string filePathName = MTP_TEST_SEND_FILE;
974 EXPECT_TRUE(GenerateFile(filePathName, mfs.length));
975 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_2900===>use libusb in PC launch bulk-in transfer(speed, expect "
976 << mfs.length << "), press enter to continue" << std::endl;
977 int32_t c;
978 while ((c = getchar()) != '\n' && c != EOF) {}
979
980 mfs.fd = open(filePathName.c_str(), O_CREAT | O_RDONLY);
981 ASSERT_GT(mfs.fd, 0);
982 auto ret = g_usbfnMtpInterface->SendFile(mfs);
983 close(mfs.fd);
984 ASSERT_EQ(0, ret);
985 }
986
987 /**
988 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_3000
989 * @tc.desc: Test functions to SendFile
990 * @tc.desc: int32_t SendFile(const UsbFnMtpFileSlice &mfs);
991 * @tc.desc: Positive test: parameters correctly, mfs.length set to max + 1: 4GB
992 * @tc.type: FUNC
993 */
994 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_3000, Function | MediumTest | Level1)
995 {
996 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
997 ASSERT_TRUE(GetCurrentProcPath() == std::string(WORKED_UT_PATH));
998 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_3000 Case Start");
999 g_fileTestCount++;
1000 struct UsbFnMtpFileSlice mfs = g_mfs;
1001 mfs.length = MTP_MAX_FILE_SIZE + 1;
1002 std::string filePathName = MTP_TEST_SEND_FILE;
1003 EXPECT_TRUE(GenerateFile(filePathName, mfs.length));
1004 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_3000===>use libusb in PC launch bulk-in transfer(speed, expect "
1005 << mfs.length << "), press enter to continue" << std::endl;
1006 int32_t c;
1007 while ((c = getchar()) != '\n' && c != EOF) {}
1008
1009 mfs.fd = open(filePathName.c_str(), O_CREAT | O_RDONLY);
1010 ASSERT_GT(mfs.fd, 0);
1011 auto ret = g_usbfnMtpInterface->SendFile(mfs);
1012 close(mfs.fd);
1013 ASSERT_EQ(0, ret);
1014 }
1015
1016 /**
1017 * @tc.name: SUB_USB_DeviceManager_HDI_MTPPTP_3100
1018 * @tc.desc: Test functions to SendFile
1019 * @tc.desc: int32_t SendFile(const UsbFnMtpFileSlice &mfs);
1020 * @tc.desc: Positive test: parameters correctly, mfs.length set to max + 1: 4GB + 1
1021 * @tc.type: FUNC
1022 */
1023 HWTEST_F(UsbfnMtpTest, SUB_USB_DeviceManager_HDI_MTPPTP_3100, Function | MediumTest | Level1)
1024 {
1025 ASSERT_TRUE(g_usbfnMtpInterface != nullptr);
1026 ASSERT_TRUE(GetCurrentProcPath() == std::string(WORKED_UT_PATH));
1027 HDF_LOGI("UsbfnMtpTest::SUB_USB_DeviceManager_HDI_MTPPTP_3100 Case Start");
1028 g_fileTestCount++;
1029 struct UsbFnMtpFileSlice mfs = g_mfs;
1030 mfs.length = MTP_MAX_FILE_SIZE + 2;
1031 std::string filePathName = MTP_TEST_SEND_FILE;
1032 EXPECT_TRUE(GenerateFile(filePathName, mfs.length));
1033 std::cout << "SUB_USB_DeviceManager_HDI_MTPPTP_3100===>use libusb in PC launch bulk-in transfer(speed, expect "
1034 << mfs.length << "), press enter to continue" << std::endl;
1035 int32_t c;
1036 while ((c = getchar()) != '\n' && c != EOF) {}
1037
1038 mfs.fd = open(filePathName.c_str(), O_CREAT | O_RDONLY);
1039 ASSERT_GT(mfs.fd, 0);
1040 auto ret = g_usbfnMtpInterface->SendFile(mfs);
1041 close(mfs.fd);
1042 ASSERT_EQ(0, ret);
1043 }
1044
1045 } // namespace
1046