1 /*
2 * Copyright (c) 2024 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 "iservice_registry.h"
17 #include "softbus_common.h"
18 #include "softbus_error_code.h"
19 #include "softbus_server.h"
20 #include "softbus_server_test_mock.h"
21 #include "system_ability_definition.h"
22 #include <gtest/gtest.h>
23
24 using namespace testing;
25 using namespace testing::ext;
26
27 namespace OHOS {
28
29 #define TEST_SESSION_NAME_SIZE_MAX 256
30
31 class SoftbusServerTest : public testing::Test {
32 public:
SoftbusServerTest()33 SoftbusServerTest()
34 {}
~SoftbusServerTest()35 ~SoftbusServerTest()
36 {}
SetUpTestCase()37 static void SetUpTestCase()
38 {}
TearDownTestCase()39 static void TearDownTestCase()
40 {}
SetUp()41 void SetUp() override
42 {}
TearDown()43 void TearDown() override
44 {}
45 };
46
GenerateRemoteObject(void)47 static sptr<IRemoteObject> GenerateRemoteObject(void)
48 {
49 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
50 if (samgr != nullptr) {
51 return samgr->GetSystemAbility(SOFTBUS_SERVER_SA_ID);
52 }
53 return nullptr;
54 }
55
56 /**
57 * @tc.name: SoftbusServerTest001
58 * @tc.desc: Verify the SoftbusRegisterService function.
59 * @tc.type: FUNC
60 * @tc.require:
61 */
62 HWTEST_F(SoftbusServerTest, SoftbusServerTest001, TestSize.Level1)
63 {
64 sptr<OHOS::SoftBusServer> softBusServer = new OHOS::SoftBusServer(SOFTBUS_SERVER_SA_ID, true);
65 ASSERT_NE(nullptr, softBusServer);
66
67 int32_t ret = softBusServer->SoftbusRegisterService("test", nullptr);
68 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
69
70 sptr<IRemoteObject> obj = GenerateRemoteObject();
71 ret = softBusServer->SoftbusRegisterService("test", obj);
72 EXPECT_EQ(SOFTBUS_OK, ret);
73 }
74
75 /**
76 * @tc.name: SoftbusServerTest002
77 * @tc.desc: Verify the OpenAuthSession function.
78 * @tc.type: FUNC
79 * @tc.require:
80 */
81 HWTEST_F(SoftbusServerTest, SoftbusServerTest002, TestSize.Level1)
82 {
83 sptr<OHOS::SoftBusServer> softBusServer = new OHOS::SoftBusServer(SOFTBUS_SERVER_SA_ID, true);
84 ASSERT_NE(nullptr, softBusServer);
85 ConnectionAddr addr;
86 addr.type = CONNECTION_ADDR_MAX;
87
88 int32_t ret = softBusServer->OpenAuthSession("test", nullptr);
89 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
90
91 ret = softBusServer->OpenAuthSession("test", &addr);
92 EXPECT_EQ(SOFTBUS_TRANS_INVALID_CONNECT_TYPE, ret);
93 }
94
95 /**
96 * @tc.name: SoftbusServerTest003
97 * @tc.desc: Verify the Dump function.
98 * @tc.type: FUNC
99 * @tc.require:
100 */
101 HWTEST_F(SoftbusServerTest, SoftbusServerTest003, TestSize.Level1)
102 {
103 sptr<OHOS::SoftBusServer> softBusServer = new OHOS::SoftBusServer(SOFTBUS_SERVER_SA_ID, true);
104 ASSERT_NE(nullptr, softBusServer);
105 int32_t fd = -1;
106 std::vector<std::u16string> args;
107
108 int32_t ret = softBusServer->Dump(fd, args);
109 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
110
111 fd = 0;
112 ret = softBusServer->Dump(fd, args);
113 EXPECT_EQ(SOFTBUS_OK, ret);
114 }
115
116 /**
117 * @tc.name: SoftbusServerTest004
118 * @tc.desc: Verify the GetSoftbusSpecObject function.
119 * @tc.type: FUNC
120 * @tc.require:
121 */
122 HWTEST_F(SoftbusServerTest, SoftbusServerTest004, TestSize.Level1)
123 {
124 sptr<OHOS::SoftBusServer> softBusServer = new OHOS::SoftBusServer(SOFTBUS_SERVER_SA_ID, true);
125 ASSERT_NE(nullptr, softBusServer);
126 sptr<IRemoteObject> object = nullptr;
127 int32_t ret = softBusServer->GetSoftbusSpecObject(object);
128 EXPECT_EQ(ret, SOFTBUS_OK);
129 }
130
131 /**
132 * @tc.name: SoftbusServerTest005
133 * @tc.desc: Verify the GetBusCenterExObj function.
134 * @tc.type: FUNC
135 * @tc.require:
136 */
137 HWTEST_F(SoftbusServerTest, SoftbusServerTest005, TestSize.Level1)
138 {
139 sptr<OHOS::SoftBusServer> softBusServer = new OHOS::SoftBusServer(SOFTBUS_SERVER_SA_ID, true);
140 ASSERT_NE(nullptr, softBusServer);
141 sptr<IRemoteObject> object = nullptr;
142 int32_t ret = softBusServer->GetBusCenterExObj(object);
143 EXPECT_EQ(ret, SOFTBUS_OK);
144 }
145
146 /**
147 * @tc.name: SoftbusServerTest006
148 * @tc.desc: Verify the EvaluateQos function.
149 * @tc.type: FUNC
150 * @tc.require:
151 */
152 HWTEST_F(SoftbusServerTest, SoftbusServerTest006, TestSize.Level1)
153 {
154 sptr<OHOS::SoftBusServer> softBusServer = new OHOS::SoftBusServer(SOFTBUS_SERVER_SA_ID, true);
155 ASSERT_NE(nullptr, softBusServer);
156 NiceMock<SoftbusServerTestInterfaceMock> softbusServerMock;
157 char networkId[NETWORK_ID_BUF_LEN] = "test";
158 TransDataType dataType = DATA_TYPE_BYTES;
159
160 EXPECT_CALL(softbusServerMock, IsValidString(_, _))
161 .WillRepeatedly(Return(false));
162 int32_t ret = softBusServer->EvaluateQos(networkId, dataType, nullptr, 0);
163 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
164
165 EXPECT_CALL(softbusServerMock, IsValidString(_, _))
166 .WillRepeatedly(Return(true));
167 ret = softBusServer->EvaluateQos(networkId, dataType, nullptr, 0);
168 EXPECT_EQ(SOFTBUS_NETWORK_NODE_OFFLINE, ret);
169 }
170 }