• 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 LOG_TAG "ObjectAssetMachineTest"
17 
18 #include "object_asset_machine.h"
19 
20 #include <gtest/gtest.h>
21 
22 #include "executor_pool.h"
23 #include "object_asset_loader.h"
24 #include "snapshot/machine_status.h"
25 
26 using namespace testing::ext;
27 using namespace OHOS::DistributedObject;
28 using namespace OHOS::DistributedData;
29 namespace OHOS::Test {
30 
31 class ObjectAssetMachineTest : public testing::Test {
32 public:
33     void SetUp();
34     void TearDown();
35 
36 protected:
37     AssetBindInfo AssetBindInfo_;
38     Asset asset_;
39     std::string uri_;
40     std::string bundleName_ = "test_bundleName";
41     std::map<std::string, ChangedAssetInfo> changedAssets_;
42     std::string sessionId = "123";
43     StoreInfo storeInfo_;
44     std::shared_ptr<ObjectAssetMachine> machine;
45 };
46 
SetUp()47 void ObjectAssetMachineTest::SetUp()
48 {
49     uri_ = "file:://com.huawei.hmos.notepad/data/storage/el2/distributedfiles/dir/asset1.jpg";
50     Asset asset{
51         .name = "test_name",
52         .uri = uri_,
53         .modifyTime = "modifyTime",
54         .size = "size",
55         .hash = "modifyTime_size",
56     };
57     asset_ = asset;
58 
59     VBucket vBucket{ { "11", 111 } };
60     AssetBindInfo AssetBindInfo{
61         .storeName = "store_test",
62         .tableName = "table_test",
63         .primaryKey = vBucket,
64         .field = "attachment",
65         .assetName = "asset1.jpg",
66     };
67     AssetBindInfo_ = AssetBindInfo;
68     StoreInfo storeInfo {
69         .tokenId = 0,
70         .bundleName = "bundleName_test",
71         .storeName = "store_test",
72         .instanceId = 1,
73         .user = 100,
74     };
75     storeInfo_ = storeInfo;
76     ChangedAssetInfo changedAssetInfo(asset, AssetBindInfo, storeInfo);
77     changedAssets_[uri_] = changedAssetInfo;
78     if (machine == nullptr) {
79         machine = std::make_shared<ObjectAssetMachine>();
80         auto executors = std::make_shared<ExecutorPool>(2, 1);
81         ObjectAssetLoader::GetInstance()->SetThreadPool(executors);
82     }
83 }
84 
TearDown()85 void ObjectAssetMachineTest::TearDown() {}
86 
87 /**
88 * @tc.name: StatusTransfer001
89 * @tc.desc: Remote changed when transferring.
90 * @tc.type: FUNC
91 * @tc.require:
92 * @tc.author: whj
93 */
94 HWTEST_F(ObjectAssetMachineTest, StatusTransfer001, TestSize.Level0)
95 {
96     Asset asset{
97         .name = "test_name",
98         .uri = uri_,
99         .modifyTime = "modifyTime2",
100         .size = "size2",
101         .hash = "modifyTime2_size2",
102     };
103     std::pair<std::string, Asset> changedAsset{ "device_2", asset };
104     changedAssets_[uri_].status = STATUS_TRANSFERRING;
105     machine->DFAPostEvent(REMOTE_CHANGED, changedAssets_[uri_], asset, changedAsset);
106     ASSERT_EQ(changedAssets_[uri_].status, STATUS_WAIT_TRANSFER);
107     ASSERT_EQ(changedAssets_[uri_].deviceId, changedAsset.first);
108     ASSERT_EQ(changedAssets_[uri_].asset.hash, asset.hash);
109 }
110 
111 /**
112 * @tc.name: StatusTransfer002
113 * @tc.desc: Transfer finished.
114 * @tc.type: FUNC
115 * @tc.require:
116 * @tc.author: whj
117 */
118 HWTEST_F(ObjectAssetMachineTest, StatusTransfer002, TestSize.Level0)
119 {
120     Asset asset{
121         .name = "test_name",
122         .uri = uri_,
123         .modifyTime = "modifyTime2",
124         .size = "size2",
125         .hash = "modifyTime2_size2",
126     };
127     std::pair<std::string, Asset> changedAsset{ "device_2", asset };
128     changedAssets_[uri_].status = STATUS_TRANSFERRING;
129     machine->DFAPostEvent(TRANSFER_FINISHED, changedAssets_[uri_], asset, changedAsset);
130     ASSERT_EQ(changedAssets_[uri_].status, STATUS_STABLE);
131 }
132 
133 /**
134 * @tc.name: StatusTransfer003
135 * @tc.desc: Transfer event
136 * @tc.type: FUNC
137 * @tc.require:
138 * @tc.author: whj
139 */
140 HWTEST_F(ObjectAssetMachineTest, StatusTransfer003, TestSize.Level0)
141 {
142     Asset asset{
143         .name = "test_name",
144         .uri = uri_,
145         .modifyTime = "modifyTime1",
146         .size = "size1",
147         .hash = "modifyTime1_size1",
148     };
149     std::pair<std::string, Asset> changedAsset{ "device_2", asset };
150     changedAssets_[uri_].status = STATUS_UPLOADING;
151     machine->DFAPostEvent(REMOTE_CHANGED, changedAssets_[uri_], asset, changedAsset);
152     ASSERT_EQ(changedAssets_[uri_].status, STATUS_WAIT_TRANSFER);
153     ASSERT_EQ(changedAssets_[uri_].deviceId, changedAsset.first);
154     ASSERT_EQ(changedAssets_[uri_].asset.hash, asset.hash);
155 }
156 
157 /**
158 * @tc.name: DFAPostEvent001
159 * @tc.desc: DFAPostEvent invalid eventId test
160 * @tc.type: FUNC
161 */
162 HWTEST_F(ObjectAssetMachineTest, DFAPostEvent001, TestSize.Level0)
163 {
164     Asset asset{
165         .name = "test_name",
166         .uri = uri_,
167         .modifyTime = "modifyTime1",
168         .size = "size1",
169         .hash = "modifyTime1_size1",
170     };
171     std::pair<std::string, Asset> changedAsset{ "device_2", asset };
172     changedAssets_[uri_].status = STATUS_UPLOADING;
173     auto ret = machine->DFAPostEvent(EVENT_BUTT, changedAssets_[uri_], asset, changedAsset);
174     ASSERT_EQ(ret, GeneralError::E_ERROR);
175 }
176 
177 /**
178 * @tc.name: StatusUpload001
179 * @tc.desc: No conflict scenarios: normal cloud sync.
180 * @tc.type: FUNC
181 * @tc.require:
182 * @tc.author: whj
183 */
184 HWTEST_F(ObjectAssetMachineTest, StatusUpload001, TestSize.Level0)
185 {
186     Asset asset{
187         .name = "test_name",
188         .uri = uri_,
189         .modifyTime = "modifyTime1",
190         .size = "size1",
191         .hash = "modifyTime1_size1",
192     };
193     std::pair<std::string, Asset> changedAsset{ "device_1", asset };
194     machine->DFAPostEvent(UPLOAD, changedAssets_[uri_], asset, changedAsset);
195     ASSERT_EQ(changedAssets_[uri_].status, STATUS_UPLOADING);
196 
197     machine->DFAPostEvent(UPLOAD_FINISHED, changedAssets_[uri_], asset);
198     ASSERT_EQ(changedAssets_[uri_].status, STATUS_STABLE);
199     // dotransfer
200     machine->DFAPostEvent(REMOTE_CHANGED, changedAssets_[uri_], asset, changedAsset);
201     ASSERT_EQ(changedAssets_[uri_].status, STATUS_TRANSFERRING);
202 }
203 
204 /**
205 * @tc.name: StatusUpload002
206 * @tc.desc: Conflict scenario: Upload before transfer.
207 * @tc.type: FUNC
208 * @tc.require:
209 * @tc.author: whj
210 */
211 HWTEST_F(ObjectAssetMachineTest, StatusUpload002, TestSize.Level0)
212 {
213     auto time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
214     auto timestamp = std::to_string(time);
215     Asset asset{
216         .name = "name_" + timestamp,
217         .uri = "uri_" + timestamp,
218         .modifyTime = "modifyTime_" + timestamp,
219         .size = "size_" + timestamp,
220         .hash = "modifyTime_size_" + timestamp,
221     };
222     AssetBindInfo bindInfo{
223         .storeName = "store_" + timestamp,
224         .tableName = "table_" + timestamp,
225         .primaryKey = {{ "key_" + timestamp, "value_" + timestamp }},
226         .field = "attachment_" + timestamp,
227         .assetName = "asset_" + timestamp + ".jpg",
228     };
229     StoreInfo storeInfo {
230         .tokenId = time,
231         .bundleName = "bundleName_" + timestamp,
232         .storeName = "store_" + timestamp,
233         .instanceId = time,
234         .user = time,
235     };
236     ChangedAssetInfo changedAssetInfo(asset, bindInfo, storeInfo);
237     std::pair<std::string, Asset> changedAsset{ "device_" + timestamp, asset };
238 
239     machine->DFAPostEvent(UPLOAD, changedAssetInfo, asset);
240     ASSERT_EQ(changedAssetInfo.status, STATUS_UPLOADING);
241 
242     machine->DFAPostEvent(REMOTE_CHANGED, changedAssetInfo, asset, changedAsset);
243     ASSERT_EQ(changedAssetInfo.status, STATUS_WAIT_TRANSFER);
244     ASSERT_EQ(changedAssetInfo.asset.hash, asset.hash);
245 
246     machine->DFAPostEvent(UPLOAD_FINISHED, changedAssetInfo, asset);
247     ASSERT_EQ(changedAssetInfo.status, STATUS_TRANSFERRING);
248 }
249 
250 /**
251 * @tc.name: StatusDownload001
252 * @tc.desc: No conflict scenarios: normal cloud sync.
253 * @tc.type: FUNC
254 * @tc.require:
255 * @tc.author: whj
256 */
257 HWTEST_F(ObjectAssetMachineTest, StatusDownload001, TestSize.Level0)
258 {
259     Asset asset{
260         .name = "name_006",
261         .uri = "uri_006",
262         .modifyTime = "modifyTime_006",
263         .size = "size_006",
264         .hash = "modifyTime_006_size_006",
265     };
266     AssetBindInfo AssetBindInfo{
267         .storeName = "store_006",
268         .tableName = "table_006",
269         .primaryKey = {{ "006", "006" }},
270         .field = "attachment_006",
271         .assetName = "asset_006.jpg",
272     };
273     StoreInfo storeInfo {
274         .tokenId = 600,
275         .bundleName = "bundleName_006",
276         .storeName = "store_006",
277         .instanceId = 600,
278         .user = 600,
279     };
280     ChangedAssetInfo changedAssetInfo(asset, AssetBindInfo, storeInfo);
281     std::pair<std::string, Asset> changedAsset{ "device_006", asset };
282 
283     machine->DFAPostEvent(DOWNLOAD, changedAssetInfo, asset, changedAsset);
284     ASSERT_EQ(changedAssetInfo.status, STATUS_DOWNLOADING);
285 
286     machine->DFAPostEvent(DOWNLOAD_FINISHED, changedAssetInfo, asset);
287     ASSERT_EQ(changedAssetInfo.status, STATUS_STABLE);
288 }
289 } // namespace OHOS::Test