• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "SoftBus_Test_Permission.h"
19 #include "net_trans_common.h"
20 #include "wifi_utils.h"
21 
22 static int g_waitFlag = WAIT_DEF_VALUE;
23 
FileSessionOpened(int sessionId,int result)24 static int FileSessionOpened(int sessionId, int result)
25 {
26     LOG("[cb][file]open session sid[%d],rst[%d]", sessionId, result);
27     if (result == SOFTBUS_OK) {
28         g_waitFlag = WAIT_SUCCESS_VALUE;
29     } else {
30         g_waitFlag = WAIT_FAIL_VALUE;
31     }
32     return SOFTBUS_OK;
33 }
34 
FileSessionClosed(int sessionId)35 static void FileSessionClosed(int sessionId)
36 {
37     LOG("[cb][file]close session sid[%d]", sessionId);
38 }
39 
FileBytesReceived(int sessionId,const void * data,unsigned int dataLen)40 static void FileBytesReceived(int sessionId, const void* data, unsigned int dataLen)
41 {
42     LOG("[cb][file]ByteRec sid:%d, data len:%d", sessionId, dataLen);
43     if (data == NULL) {
44         LOG("[cb][file]ByteRec invalid data=null sid[%d]", sessionId);
45         g_waitFlag = WAIT_FAIL_VALUE;
46     } else {
47         g_waitFlag = WAIT_SUCCESS_VALUE;
48     }
49 }
50 
FileMessageReceived(int sessionId,const void * data,unsigned int dataLen)51 static void FileMessageReceived(int sessionId, const void* data, unsigned int dataLen)
52 {
53     LOG("[cb][file]MessageRec sid:%d, data len:%d", sessionId, dataLen);
54     if (data == NULL) {
55         LOG("[cb][file]MessageRec invalid data=null sid[%d]", sessionId);
56         g_waitFlag = WAIT_FAIL_VALUE;
57     } else {
58         g_waitFlag = WAIT_SUCCESS_VALUE;
59     }
60 }
61 
62 static SessionAttribute g_fileSessionAttr = {
63     .dataType = TYPE_FILE,
64 };
65 
66 static ISessionListener g_fileSessionListener = {
67     .OnSessionOpened = FileSessionOpened,
68     .OnSessionClosed = FileSessionClosed,
69     .OnBytesReceived = FileBytesReceived,
70     .OnMessageReceived = FileMessageReceived,
71 };
72 static const char* g_fileOne[] = {
73     "/data/SoftBusNetTest_BindSelf_1.xml",
74 };
75 
76 using namespace testing::ext;
77 
78 class TransFileFuncTest : public testing::Test {
79 public:
80     // 测试套前置和后置操作
81     static void SetUpTestCase();
82     static void TearDownTestCase();
83 
84     // 测试用例前置和后置操作
85     void SetUp();
86     void TearDown();
87 };
88 
SetUp()89 void TransFileFuncTest::SetUp() {}
90 
TearDown()91 void TransFileFuncTest::TearDown() {}
92 
SetUpTestCase()93 void TransFileFuncTest::SetUpTestCase()
94 {
95     LOG("SetUp begin");
96     TestSetUp();
97     SoftBus_Test_Permission::AddPermission(DEF_PKG_NAME);
98     int ret = RegisterDeviceStateDefCallback();
99     EXPECT_EQ(SOFTBUS_OK, ret) << "call reg node state callback fail";
100 
101     ret = CheckRemoteDeviceIsNull(BOOL_TRUE);
102     ASSERT_EQ(SOFTBUS_OK, ret) << "get node fail,please check network";
103 
104     // set listener
105     ret = SetFileSendListener(DEF_PKG_NAME, SESSION_NAME_FILE, GetSendFileListener());
106     EXPECT_EQ(SOFTBUS_OK, ret) << "call SetFileSendListener fail";
107     ret = SetFileReceiveListener(DEF_PKG_NAME, SESSION_NAME_FILE, GetRecvFileListener(), RECV_FILE_PATH);
108     EXPECT_EQ(SOFTBUS_OK, ret) << "call SetFileSendListener fail";
109 
110     system(" truncate -s 3M /data/SoftBusNetTest_BindSelf_1.xml");
111     LOG("SetUp end");
112 }
113 
TearDownTestCase()114 void TransFileFuncTest::TearDownTestCase()
115 {
116     int ret = UnRegisterDeviceStateDefCallback();
117     EXPECT_EQ(SOFTBUS_OK, ret) << "call unReg node state callback fail";
118 
119     TestTearDown();
120     SoftBus_Test_Permission::RemovePermission(DEF_PKG_NAME);
121 }
122 
WaitFile(int timeout)123 static int WaitFile(int timeout)
124 {
125     LOG("start waitfile,timeout:%d", timeout);
126     int count = 0;
127     int t = timeout;
128     while (t > 0) {
129         sleep(1);
130         if (g_waitFlag != WAIT_DEF_VALUE) {
131             LOG("waitfile success[flag:%d][time:%d]", g_waitFlag, count);
132             break;
133         }
134         t--;
135         count++;
136     }
137     if (g_waitFlag != WAIT_SUCCESS_VALUE) {
138         LOG("waitfile fail[exp:%d, real:%d][used time:%d]", WAIT_SUCCESS_VALUE, g_waitFlag, count);
139         return SOFTBUS_ERR;
140     }
141     return SOFTBUS_OK;
142 }
143 
144 /**
145  * @tc.number : SUB_Softbus_Trans_SendFile_Func_0100
146  * @tc.name     : test SendFile
147  * @tc.desc       : Test send file function
148  * @tc.type       : FUNC
149  * @tc.size        : MediumTest
150  */
151 HWTEST_F(TransFileFuncTest, SUB_Softbus_Trans_SendFile_Func_0100, TestSize.Level2)
152 {
153     int ret;
154     ret = CreateSessionServer(DEF_PKG_NAME, SESSION_NAME_FILE, &g_fileSessionListener);
155     EXPECT_EQ(SOFTBUS_OK, ret) << "CreateSS[file] fail";
156 
157     g_waitFlag = WAIT_DEF_VALUE;
158     int sid = OpenSession(SESSION_NAME_FILE, SESSION_NAME_FILE, GetNetworkId(), DEF_GROUP_ID, &g_fileSessionAttr);
159     EXPECT_TRUE(sid >= SESSION_ID_MIN) << "call OpenSession[file] fail, sid=" << sid;
160     if (sid >= SESSION_ID_MIN) {
161         ret = WaitFile(10);
162         EXPECT_EQ(SOFTBUS_OK, ret) << "wait opensession fail[file]";
163     }
164 
165     ResetWaitFlag();
166     ret = SendFile(sid, g_fileOne, NULL, 1);
167     EXPECT_EQ(SOFTBUS_OK, ret) << "call SendFile fail";
168     ret = Wait(10);
169     EXPECT_EQ(SOFTBUS_OK, ret) << "wait send fail rst fail";
170 
171     CloseSession(sid);
172     ret = RemoveSessionServer(DEF_PKG_NAME, SESSION_NAME_FILE);
173     EXPECT_EQ(SOFTBUS_OK, ret) << "RemoveSS[proxy] fail";
174 }
175