• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 #include <securec.h>
18 
19 #include "lnn_net_ledger_mock.h"
20 #include "lnn_node_weight.c"
21 #include "lnn_node_weight.h"
22 #include "lnn_service_mock.h"
23 #include "softbus_error_code.h"
24 
25 namespace OHOS {
26 using namespace testing;
27 using namespace testing::ext;
28 
29 constexpr char UDID1[] = "123456789AB";
30 constexpr char UDID2[] = "123456789ab";
31 constexpr int32_t WEIGHT = 10;
32 constexpr int32_t WEIGHT2 = 5;
33 constexpr int32_t WEIGHT3 = 5;
34 
35 class LNNNodeWeightTest : public testing::Test {
36 public:
37     static void SetUpTestCase();
38     static void TearDownTestCase();
39     void SetUp();
40     void TearDown();
41 };
42 
SetUpTestCase()43 void LNNNodeWeightTest::SetUpTestCase() { }
44 
TearDownTestCase()45 void LNNNodeWeightTest::TearDownTestCase() { }
46 
SetUp()47 void LNNNodeWeightTest::SetUp() { }
48 
TearDown()49 void LNNNodeWeightTest::TearDown() { }
50 
51 /*
52  * @tc.name: LNN_COMPARE_NODE_WEIGHT_TEST_001
53  * @tc.desc: test LnnCompareNodeWeight
54  * @tc.type: FUNC
55  * @tc.require: I5OMIK
56  */
57 HWTEST_F(LNNNodeWeightTest, LNN_COMPARE_NODE_WEIGHT_TEST_001, TestSize.Level1)
58 {
59     int32_t ret = LnnCompareNodeWeight(WEIGHT, UDID1, WEIGHT2, UDID2);
60     EXPECT_TRUE(ret == (WEIGHT - WEIGHT2));
61     ret = LnnCompareNodeWeight(WEIGHT, nullptr, WEIGHT, UDID2);
62     EXPECT_TRUE(ret == (WEIGHT - WEIGHT));
63     ret = LnnCompareNodeWeight(WEIGHT, UDID1, WEIGHT, nullptr);
64     EXPECT_TRUE(ret == (WEIGHT - WEIGHT));
65     ret = LnnCompareNodeWeight(WEIGHT2, UDID1, WEIGHT3, UDID2);
66     EXPECT_TRUE(ret < 0);
67 }
68 
69 /*
70  * @tc.name: LNN_GET_LOCAL_WEIGHT_TEST_001
71  * @tc.desc: test LnnGetLocalWeight
72  * @tc.type: FUNC
73  * @tc.require:
74  */
75 HWTEST_F(LNNNodeWeightTest, LNN_GET_LOCAL_WEIGHT_TEST_001, TestSize.Level1)
76 {
77     NiceMock<LnnServicetInterfaceMock> lnnServiceMock;
78     EXPECT_CALL(lnnServiceMock, SoftBusGenerateRandomArray).WillOnce(Return(SOFTBUS_GENERATE_RANDOM_ARRAY_FAIL));
79     int32_t ret = LnnGetLocalWeight();
80     EXPECT_EQ(ret, SOFTBUS_OK);
81     unsigned char randStr = 20;
82     EXPECT_CALL(lnnServiceMock, SoftBusGenerateRandomArray)
83         .WillRepeatedly(DoAll(SetArgPointee<0>(randStr), Return(SOFTBUS_OK)));
84     NiceMock<LnnNetLedgertInterfaceMock> netLedgerMock;
85     EXPECT_CALL(netLedgerMock, LnnGetLocalNumInfo).WillOnce(Return(SOFTBUS_NETWORK_NOT_FOUND));
86     ret = LnnGetLocalWeight();
87     EXPECT_EQ(ret, 78);
88     ret = LnnGetLocalWeight();
89     EXPECT_EQ(ret, 78);
90 }
91 } // namespace OHOS
92