File: sdk_test_share_nested.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 (355 lines) | stat: -rw-r--r-- 16,211 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
/**
 * @file sdk_test_share_nested.cpp
 * @brief This file defines tests related with nested shares
 */

#include "sdk_test_share.h"
#include "SdkTestNodesSetUp.h"

using namespace sdk_test;

class SdkTestShareNested: public virtual SdkTestShare, public virtual SdkTestNodesSetUp
{
public:
    void SetUp() override
    {
        SdkTestShare::SetUp();
        ASSERT_NO_FATAL_FAILURE(getAccountsForTest(3));
        ASSERT_NO_FATAL_FAILURE(createRootTestDir());
        createNodes(getElements(), getRootTestDirectory());
    }

    const std::string& getRootTestDir() const override
    {
        return rootTestDir;
    }

    const std::vector<sdk_test::NodeInfo>& getElements() const override
    {
        return treeElements;
    }

    // Override, we don't need to have different creation time.
    bool keepDifferentCreationTimes() override
    {
        return false;
    }

    // Create a file node in the remote account.
    // Optionally, it will be validate in apiIndexB acount.
    void createRemoteFileNode(unsigned apiIndexA,
                              const sdk_test::FileNodeInfo& fileInfo,
                              MegaNode* rootnode,
                              std::optional<unsigned> apiIndexB)
    {
        bool checkA{false};
        bool checkB{false};
        mApi[apiIndexA].mOnNodesUpdateCompletion =
            createOnNodesUpdateLambda(INVALID_HANDLE, MegaNode::CHANGE_TYPE_NEW, checkA);
        if (apiIndexB)
        {
            mApi[*apiIndexB].mOnNodesUpdateCompletion =
                createOnNodesUpdateLambda(INVALID_HANDLE, MegaNode::CHANGE_TYPE_NEW, checkB);
        }
        sdk_test::LocalTempFile localFile{fileInfo.name, fileInfo.size};
        MegaHandle file1Handle = INVALID_HANDLE;
        ASSERT_EQ(MegaError::API_OK,
                  doStartUpload(apiIndexA,
                                &file1Handle,
                                fileInfo.name.c_str(),
                                rootnode,
                                nullptr /*fileName*/,
                                fileInfo.mtime,
                                nullptr /*appData*/,
                                false /*isSourceTemporary*/,
                                false /*startFirst*/,
                                nullptr /*cancelToken*/))
            << "Failure uploading a file";

        ASSERT_TRUE(waitForResponse(&checkA)) << "New node not received on client " << apiIndexA
                                              << " after " << maxTimeout << " seconds";
        if (apiIndexB)
        {
            ASSERT_TRUE(waitForResponse(&checkB))
                << "New node not received on client " << *apiIndexB << " after " << maxTimeout
                << " seconds";
        }
        resetOnNodeUpdateCompletionCBs();
        std::unique_ptr<MegaNode> nodeFile{megaApi[apiIndexA]->getNodeByHandle(file1Handle)};
        ASSERT_NE(nodeFile, nullptr)
            << "Cannot get the node for the updated file (error: " << mApi[apiIndexA].lastError
            << ")";
        setNodeAdditionalAttributes(fileInfo, nodeFile);
    }

    void matchTree(const MegaHandle rootHandle, unsigned apiIndexA, unsigned apiIndexB) const
    {
        const std::unique_ptr<MegaNode> rootNodeA{megaApi[apiIndexA]->getNodeByHandle(rootHandle)};
        const std::unique_ptr<MegaNode> rootNodeB{megaApi[apiIndexB]->getNodeByHandle(rootHandle)};
        ASSERT_TRUE(rootNodeA) << "Node not present the accout #" << apiIndexA
                               << ". Handle: " << toNodeHandle(rootHandle);
        ASSERT_TRUE(rootNodeB) << "Node not present the second accout#" << apiIndexB
                               << ". Handle: " << toNodeHandle(rootHandle);

        ASSERT_NO_FATAL_FAILURE(
            matchTreeRecurse(rootNodeA.get(), rootNodeB.get(), apiIndexA, apiIndexB));
    }

    bool waitForNodeToBeDecrypted(unsigned apiIndex, MegaHandle nodeHandle)
    {
        return WaitFor(
            [this, apiIndex, nodeHandle]()
            {
                unique_ptr<MegaNode> node(megaApi[apiIndex]->getNodeByHandle(nodeHandle));
                return node && node->isNodeKeyDecrypted();
            },
            defaultTimeoutMs);
    }

protected:
    // Name of the initial elements in the remote tree
    static constexpr auto FOLDER_A = "folderA";
    static constexpr auto FOLDER_B = "folderB";
    static constexpr auto FOLDER_C = "folderC";
    static constexpr auto FILE_A = "fileA";
    static constexpr auto FILE_B = "fileB";
    static constexpr auto FILE_C = "fileC";

