1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
|
/**
* @file
* @brief This file is expected to contain tests involving sync root paths (local
* and remote), e.g., what happens when the remote root of a sync gets deleted.
*/
#include <string_view>
#ifdef ENABLE_SYNC
#include "mock_listeners.h"
#include "SdkTestSyncNodesOperations.h"
#include <gmock/gmock.h>
using namespace sdk_test;
using namespace testing;
namespace
{
bool thereIsRenamedNode(const MegaNodeList* const nodes, const std::string_view targetName)
{
if (!nodes)
return false;
const auto nodeIsRenamed = [targetName](auto* const node)
{
return node && node->getName() == targetName &&
node->hasChanged(MegaNode::CHANGE_TYPE_NAME);
};
for (auto i = nodes->size(); i--;)
{
if (nodeIsRenamed(nodes->get(i)))
{
return true;
}
}
return false;
}
} // namespace
/**
* @class SdkTestSyncRootOperations
* @brief Test fixture designed to test operations involving sync root local and remote paths.
*/
class SdkTestSyncLocalOperations: public SdkTestSyncNodesOperations
{
public:
static constexpr std::string_view source{"test.cvj"};
static constexpr std::string_view target{"test.bak"};
const std::vector<NodeInfo>& getElements() const override
{
// To ensure "testCommonFile" is identical in both dirs
static const std::vector<NodeInfo> ELEMENTS{
DirNodeInfo(DEFAULT_SYNC_REMOTE_PATH)
.addChild(FileNodeInfo(std::string{target}).setSize(1000))
.addChild(FileNodeInfo(std::string{source}).setSize(900))};
return ELEMENTS;
}
void renameAndCreate() const
{
using clock = std::chrono::steady_clock;
const auto nodeRenamed = [](const MegaNodeList* nodes) -> bool
{
return thereIsRenamedNode(nodes, target);
};
// Track putnodes complete (move)
std::promise<clock::time_point> finishedRename;
NiceMock<MockNodesUpdateListener> mockNodesListener{megaApi[0].get()};
EXPECT_CALL(mockNodesListener, onNodesUpdate).Times(AnyNumber());
EXPECT_CALL(mockNodesListener, onNodesUpdate(_, Truly(nodeRenamed)))
.WillOnce(
[&finishedRename]
{
finishedRename.set_value(clock::now());
});
// Track new test.cvj upload. On first update, wait until move ends
const auto ExpectedTransfer = Pointer(Property(&MegaTransfer::getFileName, source));
const auto OkError = Pointer(Property(&MegaError::getErrorCode, API_OK));
std::promise<clock::time_point> finishedTransfer;
MockTransferListener mockTransferListener{megaApi[0].get()};
EXPECT_CALL(mockTransferListener, onTransferStart(_, ExpectedTransfer)).Times(1);
EXPECT_CALL(mockTransferListener, onTransferUpdate(_, ExpectedTransfer)).Times(AnyNumber());
EXPECT_CALL(mockTransferListener, onTransferFinish(_, ExpectedTransfer, OkError))
.WillOnce(
[&finishedTransfer]
{
finishedTransfer.set_value(clock::now());
});
megaApi[0]->addListener(&mockNodesListener);
megaApi[0]->addListener(&mockTransferListener);
std::filesystem::rename(localTmpPath() / source, localTmpPath() / target);
auto rnFut = finishedRename.get_future();
auto futureRenameStatus = rnFut.wait_for(COMMON_TIMEOUT);
ASSERT_EQ(futureRenameStatus, std::future_status::ready) << "Timeout rename";
sdk_test::createRandomFile(localTmpPath() / source, 950);
auto trFut = finishedTransfer.get_future();
auto futureStatus = trFut.wait_for(COMMON_TIMEOUT);
ASSERT_EQ(futureStatus, std::future_status::ready) << "Timeout transfer";
megaApi[0]->removeListener(&mockNodesListener);
megaApi[0]->removeListener(&mockTransferListener);
// Verify expectations on the mocks. If any expectation failed, this returns false.
bool nodesOk = ::testing::Mock::VerifyAndClearExpectations(&mockNodesListener);
bool transferOk = ::testing::Mock::VerifyAndClearExpectations(&mockTransferListener);
ASSERT_TRUE(nodesOk) << "Expectations on nodes listener failed.";
ASSERT_TRUE(transferOk) << "Expectations on transfer listener failed.";
const auto transferFinishTime = trFut.get();
const auto renameFinishTime = rnFut.get();
ASSERT_GT(transferFinishTime, renameFinishTime)
<< "Test is invalid, putnodes ended after the transfer finished";
std::this_thread::sleep_for(5s);
}
void disableSyncDeleteLocalFilesAndWaitForRedownloading()
{
static constexpr std::string_view logPre{
"disableSyncDeleteLocalFilesAndWaitForRedownloading : "};
LOG_verbose << logPre << "Disabling the sync";
ASSERT_NO_FATAL_FAILURE(disableSync());
LOG_verbose << logPre << "Deleting local files (source and target)";
std::filesystem::remove(localTmpPath() / source);
std::filesystem::remove(localTmpPath() / target);
std::this_thread::sleep_for(2s);
LOG_verbose << logPre << "Setting transfer expectations";
using clock = std::chrono::steady_clock;
const auto ExpectedTransfer1 = Pointer(Property(&MegaTransfer::getFileName, source));
const auto ExpectedTransfer2 = Pointer(Property(&MegaTransfer::getFileName, target));
const auto OkError1 = Pointer(Property(&MegaError::getErrorCode, API_OK));
const auto OkError2 = Pointer(Property(&MegaError::getErrorCode, API_OK));
std::promise<clock::time_point> finishedTransfer1;
std::promise<clock::time_point> finishedTransfer2;
MockTransferListener mockTransferListener{megaApi[0].get()};
EXPECT_CALL(mockTransferListener, onTransferStart(_, ExpectedTransfer1)).Times(1);
EXPECT_CALL(mockTransferListener, onTransferUpdate(_, ExpectedTransfer1))
.Times(AnyNumber());
EXPECT_CALL(mockTransferListener, onTransferFinish(_, ExpectedTransfer1, OkError1))
.WillOnce(
[&finishedTransfer1]
{
finishedTransfer1.set_value(clock::now());
});
EXPECT_CALL(mockTransferListener, onTransferStart(_, ExpectedTransfer2)).Times(1);
EXPECT_CALL(mockTransferListener, onTransferUpdate(_, ExpectedTransfer2))
.Times(AnyNumber());
EXPECT_CALL(mockTransferListener, onTransferFinish(_, ExpectedTransfer2, OkError2))
.WillOnce(
[&finishedTransfer2]
{
finishedTransfer2.set_value(clock::now());
});
megaApi[0]->addListener(&mockTransferListener);
LOG_verbose << logPre << "Resuming the sync";
ASSERT_NO_FATAL_FAILURE(resumeSync());
LOG_verbose << logPre << "Ensuring sync is running on " << DEFAULT_SYNC_REMOTE_PATH;
ASSERT_NO_FATAL_FAILURE(ensureSyncNodeIsRunning(DEFAULT_SYNC_REMOTE_PATH));
LOG_verbose << logPre << "Waiting for downloads";
ASSERT_EQ(finishedTransfer1.get_future().wait_for(COMMON_TIMEOUT),
std::future_status::ready);
ASSERT_EQ(finishedTransfer2.get_future().wait_for(COMMON_TIMEOUT),
std::future_status::ready);
LOG_verbose << logPre << "Sync-downloads completed!";
LOG_verbose << logPre << "Waiting for sync remote and local roots to have the same content";
ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocal());
LOG_verbose << logPre << "Waiting for sync completed!";
}
void renameAndCreateExtended(bool disableSyncAndCheckHashesAfterRedownload)
{
using clock = std::chrono::steady_clock;
const auto nodeRenamed = [](const MegaNodeList* nodes) -> bool
{
return thereIsRenamedNode(nodes, target);
};
// Track putnodes complete (move)
std::promise<clock::time_point> finishedRename;
NiceMock<MockNodesUpdateListener> mockNodesListener{megaApi[0].get()};
EXPECT_CALL(mockNodesListener, onNodesUpdate).Times(AnyNumber());
EXPECT_CALL(mockNodesListener, onNodesUpdate(_, Truly(nodeRenamed)))
.WillOnce(
[&finishedRename]
{
finishedRename.set_value(clock::now());
});
// Track new test.cvj upload. On first update, wait until move ends
const auto ExpectedTransfer = Pointer(Property(&MegaTransfer::getFileName, source));
const auto OkError = Pointer(Property(&MegaError::getErrorCode, API_OK));
std::promise<clock::time_point> finishedTransfer;
std::promise<std::string> gotFileName;
std::promise<error> errorTransfer;
MockTransferListener mockTransferListener{megaApi[0].get()};
EXPECT_CALL(mockTransferListener, onTransferStart(_, ExpectedTransfer)).Times(1);
EXPECT_CALL(mockTransferListener, onTransferUpdate(_, ExpectedTransfer)).Times(AnyNumber());
EXPECT_CALL(mockTransferListener, onTransferFinish(_, ExpectedTransfer, _))
.WillOnce(
[&finishedTransfer, &gotFileName, &errorTransfer](::mega::MegaApi*,
::mega::MegaTransfer* t,
::mega::MegaError* e)
{
finishedTransfer.set_value(clock::now());
LOG_debug << "[mockTransferFinish::onTransferFinish] t->getFileName = '"
<< t->getFileName() << "', t->getPath = '" << t->getPath() << "'";
gotFileName.set_value(t->getFileName());
errorTransfer.set_value((error)e->getErrorCode());
});
megaApi[0]->addListener(&mockNodesListener);
megaApi[0]->addListener(&mockTransferListener);
const auto sourceOriginalHash = hashFileHex(localTmpPath() / source);
const auto targetOriginalHash = hashFileHex(localTmpPath() / target);
std::filesystem::rename(localTmpPath() / source, localTmpPath() / target);
auto rnFut = finishedRename.get_future();
auto futureRnStatus = rnFut.wait_for(COMMON_TIMEOUT);
ASSERT_EQ(futureRnStatus, std::future_status::ready) << "Timeout rename";
const std::filesystem::path sourcePath = localTmpPath() / source;
sdk_test::createRandomFile(sourcePath, 950);
const auto sourceNewHash = hashFileHex(localTmpPath() / source);
auto trFut = finishedTransfer.get_future();
auto trFilenameFut = gotFileName.get_future();
auto errorTransferFut = errorTransfer.get_future();
auto futureStatus = trFut.wait_for(COMMON_TIMEOUT);
ASSERT_EQ(futureStatus, std::future_status::ready) << "Timeout transfer";
futureStatus = trFilenameFut.wait_for(COMMON_TIMEOUT);
ASSERT_EQ(futureStatus, std::future_status::ready) << "Timeout transfer get name";
futureStatus = errorTransferFut.wait_for(COMMON_TIMEOUT);
ASSERT_EQ(futureStatus, std::future_status::ready) << "Timeout transfer get error";
megaApi[0]->removeListener(&mockNodesListener);
megaApi[0]->removeListener(&mockTransferListener);
// Verify expectations on the mocks. If any expectation failed, this returns false.
bool nodesOk = ::testing::Mock::VerifyAndClearExpectations(&mockNodesListener);
bool transferOk = ::testing::Mock::VerifyAndClearExpectations(&mockTransferListener);
ASSERT_TRUE(nodesOk) << "Expectations on nodes listener failed.";
ASSERT_TRUE(transferOk) << "Expectations on transfer listener failed.";
const auto transferFinishTime = trFut.get();
const auto remoteName = trFilenameFut.get();
const auto transferError = errorTransferFut.get();
const auto renameFinishTime = rnFut.get();
ASSERT_GT(transferFinishTime, renameFinishTime)
<< "Test is invalid, putnodes ended after the transfer finished";
fs::path uploadedPath = localTmpPath() / remoteName;
EXPECT_EQ(uploadedPath, sourcePath);
const auto sourceCurrentHash = hashFileHex(localTmpPath() / source);
const auto targetCurrentHash = hashFileHex(localTmpPath() / target);
LOG_debug << "SourceOriginalHash: " << sourceOriginalHash
<< " [SourceCurrentHash: " << sourceCurrentHash << "]";
LOG_debug << "TargetOriginalHash: " << targetOriginalHash
<< " [TargetCurrentHash: " << targetCurrentHash << "]";
LOG_debug << "SourceNewHash: " << sourceNewHash
<< " [SourceCurrentHash: " << sourceCurrentHash << "]";
EXPECT_EQ(sourceOriginalHash, targetCurrentHash);
EXPECT_NE(sourceOriginalHash, targetOriginalHash);
EXPECT_EQ(sourceNewHash, sourceCurrentHash);
EXPECT_NE(sourceNewHash, sourceOriginalHash);
EXPECT_NE(sourceNewHash, targetOriginalHash);
ASSERT_EQ(transferError, API_OK);
std::this_thread::sleep_for(5s);
if (disableSyncAndCheckHashesAfterRedownload)
{
ASSERT_NO_FATAL_FAILURE(disableSyncDeleteLocalFilesAndWaitForRedownloading());
const auto sourceCurrentHashAfterFreshDownload = hashFileHex(localTmpPath() / source);
const auto targetCurrentHashAfterFreshDownload = hashFileHex(localTmpPath() / target);
LOG_debug << "Checking hashes of source (" << source << ") and target (" << target
<< ") after disabling the sync + deleting local files + resuming the sync + "
"sync-downloading source and target + calculate fresh hash for each";
LOG_debug << "SourceOriginalHash: " << sourceOriginalHash
<< " [SourceCurrentHash: " << sourceCurrentHash << "]";
LOG_debug << "TargetOriginalHash: " << targetOriginalHash
<< " [TargetCurrentHash: " << targetCurrentHash << "]";
LOG_debug << "SourceNewHash: " << sourceNewHash
<< " [SourceCurrentHash: " << sourceCurrentHash << "]";
EXPECT_EQ(sourceOriginalHash, targetCurrentHashAfterFreshDownload);
EXPECT_NE(sourceOriginalHash, targetOriginalHash);
EXPECT_EQ(sourceNewHash, sourceCurrentHashAfterFreshDownload);
EXPECT_NE(sourceNewHash, sourceOriginalHash);
EXPECT_NE(sourceNewHash, targetOriginalHash);
}
}
};
/**
* @brief Renames A to B (B already exists, so it's replaced) and creates a new A. After the move
* and the transfer finish, repeat the operation.
*
* The first time that a move operation takes place, the sync debris folder is not created yet,
* affecting the sequence of requests sent to the API:
* 1. The request to move (rename) the node to-be-displaced along with the request will be sent
* to create the daily SyncDebris.
* 2. Action packets are received, node to-be-displaced is not yet fully updated as there are now
* 2 duplicated nodes in the cloud, the renamed one and the old one that still needs to be sent to
* debris.
* 3. After receiving the action packets, the request to move to debris the node-to-be-displaced
* will be sent.
* 4. Immediately after, the move operation completion will be checked: the row.cloudNode still
* has the old handle (as it not has yet been moved to debris, that cloudNode is outdated). So the
* move operation is reset for evaluation.
* 5. The operation to move the node-to-be-displaced to debris will be finished, but the
* checkMoves will wait a bit (it considers the file is still changing, as it has stats that it
* didn't have before).
* 6. When the checkMoves takes place again, all the move operation in the cloud has been
* completed, so it doesn't need to start a move operation again.
*
* The second time a move operation takes place, the sync debris is created already:
* 1. The request to move (rename) the node-to-be-displaced will the request to move the
* node-to-be-displaced to the daily SyncDebris.
* 2. Action packets are received, updating the current cloud nodes accordingly, and the
* displaced node with the previous handle does not exist anymore.
* 3. Immediately after, the move operation completion will be checked: the row.cloudNode has the
* updated handle.
* 4. The move operation is completed from the sync engine: it takes all the data from the
* sourceSyncNode, including the transfer in flight, and marks the row as synced.
*
* Expectations are that only one upload transfer (the one to create the new A) is started in each
* iteration:
* 1. First iteration, there is a move operation which is cancelled. The upload transfer is never
* moved to another sync node.
* 2. Second iteration, the move operation is completed from the sync engine, and the upload
* transfer is moved to the target sync node (B). The fix must prevent this from happening for this
* scenario, avoiding a new upload to be started again from the new file A.
*/
TEST_F(SdkTestSyncLocalOperations, RenameAndCreateNew)
{
static const auto logPre{getLogPrefix()};
LOG_debug << logPre << "Starting";
for (const auto i: range(2))
{
LOG_debug << logPre << "rename n" << (i + 1);
ASSERT_NO_FATAL_FAILURE(renameAndCreate())
<< "Unexpected behaviour on the rename & create operation n" << i;
}
LOG_debug << logPre << "Finishing";
}
/**
* @brief RenameAndCreateNew test in "hard mode".
*
* 1. Uses random data.
* 2. Extended expectations for onTransferFinish, getting file names and delaying the error
* checking.
* 3. Calculates SHA256 of each file before, after and current (current = in the very moment of
* calling it, generally after some transfers or intermediate operations).
* 4. Final check at the end: disables the sync, deletes the local source and target files, resumes
* the sync so the files are sync-downloaded from the cloud, calculates the SHA-256 of each and
* checks that it is the expected one.
*/
TEST_F(SdkTestSyncLocalOperations, RenameAndCreateNewWithExtendedExpectations)
{
static const auto logPre{getLogPrefix()};
LOG_debug << logPre << "Starting";
for (const auto i: range(2))
{
LOG_debug << logPre << "rename n" << (i + 1);
ASSERT_NO_FATAL_FAILURE(renameAndCreateExtended(static_cast<bool>(i)))
<< "Unexpected behaviour on the rename & create operation n" << i;
}
LOG_debug << logPre << "Finishing";
}
#endif
|