1060ff233Sopenharmony_ci/*
2060ff233Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3060ff233Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4060ff233Sopenharmony_ci * you may not use this file except in compliance with the License.
5060ff233Sopenharmony_ci * You may obtain a copy of the License at
6060ff233Sopenharmony_ci *
7060ff233Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8060ff233Sopenharmony_ci *
9060ff233Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10060ff233Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11060ff233Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12060ff233Sopenharmony_ci * See the License for the specific language governing permissions and
13060ff233Sopenharmony_ci * limitations under the License.
14060ff233Sopenharmony_ci */
15060ff233Sopenharmony_ci
16060ff233Sopenharmony_ci#include <gtest/gtest.h>
17060ff233Sopenharmony_ci
18060ff233Sopenharmony_ci#include <cinttypes>
19060ff233Sopenharmony_ci#include "test_suite.h"
20060ff233Sopenharmony_ci#include "transport/session.h"
21060ff233Sopenharmony_ci
22060ff233Sopenharmony_ci#include "device_manager.h"
23060ff233Sopenharmony_ci
24060ff233Sopenharmony_ciconst char *groupId = "echo";
25060ff233Sopenharmony_ci
26060ff233Sopenharmony_ciusing namespace testing::ext;
27060ff233Sopenharmony_cinamespace OHOS {
28060ff233Sopenharmony_ciclass FileTest : public testing::Test {
29060ff233Sopenharmony_cipublic:
30060ff233Sopenharmony_ci    static void SetUpTestCase();
31060ff233Sopenharmony_ci    static void TearDownTestCase();
32060ff233Sopenharmony_ci    void SetUp();
33060ff233Sopenharmony_ci    void TearDown() {};
34060ff233Sopenharmony_ci};
35060ff233Sopenharmony_ci
36060ff233Sopenharmony_ciint32_t g_sessionId = -1;
37060ff233Sopenharmony_cibool g_sessionEnabled = false;
38060ff233Sopenharmony_ci
39060ff233Sopenharmony_cistatic inline int32_t WaitConnectionReady(int32_t sessionId, uint32_t timeout)
40060ff233Sopenharmony_ci{
41060ff233Sopenharmony_ci    while (!g_sessionEnabled && (timeout--) > 0) {
42060ff233Sopenharmony_ci        sleep(1);
43060ff233Sopenharmony_ci    }
44060ff233Sopenharmony_ci
45060ff233Sopenharmony_ci    if (!g_sessionEnabled) {
46060ff233Sopenharmony_ci        LOG("%s:OpenSession timeout!", __func__);
47060ff233Sopenharmony_ci        return -1;
48060ff233Sopenharmony_ci    }
49060ff233Sopenharmony_ci    return 0;
50060ff233Sopenharmony_ci}
51060ff233Sopenharmony_ci
52060ff233Sopenharmony_cistatic int32_t FtOnSendFileProcess(int32_t sessionId, uint64_t bytesUpload, uint64_t bytesTotal)
53060ff233Sopenharmony_ci{
54060ff233Sopenharmony_ci    LOG("%s:sessionId=%d,bytesUpload=%" PRIu64 ", bytesTotal=%" PRIu64, __func__, sessionId, bytesUpload, bytesTotal);
55060ff233Sopenharmony_ci    return 0;
56060ff233Sopenharmony_ci}
57060ff233Sopenharmony_cistatic int32_t FtOnSendFileFinished(int32_t sessionId, const char *firstFile)
58060ff233Sopenharmony_ci{
59060ff233Sopenharmony_ci    LOG("%s:sessionId=%d,firstfile=%s", __func__, sessionId, firstFile);
60060ff233Sopenharmony_ci    return 0;
61060ff233Sopenharmony_ci}
62060ff233Sopenharmony_cistatic void FtOnFileTransError(int32_t sessionId)
63060ff233Sopenharmony_ci{
64060ff233Sopenharmony_ci    LOG("%s:sessionId=%d", __func__, sessionId);
65060ff233Sopenharmony_ci}
66060ff233Sopenharmony_ci
67060ff233Sopenharmony_cistatic int32_t EsOnSessionOpened(int32_t sessionId, int32_t result)
68060ff233Sopenharmony_ci{
69060ff233Sopenharmony_ci    LOG("%s:enter, sessionId=%d, result=%d", __func__, sessionId, result);
70060ff233Sopenharmony_ci    if (sessionId == g_sessionId && result == 0) {
71060ff233Sopenharmony_ci        g_sessionEnabled = true;
72060ff233Sopenharmony_ci    }
73060ff233Sopenharmony_ci    return 0;
74060ff233Sopenharmony_ci}
75060ff233Sopenharmony_cistatic void EsOnSessionClosed(int32_t sessionId)
76060ff233Sopenharmony_ci{
77060ff233Sopenharmony_ci    LOG("%s:enter", __func__);
78060ff233Sopenharmony_ci    if (sessionId == g_sessionId) {
79060ff233Sopenharmony_ci        g_sessionEnabled = false;
80060ff233Sopenharmony_ci        g_sessionId = -1;
81060ff233Sopenharmony_ci    }
82060ff233Sopenharmony_ci}
83060ff233Sopenharmony_civoid FileTest::SetUpTestCase()
84060ff233Sopenharmony_ci{
85060ff233Sopenharmony_ci    static ISessionListener sessionListener = {.OnSessionOpened = EsOnSessionOpened,
86060ff233Sopenharmony_ci        .OnSessionClosed = EsOnSessionClosed,
87060ff233Sopenharmony_ci        .OnBytesReceived = EsOnDataReceived,
88060ff233Sopenharmony_ci        .OnMessageReceived = EsOnDataReceived,
89060ff233Sopenharmony_ci        .OnStreamReceived = EsOnStreamReceived,
90060ff233Sopenharmony_ci        .OnQosEvent = EsOnQosEvent};
91060ff233Sopenharmony_ci
92060ff233Sopenharmony_ci    ASSERT_EQ(0, CreateSessionServer(ECHO_SERVICE_PKGNAME, ECHO_SERVICE_SESSION_NAME, &sessionListener));
93060ff233Sopenharmony_ci
94060ff233Sopenharmony_ci    static IFileSendListener fileSendListener = {
95060ff233Sopenharmony_ci        .OnSendFileProcess = FtOnSendFileProcess,
96060ff233Sopenharmony_ci        .OnSendFileFinished = FtOnSendFileFinished,
97060ff233Sopenharmony_ci        .OnFileTransError = FtOnFileTransError,
98060ff233Sopenharmony_ci    };
99060ff233Sopenharmony_ci
100060ff233Sopenharmony_ci    ASSERT_EQ(0, SetFileSendListener(ECHO_SERVICE_PKGNAME, ECHO_SERVICE_SESSION_NAME, &fileSendListener));
101060ff233Sopenharmony_ci}
102060ff233Sopenharmony_ci
103060ff233Sopenharmony_civoid FileTest::TearDownTestCase()
104060ff233Sopenharmony_ci{
105060ff233Sopenharmony_ci    EXPECT_EQ(0, RemoveSessionServer(ECHO_SERVICE_PKGNAME, ECHO_SERVICE_SESSION_NAME));
106060ff233Sopenharmony_ci};
107060ff233Sopenharmony_ci
108060ff233Sopenharmony_cistatic SessionAttribute *GetSessionAttr()
109060ff233Sopenharmony_ci{
110060ff233Sopenharmony_ci    static SessionAttribute attr = {
111060ff233Sopenharmony_ci        .dataType = TYPE_FILE,
112060ff233Sopenharmony_ci        .linkTypeNum = 1,
113060ff233Sopenharmony_ci        .linkType = {LINK_TYPE_BR}
114060ff233Sopenharmony_ci    };
115060ff233Sopenharmony_ci    return &attr;
116060ff233Sopenharmony_ci}
117060ff233Sopenharmony_ci
118060ff233Sopenharmony_civoid FileTest::SetUp()
119060ff233Sopenharmony_ci{
120060ff233Sopenharmony_ci    DeviceManager::Instance()->WaitNetworkSizeMoreThan(1);
121060ff233Sopenharmony_ci};
122060ff233Sopenharmony_ci
123060ff233Sopenharmony_ciHWTEST_F(FileTest, SendFileDstNULL, TestSize.Level0)
124060ff233Sopenharmony_ci{
125060ff233Sopenharmony_ci    g_sessionId = OpenSession(ECHO_SERVICE_SESSION_NAME, ECHO_SERVICE_SESSION_NAME,
126060ff233Sopenharmony_ci        DeviceManager::Instance()->GetRemoteByIndex(0).c_str(), groupId, GetSessionAttr());
127060ff233Sopenharmony_ci    ASSERT_GT(g_sessionId, 0);
128060ff233Sopenharmony_ci
129060ff233Sopenharmony_ci    const char *sFileList[] = {"/data/send_files/test_a.jpg", "/data/send_files/test_b.jpg"};
130060ff233Sopenharmony_ci
131060ff233Sopenharmony_ci    ASSERT_EQ(WaitConnectionReady(g_sessionId, 20), 0);
132060ff233Sopenharmony_ci    LOG("SendFile with sessionId %d", g_sessionId);
133060ff233Sopenharmony_ci    EXPECT_EQ(0, SendFile(g_sessionId, sFileList, nullptr, sizeof(sFileList) / sizeof(const char *)));
134060ff233Sopenharmony_ci
135060ff233Sopenharmony_ci    CloseSession(g_sessionId);
136060ff233Sopenharmony_ci}
137060ff233Sopenharmony_ci}; // namespace OHOS
138