• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 #include <string>
19 #include <unistd.h>
20 #include "core_manager_inner.h"
21 #include "core_service.h"
22 #include "core_service_client.h"
23 #include "enum_convert.h"
24 #include "operator_config_cache.h"
25 #include "operator_file_parser.h"
26 #include "sim_state_type.h"
27 #include "str_convert.h"
28 #include "string_ex.h"
29 #include "tel_profile_util.h"
30 #include "telephony_ext_wrapper.h"
31 #include "gtest/gtest.h"
32 #include "sim_constant.h"
33 #include "usim_dialling_numbers_service.h"
34 
35 namespace OHOS {
36 namespace Telephony {
37 using namespace testing::ext;
38 using namespace testing;
39 
40 class UsimDiallingNumbersServiceTest : public testing::Test {
41 public:
42     static void SetUpTestCase();
43     static void TearDownTestCase();
44     void SetUp();
45     void TearDown();
46 };
47 
48 
SetUpTestCase()49 void UsimDiallingNumbersServiceTest::SetUpTestCase() {}
50 
TearDownTestCase()51 void UsimDiallingNumbersServiceTest::TearDownTestCase() {}
52 
SetUp()53 void UsimDiallingNumbersServiceTest::SetUp() {}
54 
TearDown()55 void UsimDiallingNumbersServiceTest::TearDown() {}
56 
57 HWTEST_F(UsimDiallingNumbersServiceTest, ProcessEventTest001, Function | MediumTest | Level1)
58 {
59     auto usimDiallingNumbersService = std::make_shared<UsimDiallingNumbersService>();
60     usimDiallingNumbersService->InitFuncMap();
61     int32_t invalidEventId = 0xFF3807;
62     auto event1 = AppExecFwk::InnerEvent::Get(MSG_USIM_PBR_LOAD_DONE);
63     auto event2 = AppExecFwk::InnerEvent::Get(invalidEventId);
64     usimDiallingNumbersService->ProcessEvent(event1);
65     usimDiallingNumbersService->ProcessEvent(event2);
66     std::unique_ptr<ControllerToFileMsg> obj;
67     event1->SaveUniquePtr<std::unique_ptr<ControllerToFileMsg>>(obj);
68     usimDiallingNumbersService->ProcessEvent(event1);
69     EXPECT_FALSE(usimDiallingNumbersService->memberFuncMap_.empty());
70 }
71 
72 HWTEST_F(UsimDiallingNumbersServiceTest, LoadPbrFilesTest001, Function | MediumTest | Level1)
73 {
74     int32_t slotId = 0;
75     auto usimDiallingNumbersService = std::make_shared<UsimDiallingNumbersService>();
76     usimDiallingNumbersService->InitFuncMap();
77     std::shared_ptr<IccFileController> file = std::make_shared<SimFileController>(slotId);
78     std::shared_ptr<IccDiallingNumbersHandler> handler = std::make_shared<IccDiallingNumbersHandler>(file);
79     usimDiallingNumbersService->SetFileControllerAndDiallingNumberHandler(file, handler);
80     usimDiallingNumbersService->LoadPbrFiles();
81     EXPECT_NE(usimDiallingNumbersService->fileController_, nullptr);
82 }
83 
84 HWTEST_F(UsimDiallingNumbersServiceTest, ProcessDiallingNumberLoadDone001, Function | MediumTest | Level1)
85 {
86     auto service = std::make_shared<UsimDiallingNumbersService>();
87     auto event = AppExecFwk::InnerEvent::Get(MSG_USIM_ADN_LOAD_DONE);
88     std::shared_ptr<UsimDiallingNumberFile> file;
89     std::unique_ptr<DiallingNumbersHandlerResult> result;
90     std::shared_ptr<RadioResponseInfo> responseInfo;
91 
92     service->pbrIndex_ = 0;
93     service->pbrFiles_.clear();
94     event = AppExecFwk::InnerEvent::Get(MSG_USIM_ADN_LOAD_DONE);
95     service->ProcessDiallingNumberLoadDone(event);
96     std::this_thread::sleep_for(std::chrono::milliseconds(1));
97 
98     service->pbrIndex_ = 0;
99     service->pbrFiles_.clear();
100     file = std::make_shared<UsimDiallingNumberFile>();
101     service->pbrFiles_.push_back(file);
102     event = AppExecFwk::InnerEvent::Get(MSG_USIM_ADN_LOAD_DONE);
103     service->ProcessDiallingNumberLoadDone(event);
104     std::this_thread::sleep_for(std::chrono::milliseconds(1));
105     EXPECT_TRUE(service->pbrIndex_ == 1);
106 }
107 
108 HWTEST_F(UsimDiallingNumbersServiceTest, ProcessDiallingNumberLoadDone002, Function | MediumTest | Level1)
109 {
110     auto service = std::make_shared<UsimDiallingNumbersService>();
111     auto event = AppExecFwk::InnerEvent::Get(MSG_USIM_ADN_LOAD_DONE);
112     std::shared_ptr<UsimDiallingNumberFile> file;
113     std::unique_ptr<DiallingNumbersHandlerResult> result;
114     std::shared_ptr<RadioResponseInfo> responseInfo;
115 
116     service->pbrIndex_ = 0;
117     service->pbrFiles_.clear();
118     file = std::make_shared<UsimDiallingNumberFile>();
119     service->pbrFiles_.push_back(file);
120     result = std::make_unique<DiallingNumbersHandlerResult>(nullptr);
121     responseInfo = std::make_shared<RadioResponseInfo>();
122     responseInfo->error = static_cast<Telephony::ErrType>(0);
123     result->exception = static_cast<std::shared_ptr<void>>(responseInfo);
124     event = AppExecFwk::InnerEvent::Get(MSG_USIM_ADN_LOAD_DONE, result);
125     service->ProcessDiallingNumberLoadDone(event);
126     std::this_thread::sleep_for(std::chrono::milliseconds(1));
127     EXPECT_TRUE(service->pbrIndex_ == 1);
128 
129     service->pbrIndex_ = 0;
130     service->pbrFiles_.clear();
131     file = std::make_shared<UsimDiallingNumberFile>();
132     service->pbrFiles_.push_back(file);
133     result = std::make_unique<DiallingNumbersHandlerResult>(nullptr);
134     result->exception = nullptr;
135     result->result = nullptr;
136     event = AppExecFwk::InnerEvent::Get(MSG_USIM_ADN_LOAD_DONE, result);
137     service->ProcessDiallingNumberLoadDone(event);
138     std::this_thread::sleep_for(std::chrono::milliseconds(1));
139     EXPECT_TRUE(service->pbrIndex_ == 1);
140 
141     service->pbrIndex_ = 0;
142     service->pbrFiles_.clear();
143     file = std::make_shared<UsimDiallingNumberFile>();
144     service->pbrFiles_.push_back(file);
145     result = std::make_unique<DiallingNumbersHandlerResult>(nullptr);
146     result->exception = nullptr;
147     result->result = std::make_shared<std::vector<std::shared_ptr<DiallingNumbersInfo>>>();
148     event = AppExecFwk::InnerEvent::Get(MSG_USIM_ADN_LOAD_DONE, result);
149     service->ProcessDiallingNumberLoadDone(event);
150     std::this_thread::sleep_for(std::chrono::milliseconds(1));
151     EXPECT_TRUE(service->pbrIndex_ == 1);
152 }
153 
154 HWTEST_F(UsimDiallingNumbersServiceTest, FetchAnrContent001, Function | MediumTest | Level1)
155 {
156     std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
157     auto service = std::make_shared<UsimDiallingNumbersService>();
158     std::u16string result;
159 
160     result = service->FetchAnrContent(""); // 码流为空
161     EXPECT_TRUE(result == u"");
162 
163     result = service->FetchAnrContent("000"); // 16 进制码流长度为奇数
164     EXPECT_TRUE(result == u"");
165 
166     std::string str = "000500";
167     str += "12345678901234567890"; // 不到 15 位
168     result = service->FetchAnrContent(str);
169     EXPECT_TRUE(result == u"");
170 
171     str = "000500";
172     str += "123456789012345678901234"; // 刚好 15 位,号码不超长
173     result = service->FetchAnrContent(str);
174     EXPECT_TRUE(result == u"2143658709");
175 
176     str = "000D00";
177     str += "12345678901234567890123456"; // 号码(0D: 13 * 2 = 26)超过 20 位,只截取 20 位
178     result = service->FetchAnrContent(str);
179     EXPECT_TRUE(result == u"21436587092143658709");
180 }
181 
182 HWTEST_F(UsimDiallingNumbersServiceTest, LoadDiallingNumberFiles001, Function | MediumTest | Level1)
183 {
184     auto service = std::make_shared<UsimDiallingNumbersService>();
185     std::shared_ptr<UsimDiallingNumberFile> file;
186     std::unique_ptr<DiallingNumbersHandlerResult> result;
187     std::shared_ptr<RadioResponseInfo> responseInfo;
188     bool ret;
189 
190     service->pbrIndex_ = 0;
191     service->pbrFiles_.clear();
192     ret = service->LoadDiallingNumber2Files(0);
193     std::this_thread::sleep_for(std::chrono::milliseconds(1));
194     EXPECT_FALSE(ret);
195 
196     service->pbrIndex_ = 0;
197     service->pbrFiles_.clear();
198     file = std::make_shared<UsimDiallingNumberFile>();
199     service->pbrFiles_.push_back(file);
200     ret = service->LoadDiallingNumber2Files(0);
201     std::this_thread::sleep_for(std::chrono::milliseconds(1));
202     EXPECT_FALSE(ret);
203 
204     service->pbrIndex_ = 0;
205     service->pbrFiles_.clear();
206     file = std::make_shared<UsimDiallingNumberFile>();
207     file->fileIds_.emplace(UsimDiallingNumbersService::TAG_SIM_USIM_ANR, nullptr);
208     service->pbrFiles_.push_back(file);
209     ret = service->LoadDiallingNumber2Files(0);
210     std::this_thread::sleep_for(std::chrono::milliseconds(1));
211     EXPECT_FALSE(ret);
212 
213     service->pbrIndex_ = 0;
214     service->pbrFiles_.clear();
215     file = std::make_shared<UsimDiallingNumberFile>();
216     file->fileIds_.emplace(UsimDiallingNumbersService::TAG_SIM_USIM_ANR, std::make_shared<TagData>(0, 0, 0, 0));
217     service->pbrFiles_.push_back(file);
218     ret = service->LoadDiallingNumber2Files(0);
219     std::this_thread::sleep_for(std::chrono::milliseconds(1));
220     EXPECT_FALSE(ret);
221 
222     service->pbrIndex_ = 0;
223     service->pbrFiles_.clear();
224     file = std::make_shared<UsimDiallingNumberFile>();
225     file->fileIds_.emplace(UsimDiallingNumbersService::TAG_SIM_USIM_ANR, std::make_shared<TagData>(0, 0, 0, 0));
226     service->pbrFiles_.push_back(file);
227     service->fileController_ = std::make_shared<UsimFileController>(0);
228     ret = service->LoadDiallingNumber2Files(0);
229     std::this_thread::sleep_for(std::chrono::milliseconds(1));
230     EXPECT_TRUE(ret);
231 }
232 
233 HWTEST_F(UsimDiallingNumbersServiceTest, ProcessDiallingNumber2LoadDone001, Function | MediumTest | Level1)
234 {
235     auto service = std::make_shared<UsimDiallingNumbersService>();
236     AppExecFwk::InnerEvent::Pointer event(nullptr, nullptr);
237     std::shared_ptr<MultiRecordResult> recordResult = nullptr;
238     std::u16string alphaTag = u"";
239     std::u16string number = u"12345";
240 
241     service->ProcessDiallingNumber2LoadDone(event);
242 
243     event = AppExecFwk::InnerEvent::Get(MSG_USIM_ANR_LOAD_DONE);
244     service->diallingNumbersFromAdn_.push_back(std::make_shared<DiallingNumbersInfo>(alphaTag, number));
245     service->ProcessDiallingNumber2LoadDone(event);
246     std::this_thread::sleep_for(std::chrono::milliseconds(1));
247     EXPECT_TRUE(service->diallingNumbersFromAdn_.empty());
248 
249     recordResult = std::make_shared<MultiRecordResult>(nullptr);
250     recordResult->fileResults.push_back("000500123456789012345678901234");
251     event = AppExecFwk::InnerEvent::Get(MSG_USIM_ANR_LOAD_DONE, recordResult);
252     service->diallingNumbersFromAdn_.push_back(std::make_shared<DiallingNumbersInfo>(alphaTag, number));
253     service->diallingNumbersFromAdn_.push_back(std::make_shared<DiallingNumbersInfo>(alphaTag, number));
254     service->ProcessDiallingNumber2LoadDone(event);
255     std::this_thread::sleep_for(std::chrono::milliseconds(1));
256     EXPECT_TRUE(service->diallingNumbersFromAdn_.empty());
257 
258     recordResult = std::make_shared<MultiRecordResult>(nullptr);
259     recordResult->fileResults.push_back("000500123456789012345678901234");
260     recordResult->fileResults.push_back("");
261     event = AppExecFwk::InnerEvent::Get(MSG_USIM_ANR_LOAD_DONE, recordResult);
262     service->diallingNumbersFromAdn_.push_back(std::make_shared<DiallingNumbersInfo>(alphaTag, number));
263     service->diallingNumbersFromAdn_.push_back(std::make_shared<DiallingNumbersInfo>(alphaTag, number));
264     service->ProcessDiallingNumber2LoadDone(event);
265     std::this_thread::sleep_for(std::chrono::milliseconds(1));
266     EXPECT_TRUE(service->diallingNumbersFromAdn_.empty());
267 }
268 
269 }
270 }
271