File: SdkTestCaseInsensitiveCollision_test.cpp

package info (click to toggle)
megacmd 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 32,592 kB
  • sloc: cpp: 326,437; ansic: 34,524; python: 4,630; java: 3,965; sh: 2,869; objc: 2,459; makefile: 197; xml: 113
file content (502 lines) | stat: -rw-r--r-- 17,583 bytes parent folder | download | duplicates (2)
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
/**
 * @brief Test collision behavior uploading files for case insensitive systems
 *
 */

#include "integration_test_utils.h"
#include "mega/utils.h"
#include "sdk_test_utils.h"
#include "SdkTest_test.h"

class SdkTestCapitalisationCollision: public SdkTest
{
public:
    void SetUp() override;
    void TearDown() override;

    /**
     * @brief Create and download folders
     * @param remoteNames Remote folder names (created by the test)
     * @param localNames Local folder names after download
     * @param collisionResolution Method to resolve conflicts
     */
    void testCapitalisationFolderCollision(const std::vector<std::string>& remoteNames,
                                           const std::set<std::string>& localNames,
                                           const int collisionResolution);

    /**
     * @brief Create a Folder with two subfolders and download them
     * Remote names should create a conflict when they are downloaded
     * As folder is downloaded and we don't know the order of the subfolders, we check for the
     * existence of the suffix
     * @param remoteNames Remote folder names (created by the test)
     * @param collisionResolution Method to resolve conflicts
     */
    void testCapitalisationDownloadFolderWithCollision(const std::vector<std::string>& remoteNames,
                                                       const size_t numElementsWithSuffix,
                                                       const size_t numElementsWithoutSuffix,
                                                       const int collisionResolution);

    /**
     * @brief Create and download two files
     * @param remoteNames Remote file names (created by the test)
     * @param localNames Local file names after download
     * @param collisionResolution Method to resolve conflicts
     */
    void testCapitalisationFile(const std::vector<std::string>& remoteNames,
                                const std::set<std::string>& localNames,
                                const int collisionResolution);

    bool mIsCaseInsensitive{false};

private:
    std::filesystem::path mFolderDestination;
};

void SdkTestCapitalisationCollision::SetUp()
{
    SdkTest::SetUp();
    mFolderDestination = fs::current_path() / "Destination";
    std::filesystem::create_directory(mFolderDestination);
    std::unique_ptr<::mega::FileSystemAccess> fileSystemAccess = ::mega::createFSA();
    LocalPath path = LocalPath::fromAbsolutePath(path_u8string(fs::current_path()));
    auto value = isCaseInsensitive(path, fileSystemAccess.get());
    mIsCaseInsensitive = value.has_value() ? value.value() : false;
}

void SdkTestCapitalisationCollision::TearDown()
{
    SdkTest::TearDown();
    std::filesystem::remove_all(mFolderDestination);
}

void SdkTestCapitalisationCollision::testCapitalisationFolderCollision(
    const std::vector<std::string>& remoteNames,
    const std::set<std::string>& localNames,
    const int collisionResolution)
{
    ASSERT_GE(remoteNames.size(), 2u);

    ASSERT_NO_FATAL_FAILURE(getAccountsForTest(1));
    std::unique_ptr<MegaNode> rootnode{megaApi[0]->getRootNode()};
    ASSERT_TRUE(rootnode.get());

    MegaHandle hfolder1 = createFolder(0, remoteNames[0].c_str(), rootnode.get());
    ASSERT_NE(hfolder1, INVALID_HANDLE);
    std::unique_ptr<MegaNode> n1(megaApi[0]->getNodeByHandle(hfolder1));
    ASSERT_TRUE(n1.get());

    MegaHandle hfolder2 = createFolder(0, remoteNames[1].c_str(), rootnode.get());
    ASSERT_NE(hfolder2, INVALID_HANDLE);
    std::unique_ptr<MegaNode> n2(megaApi[0]->getNodeByHandle(hfolder2));
    ASSERT_TRUE(n2.get());

    auto errCode = sdk_test::downloadNode(megaApi[0].get(),
                                          n1.get(),
                                          mFolderDestination,
                                          true,
                                          180s /*timeout*/,
                                          MegaTransfer::COLLISION_CHECK_ASSUMEDIFFERENT,
                                          collisionResolution);

    ASSERT_EQ(errCode, API_OK);

    errCode = sdk_test::downloadNode(megaApi[0].get(),
                                     n2.get(),
                                     mFolderDestination,
                                     true,
                                     180s /*timeout*/,
                                     MegaTransfer::COLLISION_CHECK_ASSUMEDIFFERENT,
                                     collisionResolution);

    ASSERT_EQ(errCode, API_OK);

    size_t numElements = 0;
    for (const auto& element: std::filesystem::directory_iterator(mFolderDestination))
    {
        numElements++;
        const auto fileName = path_u8string(element.path().filename());
        ASSERT_NE(localNames.find(fileName), localNames.end())
            << "Unexpected file name: " << fileName;
    }

    ASSERT_EQ(numElements, localNames.size());
}

