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 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
|
/**
* (c) 2019 by Mega Limited, Wellsford, New Zealand
*
* This file is part of the MEGA SDK - Client Access Engine.
*
* Applications using the MEGA API must present a valid application key
* and comply with the the rules set forth in the Terms of Service.
*
* The MEGA SDK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @copyright Simplified (2-clause) BSD License.
*
* You should have received a copy of the license along with this
* program.
*/
#include "constants.h"
#include "DefaultedDbTable.h"
#include "DefaultedDirAccess.h"
#include "DefaultedFileAccess.h"
#include "DefaultedFileSystemAccess.h"
#include "FsNode.h"
#include "utils.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <mega/filesystem.h>
#include <mega/heartbeats.h>
#include <mega/megaapp.h>
#include <mega/megaclient.h>
#include <mega/scoped_helpers.h>
#include <mega/sync.h>
#include <mega/types.h>
#include <memory>
#include <numeric>
#ifdef ENABLE_SYNC
namespace mega {
enum { TYPE_TWOWAY = SyncConfig::TYPE_TWOWAY };
enum { TYPE_UP = SyncConfig::TYPE_UP };
enum { TYPE_DOWN = SyncConfig::TYPE_DOWN };
};
namespace SyncConfigTests
{
using namespace mega;
using namespace testing;
class Directory
{
public:
Directory(FSACCESS_CLASS& fsAccess, const LocalPath& path)
: mFSAccess(fsAccess)
, mPath(path)
{
mFSAccess.mkdirlocal(mPath, false, true);
}
~Directory()
{
mFSAccess.emptydirlocal(mPath);
mFSAccess.rmdirlocal(mPath);
}
MEGA_DISABLE_COPY_MOVE(Directory)
operator const LocalPath&() const
{
return mPath;
}
const LocalPath& path() const
{
return mPath;
}
private:
FSACCESS_CLASS& mFSAccess;
LocalPath mPath;
}; // Directory
// Temporary shims so that we can easily switch to using
// NiceMock / FakeStrictMock when GMock/GTest is upgraded on Jenkins.
#if 0
template<typename MockClass>
class FakeNiceMock
: public MockClass
{
public:
using MockClass::MockClass;
}; // FakeNiceMock<T>
template<typename MockClass>
class FakeStrictMock
: public MockClass
{
public:
using MockClass::MockClass;
}; // FakeStrictMock<T>
#else
template<typename T>
using FakeNiceMock = NiceMock<T>;
template<typename T>
using FakeStrictMock = StrictMock<T>;
#endif
class Utilities
{
public:
static string randomBase64(const size_t n = 16)
{
return Base64::btoa(randomBytes(n));
}
static string randomBytes(const size_t n)
{
return mRNG.genstring(n);
}
static bool randomFile(LocalPath path, const size_t n = 64)
{
auto fileAccess = mFSAccess.newfileaccess(false);
if (!fileAccess->fopen(path, false, true, FSLogging::logOnError))
{
return false;
}
if (fileAccess->size > 0)
{
if (!fileAccess->ftruncate())
{
return false;
}
}
const string data = randomBytes(n);
const byte* bytes = reinterpret_cast<const byte*>(&data[0]);
return fileAccess->fwrite(bytes, static_cast<unsigned>(n), 0x0);
}
static LocalPath randomPath(const LocalPath& prefix, const size_t n = 16)
{
LocalPath result = prefix;
result.appendWithSeparator(randomPathRelative(n), false);
return result;
}
static LocalPath randomPathAbsolute(const size_t n = 16)
{
return LocalPath::fromAbsolutePath(randomBase64(n));
}
static LocalPath randomPathRelative(const size_t n = 16)
{
return LocalPath::fromRelativePath(randomBase64(n));
}
static LocalPath separator()
{
#ifdef _WIN32
return LocalPath::fromRelativePath("\\");
#else // _WIN32
return LocalPath::fromRelativePath("/");
#endif // ! _WIN32
}
private:
static FSACCESS_CLASS mFSAccess;
static PrnGen mRNG;
}; // Utilities
FSACCESS_CLASS Utilities::mFSAccess;
PrnGen Utilities::mRNG;
class SyncConfigTest
: public Test
{
public:
class IOContext
: public SyncConfigIOContext
{
public:
IOContext(FileSystemAccess& fsAccess,
const string& authKey,
const string& cipherKey,
const string& name,
PrnGen& rng)
: SyncConfigIOContext(fsAccess,
authKey,
cipherKey,
name,
rng)
{
// Perform real behavior by default.
ON_CALL(*this, driveID(_))
.WillByDefault(Invoke(this, &IOContext::driveIDConcrete));
ON_CALL(*this, getSlotsInOrder(_, _))
.WillByDefault(Invoke(this, &IOContext::getSlotsInOrderConcrete));
ON_CALL(*this, read(_, _, _))
.WillByDefault(Invoke(this, &IOContext::readConcrete));
ON_CALL(*this, remove(_, _))
.WillByDefault(Invoke(this, &IOContext::removeSlotConcrete));
ON_CALL(*this, remove(_))
.WillByDefault(Invoke(this, &IOContext::removeAllSlotsConcrete));
ON_CALL(*this, write(_, _, _))
.WillByDefault(Invoke(this, &IOContext::writeConcrete));
}
MOCK_CONST_METHOD1(driveID, handle(const LocalPath&));
MOCK_METHOD2(getSlotsInOrder, error(const LocalPath&, vector<unsigned int>&));
MOCK_METHOD3(read, error(const LocalPath&, string&, unsigned int));
MOCK_METHOD2(remove, error(const LocalPath&, unsigned int));
MOCK_METHOD1(remove, error(const LocalPath&));
MOCK_METHOD3(write, error(const LocalPath&, const string&, unsigned int));
private:
// Delegate to real behavior.
handle driveIDConcrete(const LocalPath& drivePath)
{
return SyncConfigIOContext::driveID(drivePath);
}
error getSlotsInOrderConcrete(const LocalPath& dbPath,
vector<unsigned int>& slotsVec)
{
return SyncConfigIOContext::getSlotsInOrder(dbPath, slotsVec);
}
error readConcrete(const LocalPath& dbPath,
string& data,
unsigned int slot)
{
return SyncConfigIOContext::read(dbPath, data, slot);
}
error removeSlotConcrete(const LocalPath& dbPath,
unsigned int slot)
{
return SyncConfigIOContext::remove(dbPath, slot);
}
error removeAllSlotsConcrete(const LocalPath& dbPath)
{
return SyncConfigIOContext::remove(dbPath);
}
error writeConcrete(const LocalPath& dbPath,
const string& data,
unsigned int slot)
{
return SyncConfigIOContext::write(dbPath, data, slot);
}
}; // IOContext
SyncConfigTest()
: Test()
, mFSAccess()
, mRNG()
, mConfigAuthKey(Utilities::randomBytes(16))
, mConfigCipherKey(Utilities::randomBytes(16))
, mConfigName(Utilities::randomBase64(16))
, mIOContext(mFSAccess,
mConfigAuthKey,
mConfigCipherKey,
mConfigName,
mRNG)
{
}
string emptyDB() const
{
return "{\"sy\":[]}";
}
FSACCESS_CLASS& fsAccess()
{
return mFSAccess;
}
IOContext& ioContext()
{
return mIOContext;
}
protected:
FSACCESS_CLASS mFSAccess;
PrnGen mRNG;
const string mConfigAuthKey;
const string mConfigCipherKey;
const string mConfigName;
FakeNiceMock<IOContext> mIOContext;
}; // SyncConfigTest
class SyncConfigIOContextTest
: public SyncConfigTest
{
public:
SyncConfigIOContextTest()
: SyncConfigTest()
{
}
string configName() const
{
return configPrefix() + mConfigName;
}
const string& configPrefix() const
{
return SyncConfigIOContext::NAME_PREFIX;
}
}; // SyncConfigIOContextTest
TEST_F(SyncConfigIOContextTest, GetBadPath)
{
vector<unsigned int> slotsVec;
// Generate a bogus path.
const auto drivePath = Utilities::randomPathAbsolute();
// Try to read slots from an invalid path.
EXPECT_NE(ioContext().getSlotsInOrder(drivePath, slotsVec), API_OK);
// Slots should be empty.
EXPECT_TRUE(slotsVec.empty());
}
TEST_F(SyncConfigIOContextTest, GetNoSlots)
{
// Make sure the drive path exists.
Directory drive(fsAccess(), Utilities::randomPathAbsolute());
// Generate some malformed slots for this user.
{
LocalPath configPath = drive;
// This file will be ignored as it has no slot suffix.
configPath.appendWithSeparator(
LocalPath::fromRelativePath(configName()), false);
EXPECT_TRUE(Utilities::randomFile(configPath));
// This file will be ignored as it has a malformed slot suffix.
configPath.append(LocalPath::fromRelativePath("."));
EXPECT_TRUE(Utilities::randomFile(configPath));
// This file will be ignored as it has an invalid slot suffix.
configPath.append(LocalPath::fromRelativePath("Q"));
EXPECT_TRUE(Utilities::randomFile(configPath));
}
// Generate a slot for a different user.
{
LocalPath configPath = drive;
configPath.appendWithSeparator(
LocalPath::fromRelativePath(configPrefix()), false);
configPath.append(Utilities::randomPathRelative());
configPath.append(LocalPath::fromRelativePath(".0"));
EXPECT_TRUE(Utilities::randomFile(configPath));
}
vector<unsigned int> slotsVec;
// Try and get a list of slots.
EXPECT_EQ(ioContext().getSlotsInOrder(drive.path(), slotsVec), API_OK);
// Slots should be empty.
EXPECT_TRUE(slotsVec.empty());
}
TEST_F(SyncConfigIOContextTest, GetSlotsOrderedByModificationTime)
{
const size_t NUM_SLOTS = 3;
// Make sure drive path exists.
Directory drive(fsAccess(), Utilities::randomPathAbsolute());
// Generate some slots for this user.
{
LocalPath configPath = drive;
// Generate suitable config path prefix.
configPath.appendWithSeparator(
LocalPath::fromRelativePath(configName()), false);
for (size_t i = 0; i < NUM_SLOTS; ++i)
{
using std::to_string;
LocalPath newPath = configPath;
// Generate suffix.
LocalPath suffixPath =
LocalPath::fromRelativePath("." + to_string(i));
// Complete config path.
newPath.append(suffixPath);
// Populate the file.
EXPECT_TRUE(Utilities::randomFile(newPath));
// Set the modification time.
EXPECT_TRUE(fsAccess().setmtimelocal(newPath, static_cast<m_time_t>(i * 1000)));
}
}
vector<unsigned int> slotsVec;
// Get the slots.
EXPECT_EQ(ioContext().getSlotsInOrder(drive.path(), slotsVec), API_OK);
// Did we retrieve the correct number of slots?
ASSERT_EQ(slotsVec.size(), NUM_SLOTS);
// Are the slots ordered by descending modification time?
{
vector<unsigned int> expected(NUM_SLOTS, 0);
iota(begin(expected), end(expected), 0);
EXPECT_TRUE(equal(begin(expected), end(expected), slotsVec.rbegin()));
}
}
TEST_F(SyncConfigIOContextTest, GetSlotsOrderedBySlotSuffix)
{
const size_t NUM_SLOTS = 3;
// Make sure drive path exists.
Directory drive(fsAccess(), Utilities::randomPathAbsolute());
// Generate some slots for this user.
{
LocalPath configPath = drive;
// Generate suitable config path prefix.
configPath.appendWithSeparator(
LocalPath::fromRelativePath(configName()), false);
for (size_t i = 0; i < NUM_SLOTS; ++i)
{
using std::to_string;
LocalPath newPath = configPath;
// Generate suffix.
LocalPath suffixPath =
LocalPath::fromRelativePath("." + to_string(i));
// Complete config path.
newPath.append(suffixPath);
// Populate the file.
EXPECT_TRUE(Utilities::randomFile(newPath));
// Set the modification time.
EXPECT_TRUE(fsAccess().setmtimelocal(newPath, 0));
}
}
vector<unsigned int> slotsVec;
// Get the slots.
EXPECT_EQ(ioContext().getSlotsInOrder(drive.path(), slotsVec), API_OK);
// Did we retrieve the correct number of slots?
EXPECT_EQ(slotsVec.size(), NUM_SLOTS);
// Are the slots ordered by descending slot number when their
// modification time is the same?
{
vector<unsigned int> expected(NUM_SLOTS, 0);
iota(begin(expected), end(expected), 0);
EXPECT_TRUE(equal(begin(expected), end(expected), slotsVec.rbegin()));
}
}
TEST_F(SyncConfigIOContextTest, Read)
{
// Make sure the drive path exists.
Directory drive(fsAccess(), Utilities::randomPathAbsolute());
// Try writing some data out and reading it back again.
{
string read;
string written = Utilities::randomBytes(64);
EXPECT_EQ(ioContext().write(drive.path(), written, 0), API_OK);
EXPECT_EQ(ioContext().read(drive.path(), read, 0), API_OK);
EXPECT_EQ(read, written);
}
// Try a different slot to make sure it has an effect.
{
string read;
string written = Utilities::randomBytes(64);
EXPECT_EQ(ioContext().read(drive.path(), read, 1), API_EREAD);
EXPECT_TRUE(read.empty());
EXPECT_EQ(ioContext().write(drive.path(), written, 1), API_OK);
EXPECT_EQ(ioContext().read(drive.path(), read, 1), API_OK);
EXPECT_EQ(read, written);
}
}
TEST_F(SyncConfigIOContextTest, ReadBadData)
{
string data;
// Make sure the drive path exists.
Directory drive(fsAccess(), Utilities::randomPathAbsolute());
// Generate slot path.
LocalPath slotPath = drive;
slotPath.appendWithSeparator(
LocalPath::fromRelativePath(configName()), false);
slotPath.append(LocalPath::fromRelativePath(".0"));
// Try loading a file that's too short to be valid.
EXPECT_TRUE(Utilities::randomFile(slotPath, 1));
EXPECT_EQ(ioContext().read(drive.path(), data, 0), API_EREAD);
EXPECT_TRUE(data.empty());
// Try loading a file composed entirely of junk.
EXPECT_TRUE(Utilities::randomFile(slotPath, 128));
EXPECT_EQ(ioContext().read(drive.path(), data, 0), API_EREAD);
EXPECT_TRUE(data.empty());
}
TEST_F(SyncConfigIOContextTest, ReadBadPath)
{
const LocalPath drivePath = Utilities::randomPathAbsolute();
string data;
// Try and read data from an insane path.
EXPECT_EQ(ioContext().read(drivePath, data, 0), API_EREAD);
EXPECT_TRUE(data.empty());
}
TEST_F(SyncConfigIOContextTest, RemoveSlot)
{
// Make sure drive path exists.
Directory drive(fsAccess(), Utilities::randomPathAbsolute());
// Generate a slot for this user.
{
LocalPath configPath = drive;
// Generate path prefix.
configPath.appendWithSeparator(
LocalPath::fromRelativePath(configName()), false);
// Generate suffix.
configPath.append(LocalPath::fromRelativePath(".0"));
// Populate slot.
EXPECT_TRUE(Utilities::randomFile(configPath));
}
// Remove the slot.
EXPECT_EQ(ioContext().remove(drive.path(), 0), API_OK);
// Remove again won't fail since we don't want errors in the log that are not actually problems
EXPECT_EQ(ioContext().remove(drive.path(), 0), API_OK);
}
TEST_F(SyncConfigIOContextTest, RemoveSlots)
{
const auto drivePath = Utilities::randomPathAbsolute();
// No slots to remove.
EXPECT_CALL(ioContext(),
getSlotsInOrder(Eq(drivePath), _))
.WillOnce(Return(API_ENOENT));
EXPECT_EQ(ioContext().remove(drivePath), API_ENOENT);
// Two slots to remove.
static const vector<unsigned int> slotsVec = {0, 1};
EXPECT_CALL(ioContext(),
getSlotsInOrder(Eq(drivePath), _))
.WillRepeatedly(DoAll(SetArgReferee<1>(slotsVec),
Return(API_OK)));
// All slots should be removed successfully.
EXPECT_CALL(ioContext(),
remove(Eq(drivePath), _))
.WillRepeatedly(Return(API_OK));
EXPECT_EQ(ioContext().remove(drivePath), API_OK);
// Should only succeed if all slots can be removed.
EXPECT_CALL(ioContext(),
remove(Eq(drivePath), Eq(0u)))
.WillRepeatedly(Return(API_EWRITE));
EXPECT_EQ(ioContext().remove(drivePath), API_EWRITE);
}
TEST_F(SyncConfigIOContextTest, Serialize)
{
SyncConfigVector read;
SyncConfigVector written;
JSONWriter writer;
// Populate the database with two configs.
{
SyncConfig config;
config.mBackupId = 1;
config.mEnabled = false;
config.mError = NO_SYNC_ERROR;
config.mFilesystemFingerprint = fsfp_t(2, "1");
config.mLocalPath = Utilities::randomPathAbsolute();
config.mName = Utilities::randomBase64();
config.mOriginalPathOfRemoteRootNode = Utilities::randomBase64();
config.mRemoteNode = NodeHandle();
config.mWarning = NO_SYNC_WARNING;
config.mSyncType = SyncConfig::TYPE_TWOWAY;
config.mBackupState = SYNC_BACKUP_NONE;
written.emplace_back(config);
config.mBackupId = 2;
config.mEnabled = true;
config.mError = UNKNOWN_ERROR;
config.mFilesystemFingerprint = fsfp_t(2, "2");
config.mLocalPath = Utilities::randomPathAbsolute();
config.mName = Utilities::randomBase64();
config.mOriginalPathOfRemoteRootNode = Utilities::randomBase64();
config.mRemoteNode.set6byte(3);
config.mWarning = LOCAL_IS_FAT;
config.mSyncType = SyncConfig::TYPE_BACKUP;
config.mBackupState = SYNC_BACKUP_MIRROR;
written.emplace_back(config);
}
// Serialize the database.
{
ioContext().serialize(written, writer);
EXPECT_FALSE(writer.getstring().empty());
}
// Deserialize the database.
{
JSON reader(writer.getstring());
EXPECT_TRUE(ioContext().deserialize(read, reader, false));
}
// Are the databases identical?
ASSERT_EQ(read.size(), written.size());
for (auto i = read.size(); i--; )
{
auto& a = read[i];
auto& b = written[i];
EXPECT_EQ(a.mBackupId, b.mBackupId);
EXPECT_EQ(a.mEnabled, b.mEnabled);
EXPECT_EQ(a.mError, b.mError);
EXPECT_EQ(a.mFilesystemFingerprint, b.mFilesystemFingerprint);
EXPECT_EQ(a.mLocalPath, b.mLocalPath);
EXPECT_EQ(a.mName, b.mName);
EXPECT_EQ(a.mOriginalPathOfRemoteRootNode, b.mOriginalPathOfRemoteRootNode);
EXPECT_EQ(a.mRemoteNode, b.mRemoteNode);
EXPECT_EQ(a.mWarning, b.mWarning);
EXPECT_EQ(a.mSyncType, b.mSyncType);
EXPECT_EQ(a.mBackupState, b.mBackupState);
}
}
TEST_F(SyncConfigIOContextTest, SerializeEmpty)
{
JSONWriter writer;
// Serialize an empty database.
{
static const SyncConfigVector empty;
// Does serializing an empty database yield an empty array?
ioContext().serialize(empty, writer);
EXPECT_EQ(writer.getstring(), emptyDB());
}
// Deserialize the empty database.
{
SyncConfigVector configs;
JSON reader(writer.getstring());
// Can we deserialize an empty database?
EXPECT_TRUE(ioContext().deserialize(configs, reader, true));
EXPECT_TRUE(configs.empty());
}
}
TEST_F(SyncConfigIOContextTest, WriteBadPath)
{
const LocalPath drivePath = Utilities::randomPathAbsolute();
const string data = Utilities::randomBytes(64);
auto dbPath = drivePath;
dbPath.appendWithSeparator(Utilities::randomPathRelative(), false);
// Try and write data to an insane path.
EXPECT_NE(ioContext().write(dbPath, data, 0), API_OK);
}
class SyncConfigStoreTest
: public SyncConfigTest
{
public:
SyncConfigStoreTest()
: SyncConfigTest()
{
}
}; // SyncConfigStoreTest
TEST_F(SyncConfigStoreTest, Read)
{
Directory db(fsAccess(), Utilities::randomPathAbsolute());
SyncConfigVector written;
// Create a database for later reading.
{
SyncConfigStore store(db, ioContext());
// Read empty so that the drive is known.
EXPECT_EQ(store.read(LocalPath(), written, false), API_ENOENT);
// Drive should be known.
ASSERT_TRUE(store.driveKnown(LocalPath()));
// Create a config for writing.
SyncConfig config;
config.mBackupId = 1;
config.mLocalPath = Utilities::randomPathAbsolute();
config.mRemoteNode.set6byte(2);
written.emplace_back(config);
// Write database to disk.
EXPECT_EQ(store.write(LocalPath(), written), API_OK);
}
SyncConfigStore store(db, ioContext());
SyncConfigVector read;
// Read database back.
EXPECT_EQ(store.read(LocalPath(), read, false), API_OK);
// Drive should now be known.
EXPECT_TRUE(store.driveKnown(LocalPath()));
// Configs should be precisely what we wrote.
ASSERT_EQ(read.size(), written.size());
for (auto i = read.size(); i--; )
{
auto& a = read[i];
auto& b = written[i];
EXPECT_EQ(a.mBackupId, b.mBackupId);
EXPECT_EQ(a.mEnabled, b.mEnabled);
EXPECT_EQ(a.mError, b.mError);
EXPECT_EQ(a.mFilesystemFingerprint, b.mFilesystemFingerprint);
EXPECT_EQ(a.mLocalPath, b.mLocalPath);
EXPECT_EQ(a.mName, b.mName);
EXPECT_EQ(a.mOriginalPathOfRemoteRootNode, b.mOriginalPathOfRemoteRootNode);
EXPECT_EQ(a.mRemoteNode, b.mRemoteNode);
EXPECT_EQ(a.mWarning, b.mWarning);
EXPECT_EQ(a.mSyncType, b.mSyncType);
EXPECT_EQ(a.mBackupState, b.mBackupState);
}
}
TEST_F(SyncConfigStoreTest, ReadEmpty)
{
Directory db(fsAccess(), Utilities::randomPathAbsolute());
SyncConfigStore store(db, ioContext());
// No slots available for reading.
EXPECT_CALL(ioContext(),
getSlotsInOrder(Eq(db.path()), _))
.WillOnce(Return(API_ENOENT));
// No attempts should be made to read from disk.
EXPECT_CALL(ioContext(),
read(Eq(db.path()), _, _))
.Times(0);
SyncConfigVector configs;
// Read should inform the caller that no database is present.
EXPECT_EQ(store.read(LocalPath(), configs, false), API_ENOENT);
// Configs should remain empty.
EXPECT_TRUE(configs.empty());
// Drive should be known as read didn't signal a fatal error.
EXPECT_TRUE(store.driveKnown(LocalPath()));
// Store should remain clean.
EXPECT_FALSE(store.dirty());
}
TEST_F(SyncConfigStoreTest, ReadFailNoDriveID)
{
Directory db(fsAccess(), Utilities::randomPathAbsolute());
Directory drive(fsAccess(), Utilities::randomPathAbsolute());
SyncConfigStore store(db, ioContext());
// Shouldn't try to read slots if we can't read the drive ID.
EXPECT_CALL(ioContext(),
getSlotsInOrder(_, _))
.Times(0);
// Read of drive ID should fail.
EXPECT_CALL(ioContext(),
driveID(Eq(drive.path())))
.WillOnce(Return(UNDEF));
SyncConfigVector configs;
// Read should report a fatal read error.
EXPECT_EQ(store.read(drive, configs, true), API_EREAD);
// Drive shouldn't be known.
EXPECT_FALSE(store.driveKnown(drive));
// No slots available for reading.
EXPECT_CALL(ioContext(),
getSlotsInOrder(_, _))
.WillOnce(Return(API_ENOENT));
// Drive ID read should succeed.
EXPECT_CALL(ioContext(),
driveID(Eq(drive.path())))
.WillOnce(Return(1u));
// Read should report no entries.
EXPECT_EQ(store.read(drive, configs, true), API_ENOENT);
// Drive should now be known.
EXPECT_TRUE(store.driveKnown(drive));
// Drive ID should be cached.
EXPECT_EQ(store.driveID(drive), 1u);
}
TEST_F(SyncConfigStoreTest, ReadFail)
{
Directory db(fsAccess(), Utilities::randomPathAbsolute());
SyncConfigStore store(db, ioContext());
// Return a single slot for reading.
Expectation get =
EXPECT_CALL(ioContext(),
getSlotsInOrder(Eq(db.path()), _))
.WillOnce(DoAll(SetArgReferee<1>(vector<unsigned>{0}),
Return(API_OK)));
// Attempts to read the slot should fail.
Expectation read =
EXPECT_CALL(ioContext(),
read(Eq(db.path()), _, Eq(0u)))
.After(get)
.WillOnce(Return(API_EREAD));
SyncConfigVector configs;
// Read should report a fatal read error.
EXPECT_EQ(store.read(LocalPath(), configs, false), API_EREAD);
// Configs should remain empty.
EXPECT_TRUE(configs.empty());
// Drive should remain unknown as read failed fatally.
EXPECT_FALSE(store.driveKnown(LocalPath()));
// Store should remain clean.
EXPECT_FALSE(store.dirty());
}
TEST_F(SyncConfigStoreTest, ReadFailFallback)
{
Directory db(fsAccess(), Utilities::randomPathAbsolute());
SyncConfigStore store(db, ioContext());
// Return three slots for reading.
Expectation get =
EXPECT_CALL(ioContext(),
getSlotsInOrder(Eq(db.path()), _))
.WillOnce(DoAll(SetArgReferee<1>(vector<unsigned>{2, 1, 0}),
Return(API_OK)));
// Attempts to read slots 2 and 1 should fail.
Expectation read21 =
EXPECT_CALL(ioContext(),
read(Eq(db.path()), _, _))
.Times(2)
.After(get)
.WillRepeatedly(Return(API_EREAD));
// Attempts to read slot 0 should succeed.
Expectation read0 =
EXPECT_CALL(ioContext(),
read(Eq(db.path()), _, Eq(0u)))
.After(read21)
.WillOnce(DoAll(SetArgReferee<1>(emptyDB()),
Return(API_OK)));
SyncConfigVector configs;
// Read should succeed.
EXPECT_EQ(store.read(LocalPath(), configs, false), API_OK);
// Configs should remain empty.
EXPECT_TRUE(configs.empty());
// Drive should be known as the read eventually succeeded.
EXPECT_TRUE(store.driveKnown(LocalPath()));
// Should should remain clean.
EXPECT_FALSE(store.dirty());
}
TEST_F(SyncConfigStoreTest, WriteDirty)
{
Directory db(fsAccess(), Utilities::randomPathAbsolute());
SyncConfigStore store(db, ioContext());
SyncConfigVector configs;
// Perform a read such that the drive is known.
EXPECT_EQ(store.read(LocalPath(), configs, false), API_ENOENT);
SyncConfigVector internal;
// Populate configs.
{
SyncConfig config;
// External
config.mBackupId = 1;
config.mExternalDrivePath = Utilities::randomPathAbsolute();
config.mLocalPath = Utilities::randomPath(config.mExternalDrivePath);
config.mRemoteNode.set6byte(2);
configs.emplace_back(config);
// Internal
config.mBackupId = 2;
config.mExternalDrivePath.clear();
config.mLocalPath = Utilities::randomPathAbsolute();
config.mRemoteNode.set6byte(3);
configs.emplace_back(config);
internal.emplace_back(config);
}
// Mark internal database for writing.
store.markDriveDirty(LocalPath());
// Serialize configs for later comparison.
JSONWriter writer;
ioContext().serialize(internal, writer);
// First attempt to write the database should fail.
// Subsequent attempts should succeed.
EXPECT_CALL(ioContext(),
write(Eq(db.path()), Eq(writer.getstring()), Eq(0u)))
.Times(2)
.WillOnce(Return(API_EWRITE))
.WillRepeatedly(Return(API_OK));
// First attempt to write dirty databases should fail.
store.writeDirtyDrives(configs);
// Store should be clean.
EXPECT_FALSE(store.dirty());
// Mark drive as dirty.
store.markDriveDirty(LocalPath());
// Second attempt to write dirty databases should succeed.
store.writeDirtyDrives(configs);
// Store should now be clean.
EXPECT_FALSE(store.dirty());
}
TEST_F(SyncConfigStoreTest, WriteEmpty)
{
Directory db(fsAccess(), Utilities::randomPathAbsolute());
SyncConfigStore store(db, ioContext());
SyncConfigVector configs;
// Read empty so that the drive is known.
EXPECT_EQ(store.read(LocalPath(), configs, false), API_ENOENT);
// Drive should now be known.
ASSERT_TRUE(store.driveKnown(LocalPath()));
// Attempt to remove the database should fail.
EXPECT_CALL(ioContext(),
remove(Eq(db.path())))
.WillOnce(Return(API_EWRITE));
// Mark drive as dirty.
store.markDriveDirty(LocalPath());
// Write should fail.
EXPECT_EQ(store.write(LocalPath(), configs), API_EWRITE);
// Store should be clean.
EXPECT_FALSE(store.dirty());
// Mark drive as dirty.
store.markDriveDirty(LocalPath());
// Next attempt to remove should succeed.
EXPECT_CALL(ioContext(),
remove(Eq(db.path())))
.WillOnce(Return(API_OK));
// Write should succeed.
EXPECT_EQ(store.write(LocalPath(), configs), API_OK);
// Store should now be clean.
EXPECT_FALSE(store.dirty());
}
TEST_F(SyncConfigStoreTest, Write)
{
Directory db(fsAccess(), Utilities::randomPathAbsolute());
SyncConfigStore store(db, ioContext());
SyncConfigVector configs;
// Read empty so that the drive is known.
EXPECT_EQ(store.read(LocalPath(), configs, false), API_ENOENT);
// Drive should be known.
ASSERT_TRUE(store.driveKnown(LocalPath()));
// Populate config vector.
{
SyncConfig config;
config.mBackupId = 2;
config.mLocalPath = Utilities::randomPathAbsolute();
config.mRemoteNode.set6byte(3);
configs.emplace_back(config);
}
// Serialize configs for later comparison.
JSONWriter writer;
ioContext().serialize(configs, writer);
// Attempts to write the first slot should fail.
EXPECT_CALL(ioContext(),
write(Eq(db.path()), Eq(writer.getstring()), Eq(0u)))
.WillOnce(Return(API_EWRITE));
// No databases should be removed in anticipation as the write failed.
EXPECT_CALL(ioContext(),
remove(Eq(db.path()), Eq(1u)))
.Times(0);
// Mark drive as being dirty.
store.markDriveDirty(LocalPath());
// Write should fail.
EXPECT_EQ(store.write(LocalPath(), configs), API_EWRITE);
// Store should be clean.
EXPECT_FALSE(store.dirty());
// Mark drive as dirty.
store.markDriveDirty(LocalPath());
// Attempts to write the first slot should succeed.
EXPECT_CALL(ioContext(),
write(Eq(db.path()), Eq(writer.getstring()), Eq(0u)))
.WillOnce(Return(API_OK));
// Next slot should be removed in anticipation of future writes.
EXPECT_CALL(ioContext(),
remove(Eq(db.path()), Eq(1u)))
.WillOnce(Return(API_OK));
// Write should succeed.
EXPECT_EQ(store.write(LocalPath(), configs), API_OK);
// Store should be clean.
EXPECT_FALSE(store.dirty());
}
} // SyncConfigTests
#endif
|