1 /*
2 * Copyright (c) 2022 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 <gtest/gtest.h>
17
18 #include <cinttypes>
19 #include "test_suite.h"
20 #include "transport/session.h"
21
22 #include "device_manager.h"
23
24 const char *groupId = "echo";
25
26 using namespace testing::ext;
27 namespace OHOS {
28 class FileTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
31 static void TearDownTestCase();
32 void SetUp();
TearDown()33 void TearDown() {};
34 };
35
36 int g_sessionId = -1;
37 bool g_sessionEnabled = false;
38
WaitConnectionReady(int sessionId,uint32_t timeout)39 static inline int WaitConnectionReady(int sessionId, uint32_t timeout)
40 {
41 while (!g_sessionEnabled && (timeout--) > 0) {
42 sleep(1);
43 }
44
45 if (!g_sessionEnabled) {
46 LOG("%s:OpenSession timeout!", __func__);
47 return -1;
48 }
49 return 0;
50 }
51
FtOnSendFileProcess(int sessionId,uint64_t bytesUpload,uint64_t bytesTotal)52 static int FtOnSendFileProcess(int sessionId, uint64_t bytesUpload, uint64_t bytesTotal)
53 {
54 LOG("%s:sessionId=%d,bytesUpload=%" PRIu64 ", bytesTotal=%" PRIu64, __func__, sessionId, bytesUpload, bytesTotal);
55 return 0;
56 }
FtOnSendFileFinished(int sessionId,const char * firstFile)57 static int FtOnSendFileFinished(int sessionId, const char *firstFile)
58 {
59 LOG("%s:sessionId=%d,firstfile=%s", __func__, sessionId, firstFile);
60 return 0;
61 }
FtOnFileTransError(int sessionId)62 static void FtOnFileTransError(int sessionId)
63 {
64 LOG("%s:sessionId=%d", __func__, sessionId);
65 }
66
EsOnSessionOpened(int sessionId,int result)67 static int EsOnSessionOpened(int sessionId, int result)
68 {
69 LOG("%s:enter, sessionId=%d, result=%d", __func__, sessionId, result);
70 if (sessionId == g_sessionId && result == 0) {
71 g_sessionEnabled = true;
72 }
73 return 0;
74 }
EsOnSessionClosed(int sessionId)75 static void EsOnSessionClosed(int sessionId)
76 {
77 LOG("%s:enter", __func__);
78 if (sessionId == g_sessionId) {
79 g_sessionEnabled = false;
80 g_sessionId = -1;
81 }
82 }
SetUpTestCase()83 void FileTest::SetUpTestCase()
84 {
85 static ISessionListener sessionListener = {.OnSessionOpened = EsOnSessionOpened,
86 .OnSessionClosed = EsOnSessionClosed,
87 .OnBytesReceived = EsOnDataReceived,
88 .OnMessageReceived = EsOnDataReceived,
89 .OnStreamReceived = EsOnStreamReceived,
90 .OnQosEvent = EsOnQosEvent};
91
92 ASSERT_EQ(0, CreateSessionServer(ECHO_SERVICE_PKGNAME, ECHO_SERVICE_SESSION_NAME, &sessionListener));
93
94 static IFileSendListener fileSendListener = {
95 .OnSendFileProcess = FtOnSendFileProcess,
96 .OnSendFileFinished = FtOnSendFileFinished,
97 .OnFileTransError = FtOnFileTransError,
98 };
99
100 ASSERT_EQ(0, SetFileSendListener(ECHO_SERVICE_PKGNAME, ECHO_SERVICE_SESSION_NAME, &fileSendListener));
101 }
102
TearDownTestCase()103 void FileTest::TearDownTestCase()
104 {
105 EXPECT_EQ(0, RemoveSessionServer(ECHO_SERVICE_PKGNAME, ECHO_SERVICE_SESSION_NAME));
106 };
107
GetSessionAttr()108 static SessionAttribute *GetSessionAttr()
109 {
110 static SessionAttribute attr = {
111 .dataType = TYPE_FILE,
112 .linkTypeNum = 1,
113 .linkType = {LINK_TYPE_BR}
114 };
115 return &attr;
116 }
117
SetUp()118 void FileTest::SetUp()
119 {
120 DeviceManager::Instance()->WaitNetworkSizeMoreThan(1);
121 };
122
123 HWTEST_F(FileTest, SendFileDstNULL, TestSize.Level0)
124 {
125 g_sessionId = OpenSession(ECHO_SERVICE_SESSION_NAME, ECHO_SERVICE_SESSION_NAME,
126 DeviceManager::Instance()->GetRemoteByIndex(0).c_str(), groupId, GetSessionAttr());
127 ASSERT_GT(g_sessionId, 0);
128
129 const char *sFileList[] = {"/data/send_files/test_a.jpg", "/data/send_files/test_b.jpg"};
130
131 ASSERT_EQ(WaitConnectionReady(g_sessionId, 20), 0);
132 LOG("SendFile with sessionId %d", g_sessionId);
133 EXPECT_EQ(0, SendFile(g_sessionId, sFileList, nullptr, sizeof(sFileList) / sizeof(const char *)));
134
135 CloseSession(g_sessionId);
136 }
137 }; // namespace OHOS
138