void SdkTestCapitalisationCollision::testCapitalisationDownloadFolderWithCollision(
    const std::vector<std::string>& remoteNames,
    const size_t numElementsWithSuffix,
    const size_t numElementsWithoutSuffix,
    const int collisionResolution)
{
    ASSERT_NO_FATAL_FAILURE(getAccountsForTest(1));
    std::unique_ptr<MegaNode> rootnode{megaApi[0]->getRootNode()};
    ASSERT_TRUE(rootnode.get());

    MegaHandle hTest = createFolder(0, "Test", rootnode.get());
    std::unique_ptr<MegaNode> testNode(megaApi[0]->getNodeByHandle(hTest));
    ASSERT_TRUE(testNode.get());

    MegaHandle hfolder1 = createFolder(0, remoteNames[0].c_str(), testNode.get());
    ASSERT_NE(hfolder1, INVALID_HANDLE);
    MegaHandle hfolder2 = createFolder(0, remoteNames[1].c_str(), testNode.get());
    ASSERT_NE(hfolder2, INVALID_HANDLE);

    auto errCode = sdk_test::downloadNode(megaApi[0].get(),
                                          testNode.get(),
                                          mFolderDestination,
                                          true,
                                          180s /*timeout*/,
                                          MegaTransfer::COLLISION_CHECK_ASSUMEDIFFERENT,
                                          collisionResolution);

    ASSERT_EQ(errCode, API_OK);

    size_t elementsWithSuffix = 0;
    size_t elementsWithoutSuffix = 0;
    std::string suffix{};
    switch (collisionResolution)
    {
        case MegaTransfer::COLLISION_RESOLUTION_EXISTING_TO_OLDN:
            suffix = ".old1";
            break;

        case MegaTransfer::COLLISION_RESOLUTION_NEW_WITH_N:
            suffix = "(1)";
            break;

        case MegaTransfer::COLLISION_RESOLUTION_OVERWRITE:
            suffix = "No suffix";
            break;
    }

    for (const auto& element: std::filesystem::directory_iterator(mFolderDestination / "Test"))
    {
        if (path_u8string(element.path().filename()).find(suffix) != std::string::npos)
        {
            elementsWithSuffix++;
        }
        else
        {
            elementsWithoutSuffix++;
        }
    }

    ASSERT_EQ(elementsWithoutSuffix, numElementsWithoutSuffix);
    ASSERT_EQ(elementsWithSuffix, numElementsWithSuffix);
}

void SdkTestCapitalisationCollision::testCapitalisationFile(
    const std::vector<std::string>& remoteNames,
    const std::set<std::string>& localNames,
    const int collisionResolution)
{
    std::string fileName{"f.txt"};
    ASSERT_NO_FATAL_FAILURE(getAccountsForTest(1));
    std::unique_ptr<MegaNode> rootnode{megaApi[0]->getRootNode()};
    ASSERT_TRUE(rootnode.get());
    ASSERT_TRUE(createFile(fileName, false)) << "Couldn't create " << fileName;

    MegaHandle uploadedNode1 = UNDEF;
    ASSERT_EQ(MegaError::API_OK,
              doStartUpload(0,
                            &uploadedNode1,
                            fileName.c_str(),
                            rootnode.get(),
                            remoteNames[0].c_str(),
                            ::mega::MegaApi::INVALID_CUSTOM_MOD_TIME,
                            nullptr /*appData*/,
                            false /*isSourceTemporary*/,
                            false /*startFirst*/,
                            nullptr /*cancelToken*/))
        << "Cannot upload a test file";

    std::unique_ptr<MegaNode> n1(megaApi[0]->getNodeByHandle(uploadedNode1));
    ASSERT_TRUE(n1.get());

    MegaHandle uploadedNode2 = UNDEF;
    ASSERT_EQ(MegaError::API_OK,
              doStartUpload(0,
                            &uploadedNode2,
                            fileName.c_str(),
                            rootnode.get(),
                            remoteNames[1].c_str(),
                            ::mega::MegaApi::INVALID_CUSTOM_MOD_TIME,
                            nullptr /*appData*/,
                            false /*isSourceTemporary*/,
                            false /*startFirst*/,
                            nullptr /*cancelToken*/))
        << "Cannot upload a test file";

    std::unique_ptr<MegaNode> n2(megaApi[0]->getNodeByHandle(uploadedNode2));
    ASSERT_TRUE(n2.get());

    auto errCode = sdk_test::downloadNode(megaApi[0].get(),
                                          n1.get(),
                                          mFolderDestination,
                                          true,
                                          180s /*timeout*/,
                                          MegaTransfer::COLLISION_CHECK_ASSUMEDIFFERENT,
                                          collisionResolution);

    ASSERT_EQ(errCode, API_OK);

    errCode = sdk_test::downloadNode(megaApi[0].get(),
                                     n2.get(),
                                     mFolderDestination,
                                     true,
                                     180s /*timeout*/,
                                     MegaTransfer::COLLISION_CHECK_ASSUMEDIFFERENT,
                                     collisionResolution);

    ASSERT_EQ(errCode, API_OK);

    size_t numElements = 0;
    for (const auto& element: std::filesystem::directory_iterator(mFolderDestination))
    {
        numElements++;
        const auto fileName = path_u8string(element.path().filename());
        ASSERT_NE(localNames.find(fileName), localNames.end())
            << "Unexpected file name: " << fileName;
    }

    ASSERT_EQ(numElements, localNames.size());
}

