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