    static constexpr unsigned sharerIndex{0};
    static constexpr unsigned shareeAliceIndex{1};
    static constexpr unsigned shareeBobIndex{2};

private:
    // root in the cloud where the tree is created
    const std::string rootTestDir{"locklessCS"};

    // It represents the following tree:
    // RemoteRoot
    // └── "folderA"
    //     ├── "fileA"
    //     └── "folderB"
    //         ├── "fileB"
    //         └── "folderC"
    //             └── "fileC"
    const std::vector<NodeInfo> treeElements{
        DirNodeInfo(FOLDER_A)
            .addChild(FileNodeInfo(FILE_A).setSize(100))
            .addChild(
                DirNodeInfo(FOLDER_B)
                    .addChild(FileNodeInfo(FILE_B).setSize(100))
                    .addChild(DirNodeInfo(FOLDER_C).addChild(FileNodeInfo(FILE_C).setSize(100))))};

    // Check if the passed nodes have the same handle, if they are are decrypted and if they have
    // the same name. Print meaninful messages depending on the assert.
    void verifySameNodes(MegaNode* nodeA,
                         unsigned apiIndexA,
                         MegaNode* nodeB,
                         unsigned apiIndexB) const
    {
        ASSERT_TRUE(nodeA && nodeB) << "Invalid nodes in the comparision.";
        ASSERT_EQ(nodeA->getHandle(), nodeB->getHandle())
            << "Handles don't match. " << toNodeHandle(nodeA->getHandle()) << " vs "
            << toNodeHandle(nodeB->getHandle());
        ASSERT_TRUE(nodeA->isNodeKeyDecrypted() || nodeB->isNodeKeyDecrypted())
            << "Node " << toNodeHandle(nodeA->getHandle())
            << " is not decryptable in both accounts " << apiIndexA << " and " << apiIndexB;
        ASSERT_FALSE(nodeA->isNodeKeyDecrypted() && !nodeB->isNodeKeyDecrypted())
            << "Account " << apiIndexB << " can't decrypt " << nodeA->getName();
        ASSERT_FALSE(!nodeA->isNodeKeyDecrypted() && nodeB->isNodeKeyDecrypted())
            << "Account " << apiIndexA << " can't decrypt " << nodeB->getName();
        ASSERT_STREQ(nodeA->getName(), nodeB->getName())
            << "Node names don't match in in both accounts.";
    };

    // It validates the passed nodes and their descents.
    // The function is called recursively for folders.
    void matchTreeRecurse(MegaNode* rootNodeA,
                          MegaNode* rootNodeB,
                          unsigned apiIndexA,
                          unsigned apiIndexB) const
    {
        ASSERT_NO_FATAL_FAILURE(verifySameNodes(rootNodeA, apiIndexA, rootNodeB, apiIndexB));

        std::unique_ptr<MegaNodeList> childrenListA{megaApi[apiIndexA]->getChildren(rootNodeA)};
        std::unique_ptr<MegaNodeList> childrenListB{megaApi[apiIndexB]->getChildren(rootNodeB)};
        std::unordered_map<MegaHandle, MegaNode*>
            indexB; // Index for childrenListB using the handles.

        for (auto j = 0; j < childrenListB->size(); ++j)
        {
            auto childNodeB{childrenListB->get(j)};
            ASSERT_TRUE(childNodeB) << "null node in the list of childs of " << rootNodeB->getName()
                                    << "in the " << apiIndexB << " account.";
            indexB.emplace(childNodeB->getHandle(), childNodeB);
        }

        for (auto i = 0; i < childrenListA->size(); ++i)
        {
            auto childNodeA = childrenListA->get(i);
            ASSERT_TRUE(childNodeA) << "null node in the list of childs of " << rootNodeA->getName()
                                    << "in the " << apiIndexA << " account.";
            auto itChildNodeB = indexB.find(childNodeA->getHandle());
            ASSERT_NE(itChildNodeB, indexB.end())
                << "Can't find "
                << (childNodeA->isNodeKeyDecrypted() ? childNodeA->getName() :
                                                       toNodeHandle(childNodeA->getHandle()))
                << " in the " << apiIndexB << " account";
            auto childNodeB = itChildNodeB->second;
            if (childNodeA->isFolder() && childNodeB->isFolder())
            {
                ASSERT_NO_FATAL_FAILURE(
                    matchTreeRecurse(childNodeA, childNodeB, apiIndexA, apiIndexB));
            }
            else
            {
                ASSERT_NO_FATAL_FAILURE(
                    verifySameNodes(childNodeA, apiIndexA, childNodeB, apiIndexB));
            }
            indexB.erase(itChildNodeB);
        }

        std::string extraNodes;
        for (auto [_, unmatchedChildNodeB]: indexB)
        {
            extraNodes += " " + toNodeHandle(unmatchedChildNodeB->getHandle());
            if (unmatchedChildNodeB->isNodeKeyDecrypted())
                extraNodes += ":" + string(unmatchedChildNodeB->getName());
        }
        ASSERT_EQ(indexB.size(), 0) << "Unexpected " << indexB.size() << "node(s) found in the "
                                    << apiIndexB << " account: " << extraNodes;
    }
};