/**
 * @brief TEST_F CapitalisationCollistionFileOldN
 *
 * Steps:
 * - Upload two files with names "File.txt" and "FILE.TXT"
 * - Download with conflict resolution EXISTING_TO_OLDN
 * - Check result "File.old1.txt" and "FILE.TXT"
 */
TEST_F(SdkTestCapitalisationCollision, CapitalisationCollistionFileOldN)
{
    std::set<std::string> localNames;
    if (mIsCaseInsensitive)
    {
        localNames = {"File.old1.txt", "FILE.TXT"};
    }
    else
    {
        localNames = {"File.txt", "FILE.TXT"};
    }

    std::vector<std::string> remoteNames{"File.txt", "FILE.TXT"};

    testCapitalisationFile(remoteNames,
                           localNames,
                           MegaTransfer::COLLISION_RESOLUTION_EXISTING_TO_OLDN);
}

/**
 * @brief TEST_F CapitalisationCollistionFileNewWithN
 *
 * Steps:
 * - Upload two files with names "File.txt" and "FILE.TXT"
 * - Download with conflict resolution NEW_WITH_N
 * - Check result "File.txt" and "FILE (1).TXT"
 */
TEST_F(SdkTestCapitalisationCollision, CapitalisationCollistionFileNewWithN)
{
    std::set<std::string> localNames;
    if (mIsCaseInsensitive)
    {
        localNames = {"File.txt", "FILE (1).TXT"};
    }
    else
    {
        localNames = {"File.txt", "FILE.TXT"};
    }

    std::vector<std::string> remoteNames{"File.txt", "FILE.TXT"};

    testCapitalisationFile(remoteNames, localNames, MegaTransfer::COLLISION_RESOLUTION_NEW_WITH_N);
}

/**
 * @brief TEST_F CapitalisationCollistionFileOverwrite
 *
 * Steps:
 * - Upload two files with names "File.txt" and "FILE.TXT"
 * - Download with conflict resolution OVERWRITE
 * - Check result "FILE.TXT"
 */
TEST_F(SdkTestCapitalisationCollision, CapitalisationCollistionFileOverwrite)
{
    std::set<std::string> localNames;
    if (mIsCaseInsensitive)
    {
        localNames = {"FILE.TXT"};
    }
    else
    {
        localNames = {"File.txt", "FILE.TXT"};
    }

    std::vector<std::string> remoteNames{"File.txt", "FILE.TXT"};

    testCapitalisationFile(remoteNames, localNames, MegaTransfer::COLLISION_RESOLUTION_OVERWRITE);
}

/**
 * @brief TEST_F FolderCapitalisationCollistionNewWithN
 *
 * Steps:
 * - Create in the cloud two folders with names "Folder" and "FOLDER"
 * - Download with conflict resolution NEW_WITH_N
 * - Check result "Folder" and "FOLDER (1)"
 */
TEST_F(SdkTestCapitalisationCollision, FolderCapitalisationCollistionNewWithN)
{
    std::set<std::string> localNames;
    if (mIsCaseInsensitive)
    {
        localNames = {"Folder", "FOLDER (1)"};
    }
    else
    {
        localNames = {"Folder", "FOLDER"};
    }

    std::vector<std::string> remoteNames{"Folder", "FOLDER"};
    testCapitalisationFolderCollision(remoteNames,
                                      localNames,
                                      MegaTransfer::COLLISION_RESOLUTION_NEW_WITH_N);
}

/**
 * @brief TEST_F FolderCapitalisationCollistionOldN
 *
 * Steps:
 * - Create in the cloud two folders with names "Folder" and "FOLDER"
 * - Download with conflict resolution EXISTING_TO_OLDN
 * - Check result "Folder.old1" and "FOLDER"
 */
