1 /*
2 * Copyright (c) 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 #include <cstdint>
16 #include <gtest/gtest.h>
17 #include <securec.h>
18 #include <string>
19
20 #include "gmock/gmock.h"
21 #include "gtest/gtest.h"
22 #include "iservice_registry.h"
23 #include "lnn_async_callback_utils.h"
24 #include "lnn_device_info.h"
25 #include "lnn_devicename_info.h"
26 #include "lnn_local_net_ledger.h"
27 #include "lnn_node_info.h"
28 #include "lnn_ohos_account_adapter.h"
29 #include "lnn_settingdata_event_monitor.h"
30 #include "message_handler.h"
31 #include "parameter.h"
32 #include "softbus_adapter_mem.h"
33 #include "softbus_bus_center.h"
34 #include "softbus_def.h"
35 #include "softbus_error_code.h"
36 #include "system_ability_definition.h"
37
38 using namespace std;
39 using namespace testing;
40 using namespace testing::ext;
41 namespace OHOS {
42 #define DEVICE_NAME_BUF_LEN 128
43
44 const std::string CHINESE_LANGUAGE = "zh-Hans";
45 const std::string TRADITIONAL_CHINESE_LANGUAGE = "zh-Hant";
46 const char *NICK_NAME = "TEST_NICK_NAME";
47 const char *DEFAULT_NAME = "TEST_DEFAULT_NAME";
48 static constexpr const char *INTERNAL_NAME_CONCAT_STRING = "的";
49 static constexpr const char *EXTERNAL_NAME_CONCAT_STRING = "-";
50 static constexpr const char *LANGUAGE_KEY = "persist.global.language";
51 static constexpr const char *DEFAULT_LANGUAGE_KEY = "const.global.language";
52 static constexpr const int32_t CONFIG_LEN = 128;
53
54 class LnnSettingdataEventMonitorTest : public testing::Test {
55 protected:
56 static void SetUpTestCase(void);
57 static void TearDownTestCase(void);
58 void SetUp();
59 void TearDown();
60 };
SetUpTestCase(void)61 void LnnSettingdataEventMonitorTest::SetUpTestCase(void) { }
TearDownTestCase(void)62 void LnnSettingdataEventMonitorTest::TearDownTestCase(void) { }
SetUp(void)63 void LnnSettingdataEventMonitorTest::SetUp(void) { }
TearDown(void)64 void LnnSettingdataEventMonitorTest::TearDown(void) { }
65
ReadSystemParameter(const char * paramKey)66 static std::string ReadSystemParameter(const char *paramKey)
67 {
68 char param[CONFIG_LEN + 1];
69 (void)memset_s(param, CONFIG_LEN + 1, 0, CONFIG_LEN + 1);
70 int32_t ret = GetParameter(paramKey, "", param, CONFIG_LEN);
71 if (ret > 0) {
72 return param;
73 }
74 return "";
75 }
76
IsZHLanguage(void)77 static bool IsZHLanguage(void)
78 {
79 std::string systemLanguage = ReadSystemParameter(LANGUAGE_KEY);
80 if (!systemLanguage.empty()) {
81 return CHINESE_LANGUAGE == systemLanguage || TRADITIONAL_CHINESE_LANGUAGE == systemLanguage;
82 }
83 systemLanguage = ReadSystemParameter(DEFAULT_LANGUAGE_KEY);
84 if (!systemLanguage.empty()) {
85 return CHINESE_LANGUAGE == systemLanguage || TRADITIONAL_CHINESE_LANGUAGE == systemLanguage;
86 }
87 // Default language is Chinese.
88 return true;
89 }
90
91 /*
92 * @tc.name: LnnGetSettingDeviceNameTest003
93 * @tc.desc:
94 * @tc.type: FUNC
95 * @tc.require: 1
96 */
97 HWTEST_F(LnnSettingdataEventMonitorTest, LnnGetSettingDeviceNameTest003, TestSize.Level1)
98 {
99 char deviceName[DEVICE_NAME_BUF_LEN] = { 0 };
100 int32_t ret = LnnGetDeviceDisplayName(NICK_NAME, DEFAULT_NAME, deviceName, DEVICE_NAME_BUF_LEN);
101 EXPECT_TRUE(ret == SOFTBUS_OK || ret == SOFTBUS_NOT_IMPLEMENT);
102 if (ret != SOFTBUS_NOT_IMPLEMENT) {
103 char devName[DEVICE_NAME_BUF_LEN] = {0};
104 if (IsZHLanguage()) {
105 ASSERT_GT(sprintf_s(devName, DEVICE_NAME_BUF_LEN, "%s%s%s", NICK_NAME,
106 INTERNAL_NAME_CONCAT_STRING, DEFAULT_NAME), 0);
107 EXPECT_EQ(strncmp(devName, deviceName, DEVICE_NAME_BUF_LEN), 0);
108 } else {
109 ASSERT_GT(sprintf_s(devName, DEVICE_NAME_BUF_LEN, "%s%s%s", NICK_NAME,
110 EXTERNAL_NAME_CONCAT_STRING, DEFAULT_NAME), 0);
111 EXPECT_EQ(strncmp(devName, deviceName, DEVICE_NAME_BUF_LEN), 0);
112 }
113 }
114 }
115
116 /*
117 * @tc.name: LnnGetSettingDeviceNameTest004
118 * @tc.desc: LnnGetDeviceDisplayName invalid param
119 * @tc.type: FUNC
120 * @tc.require: 1
121 */
122 HWTEST_F(LnnSettingdataEventMonitorTest, LnnGetSettingDeviceNameTest004, TestSize.Level1)
123 {
124 uint32_t len = DEVICE_NAME_BUF_LEN;
125 char deviceName[] = "deviceName";
126 int32_t ret = LnnGetDeviceDisplayName(nullptr, DEFAULT_NAME, deviceName, len);
127 EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
128 }
129
130 /*
131 * @tc.name: LnnGetSettingDeviceNameTest005
132 * @tc.desc: LnnGetDeviceDisplayName invalid param
133 * @tc.type: FUNC
134 * @tc.require: 1
135 */
136 HWTEST_F(LnnSettingdataEventMonitorTest, LnnGetSettingDeviceNameTest005, TestSize.Level1)
137 {
138 uint32_t len = DEVICE_NAME_BUF_LEN;
139 char deviceName[] = "deviceName";
140 int32_t ret = LnnGetDeviceDisplayName(NICK_NAME, nullptr, deviceName, len);
141 EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
142 }
143
144 /*
145 * @tc.name: LnnGetSettingDeviceNameTest006
146 * @tc.desc: LnnGetDeviceDisplayName invalid param
147 * @tc.type: FUNC
148 * @tc.require: 1
149 */
150 HWTEST_F(LnnSettingdataEventMonitorTest, LnnGetSettingDeviceNameTest006, TestSize.Level1)
151 {
152 uint32_t len = DEVICE_NAME_BUF_LEN;
153 int32_t ret = LnnGetDeviceDisplayName(NICK_NAME, DEFAULT_NAME, nullptr, len);
154 EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
155 }
156
157 /*
158 * @tc.name: LnnGetSettingDeviceNameTest007
159 * @tc.desc: LnnGetDeviceDisplayName invalid param
160 * @tc.type: FUNC
161 * @tc.require: 1
162 */
163 HWTEST_F(LnnSettingdataEventMonitorTest, LnnGetSettingDeviceNameTest007, TestSize.Level1)
164 {
165 uint32_t len = 0;
166 char deviceName[] = "deviceName";
167 int32_t ret = LnnGetDeviceDisplayName(NICK_NAME, DEFAULT_NAME, deviceName, len);
168 EXPECT_EQ(ret, SOFTBUS_STRCPY_ERR);
169 }
170 } // namespace OHOS