/**
 * @brief Basic test for nested shares
 *
 * It tests the basic functionality, creating a nested share and ensuring
 * that all peers can see their respective files.
 *
 */
TEST_F(SdkTestShareNested, BasicNestedShares)
{
    const auto logPre = getLogPrefix();

    LOG_info << "Starting body of " << logPre;

    // Make sharer and sharees contacts.
    ASSERT_NO_FATAL_FAILURE(
        inviteTestAccount(sharerIndex, shareeAliceIndex, "Sharer inviting Alice"))
        << "Failure inviting Alice";
    ASSERT_NO_FATAL_FAILURE(inviteTestAccount(sharerIndex, shareeBobIndex, "Sharer inviting Bob"))
        << "Failure inviting Bob";

    if (gManualVerification)
    {
        ASSERT_NO_FATAL_FAILURE(verifyContactCredentials(sharerIndex, shareeAliceIndex));
        ASSERT_NO_FATAL_FAILURE(verifyContactCredentials(sharerIndex, shareeBobIndex));
    }

    LOG_info << logPre << "Share folder \"folderA\" to Alice and subfolder \"folderB\" to Bob";
    auto sharerFolderANode = getNodeByPath(FOLDER_A);
    auto sharerFolderBNode = getNodeByPath(string(FOLDER_A) + "/" + FOLDER_B);
    ASSERT_TRUE(sharerFolderANode) << "folder \"folderA\" not found.";
    ASSERT_TRUE(sharerFolderBNode) << "folder \"folderB\" not found.";
    ASSERT_NO_FATAL_FAILURE(createShareAtoB(sharerFolderANode.get(),
                                            {sharerIndex, true},
                                            {shareeAliceIndex, true},
                                            MegaShare::ACCESS_FULL));
    ASSERT_NO_FATAL_FAILURE(createShareAtoB(sharerFolderBNode.get(),
                                            {sharerIndex, true},
                                            {shareeBobIndex, true},
                                            MegaShare::ACCESS_FULL));

    LOG_info << logPre
             << "Ensure that the sharer, Alice and Bob can see the same nodes and that the tree is "
                "decrypted.";
    waitForNodeToBeDecrypted(shareeAliceIndex, sharerFolderANode->getHandle());
    ASSERT_NO_FATAL_FAILURE(
        matchTree(sharerFolderANode->getHandle(), sharerIndex, shareeAliceIndex));
    waitForNodeToBeDecrypted(shareeBobIndex, sharerFolderBNode->getHandle());
    ASSERT_NO_FATAL_FAILURE(matchTree(sharerFolderBNode->getHandle(), sharerIndex, shareeBobIndex));
    ASSERT_NO_FATAL_FAILURE(
        matchTree(sharerFolderBNode->getHandle(), shareeAliceIndex, shareeBobIndex));

    LOG_info << logPre << "Logout and login to ensure that all is correct after fetching nodes.";
    ASSERT_NO_FATAL_FAILURE(logout(sharerIndex, false, maxTimeout));
    ASSERT_NO_FATAL_FAILURE(login(sharerIndex));
    ASSERT_NO_FATAL_FAILURE(fetchnodes(sharerIndex));
    ASSERT_NO_FATAL_FAILURE(logout(shareeAliceIndex, false, maxTimeout));
    ASSERT_NO_FATAL_FAILURE(login(shareeAliceIndex));
    ASSERT_NO_FATAL_FAILURE(fetchnodes(shareeAliceIndex));
    ASSERT_NO_FATAL_FAILURE(logout(shareeBobIndex, false, maxTimeout));
    ASSERT_NO_FATAL_FAILURE(login(shareeBobIndex));
    ASSERT_NO_FATAL_FAILURE(fetchnodes(shareeBobIndex));

    LOG_info << logPre
             << "Check again that the sharer, Alice and Bob can see the same nodes and that "
                "the tree is decrypted.";
    ASSERT_NO_FATAL_FAILURE(
        matchTree(sharerFolderANode->getHandle(), sharerIndex, shareeAliceIndex));
    ASSERT_NO_FATAL_FAILURE(matchTree(sharerFolderBNode->getHandle(), sharerIndex, shareeBobIndex));
    ASSERT_NO_FATAL_FAILURE(
        matchTree(sharerFolderBNode->getHandle(), shareeAliceIndex, shareeBobIndex));
}