TEST_F(SdkTestCapitalisationCollision, FolderCapitalisationCollistionOldN)
{
    std::set<std::string> localNames;
    if (mIsCaseInsensitive)
    {
        localNames = {"Folder.old1", "FOLDER"};
    }
    else
    {
        localNames = {"Folder", "FOLDER"};
    }
    std::vector<std::string> remoteNames{"Folder", "FOLDER"};
    testCapitalisationFolderCollision(remoteNames,
                                      localNames,
                                      MegaTransfer::COLLISION_RESOLUTION_EXISTING_TO_OLDN);
}

/**
 * @brief TEST_F FolderCapitalisationCollistionOverwrite
 *
 * Steps:
 * - Create in the cloud two folders with names "Folder" and "FOLDER"
 * - Download with conflict resolution OVERWRITE
 * - Check result "Folder"
 */
TEST_F(SdkTestCapitalisationCollision, FolderCapitalisationCollistionOverwrite)
{
    std::set<std::string> localNames;
    if (mIsCaseInsensitive)
    {
        localNames = {"Folder"};
    }
    else
    {
        localNames = {"Folder", "FOLDER"};
    }

    std::vector<std::string> remoteNames{"Folder", "FOLDER"};
    testCapitalisationFolderCollision(remoteNames,
                                      localNames,
                                      MegaTransfer::COLLISION_RESOLUTION_OVERWRITE);
}

/**
 * @brief TEST_F DownloadFolderWithCapitalisationCollisionNewWithN
 *
 * Steps:
 * - Create in the cloud a Folder "Test" with two subfolders with names "Folder" and "FOLDER"
 * - Download with "Test" conflict resolution NEW_WITH_N
 * - Check result (two folders, one with suffix (1) order is not guaranteed)
 */
TEST_F(SdkTestCapitalisationCollision, DownloadFolderWithCapitalisationCollisionNewWithN)
{
    size_t numElementWithSuffix = 0;
    size_t numElementWithoutSuffix = 0;
    if (mIsCaseInsensitive)
    {
        numElementWithSuffix = 1;
        numElementWithoutSuffix = 1;
    }
    else
    {
        numElementWithoutSuffix = 2;
    }
    std::vector<std::string> remoteNames{"Folder", "FOLDER"};
    testCapitalisationDownloadFolderWithCollision(remoteNames,
                                                  numElementWithSuffix,
                                                  numElementWithoutSuffix,
                                                  MegaTransfer::COLLISION_RESOLUTION_NEW_WITH_N);
}

/**
 * @brief TEST_FDownloadFolderWithCapitalisationCollisionOldN
 *
 * Steps:
 * - Create in the cloud a Folder "Test" with two subfolders with names "Folder" and "FOLDER"
 * - Download with "Test" conflict resolution EXISTING_TO_OLDN
 * - Check result (two folders, one with suffix (.old1) order is not guaranteed)
 */
TEST_F(SdkTestCapitalisationCollision, DownloadFolderWithCapitalisationCollisionOldN)
{
    size_t numElementWithSuffix = 0;
    size_t numElementWithoutSuffix = 0;
    if (mIsCaseInsensitive)
    {
        numElementWithSuffix = 1;
        numElementWithoutSuffix = 1;
    }
    else
    {
        numElementWithoutSuffix = 2;
    }
    std::vector<std::string> remoteNames{"Folder", "FOLDER"};
    testCapitalisationDownloadFolderWithCollision(
        remoteNames,
        numElementWithSuffix,
        numElementWithoutSuffix,
        MegaTransfer::COLLISION_RESOLUTION_EXISTING_TO_OLDN);
}

/**
 * @brief TEST_F DownloadFolderWithCapitalisationCollisionOverwrite
 *
 * Steps:
 * - Create in the cloud a Folder "Test" with two subfolders with names "Folder" and "FOLDER"
 * - Download with "Test" conflict resolution OVERWRITE
 * - Check result (one folder, order is not guaranteed)
 */
TEST_F(SdkTestCapitalisationCollision, DownloadFolderWithCapitalisationCollisionOverwrite)
{
    size_t numElementWithSuffix = 0;
    size_t numElementWithoutSuffix = 0;
    if (mIsCaseInsensitive)
    {
        numElementWithoutSuffix = 1;
    }
    else
    {
        numElementWithoutSuffix = 2;
    }
    std::vector<std::string> remoteNames{"Folder", "FOLDER"};
    testCapitalisationDownloadFolderWithCollision(remoteNames,
                                                  numElementWithSuffix,
                                                  numElementWithoutSuffix,
                                                  MegaTransfer::COLLISION_RESOLUTION_OVERWRITE);
}