1 /*
2 * Copyright (C) 2025 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 #define private public
16 #define protected public
17 #include "cell_info.h"
18 #include "cell_location.h"
19 #include "common_event_manager.h"
20 #include "common_event_support.h"
21 #include "core_manager_inner.h"
22 #include "core_service_client.h"
23 #include "csim_file_controller.h"
24 #include "gtest/gtest.h"
25 #include "tel_ril_base_parcel.h"
26 #include "icc_file.h"
27 #include "icc_file_controller.h"
28 #include "icc_operator_rule.h"
29 #include "ims_core_service_callback_proxy.h"
30 #include "ims_core_service_callback_stub.h"
31 #include "ims_core_service_proxy.h"
32 #include "ims_reg_info_callback_proxy.h"
33 #include "isim_file_controller.h"
34 #include "multi_sim_controller.h"
35 #include "multi_sim_monitor.h"
36 #include "network_register.h"
37 #include "network_search_manager.h"
38 #include "network_search_state.h"
39 #include "operator_matching_rule.h"
40 #include "operator_name.h"
41 #include "radio_protocol_controller.h"
42 #include "ruim_file_controller.h"
43 #include "sim_file_controller.h"
44 #include "sim_file_init.h"
45 #include "sim_file_manager.h"
46 #include "sim_manager.h"
47 #include "sim_number_decode.h"
48 #include "sim_rdb_helper.h"
49 #include "sim_sms_controller.h"
50 #include "sim_state_manager.h"
51 #include "sim_utils.h"
52 #include "stk_controller.h"
53 #include "stk_manager.h"
54 #include "tag_service.h"
55 #include "tel_ril_manager.h"
56 #include "telephony_errors.h"
57 #include "telephony_hisysevent.h"
58 #include "telephony_log_wrapper.h"
59 #include "telephony_permission.h"
60 #include "usim_file_controller.h"
61 #include "telephony_data_helper.h"
62 #include "sim_data.h"
63 #include "accesstoken_kit.h"
64 #include "token_setproc.h"
65 #include "nativetoken_kit.h"
66 #include "telephony_ext_wrapper.h"
67
68 namespace OHOS {
69 namespace Telephony {
70 using namespace testing::ext;
71
72 #ifndef TEL_TEST_UNSUPPORT
73 class SimUtilsTest : public testing::Test {
74 public:
75 static void SetUpTestCase();
76 static void TearDownTestCase();
77 void SetUp();
78 void TearDown();
79 };
80
81 namespace {
82 static const int32_t SLEEP_TIME = 3;
83 } // namespace
84
TearDownTestCase()85 void SimUtilsTest::TearDownTestCase()
86 {
87 sleep(SLEEP_TIME);
88 }
89
SetUp()90 void SimUtilsTest::SetUp() {}
91
TearDown()92 void SimUtilsTest::TearDown() {}
93
SetUpTestCase()94 void SimUtilsTest::SetUpTestCase()
95 {
96 constexpr int permissionNum = 2;
97 const char *perms[permissionNum] = {"ohos.permission.GET_TELEPHONY_STATE",
98 "ohos.permission.SET_TELEPHONY_STATE"};
99 NativeTokenInfoParams infoInstance = {.dcapsNum = 0, .permsNum = permissionNum, .aclsNum = 0, .dcaps = nullptr,
100 .perms = perms, .acls = nullptr, .processName = "SimUtilsTest", .aplStr = "system_basic",
101 };
102 uint64_t tokenId = GetAccessTokenId(&infoInstance);
103 SetSelfTokenID(tokenId);
104 auto result = Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
105 EXPECT_EQ(result, Security::AccessToken::RET_SUCCESS);
106 }
107
108 /**
109 * @tc.number Telephony_IsShowableAscii_001
110 * @tc.name test error branch
111 * @tc.desc Function test
112 */
113 HWTEST_F(SimUtilsTest, Telephony_IsShowableAscii_001, Function | MediumTest | Level1)
114 {
115 auto simUtils = std::make_shared<SIMUtils>();
116 char c = ' ';
117 bool result = simUtils->IsShowableAscii(c);
118 EXPECT_TRUE(result);
119 }
120
121 /**
122 * @tc.number Telephony_IsShowableAsciiOnly_001
123 * @tc.name test error branch
124 * @tc.desc Function test
125 */
126 HWTEST_F(SimUtilsTest, Telephony_IsShowableAsciiOnly_001, Function | MediumTest | Level1)
127 {
128 auto simUtils = std::make_shared<SIMUtils>();
129 std::string str1 = "HelloWorld";
130 bool result1 = simUtils->IsShowableAsciiOnly(str1);
131 EXPECT_TRUE(result1);
132 std::string str2 = "HelloWorld123!";
133 bool result2 = simUtils->IsShowableAsciiOnly(str2);
134 EXPECT_TRUE(result2);
135 }
136
137 /**
138 * @tc.number Telephony_CharsConvertToChar16_001
139 * @tc.name test error branch
140 * @tc.desc Function test
141 */
142 HWTEST_F(SimUtilsTest, Telephony_CharsConvertToChar16_001, Function | MediumTest | Level1)
143 {
144 auto simUtils = std::make_shared<SIMUtils>();
145 int outLen = 0;
146 std::shared_ptr<char16_t> result = simUtils->CharsConvertToChar16(nullptr, 0, outLen, false);
147 EXPECT_EQ(result, nullptr);
148 unsigned char input[] = {0x01, 0x02, 0x03, 0x04};
149 result = simUtils->CharsConvertToChar16(input, 4, outLen, false);
150 EXPECT_EQ(outLen, 2);
151 }
152
153 /**
154 * @tc.number Telephony_UcsConvertToString_001
155 * @tc.name test error branch
156 * @tc.desc Function test
157 */
158 HWTEST_F(SimUtilsTest, Telephony_UcsConvertToString_001, Function | MediumTest | Level1)
159 {
160 auto simUtils = std::make_shared<SIMUtils>();
161 std::u16string result = simUtils->UcsConvertToString(nullptr, 0, 0);
162 EXPECT_TRUE(result.empty());
163 unsigned char data[] = {0x00, 0x01, 0x00, 0x61, 0x00, 0x62, 0x00, 0x63};
164 result = simUtils->UcsConvertToString(data, 10, 2);
165 EXPECT_EQ(result, u"b");
166 }
167
168 /**
169 * @tc.number Telephony_UcsWideConvertToString_001
170 * @tc.name test error branch
171 * @tc.desc Function test
172 */
173 HWTEST_F(SimUtilsTest, Telephony_UcsWideConvertToString_001, Function | MediumTest | Level1)
174 {
175 auto simUtils = std::make_shared<SIMUtils>();
176 std::u16string result = simUtils->UcsWideConvertToString(nullptr, 0, 0);
177 EXPECT_TRUE(result.empty());
178 unsigned char data[] = {0x00, 0x01, 0x00, 0x61};
179 result = simUtils->UcsWideConvertToString(data, 4, 2);
180 EXPECT_EQ(result, u"");
181 }
182
183 /**
184 * @tc.number Telephony_UcsWideConvertToString_001
185 * @tc.name test error branch
186 * @tc.desc Function test
187 */
188 HWTEST_F(SimUtilsTest, Telephony_Normal_Method_Test_001, Function | MediumTest | Level1)
189 {
190 auto simUtils = std::make_shared<SIMUtils>();
191 std::string rawData = "";
192 auto res = simUtils->Cphs7bitConvertToString(rawData);
193 EXPECT_EQ(res, "");
194 }
195
196 /**
197 * @tc.number Telephony_UcsWideConvertToString_001
198 * @tc.name test error branch
199 * @tc.desc Function test
200 */
201 HWTEST_F(SimUtilsTest, Telephony_Decode8BitConvertToString_001, Function | MediumTest | Level1)
202 {
203 auto simUtils = std::make_shared<SIMUtils>();
204 int length = 0;
205 int offset = 0;
206 auto res = simUtils->Decode8BitConvertToString(nullptr, length, offset);
207 EXPECT_EQ(res, "");
208 }
209
210 /**
211 * @tc.number Telephony_telephony_permission_001
212 * @tc.name test error branch
213 * @tc.desc Function test
214 */
215 HWTEST_F(SimUtilsTest, Telephony_telephony_permission_001, Function | MediumTest | Level1)
216 {
217 auto telephonyPermission = std::make_shared<TelephonyPermission>();
218 std::string bundleName = "test";
219 auto res = telephonyPermission->GetBundleNameByUid(1, bundleName);
220 bundleName = "";
221 telephonyPermission->CheckPermission(bundleName);
222 EXPECT_FALSE(res);
223 }
224 #endif // TEL_TEST_UNSUPPORT
225 } // namespace Telephony
226 } // namespace OHOS