/**
 * @brief Test upload a file in the nested share
 *
 * It test if a file uploaded by a sharee is decryptable, creating a nested share and uploading a
 * file in the inshare of the nested sharee, ensuring that all peers can see their respective files.
 */
TEST_F(SdkTestShareNested, DISABLED_UploadFileInNestedShare)
{
    const auto logPre = getLogPrefix();

    LOG_info << "Starting body of " << logPre;

    // Make sharer and sharees contacts.
    ASSERT_NO_FATAL_FAILURE(
        inviteTestAccount(sharerIndex, shareeAliceIndex, "Sharer inviting Alice"))
        << "Failure inviting Alice";
    ASSERT_NO_FATAL_FAILURE(inviteTestAccount(sharerIndex, shareeBobIndex, "Sharer inviting Bob"))
        << "Failure inviting Bob";

    if (gManualVerification)
    {
        ASSERT_NO_FATAL_FAILURE(verifyContactCredentials(sharerIndex, shareeAliceIndex));
        ASSERT_NO_FATAL_FAILURE(verifyContactCredentials(sharerIndex, shareeBobIndex));
    }

    LOG_info << logPre << "Share folder \"folderA\" to Alice and subfolder \"folderB\" to Bob.";
    auto sharerFolderANode = getNodeByPath(FOLDER_A);
    auto sharerFolderBNode = getNodeByPath(string(FOLDER_A) + "/" + FOLDER_B);
    ASSERT_TRUE(sharerFolderANode) << "folder \"folderA\" not found.";
    ASSERT_TRUE(sharerFolderBNode) << "folder \"folderB\" not found.";
    ASSERT_NO_FATAL_FAILURE(createShareAtoB(sharerFolderANode.get(),
                                            {sharerIndex, true},
                                            {shareeAliceIndex, true},
                                            MegaShare::ACCESS_FULL));
    ASSERT_NO_FATAL_FAILURE(createShareAtoB(sharerFolderBNode.get(),
                                            {sharerIndex, true},
                                            {shareeBobIndex, true},
                                            MegaShare::ACCESS_FULL));

    LOG_info << logPre
             << "Ensure that the sharer, Alice and Bob can see the same nodes and that the tree is "
                "decrypted.";
    ASSERT_NO_FATAL_FAILURE(
        matchTree(sharerFolderANode->getHandle(), sharerIndex, shareeAliceIndex));
    ASSERT_NO_FATAL_FAILURE(matchTree(sharerFolderBNode->getHandle(), sharerIndex, shareeBobIndex));
    ASSERT_NO_FATAL_FAILURE(
        matchTree(sharerFolderBNode->getHandle(), shareeAliceIndex, shareeBobIndex));

    LOG_info
        << logPre
        << "Bob puts a file in the inshare folder. Check if Alice and the sharer can see the node.";
    auto shareeBobFolderBNode = std::unique_ptr<MegaNode>{
        megaApi[shareeBobIndex]->getNodeByHandle(sharerFolderBNode->getHandle())};
    ASSERT_NO_FATAL_FAILURE(createRemoteFileNode(shareeBobIndex,
                                                 FileNodeInfo("fromBobInFolderB").setSize(100),
                                                 shareeBobFolderBNode.get(),
                                                 sharerIndex));
    ASSERT_NO_FATAL_FAILURE(matchTree(sharerFolderBNode->getHandle(), sharerIndex, shareeBobIndex));
    ASSERT_NO_FATAL_FAILURE(
        matchTree(sharerFolderBNode->getHandle(), shareeAliceIndex, shareeBobIndex));
}