• 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 #define private public
17 #define protected public
18 
19 #include "input_method_system_ability.h"
20 #include "security_mode_parser.h"
21 #include "settings_data_utils.h"
22 
23 #undef private
24 
25 #include <gtest/gtest.h>
26 
27 using namespace testing::ext;
28 using namespace OHOS::DataShare;
29 namespace OHOS {
30 namespace MiscServices {
31 const std::string SECURITY_KEY = "settings.inputmethod.full_experience";
32 constexpr uint32_t USER_100_TOTAL_COUNT = 3;
33 constexpr uint32_t USER_101_TOTAL_COUNT = 1;
34 class SecurityModeParserTest : public testing::Test {
35 public:
36     static void SetUpTestCase(void);
37     static void TearDownTestCase(void);
38     void SetUp();
39     void TearDown();
40     static std::shared_ptr<DataShareHelper> helper_;
41     static std::shared_ptr<DataShareResultSet> resultSet_;
42     static sptr<InputMethodSystemAbility> service_;
43     static constexpr int32_t USER_ID = 100;
44 };
45 std::shared_ptr<DataShareHelper> SecurityModeParserTest::helper_;
46 std::shared_ptr<DataShareResultSet> SecurityModeParserTest::resultSet_;
47 sptr<InputMethodSystemAbility> SecurityModeParserTest::service_ { nullptr };
SetUpTestCase(void)48 void SecurityModeParserTest::SetUpTestCase(void)
49 {
50     IMSA_HILOGI("SecurityModeParserTest::SetUpTestCase");
51     std::vector<std::string> columns = { "VALUE" };
52     helper_ = DataShare::DataShareHelper::Creator(nullptr, "tsetUri", "tsetUri");
53     DataSharePredicates predicates;
54     Uri uri("tsetUri");
55     resultSet_ = helper_->Query(uri, predicates, columns);
56     SecurityModeParser::GetInstance()->Initialize(USER_ID);
57 
58     service_ = new (std::nothrow) InputMethodSystemAbility();
59     if (service_ == nullptr) {
60         IMSA_HILOGE("failed to new service");
61         return;
62     }
63     service_->OnStart();
64     service_->userId_ = USER_ID;
65 }
66 
TearDownTestCase(void)67 void SecurityModeParserTest::TearDownTestCase(void)
68 {
69     service_->OnStop();
70     IMSA_HILOGI("SecurityModeParserTest::TearDownTestCase");
71 }
72 
SetUp()73 void SecurityModeParserTest::SetUp()
74 {
75     IMSA_HILOGI("SecurityModeParserTest::SetUp");
76     resultSet_->strValue_ = "{\"fullExperienceList\" : {\"100\" : [ \"xiaoyiIme\", \"baiduIme\", "
77                             "\"sougouIme\"],\"101\" : [\"sougouIme\"]}}";
78 
79     SecurityModeParser::GetInstance()->fullModeList_.clear();
80 }
81 
TearDown()82 void SecurityModeParserTest::TearDown()
83 {
84     IMSA_HILOGI("SecurityModeParserTest::TearDown");
85 }
86 
87 /**
88  * @tc.name: testGetFullModeList_001
89  * @tc.desc: Get 101 user fullModeList
90  * @tc.type: FUNC
91  * @tc.require:
92  * @tc.author: guojin
93  */
94 HWTEST_F(SecurityModeParserTest, testGetFullModeList_001, TestSize.Level0)
95 {
96     IMSA_HILOGI("SecurityModeParserTest testGetFullModeList_001 START");
97     int32_t ret = SecurityModeParser::GetInstance()->UpdateFullModeList(101);
98     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
99     EXPECT_EQ(SecurityModeParser::GetInstance()->fullModeList_.size(), USER_101_TOTAL_COUNT);
100     if (SecurityModeParser::GetInstance()->fullModeList_.size() == USER_101_TOTAL_COUNT) {
101         EXPECT_EQ(SecurityModeParser::GetInstance()->fullModeList_[0], "sougouIme");
102     }
103 }
104 
105 /**
106  * @tc.name: testGetFullModeList_002
107  * @tc.desc: Get 100 user fullModeList
108  * @tc.type: FUNC
109  * @tc.require:
110  * @tc.author: guojin
111  */
112 HWTEST_F(SecurityModeParserTest, testGetFullModeList_002, TestSize.Level0)
113 {
114     IMSA_HILOGI("SecurityModeParserTest testGetFullModeList_002 START");
115     int32_t ret = SecurityModeParser::GetInstance()->UpdateFullModeList(SecurityModeParserTest::USER_ID);
116     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
117     EXPECT_EQ(SecurityModeParser::GetInstance()->fullModeList_.size(), USER_100_TOTAL_COUNT);
118     if (SecurityModeParser::GetInstance()->fullModeList_.size() == USER_100_TOTAL_COUNT) {
119         EXPECT_EQ(SecurityModeParser::GetInstance()->fullModeList_[0], "xiaoyiIme");
120         EXPECT_EQ(SecurityModeParser::GetInstance()->fullModeList_[1], "baiduIme");
121         EXPECT_EQ(SecurityModeParser::GetInstance()->fullModeList_[2], "sougouIme");
122     }
123 }
124 
125 /**
126  * @tc.name: testGetSecurityMode_001
127  * @tc.desc: Get 100 user security mode
128  * @tc.type: FUNC
129  * @tc.require:
130  * @tc.author: guojin
131  */
132 HWTEST_F(SecurityModeParserTest, testGetSecurityMode_001, TestSize.Level0)
133 {
134     IMSA_HILOGI("SecurityModeParserTest testGetSecurityMode_001 START");
135     int32_t ret = SecurityModeParser::GetInstance()->UpdateFullModeList(SecurityModeParserTest::USER_ID);
136     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
137     SecurityMode security =
138         SecurityModeParser::GetInstance()->GetSecurityMode("xiaoyiIme", SecurityModeParserTest::USER_ID);
139     EXPECT_EQ(static_cast<int32_t>(security), 1);
140 }
141 
142 /**
143  * @tc.name: testGetSecurityMode_002
144  * @tc.desc: Get 100 user security mode
145  * @tc.type: FUNC
146  * @tc.require:
147  * @tc.author: guojin
148  */
149 HWTEST_F(SecurityModeParserTest, testGetSecurityMode_002, TestSize.Level0)
150 {
151     IMSA_HILOGI("SecurityModeParserTest testGetSecurityMode_002 START");
152     int32_t ret = SecurityModeParser::GetInstance()->UpdateFullModeList(SecurityModeParserTest::USER_ID);
153     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
154     SecurityMode security = SecurityModeParser::GetInstance()->GetSecurityMode("test", SecurityModeParserTest::USER_ID);
155     EXPECT_EQ(static_cast<int32_t>(security), 0);
156 }
157 
158 /**
159  * @tc.name: testGetSecurityMode_003
160  * @tc.desc: Get 100 user security mode
161  * @tc.type: FUNC
162  * @tc.require:
163  * @tc.author: guojin
164  */
165 HWTEST_F(SecurityModeParserTest, testGetSecurityMode_003, TestSize.Level0)
166 {
167     IMSA_HILOGI("SecurityModeParserTest testGetSecurityMode_003 START");
168     service_->enableSecurityMode_ = false;
169     int32_t securityMode;
170     auto ret = service_->GetSecurityMode(securityMode);
171     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
172     EXPECT_EQ(securityMode, 1);
173 }
174 
175 /**
176  * @tc.name: testGetSecurityMode_004
177  * @tc.desc: Get 100 user security mode
178  * @tc.type: FUNC
179  * @tc.require:
180  * @tc.author: guojin
181  */
182 HWTEST_F(SecurityModeParserTest, testGetSecurityMode_004, TestSize.Level0)
183 {
184     IMSA_HILOGI("SecurityModeParserTest testGetSecurityMode_004 START");
185     service_->enableSecurityMode_ = true;
186     int32_t securityMode;
187     auto ret = service_->GetSecurityMode(securityMode);
188     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
189     EXPECT_EQ(securityMode, 1);
190 }
191 
192 /**
193  * @tc.name: testInitialize
194  * @tc.type: FUNC
195  * @tc.require:
196  */
197 HWTEST_F(SecurityModeParserTest, testInitialize, TestSize.Level0)
198 {
199     IMSA_HILOGI("SecurityModeParserTest testInitialize START");
200     auto ret = SecurityModeParser::GetInstance()->Initialize(SecurityModeParserTest::USER_ID);
201     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
202 }
203 
204 /**
205  * @tc.name: testIsExpired
206  * @tc.type: FUNC
207  * @tc.require:
208  */
209 HWTEST_F(SecurityModeParserTest, testIsExpired, TestSize.Level0)
210 {
211     IMSA_HILOGI("SecurityModeParserTest testIsExpired START");
212     auto ret = SecurityModeParser::GetInstance()->IsExpired("test");
213     EXPECT_FALSE(ret);
214 }
215 
216 /**
217  * @tc.name: testRegisterObserver
218  * @tc.type: FUNC
219  * @tc.require:
220  */
221 HWTEST_F(SecurityModeParserTest, testRegisterObserver, TestSize.Level0)
222 {
223     IMSA_HILOGI("SecurityModeParserTest testRegisterObserver START");
224     sptr<SettingsDataObserver> observer = nullptr;
225     auto ret = SettingsDataUtils::GetInstance()->RegisterObserver(observer);
226     EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER);
227 }
228 
229 /**
230  * @tc.name: testReleaseDataShareHelper
231  * @tc.type: FUNC
232  * @tc.require:
233  */
234 HWTEST_F(SecurityModeParserTest, testReleaseDataShareHelper, TestSize.Level0)
235 {
236     IMSA_HILOGI("SecurityModeParserTest testReleaseDataShareHelper START");
237     std::shared_ptr<DataShare::DataShareHelper> helper;
238     auto ret = SettingsDataUtils::GetInstance()->ReleaseDataShareHelper(helper);
239     EXPECT_TRUE(ret);
240     helper = nullptr;
241     ret = SettingsDataUtils::GetInstance()->ReleaseDataShareHelper(helper);
242     EXPECT_TRUE(ret);
243 }
244 
245 /**
246  * @tc.name: testParseSecurityMode_001
247  * @tc.type: FUNC
248  * @tc.require:
249  */
250 HWTEST_F(SecurityModeParserTest, testParseSecurityMode_001, TestSize.Level0)
251 {
252     IMSA_HILOGI("SecurityModeParserTest testParseSecurityMode_001 START");
253     auto ret = SecurityModeParser::GetInstance()->ParseSecurityMode("", SecurityModeParserTest::USER_ID);
254     EXPECT_FALSE(ret);
255 }
256 
257 /**
258  * @tc.name: testParseSecurityMode_002
259  * @tc.type: FUNC
260  * @tc.require:
261  */
262 HWTEST_F(SecurityModeParserTest, testParseSecurityMode_002, TestSize.Level0)
263 {
264     IMSA_HILOGI("SecurityModeParserTest testParseSecurityMode_002 START");
265     auto ret = SecurityModeParser::GetInstance()->ParseSecurityMode(
266         "{\"fullExperienceList\" : {\"100\" : ["
267         "\"xiaoyiIme\", \"baiduIme\", \"sougouIme\"],\"101\" : [\"sougouIme\"]}}",
268         SecurityModeParserTest::USER_ID);
269     EXPECT_TRUE(ret);
270 }
271 } // namespace MiscServices
272 } // namespace OHOS