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