1 /* 2 * Copyright (c) 2024 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 LOG_TAG "UdmfServiceImplTest" 17 #include "udmf_service_impl.h" 18 #include "gtest/gtest.h" 19 #include "error_code.h" 20 #include "text.h" 21 22 using namespace OHOS::DistributedData; 23 namespace OHOS::UDMF { 24 using namespace testing::ext; 25 class UdmfServiceImplTest : public testing::Test { 26 public: SetUpTestCase(void)27 static void SetUpTestCase(void) {} TearDownTestCase(void)28 static void TearDownTestCase(void) {} SetUp()29 void SetUp() {} TearDown()30 void TearDown() {} 31 }; 32 33 /** 34 * @tc.name: SaveData001 35 * @tc.desc: Abnormal test of SaveData, unifiedData is invalid 36 * @tc.type: FUNC 37 * @tc.require: 38 */ 39 HWTEST_F(UdmfServiceImplTest, SaveData001, TestSize.Level1) 40 { 41 CustomOption option; 42 UnifiedData data; 43 std::string key = ""; 44 option.intention = UD_INTENTION_BASE; 45 UdmfServiceImpl udmfServiceImpl; 46 int32_t ret = udmfServiceImpl.SaveData(option, data, key); 47 EXPECT_EQ(ret, E_INVALID_PARAMETERS); 48 } 49 50 /** 51 * @tc.name: RetrieveData001 52 * @tc.desc: Abnormal test of RetrieveData, query.key is invalid 53 * @tc.type: FUNC 54 * @tc.require: 55 */ 56 HWTEST_F(UdmfServiceImplTest, RetrieveData001, TestSize.Level1) 57 { 58 QueryOption query; 59 UnifiedData data; 60 query.intention = UD_INTENTION_BASE; 61 UdmfServiceImpl udmfServiceImpl; 62 int32_t ret = udmfServiceImpl.RetrieveData(query, data); 63 EXPECT_EQ(ret, E_INVALID_PARAMETERS); 64 } 65 66 /** 67 * @tc.name: IsPermissionInCache002 68 * @tc.desc: Abnormal test of IsPermissionInCache, privilegeCache_ has no query.key 69 * @tc.type: FUNC 70 * @tc.require: 71 */ 72 HWTEST_F(UdmfServiceImplTest, IsPermissionInCache002, TestSize.Level1) 73 { 74 QueryOption query; 75 UdmfServiceImpl udmfServiceImpl; 76 bool ret = udmfServiceImpl.IsPermissionInCache(query); 77 EXPECT_EQ(ret, false); 78 } 79 80 /** 81 * @tc.name: UpdateData001 82 * @tc.desc: Abnormal test of UpdateData, query.key is invalid 83 * @tc.type: FUNC 84 * @tc.require: 85 */ 86 HWTEST_F(UdmfServiceImplTest, UpdateData001, TestSize.Level1) 87 { 88 QueryOption query; 89 UnifiedData data; 90 UdmfServiceImpl udmfServiceImpl; 91 int32_t ret = udmfServiceImpl.UpdateData(query, data); 92 EXPECT_EQ(ret, E_INVALID_PARAMETERS); 93 } 94 95 /** 96 * @tc.name: GetSummary001 97 * @tc.desc: Abnormal test of UpdateData, query.key is invalid 98 * @tc.type: FUNC 99 * @tc.require: 100 */ 101 HWTEST_F(UdmfServiceImplTest, GetSummary001, TestSize.Level1) 102 { 103 QueryOption query; 104 Summary summary; 105 UdmfServiceImpl udmfServiceImpl; 106 int32_t ret = udmfServiceImpl.GetSummary(query, summary); 107 EXPECT_EQ(ret, E_INVALID_PARAMETERS); 108 } 109 110 /** 111 * @tc.name: Sync001 112 * @tc.desc: Abnormal test of Sync, query.key is invalid 113 * @tc.type: FUNC 114 * @tc.require: 115 */ 116 HWTEST_F(UdmfServiceImplTest, Sync001, TestSize.Level1) 117 { 118 QueryOption query; 119 std::vector<std::string> devices = {"device1"}; 120 UdmfServiceImpl udmfServiceImpl; 121 int32_t ret = udmfServiceImpl.Sync(query, devices); 122 EXPECT_EQ(ret, E_INVALID_PARAMETERS); 123 } 124 125 /** 126 * @tc.name: IsRemoteData001 127 * @tc.desc: Abnormal test of IsRemoteData, query.key is invalid 128 * @tc.type: FUNC 129 * @tc.require: 130 */ 131 HWTEST_F(UdmfServiceImplTest, IsRemoteData001, TestSize.Level1) 132 { 133 QueryOption query; 134 bool result = false; 135 UdmfServiceImpl udmfServiceImpl; 136 int32_t ret = udmfServiceImpl.IsRemoteData(query, result); 137 EXPECT_EQ(ret, E_INVALID_PARAMETERS); 138 } 139 140 /** 141 * @tc.name: SetAppShareOption001 142 * @tc.desc: Abnormal test of SetAppShareOption, intention is empty 143 * @tc.type: FUNC 144 * @tc.require: 145 */ 146 HWTEST_F(UdmfServiceImplTest, SetAppShareOption001, TestSize.Level1) 147 { 148 std::string intention = ""; 149 int32_t shareOption = 1; 150 UdmfServiceImpl udmfServiceImpl; 151 int32_t ret = udmfServiceImpl.SetAppShareOption(intention, shareOption); 152 EXPECT_EQ(ret, E_INVALID_PARAMETERS); 153 } 154 155 156 /** 157 * @tc.name: SetAppShareOption002 158 * @tc.desc: Abnormal test of SetAppShareOption, shareOption > SHARE_OPTIONS_BUTT 159 * @tc.type: FUNC 160 * @tc.require: 161 */ 162 HWTEST_F(UdmfServiceImplTest, SetAppShareOption002, TestSize.Level1) 163 { 164 std::string intention = "intention"; 165 int32_t shareOption = 4; 166 UdmfServiceImpl udmfServiceImpl; 167 int32_t ret = udmfServiceImpl.SetAppShareOption(intention, shareOption); 168 EXPECT_EQ(ret, E_INVALID_PARAMETERS); 169 } 170 171 /** 172 * @tc.name: SetAppShareOption003 173 * @tc.desc: Abnormal test of SetAppShareOption, shareOption = SHARE_OPTIONS_BUTT 174 * @tc.type: FUNC 175 * @tc.require: 176 */ 177 HWTEST_F(UdmfServiceImplTest, SetAppShareOption003, TestSize.Level1) 178 { 179 std::string intention = "intention"; 180 int32_t shareOption = 3; 181 UdmfServiceImpl udmfServiceImpl; 182 int32_t ret = udmfServiceImpl.SetAppShareOption(intention, shareOption); 183 EXPECT_EQ(ret, E_INVALID_PARAMETERS); 184 } 185 186 /** 187 * @tc.name: SetAppShareOption004 188 * @tc.desc: Abnormal test of SetAppShareOption, shareOption < IN_APP 189 * @tc.type: FUNC 190 * @tc.require: 191 */ 192 HWTEST_F(UdmfServiceImplTest, SetAppShareOption004, TestSize.Level1) 193 { 194 std::string intention = "intention"; 195 int32_t shareOption = -1; 196 UdmfServiceImpl udmfServiceImpl; 197 int32_t ret = udmfServiceImpl.SetAppShareOption(intention, shareOption); 198 EXPECT_EQ(ret, E_INVALID_PARAMETERS); 199 } 200 201 /** 202 * @tc.name: OnUserChangeTest001 203 * @tc.desc: OnUserChange test 204 * @tc.type: FUNC 205 * @tc.require: 206 */ 207 HWTEST_F(UdmfServiceImplTest, OnUserChangeTest001, TestSize.Level1) 208 { 209 uint32_t code = 4; 210 std::string user = "OH_USER_test"; 211 std::string account = "OH_ACCOUNT_test"; 212 UdmfServiceImpl udmfServiceImpl; 213 auto status = udmfServiceImpl.OnUserChange(code, user, account); 214 ASSERT_EQ(status, E_OK); 215 auto sizeAfter = StoreCache::GetInstance().stores_.Size(); 216 ASSERT_EQ(sizeAfter, 0); 217 } 218 219 /** 220 * @tc.name: TransferToEntriesIfNeedTest001 221 * @tc.desc: TransferToEntriesIfNeed test 222 * @tc.type: FUNC 223 * @tc.require: 224 */ 225 HWTEST_F(UdmfServiceImplTest, TransferToEntriesIfNeedTest001, TestSize.Level1) 226 { 227 UnifiedData data; 228 QueryOption query; 229 auto record1 = std::make_shared<UnifiedRecord>(); 230 auto record2 = std::make_shared<UnifiedRecord>(); 231 data.AddRecord(record1); 232 data.AddRecord(record2); 233 auto properties = std::make_shared<UnifiedDataProperties>(); 234 properties->tag = "records_to_entries_data_format"; 235 data.SetProperties(properties); 236 query.tokenId = 1; 237 UdmfServiceImpl udmfServiceImpl; 238 udmfServiceImpl.TransferToEntriesIfNeed(query, data); 239 EXPECT_TRUE(data.IsNeedTransferToEntries()); 240 int recordSize = 2; 241 EXPECT_EQ(data.GetRecords().size(), recordSize); 242 } 243 }; // namespace UDMF