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