• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "set_apn_plugin_test.h"
17 
18 #include "edm_ipc_interface_code.h"
19 #include "plugin_singleton.h"
20 #include "utils.h"
21 #include "core_service_client.h"
22 #include "telephony_types.h"
23 #include "parameters.h"
24 
25 using namespace testing::ext;
26 
27 namespace OHOS {
28 namespace EDM {
29 namespace TEST {
30 namespace {
31 const std::string ADD_FLAG = "AddApn";
32 const std::string UPDATE_FLAG = "UpdateApn";
33 const std::string SET_PREFER_FLAG = "SetPreferApn";
34 const std::string QUERY_ID_FLAG = "QueryApnIds";
35 const std::string QUERY_INFO_FLAG = "QueryApn";
36 constexpr int32_t ADD_FIELD_SIZE = 8;
37 constexpr int32_t UPDATE_FIELD_SIZE = 2;
38 constexpr int32_t OPKEY_QUERY_SIZE = 1;
39 constexpr std::string_view PASSWORD = "password";
40 constexpr int32_t PASSWORD_SIZE = static_cast<int32_t>(PASSWORD.size());
41 std::string g_testApnId = "-1";
42 }
43 
HasValidSimCard(std::vector<int32_t> & slotIds)44 bool SetApnPluginTest::HasValidSimCard(std::vector<int32_t> &slotIds)
45 {
46     if (Telephony::CoreServiceClient::GetInstance().GetProxy() == nullptr) {
47         return false;
48     }
49     int32_t slotCount = Telephony::CoreServiceClient::GetInstance().GetMaxSimCount();
50     bool hasValidSimCard = false;
51     for (int32_t i = 0; i < slotCount; i++) {
52         if (!Telephony::CoreServiceClient::GetInstance().IsSimActive(i)) {
53             continue;
54         }
55         int32_t id = Telephony::CoreServiceClient::GetInstance().GetSimId(i);
56         if (id >= 0) {
57             hasValidSimCard = true;
58             slotIds.push_back(i);
59         }
60     }
61     return hasValidSimCard;
62 }
63 
AddTestData(MessageParcel & data)64 void SetApnPluginTest::AddTestData(MessageParcel &data)
65 {
66     data.WriteInt32(ADD_FIELD_SIZE);
67     data.WriteString("profile_name");
68     data.WriteString("apn");
69     data.WriteString("mcc");
70     data.WriteString("mnc");
71     data.WriteString("auth_user");
72     data.WriteString("apn_types");
73     data.WriteString("proxy_ip_address");
74     data.WriteString("mms_ip_address");
75 
76     data.WriteString("TEST_ADD_PROFILE_NAME");
77     data.WriteString("TEST_ADD_APN");
78     data.WriteString(system::GetParameter("telephony.sim.opkey0", ""));
79     data.WriteString("");
80     data.WriteString("TEST_ADD_AUTH_USER");
81     data.WriteString("TEST_ADD_APN_TYPES");
82     data.WriteString("TEST_ADD_PROXY_IP_ADDRESS");
83     data.WriteString("TEST_ADD_MMS_IP_ADDRESS");
84 
85     data.WriteInt32(PASSWORD_SIZE);
86     data.WriteString(std::string(PASSWORD));
87 }
88 
UpdateTestData(MessageParcel & data)89 void SetApnPluginTest::UpdateTestData(MessageParcel &data)
90 {
91     data.WriteInt32(UPDATE_FIELD_SIZE);
92     data.WriteString("profile_name");
93     data.WriteString("apn");
94     data.WriteString("TEST_UPDATE_PROFILE_NAME");
95     data.WriteString("TEST_UPDATE_APN");
96 }
97 
GetApnId()98 void SetApnPluginTest::GetApnId()
99 {
100     std::shared_ptr<SetApnPlugin> plugin = std::make_shared<SetApnPlugin>();
101     std::string policyData;
102     MessageParcel data;
103     data.WriteString(QUERY_ID_FLAG);
104     AddTestData(data);
105     MessageParcel reply;
106     ErrCode ret = plugin->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
107     if (ret != ERR_OK) {
108         EDMLOGE("SetApnPluginTest GetApnId failed");
109         return;
110     }
111     ret = reply.ReadInt32();
112     if (ret == ERR_OK && reply.ReadInt32() > 0) {
113         g_testApnId = reply.ReadString();
114     }
115 }
116 
GetApnId(const std::string & opkey,std::string & apnId)117 void SetApnPluginTest::GetApnId(const std::string &opkey, std::string &apnId)
118 {
119     std::shared_ptr<SetApnPlugin> plugin = std::make_shared<SetApnPlugin>();
120     std::string policyData;
121     MessageParcel data;
122     data.WriteString(QUERY_ID_FLAG);
123     data.WriteInt32(OPKEY_QUERY_SIZE);
124     data.WriteString("opkey");
125     data.WriteString(opkey);
126     MessageParcel reply;
127     ErrCode ret = plugin->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
128     if (ret != ERR_OK) {
129         EDMLOGE("SetApnPluginTest GetApnId failed");
130         return;
131     }
132     ret = reply.ReadInt32();
133     if (ret == ERR_OK && reply.ReadInt32() > 0) {
134         apnId = reply.ReadString();
135     }
136 }
137 
AddApn()138 void SetApnPluginTest::AddApn()
139 {
140     std::shared_ptr<SetApnPlugin> plugin = std::make_shared<SetApnPlugin>();
141     uint32_t code = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::SET_APN_INFO);
142     HandlePolicyData handlePolicyData{"", "", false};
143     MessageParcel data;
144     data.WriteString(ADD_FLAG);
145     AddTestData(data);
146     MessageParcel reply;
147     ErrCode ret = plugin->OnHandlePolicy(code, data, reply, handlePolicyData, DEFAULT_USER_ID);
148     if (ret != ERR_OK) {
149         EDMLOGE("SetApnPluginTest AddApn failed");
150         return;
151     }
152     GetApnId();
153 }
154 
DeleteApn()155 void SetApnPluginTest::DeleteApn()
156 {
157     std::shared_ptr<SetApnPlugin> plugin = std::make_shared<SetApnPlugin>();
158     uint32_t code = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, EdmInterfaceCode::SET_APN_INFO);
159     HandlePolicyData handlePolicyData{"", "", false};
160     MessageParcel data;
161     data.WriteString(g_testApnId);
162     MessageParcel reply;
163     ErrCode ret = plugin->OnHandlePolicy(code, data, reply, handlePolicyData, DEFAULT_USER_ID);
164     if (ret != ERR_OK) {
165         EDMLOGE("SetApnPluginTest AddApn failed");
166     }
167 }
168 
SetUpTestSuite(void)169 void SetApnPluginTest::SetUpTestSuite(void)
170 {
171     Utils::SetEdmServiceEnable();
172     Utils::SetEdmInitialEnv();
173     AddApn();
174 }
175 
TearDownTestSuite(void)176 void SetApnPluginTest::TearDownTestSuite(void)
177 {
178     DeleteApn();
179     Utils::SetEdmServiceDisable();
180     Utils::ResetTokenTypeAndUid();
181     ASSERT_TRUE(Utils::IsOriginalUTEnv());
182     std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
183 }
184 
185 /**
186  * @tc.name: TestQueryApnInfoForAdd
187  * @tc.desc: Test SetApnPlugin::OnQueryPolicy function.
188  * @tc.type: FUNC
189  */
190 HWTEST_F(SetApnPluginTest, TestQueryApnInfoForAdd, TestSize.Level1)
191 {
192     std::shared_ptr<SetApnPlugin> plugin = std::make_shared<SetApnPlugin>();
193     std::string policyData;
194     MessageParcel data;
195     data.WriteString(QUERY_INFO_FLAG);
196     data.WriteString(g_testApnId);
197     MessageParcel reply;
198     ErrCode ret = plugin->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
199     ASSERT_TRUE(ret == ERR_OK);
200     ASSERT_TRUE(reply.ReadInt32() == ERR_OK);
201     int size = reply.ReadInt32();
202     std::vector<std::string> keys;
203     for (int idx = 0; idx < size; idx++) {
204         keys.push_back(reply.ReadString());
205     }
206     std::vector<std::string> values;
207     for (int idx = 0; idx < size; idx++) {
208         values.push_back(reply.ReadString());
209     }
210     ASSERT_TRUE(keys[0] == "apn");
211     ASSERT_TRUE(values[0] == "TEST_ADD_APN");
212 }
213 
214 /**
215  * @tc.name: TestUpdateApn
216  * @tc.desc: Test SetApnPlugin::OnSetPolicy function.
217  * @tc.type: FUNC
218  */
219 HWTEST_F(SetApnPluginTest, TestUpdateApn, TestSize.Level1)
220 {
221     std::shared_ptr<SetApnPlugin> plugin = std::make_shared<SetApnPlugin>();
222     uint32_t code = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::SET_APN_INFO);
223     HandlePolicyData handlePolicyData{"", "", false};
224     MessageParcel data;
225     data.WriteString(UPDATE_FLAG);
226     data.WriteString(g_testApnId);
227     UpdateTestData(data);
228     MessageParcel reply;
229     ErrCode ret = plugin->OnHandlePolicy(code, data, reply, handlePolicyData, DEFAULT_USER_ID);
230     ASSERT_TRUE(ret == ERR_OK);
231 }
232 
233 /**
234  * @tc.name: TestQueryApnInfoForUpdate
235  * @tc.desc: Test SetApnPlugin::OnQueryPolicy function.
236  * @tc.type: FUNC
237  */
238 HWTEST_F(SetApnPluginTest, TestQueryApnInfoForUpdate, TestSize.Level1)
239 {
240     std::shared_ptr<SetApnPlugin> plugin = std::make_shared<SetApnPlugin>();
241     std::string policyData;
242     MessageParcel data;
243     data.WriteString(QUERY_INFO_FLAG);
244     data.WriteString(g_testApnId);
245     MessageParcel reply;
246     ErrCode ret = plugin->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
247     ASSERT_TRUE(ret == ERR_OK);
248     ASSERT_TRUE(reply.ReadInt32() == ERR_OK);
249     int size = reply.ReadInt32();
250     std::vector<std::string> keys;
251     for (int idx = 0; idx < size; idx++) {
252         keys.push_back(reply.ReadString());
253     }
254     std::vector<std::string> values;
255     for (int idx = 0; idx < size; idx++) {
256         values.push_back(reply.ReadString());
257     }
258     ASSERT_TRUE(keys[0] == "apn");
259     ASSERT_TRUE(values[0] == "TEST_UPDATE_APN");
260 }
261 
262 /**
263  * @tc.name: TestSetPreferApn
264  * @tc.desc: Test SetApnPlugin::OnSetPolicy function.
265  * @tc.type: FUNC
266  */
267 HWTEST_F(SetApnPluginTest, TestSetPreferApn, TestSize.Level1)
268 {
269     std::vector<int32_t> slotIds;
270     if (!HasValidSimCard(slotIds)) {
271         return;
272     }
273 
274     std::string opkey = system::GetParameter(std::string("telephony.sim.opkey") + std::to_string(slotIds[0]), "");
275     std::string apnId;
276     GetApnId(opkey, apnId);
277     ASSERT_TRUE(apnId.empty() == false);
278 
279     std::shared_ptr<SetApnPlugin> plugin = std::make_shared<SetApnPlugin>();
280     uint32_t code = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::SET_APN_INFO);
281     HandlePolicyData handlePolicyData{"", "", false};
282     MessageParcel data;
283     data.WriteString(SET_PREFER_FLAG);
284     data.WriteString(apnId);
285     MessageParcel reply;
286     ErrCode ret = plugin->OnHandlePolicy(code, data, reply, handlePolicyData, DEFAULT_USER_ID);
287     ASSERT_TRUE(ret == ERR_OK);
288 }
289 } // namespace TEST
290 } // namespace EDM
291 } // namespace OHOS