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 <securec.h> 17 18 #include <gtest/gtest.h> 19 #include <unistd.h> 20 #include "disc_nstackx_adapter.h" 21 #include "nstackx.h" 22 #include "bus_center_info_key.h" 23 #include "softbus_adapter_mem.h" 24 #include "softbus_error_code.h" 25 #include "nstackx_error.h" 26 #include "disc_manager.h" 27 #include "lnn_local_net_ledger.h" 28 29 using namespace testing::ext; 30 namespace OHOS { 31 32 class DiscNstackxAdapterTest : public testing::Test { 33 public: SetUpTestCase()34 static void SetUpTestCase() {} TearDownTestCase()35 static void TearDownTestCase() {} SetUp()36 void SetUp() override {} TearDown()37 void TearDown() override {} 38 }; 39 40 /* 41 * @tc.name: testDiscCoapRegisterCb001 42 * @tc.desc: test DiscCoapRegisterCb NULL 43 * @tc.type: FUNC 44 * @tc.require: 45 */ 46 HWTEST_F(DiscNstackxAdapterTest, TestDiscCoapRegisterCb001, TestSize.Level1) 47 { 48 int32_t ret; 49 50 ret = DiscMgrInit(); 51 EXPECT_EQ(ret, SOFTBUS_OK); 52 53 ret = DiscCoapRegisterCb(NULL); 54 EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM); 55 } 56 57 /* 58 * @tc.name: testDiscCoapRegisterCapability001 59 * @tc.desc: test DiscCoapRegisterCapability 60 * @tc.type: FUNC 61 * @tc.require: 62 63 */ 64 HWTEST_F(DiscNstackxAdapterTest, TestDiscCoapRegisterCapability001, TestSize.Level1) 65 { 66 int32_t ret; 67 uint32_t mapNum = 3; 68 NSTACKX_Parameter g_parameter; 69 70 NSTACKX_Init(&g_parameter); 71 ret = DiscCoapRegisterCapability(0, 0); 72 EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM); 73 74 ret = DiscCoapRegisterCapability(mapNum, 0); 75 EXPECT_EQ(ret, SOFTBUS_DISCOVER_COAP_REGISTER_CAP_FAIL); 76 77 NSTACKX_Deinit(); 78 } 79 80 /* 81 * @tc.name: testDiscCoapSetFilterCapability001 82 * @tc.desc: test DiscCoapSetFilterCapability 83 * @tc.type: FUNC 84 * @tc.require: 85 */ 86 HWTEST_F(DiscNstackxAdapterTest, TestDiscCoapSetFilterCapability001, TestSize.Level1) 87 { 88 int32_t ret; 89 90 uint32_t capabilityBitmap[] = {1, 2, 3, 4, 5}; 91 uint32_t capabilityBitmapNum = 1; 92 93 ret = DiscCoapSetFilterCapability(0, capabilityBitmap); 94 EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM); 95 96 ret = DiscCoapSetFilterCapability(capabilityBitmapNum, capabilityBitmap); 97 EXPECT_EQ(ret, SOFTBUS_DISCOVER_COAP_SET_FILTER_CAP_FAIL); 98 } 99 100 /* 101 * @tc.name: testDiscCoapStartDiscovery001 102 * @tc.desc: test DiscCoapStartDiscovery 103 * @tc.type: FUNC 104 * @tc.require: 105 */ 106 HWTEST_F(DiscNstackxAdapterTest, TestDiscCoapStartDiscovery001, TestSize.Level1) 107 { 108 int32_t ret; 109 110 ret = DiscCoapStartDiscovery(NULL); 111 EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM); 112 113 DiscCoapOption *option = (DiscCoapOption*)SoftBusMalloc(sizeof(DiscCoapOption)); 114 ASSERT_TRUE(option != nullptr); 115 memset_s(option, sizeof(DiscCoapOption), 0, sizeof(DiscCoapOption)); 116 option->mode = INVALID_MODE; 117 118 ret = DiscCoapStartDiscovery(option); 119 EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM); 120 SoftBusFree(option); 121 } 122 123 /* 124 * @tc.name: testDiscCoapUpdateLocalIp 125 * @tc.desc: test DiscCoapUpdateLocalIp 126 * @tc.type: FUNC 127 * @tc.require: 128 */ 129 HWTEST_F(DiscNstackxAdapterTest, TestDiscCoapUpdateLocalIp, TestSize.Level1) 130 { 131 int32_t ret = DiscNstackxInit(); 132 EXPECT_EQ(ret, SOFTBUS_OK); 133 134 LinkStatus status = LINK_STATUS_UP; 135 DiscCoapUpdateLocalIp(status); 136 137 status = LINK_STATUS_DOWN; 138 DiscCoapUpdateLocalIp(status); 139 140 status = (LinkStatus)(-1); 141 DiscCoapUpdateLocalIp(status); 142 143 DiscNstackxDeinit(); 144 } 145 146 /* 147 * @tc.name: testDiscCoapRegisterServiceData 148 * @tc.desc: test DiscCoapRegisterServiceData 149 * @tc.type: FUNC 150 * @tc.require: 151 */ 152 HWTEST_F(DiscNstackxAdapterTest, TestDiscCoapRegisterServiceData001, TestSize.Level1) 153 { 154 uint32_t dataLen = 1; 155 int32_t ret = DiscCoapRegisterServiceData(nullptr, dataLen); 156 EXPECT_EQ(ret, SOFTBUS_DISCOVER_COAP_INIT_FAIL); 157 } 158 159 /* 160 * @tc.name: testDiscCoapStopDiscovery 161 * @tc.desc: test DiscCoapStopDiscovery 162 * @tc.type: FUNC 163 * @tc.require: 164 */ 165 HWTEST_F(DiscNstackxAdapterTest, testDiscCoapStopDiscovery001, TestSize.Level1) 166 { 167 int32_t ret = DiscCoapStopDiscovery(); 168 EXPECT_EQ(ret, SOFTBUS_DISCOVER_COAP_STOP_DISCOVER_FAIL); 169 170 DiscCoapUpdateDevName(); 171 } 172 } 173