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 "snapshot/machine_status.h"
23 #include "object_asset_loader.h"
24 #include "executor_pool.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 };
45
SetUp()46 void ObjectAssetMachineTest::SetUp()
47 {
48 uri_ = "file:://com.huawei.hmos.notepad/data/storage/el2/distributedfiles/dir/asset1.jpg";
49 Asset asset{
50 .name = "test_name",
51 .uri = uri_,
52 .modifyTime = "modifyTime",
53 .size = "size",
54 .hash = "modifyTime_size",
55 };
56 asset_ = asset;
57
58 VBucket vBucket{ { "11", 111 } };
59 AssetBindInfo AssetBindInfo{
60 .storeName = "store_test",
61 .tableName = "table_test",
62 .primaryKey = vBucket,
63 .field = "attachment",
64 .assetName = "asset1.jpg",
65 };
66 AssetBindInfo_ = AssetBindInfo;
67 StoreInfo storeInfo{
68 .tokenId = 0,
69 .instanceId = 1,
70 .user = 100,
71 .bundleName = "bundleName_test",
72 .storeName = "store_test",
73 };
74 storeInfo_ = storeInfo;
75 ChangedAssetInfo changedAssetInfo(asset, AssetBindInfo, storeInfo);
76 changedAssets_[uri_] = changedAssetInfo;
77 auto executors = std::make_shared<ExecutorPool>(1, 0);
78 ObjectAssetLoader::GetInstance()->SetThreadPool(executors);
79 }
80
TearDown()81 void ObjectAssetMachineTest::TearDown() {}
82
83 /**
84 * @tc.name: StatusTransfer001
85 * @tc.desc: Transfer event.
86 * @tc.type: FUNC
87 * @tc.require:
88 * @tc.author: whj
89 */
90 HWTEST_F(ObjectAssetMachineTest, StatusTransfer001, TestSize.Level0)
91 {
92 auto machine = std::make_shared<ObjectAssetMachine>();
93 Asset asset{
94 .name = "test_name",
95 .uri = uri_,
96 .modifyTime = "modifyTime1",
97 .size = "size1",
98 .hash = "modifyTime1_size1",
99 };
100 std::pair<std::string, Asset> changedAsset{ "device_1", asset };
101 changedAssets_[uri_].status = STATUS_STABLE;
102 machine->DFAPostEvent(REMOTE_CHANGED, changedAssets_[uri_], asset, changedAsset);
103 ASSERT_EQ(changedAssets_[uri_].status, STATUS_TRANSFERRING);
104 ASSERT_EQ(changedAssets_[uri_].deviceId, changedAsset.first);
105 ASSERT_EQ(changedAssets_[uri_].asset.hash, asset.hash);
106 }
107
108 /**
109 * @tc.name: StatusTransfer002
110 * @tc.desc: Remote changed when transferring.
111 * @tc.type: FUNC
112 * @tc.require:
113 * @tc.author: whj
114 */
115 HWTEST_F(ObjectAssetMachineTest, StatusTransfer002, TestSize.Level0)
116 {
117 auto machine = std::make_shared<ObjectAssetMachine>();
118 Asset asset{
119 .name = "test_name",
120 .uri = uri_,
121 .modifyTime = "modifyTime2",
122 .size = "size2",
123 .hash = "modifyTime2_size2",
124 };
125 std::pair<std::string, Asset> changedAsset{ "device_2", asset };
126 changedAssets_[uri_].status = STATUS_TRANSFERRING;
127 machine->DFAPostEvent(REMOTE_CHANGED, changedAssets_[uri_], asset, changedAsset);
128 ASSERT_EQ(changedAssets_[uri_].status, STATUS_WAIT_TRANSFER);
129 ASSERT_EQ(changedAssets_[uri_].deviceId, changedAsset.first);
130 ASSERT_EQ(changedAssets_[uri_].asset.hash, asset.hash);
131 }
132
133 /**
134 * @tc.name: StatusTransfer003
135 * @tc.desc: Compensate transfer in conflict scenario.
136 * @tc.type: FUNC
137 * @tc.require:
138 * @tc.author: whj
139 */
140 HWTEST_F(ObjectAssetMachineTest, StatusTransfer003, TestSize.Level0)
141 {
142 auto machine = std::make_shared<ObjectAssetMachine>();
143 Asset asset{
144 .name = "test_name",
145 .uri = uri_,
146 .modifyTime = "modifyTime1",
147 .size = "size1",
148 .hash = "modifyTime1_size1",
149 };
150 std::pair<std::string, Asset> changedAsset{ "device_1", asset };
151 changedAssets_[uri_].status = STATUS_WAIT_TRANSFER;
152 changedAssets_[uri_].deviceId = "device_2";
153 changedAssets_[uri_].asset.hash = "modifyTime2_size2";
154 machine->DFAPostEvent(TRANSFER_FINISHED, changedAssets_[uri_], asset, changedAsset);
155 ASSERT_EQ(changedAssets_[uri_].status, STATUS_TRANSFERRING);
156 ASSERT_EQ(changedAssets_[uri_].deviceId, "device_2");
157 ASSERT_EQ(changedAssets_[uri_].asset.hash, "modifyTime2_size2");
158 }
159
160 /**
161 * @tc.name: StatusTransfer004
162 * @tc.desc: Transfer finished.
163 * @tc.type: FUNC
164 * @tc.require:
165 * @tc.author: whj
166 */
167 HWTEST_F(ObjectAssetMachineTest, StatusTransfer004, TestSize.Level0)
168 {
169 auto machine = std::make_shared<ObjectAssetMachine>();
170 Asset asset{
171 .name = "test_name",
172 .uri = uri_,
173 .modifyTime = "modifyTime2",
174 .size = "size2",
175 .hash = "modifyTime2_size2",
176 };
177 std::pair<std::string, Asset> changedAsset{ "device_2", asset };
178 changedAssets_[uri_].status = STATUS_TRANSFERRING;
179 machine->DFAPostEvent(TRANSFER_FINISHED, changedAssets_[uri_], asset, changedAsset);
180 ASSERT_EQ(changedAssets_[uri_].status, STATUS_STABLE);
181 }
182
183 /**
184 * @tc.name: StatusUpload001
185 * @tc.desc: No conflict scenarios: normal cloud sync.
186 * @tc.type: FUNC
187 * @tc.require:
188 * @tc.author: whj
189 */
190 HWTEST_F(ObjectAssetMachineTest, StatusUpload001, TestSize.Level0)
191 {
192 auto machine = std::make_shared<ObjectAssetMachine>();
193 Asset asset{
194 .name = "test_name",
195 .uri = uri_,
196 .modifyTime = "modifyTime1",
197 .size = "size1",
198 .hash = "modifyTime1_size1",
199 };
200 std::pair<std::string, Asset> changedAsset{ "device_1", asset };
201 machine->DFAPostEvent(UPLOAD, changedAssets_[uri_], asset, changedAsset);
202 ASSERT_EQ(changedAssets_[uri_].status, STATUS_UPLOADING);
203
204 machine->DFAPostEvent(UPLOAD_FINISHED, changedAssets_[uri_], asset);
205 ASSERT_EQ(changedAssets_[uri_].status, STATUS_STABLE);
206 }
207
208 /**
209 * @tc.name: StatusUpload002
210 * @tc.desc: Conflict scenario: Upload before transfer.
211 * @tc.type: FUNC
212 * @tc.require:
213 * @tc.author: whj
214 */
215 HWTEST_F(ObjectAssetMachineTest, StatusUpload002, TestSize.Level0)
216 {
217 auto machine = std::make_shared<ObjectAssetMachine>();
218 Asset asset{
219 .name = "test_name",
220 .uri = uri_,
221 .modifyTime = "modifyTime1",
222 .size = "size1",
223 .hash = "modifyTime1_size1",
224 };
225 std::pair<std::string, Asset> changedAsset{ "device_1", asset };
226 machine->DFAPostEvent(UPLOAD, changedAssets_[uri_], asset);
227 ASSERT_EQ(changedAssets_[uri_].status, STATUS_UPLOADING);
228
229 machine->DFAPostEvent(REMOTE_CHANGED, changedAssets_[uri_], asset, changedAsset);
230 ASSERT_EQ(changedAssets_[uri_].status, STATUS_WAIT_TRANSFER);
231 ASSERT_EQ(changedAssets_[uri_].asset.hash, asset.hash);
232
233 machine->DFAPostEvent(UPLOAD_FINISHED, changedAssets_[uri_], asset);
234 ASSERT_EQ(changedAssets_[uri_].status, STATUS_TRANSFERRING);
235 }
236 } // namespace OHOS::Test
237