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 <memory> 17 #include <unistd.h> 18 19 #include "gtest/gtest.h" 20 #include "network/softbus/softbus_agent.h" 21 #include "network/softbus/softbus_session.h" 22 #include "utils_log.h" 23 24 namespace OHOS { 25 namespace Storage { 26 namespace DistributedFile { 27 namespace Test { 28 using namespace testing::ext; 29 using namespace std; 30 31 constexpr int E_OK = 0; 32 constexpr int USER_ID = 100; 33 static const string SAME_ACCOUNT = "account"; 34 35 class SoftbusAgentTest : public testing::Test { 36 public: SetUpTestCase(void)37 static void SetUpTestCase(void) {}; TearDownTestCase(void)38 static void TearDownTestCase(void) {}; SetUp()39 void SetUp() {}; TearDown()40 void TearDown() {}; 41 }; 42 43 /** 44 * @tc.name: SoftbusAgentTest_OnSessionOpened_0100 45 * @tc.desc: Verify the OnSessionOpened function. 46 * @tc.type: FUNC 47 * @tc.require: SR000H0387 48 */ 49 HWTEST_F(SoftbusAgentTest, SoftbusAgentTest_OnSessionOpened_0100, TestSize.Level1) 50 { 51 GTEST_LOG_(INFO) << "SoftbusAgentTest_OnSessionOpened_0100 start"; 52 auto mp = make_unique<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, SAME_ACCOUNT)); 53 shared_ptr<MountPoint> smp = move(mp); 54 weak_ptr<MountPoint> wmp(smp); 55 std::shared_ptr<SoftbusAgent> agent = std::make_shared<SoftbusAgent>(wmp); 56 __anon21022d4b0102(std::shared_ptr<SoftbusAgent> ag) 57 auto execFun = [](std::shared_ptr<SoftbusAgent> ag) { 58 const int sessionId = 1; 59 const int result = E_OK; 60 int ret = ag->OnSessionOpened(sessionId, result); 61 EXPECT_TRUE(ret == E_OK); 62 }; 63 64 std::thread execThread(execFun, agent); 65 sleep(1); 66 execThread.join(); 67 GTEST_LOG_(INFO) << "SoftbusAgentTest_OnSessionOpened_0100 end"; 68 } 69 70 /** 71 * @tc.name: SoftbusAgentTest_OnSessionOpened_0200 72 * @tc.desc: Verify the OnSessionOpened function. 73 * @tc.type: FUNC 74 * @tc.require: SR000H0387 75 */ 76 HWTEST_F(SoftbusAgentTest, SoftbusAgentTest_OnSessionOpened_0200, TestSize.Level1) 77 { 78 GTEST_LOG_(INFO) << "SoftbusAgentTest_OnSessionOpened_0200 start"; 79 auto mp = make_unique<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, SAME_ACCOUNT)); 80 shared_ptr<MountPoint> smp = move(mp); 81 weak_ptr<MountPoint> wmp(smp); 82 std::shared_ptr<SoftbusAgent> agent = std::make_shared<SoftbusAgent>(wmp); 83 __anon21022d4b0202(std::shared_ptr<SoftbusAgent> ag) 84 auto execFun = [](std::shared_ptr<SoftbusAgent> ag) { 85 const int sessionId = 1; 86 const int result = -1; 87 int ret = ag->OnSessionOpened(sessionId, result); 88 EXPECT_TRUE(ret == result); 89 }; 90 91 std::thread execThread(execFun, agent); 92 sleep(1); 93 execThread.join(); 94 GTEST_LOG_(INFO) << "SoftbusAgentTest_OnSessionOpened_0200 end"; 95 } 96 97 /** 98 * @tc.name: SoftbusAgentTest_OnSessionClosed_0100 99 * @tc.desc: Verify the OnSessionClosed function. 100 * @tc.type: FUNC 101 * @tc.require: SR000H0387 102 */ 103 HWTEST_F(SoftbusAgentTest, SoftbusAgentTest_OnSessionClosed_0100, TestSize.Level1) 104 { 105 GTEST_LOG_(INFO) << "SoftbusAgentTest_OnSessionClosed_0100 start"; 106 auto mp = make_unique<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, SAME_ACCOUNT)); 107 shared_ptr<MountPoint> smp = move(mp); 108 weak_ptr<MountPoint> wmp(smp); 109 std::shared_ptr<SoftbusAgent> agent = std::make_shared<SoftbusAgent>(wmp); 110 const int sessionId = 1; 111 bool res = true; 112 try { 113 agent->OnSessionClosed(sessionId); 114 } catch (const exception &e) { 115 res = false; 116 LOGE("%{public}s", e.what()); 117 } 118 EXPECT_TRUE(res == true); 119 GTEST_LOG_(INFO) << "SoftbusAgentTest_OnSessionClosed_0100 end"; 120 } 121 122 /** 123 * @tc.name: SoftbusAgentTest_Start_0100 124 * @tc.desc: Verify the Start function. 125 * @tc.type: FUNC 126 * @tc.require: SR000H0387 127 */ 128 HWTEST_F(SoftbusAgentTest, SoftbusAgentTest_Start_0100, TestSize.Level1) 129 { 130 GTEST_LOG_(INFO) << "SoftbusAgentTest_Start_0100 start"; 131 auto mp = make_unique<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, SAME_ACCOUNT)); 132 shared_ptr<MountPoint> smp = move(mp); 133 weak_ptr<MountPoint> wmp(smp); 134 std::shared_ptr<SoftbusAgent> agent = std::make_shared<SoftbusAgent>(wmp); 135 bool res = true; 136 try { 137 agent->Start(); 138 } catch (const exception &e) { 139 res = false; 140 LOGE("%{public}s", e.what()); 141 } 142 EXPECT_TRUE(res == false); 143 GTEST_LOG_(INFO) << "SoftbusAgentTest_Start_0100 end"; 144 } 145 146 /** 147 * @tc.name: SoftbusAgentTest_Stop_0100 148 * @tc.desc: Verify the Stop function. 149 * @tc.type: FUNC 150 * @tc.require: SR000H0387 151 */ 152 HWTEST_F(SoftbusAgentTest, SoftbusAgentTest_Stop_0100, TestSize.Level1) 153 { 154 GTEST_LOG_(INFO) << "SoftbusAgentTest_Stop_0100 start"; 155 auto mp = make_unique<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, SAME_ACCOUNT)); 156 shared_ptr<MountPoint> smp = move(mp); 157 weak_ptr<MountPoint> wmp(smp); 158 std::shared_ptr<SoftbusAgent> agent = std::make_shared<SoftbusAgent>(wmp); 159 bool res = true; 160 try { 161 agent->Stop(); 162 } catch (const exception &e) { 163 res = false; 164 LOGE("%{public}s", e.what()); 165 } 166 EXPECT_TRUE(res == false); 167 GTEST_LOG_(INFO) << "SoftbusAgentTest_Stop_0100 end"; 168 } 169 170 /** 171 * @tc.name: SoftbusAgentTest_ConnectOnlineDevices_0100 172 * @tc.desc: Verify the ConnectOnlineDevices function. 173 * @tc.type: FUNC 174 * @tc.require: SR000H0387 175 */ 176 HWTEST_F(SoftbusAgentTest, SoftbusAgentTest_ConnectOnlineDevices_0100, TestSize.Level1) 177 { 178 GTEST_LOG_(INFO) << "SoftbusAgentTest_ConnectOnlineDevices_0100 start"; 179 auto mp = make_unique<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, SAME_ACCOUNT)); 180 shared_ptr<MountPoint> smp = move(mp); 181 weak_ptr<MountPoint> wmp(smp); 182 std::shared_ptr<SoftbusAgent> agent = std::make_shared<SoftbusAgent>(wmp); 183 bool res = true; 184 try { 185 agent->ConnectOnlineDevices(); 186 } catch (const exception &e) { 187 res = false; 188 LOGE("%{public}s", e.what()); 189 } 190 EXPECT_TRUE(res == false); 191 GTEST_LOG_(INFO) << "SoftbusAgentTest_ConnectOnlineDevices_0100 end"; 192 } 193 194 /** 195 * @tc.name: SoftbusAgentTest_DisconnectAllDevices_0100 196 * @tc.desc: Verify the DisconnectAllDevices function. 197 * @tc.type: FUNC 198 * @tc.require: SR000H0387 199 */ 200 HWTEST_F(SoftbusAgentTest, SoftbusAgentTest_DisconnectAllDevices_0100, TestSize.Level1) 201 { 202 GTEST_LOG_(INFO) << "SoftbusAgentTest_DisconnectAllDevices_0100 start"; 203 auto mp = make_unique<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, SAME_ACCOUNT)); 204 shared_ptr<MountPoint> smp = move(mp); 205 weak_ptr<MountPoint> wmp(smp); 206 std::shared_ptr<SoftbusAgent> agent = std::make_shared<SoftbusAgent>(wmp); 207 bool res = true; 208 try { 209 agent->DisconnectAllDevices(); 210 } catch (const exception &e) { 211 res = false; 212 LOGE("%{public}s", e.what()); 213 } 214 EXPECT_TRUE(res == true); 215 GTEST_LOG_(INFO) << "SoftbusAgentTest_DisconnectAllDevices_0100 end"; 216 } 217 218 /** 219 * @tc.name: SoftbusAgentTest_ConnectDeviceAsync_0100 220 * @tc.desc: Verify the ConnectDeviceAsync function. 221 * @tc.type: FUNC 222 * @tc.require: SR000H0387 223 */ 224 HWTEST_F(SoftbusAgentTest, SoftbusAgentTest_ConnectDeviceAsync_0100, TestSize.Level1) 225 { 226 GTEST_LOG_(INFO) << "SoftbusAgentTest_ConnectDeviceAsync_0100 start"; 227 auto mp = make_unique<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, SAME_ACCOUNT)); 228 shared_ptr<MountPoint> smp = move(mp); 229 weak_ptr<MountPoint> wmp(smp); 230 std::shared_ptr<SoftbusAgent> agent = std::make_shared<SoftbusAgent>(wmp); 231 DistributedHardware::DmDeviceInfo info = { 232 .deviceId = "testdevid", 233 .deviceName = "testdevname", 234 .deviceTypeId = 1, 235 }; 236 DeviceInfo devInfo(info); 237 238 bool res = true; 239 try { 240 agent->ConnectDeviceAsync(devInfo); 241 } catch (const exception &e) { 242 res = false; 243 LOGE("%{public}s", e.what()); 244 } 245 EXPECT_TRUE(res == false); 246 GTEST_LOG_(INFO) << "SoftbusAgentTest_ConnectDeviceAsync_0100 end"; 247 } 248 249 /** 250 * @tc.name: SoftbusAgentTest_DisconnectDevice_0100 251 * @tc.desc: Verify the DisconnectDevice function. 252 * @tc.type: FUNC 253 * @tc.require: SR000H0387 254 */ 255 HWTEST_F(SoftbusAgentTest, SoftbusAgentTest_DisconnectDevice_0100, TestSize.Level1) 256 { 257 GTEST_LOG_(INFO) << "SoftbusAgentTest_DisconnectDevice_0100 start"; 258 auto mp = make_unique<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, SAME_ACCOUNT)); 259 shared_ptr<MountPoint> smp = move(mp); 260 weak_ptr<MountPoint> wmp(smp); 261 std::shared_ptr<SoftbusAgent> agent = std::make_shared<SoftbusAgent>(wmp); 262 DistributedHardware::DmDeviceInfo info = { 263 .deviceId = "testdevid", 264 .deviceName = "testdevname", 265 .deviceTypeId = 1, 266 }; 267 DeviceInfo devInfo(info); 268 269 bool res = true; 270 try { 271 agent->DisconnectDevice(devInfo); 272 } catch (const exception &e) { 273 res = false; 274 LOGE("%{public}s", e.what()); 275 } 276 EXPECT_TRUE(res == true); 277 GTEST_LOG_(INFO) << "SoftbusAgentTest_DisconnectDevice_0100 end"; 278 } 279 280 /** 281 * @tc.name: SoftbusAgentTest_AcceptSession_0100 282 * @tc.desc: Verify the AcceptSession function. 283 * @tc.type: FUNC 284 * @tc.require: SR000H0387 285 */ 286 HWTEST_F(SoftbusAgentTest, SoftbusAgentTest_AcceptSession_0100, TestSize.Level1) 287 { 288 GTEST_LOG_(INFO) << "SoftbusAgentTest_AcceptSession_0100 start"; 289 auto mp = make_unique<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, SAME_ACCOUNT)); 290 shared_ptr<MountPoint> smp = move(mp); 291 weak_ptr<MountPoint> wmp(smp); 292 std::shared_ptr<SoftbusAgent> agent = std::make_shared<SoftbusAgent>(wmp); 293 const int testSessionId = 99; 294 auto session = make_shared<SoftbusSession>(testSessionId); 295 bool res = true; 296 try { 297 agent->AcceptSession(session); 298 } catch (const exception &e) { 299 res = false; 300 LOGE("%{public}s", e.what()); 301 } 302 EXPECT_TRUE(res == true); 303 GTEST_LOG_(INFO) << "SoftbusAgentTest_AcceptSession_0100 end"; 304 } 305 306 /** 307 * @tc.name: SoftbusAgentTest_GetMountPoint_0100 308 * @tc.desc: Verify the GetMountPoint function. 309 * @tc.type: FUNC 310 * @tc.require: SR000H0387 311 */ 312 HWTEST_F(SoftbusAgentTest, SoftbusAgentTest_GetMountPoint_0100, TestSize.Level1) 313 { 314 GTEST_LOG_(INFO) << "SoftbusAgentTest_GetMountPoint_0100 start"; 315 auto mp = make_unique<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, SAME_ACCOUNT)); 316 shared_ptr<MountPoint> smp = move(mp); 317 weak_ptr<MountPoint> wmp(smp); 318 std::shared_ptr<SoftbusAgent> agent = std::make_shared<SoftbusAgent>(wmp); 319 bool res = true; 320 try { 321 agent->GetMountPoint(); 322 } catch (const exception &e) { 323 res = false; 324 LOGE("%{public}s", e.what()); 325 } 326 EXPECT_TRUE(res == true); 327 GTEST_LOG_(INFO) << "SoftbusAgentTest_GetMountPoint_0100 end"; 328 } 329 } // namespace Test 330 } // namespace DistributedFile 331 } // namespace Storage 332 } // namespace OHOS 333