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 #include <securec.h>
16 #include <gtest/gtest.h>
17
18 #include "softbus_client_event_manager.h"
19 #include "softbus_client_frame_manager.h"
20 #include "softbus_error_code.h"
21 #include "softbus_adapter_mem.h"
22
23 using namespace std;
24 using namespace testing::ext;
25
26 namespace OHOS {
27 #define FRAME_HEADER_LEN 4
28
29 class SoftbusClientEventManagerTest : public testing::Test {
30 public:
SoftbusClientEventManagerTest()31 SoftbusClientEventManagerTest()
32 {}
~SoftbusClientEventManagerTest()33 ~SoftbusClientEventManagerTest()
34 {}
35 static void SetUpTestCase(void);
36 static void TearDownTestCase(void);
SetUp()37 void SetUp() override
38 {}
TearDown()39 void TearDown() override
40 {}
41 };
42
SetUpTestCase(void)43 void SoftbusClientEventManagerTest::SetUpTestCase(void)
44 {}
45
TearDownTestCase(void)46 void SoftbusClientEventManagerTest::TearDownTestCase(void)
47 {}
48
49 /**
50 * @tc.name: EventClientInit001
51 * @tc.desc: EventClientInit, use the wrong parameter.
52 * @tc.desc: EventClientDeinit, use the wrong parameter.
53 * @tc.type: FUNC
54 * @tc.require:
55 */
56 HWTEST_F(SoftbusClientEventManagerTest, EventClientInit001, TestSize.Level1)
57 {
58 EventClientDeinit();
59 int ret = EventClientInit();
60 EXPECT_EQ(SOFTBUS_OK, ret);
61
62 EventClientDeinit();
63 }
64
65 /**
66 * @tc.name: RegisterEventCallback001
67 * @tc.desc: RegisterEventCallback, use the wrong parameter.
68 * @tc.desc: CLIENT_NotifyObserver, use the wrong parameter.
69 * @tc.type: FUNC
70 * @tc.require:
71 */
72 HWTEST_F(SoftbusClientEventManagerTest, RegisterEventCallback001, TestSize.Level1)
73 {
74 EventCallback *cb =
75 (EventCallback *)SoftBusCalloc(sizeof(EventCallback));
76 ASSERT_TRUE(cb != nullptr);
77
78 std::unique_ptr<char[]> data = nullptr;
79 ssize_t len = 2;
80 data = std::make_unique<char[]>(len + FRAME_HEADER_LEN);
81
82 int res = -1;
83 int ret = RegisterEventCallback((enum SoftBusEvent)res, *cb, data.get() + FRAME_HEADER_LEN);
84 EXPECT_EQ(SOFTBUS_ERR, ret);
85 res = 4;
86 ret = RegisterEventCallback((enum SoftBusEvent)res, *cb, data.get() + FRAME_HEADER_LEN);
87 EXPECT_EQ(SOFTBUS_ERR, ret);
88 res = 1;
89 ret = RegisterEventCallback(EVENT_SERVER_DEATH, NULL, data.get() + FRAME_HEADER_LEN);
90 EXPECT_EQ(SOFTBUS_ERR, ret);
91
92
93 ret = RegisterEventCallback((enum SoftBusEvent)res, *cb, data.get() + FRAME_HEADER_LEN);
94 EXPECT_EQ(SOFTBUS_ERR, ret);
95
96 unsigned int argLen = 2;
97 CLIENT_NotifyObserver((enum SoftBusEvent)res, data.get() + FRAME_HEADER_LEN, argLen);
98
99 res = 4;
100 CLIENT_NotifyObserver(EVENT_SERVER_DEATH, data.get() + FRAME_HEADER_LEN, argLen);
101
102 if (cb != nullptr) {
103 SoftBusFree(cb);
104 }
105 }
106
107 /**
108 * @tc.name: DelClientPkgName001
109 * @tc.desc: DelClientPkgName, use the wrong parameter.
110 * @tc.desc: EventClientDeinit, use the wrong parameter.
111 * @tc.type: FUNC
112 * @tc.require:
113 */
114 HWTEST_F(SoftbusClientEventManagerTest, DelClientPkgName001, TestSize.Level1)
115 {
116 const char *pkgName = "000";
117 int32_t ret = InitSoftBus(NULL);
118 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
119
120 ret = InitSoftBus(pkgName);
121 EXPECT_EQ(SOFTBUS_OK, ret);
122
123 char *clientName[SOFTBUS_PKGNAME_MAX_NUM] = {0};
124 uint32_t clientNameNum = GetSoftBusClientNameList(NULL, SOFTBUS_PKGNAME_MAX_NUM);
125 EXPECT_EQ(0, clientNameNum);
126 clientNameNum = GetSoftBusClientNameList(clientName, 0);
127 EXPECT_EQ(0, clientNameNum);
128
129 clientNameNum = GetSoftBusClientNameList(clientName, SOFTBUS_PKGNAME_MAX_NUM);
130 EXPECT_NE(0, clientNameNum);
131 }
132
133 /**
134 * @tc.name: CheckPackageName001
135 * @tc.desc: CheckPackageName, use the wrong parameter.
136 * @tc.desc: EventClientDeinit, use the wrong parameter.
137 * @tc.type: FUNC
138 * @tc.require:
139 */
140 HWTEST_F(SoftbusClientEventManagerTest, CheckPackageName001, TestSize.Level1)
141 {
142 const char *pkgName = "000";
143 int ret = CheckPackageName(pkgName);
144 EXPECT_EQ(SOFTBUS_OK, ret);
145
146 const char *tmpPkgName = "000111";
147 ret = CheckPackageName(tmpPkgName);
148 EXPECT_EQ(SOFTBUS_INVALID_PKGNAME, ret);
149
150 ret = CheckPackageName(pkgName);
151 EXPECT_EQ(SOFTBUS_OK, ret);
152 }
153 } // OHOS
154