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