File: ExportTests.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 (259 lines) | stat: -rw-r--r-- 11,659 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
/**
 * (c) 2013 by Mega Limited, Auckland, New Zealand
 *
 * This file is part of MEGAcmd.
 *
 * MEGAcmd 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 <gtest/gtest.h>
#include <gmock/gmock.h>

#include "MegaCmdTestingTools.h"
#include "TestUtils.h"

using TI = TestInstruments;

class ExportTest : public NOINTERACTIVELoggedInTest
{
protected:
    void SetUp() override
    {
        NOINTERACTIVELoggedInTest::SetUp();

        // Do not assert the output of this; we just want to ensure a clean state in the setup
        executeInClient({"rm", "-r", "-f", "testExportFolder"});
        executeInClient({"rm", "-f", "testExportFile01.txt"});

        auto result = executeInClient({"import", LINK_TESTEXPORTFOLDER});
        ASSERT_TRUE(result.ok()) << "could not import testExportFolder";
        ASSERT_TRUE(executeInClient({"ls", "testExportFolder"}).ok()) << "could not find folder testExportFolder";

        result = executeInClient({"import", LINK_TESTEXPORTFILE01TXT});
        ASSERT_TRUE(result.ok()) << "could not import testExportFile01.txt";
        ASSERT_TRUE(executeInClient({"ls", "testExportFile01.txt"}).ok()) << "could not find file testExportFile01.txt";
    }

    void TearDown() override
    {
        ASSERT_TRUE(executeInClient({"rm", "-r", "-f", "testExportFolder"}).ok()) << "could not delete folder testExportFolder";
        ASSERT_TRUE(executeInClient({"rm", "-f", "testExportFile01.txt"}).ok()) << "could not delete file testExportFile01.txt";
        NOINTERACTIVELoggedInTest::TearDown();
    }

};

namespace {
    // We'll use these regex to verify the links and authentication keys are well-formed
    // Links end in <handle>#<key>, whereas tokens end in <handle>#<key>:<auth-key>
    // <handle> is simply alphanumeric, while <auth-key> and <key> can also contain '-' or '_'
    // Since GTest uses a very simple Regex implementation on Windows, we cannot use groups or brackets (see: https://google.github.io/googletest/advanced.html#regular-expression-syntax)
    const std::string megaFileLinkRegex(R"(https:\/\/mega.nz\/file\/\w+\#\S+)");
    const std::string megaFolderLinkRegex(R"(https:\/\/mega.nz\/folder\/\w+\#\S+)");
    const std::string megaPasswordLinkRegex(R"(https:\/\/mega.nz\/\S+)");
    const std::string authTokenRegex(R"(\w+\#\S+\:\S+)");
}

TEST_F(ExportTest, Basic)
{
    {
        G_SUBTEST << "File";
        const std::string file_path = "testExportFile01.txt";

        auto rExport = executeInClient({"export", file_path});
        ASSERT_FALSE(rExport.ok());
        EXPECT_THAT(rExport.out(), testing::HasSubstr(file_path + " is not exported"));
        EXPECT_THAT(rExport.out(), testing::HasSubstr("Use -a to export it"));

        auto rCreate = executeInClient({"export", "-a", "-f", file_path});
        ASSERT_TRUE(rCreate.ok());
        EXPECT_THAT(rCreate.out(), testing::HasSubstr("Exported /" + file_path));
        EXPECT_THAT(rCreate.out(), ContainsStdRegex(megaFileLinkRegex));

        // Verify it shows up as exported
        rExport = executeInClient({"export", file_path});
        ASSERT_TRUE(rExport.ok());
        EXPECT_THAT(rExport.out(), testing::HasSubstr(file_path));
        EXPECT_THAT(rExport.out(), testing::HasSubstr("shared as exported permanent file link"));
        EXPECT_THAT(rExport.out(), ContainsStdRegex(megaFileLinkRegex));

        auto rDisable = executeInClient({"export", "-d", file_path});
        ASSERT_TRUE(rDisable.ok());
        EXPECT_THAT(rDisable.out(), testing::StartsWith("Disabled export: /" + file_path));

        // Again, verify it's not exported
        rExport = executeInClient({"export", file_path});
        ASSERT_FALSE(rExport.ok());
        EXPECT_THAT(rExport.out(), testing::HasSubstr(file_path + " is not exported"));
        EXPECT_THAT(rExport.out(), testing::HasSubstr("Use -a to export it"));
    }

    {
        G_SUBTEST << "Directory";
        const std::string dir_path = "testExportFolder";

        auto rExport = executeInClient({"export", dir_path});
        ASSERT_FALSE(rExport.ok());
        EXPECT_THAT(rExport.out(), testing::HasSubstr("Couldn't find anything exported below"));
        EXPECT_THAT(rExport.out(), testing::HasSubstr(dir_path));
        EXPECT_THAT(rExport.out(), testing::HasSubstr("Use -a to export it"));

        auto rCreate = executeInClient({"export", "-a", "-f", dir_path});
        ASSERT_TRUE(rCreate.ok());
        EXPECT_THAT(rCreate.out(), testing::HasSubstr("Exported /" + dir_path));
        EXPECT_THAT(rCreate.out(), ContainsStdRegex(megaFolderLinkRegex));

        // Verify it shows up as exported
        rExport = executeInClient({"export", dir_path});
        ASSERT_TRUE(rExport.ok());
        EXPECT_THAT(rExport.out(), testing::HasSubstr(dir_path));
        EXPECT_THAT(rExport.out(), testing::HasSubstr("shared as exported permanent folder link"));
        EXPECT_THAT(rExport.out(), ContainsStdRegex(megaFolderLinkRegex));

        auto rDisable = executeInClient({"export", "-d", dir_path});
        ASSERT_TRUE(rDisable.ok());
        EXPECT_THAT(rDisable.out(), testing::StartsWith("Disabled export: /" + dir_path));

        // Again, verify it's not exported
        rExport = executeInClient({"export", dir_path});
        ASSERT_FALSE(rExport.ok());
        EXPECT_THAT(rExport.out(), testing::HasSubstr("Couldn't find anything exported below"));
        EXPECT_THAT(rExport.out(), testing::HasSubstr(dir_path));
        EXPECT_THAT(rExport.out(), testing::HasSubstr("Use -a to export it"));
    }
}

TEST_F(ExportTest, FailedRecreation)
{
    const std::string file_path = "testExportFile01.txt";
    const std::vector<std::string> createCommand{"export", "-a", "-f", file_path};

    auto rCreate = executeInClient(createCommand);
    ASSERT_TRUE(rCreate.ok());
    EXPECT_THAT(rCreate.out(), testing::HasSubstr("Exported /" + file_path));
    EXPECT_THAT(rCreate.out(), ContainsStdRegex(megaFileLinkRegex));

    rCreate = executeInClient(createCommand);
    ASSERT_FALSE(rCreate.ok());
    EXPECT_THAT(rCreate.err(), testing::HasSubstr(file_path + " is already exported"));

    auto rDisable = executeInClient({"export", "-d", file_path});
    ASSERT_TRUE(rDisable.ok());
    EXPECT_THAT(rDisable.out(), testing::StartsWith("Disabled export: /" + file_path));
}

TEST_F(ExportTest, Writable)
{
    const std::string dir_path = "testExportFolder";

    auto rOnlyWritable = executeInClient({"export", "--writable", dir_path});
    ASSERT_FALSE(rOnlyWritable.ok());
    EXPECT_THAT(rOnlyWritable.err(), testing::HasSubstr("Option can only be used when adding an export"));

    auto rCreate = executeInClient({"export", "-a", "-f", "--writable", dir_path});
    ASSERT_TRUE(rCreate.ok());
    EXPECT_THAT(rCreate.out(), testing::HasSubstr("Exported /" + dir_path));
    EXPECT_THAT(rCreate.out(), ContainsStdRegex(megaFolderLinkRegex));
    EXPECT_THAT(rCreate.out(), ContainsStdRegex("AuthToken = " + authTokenRegex));

    // Verify the authToken is also present when checking the export (not just when creating it)
    auto rCheck = executeInClient({"export", dir_path});
    ASSERT_TRUE(rCheck.ok());
    EXPECT_THAT(rCheck.out(), testing::HasSubstr(dir_path));
    EXPECT_THAT(rCheck.out(), testing::HasSubstr("shared as exported permanent folder link"));
    EXPECT_THAT(rCheck.out(), ContainsStdRegex(megaFolderLinkRegex));
    EXPECT_THAT(rCheck.out(), ContainsStdRegex("AuthToken=" + authTokenRegex));

    auto rDisable = executeInClient({"export", "-d", dir_path});
    ASSERT_TRUE(rDisable.ok());
    EXPECT_THAT(rDisable.out(), testing::StartsWith("Disabled export: /" + dir_path));
}

TEST_F(ExportTest, NestedDirectoryStructure)
{
    {
        G_SUBTEST << "Directory";
        const std::string dir_path = "testExportFolder/subDirectoryExport";

        auto rCreate = executeInClient({"export", "-a", "-f", dir_path});
        ASSERT_TRUE(rCreate.ok());
        EXPECT_THAT(rCreate.out(), testing::HasSubstr("Exported /" + dir_path));
        EXPECT_THAT(rCreate.out(), ContainsStdRegex(megaFolderLinkRegex));

        auto rExport = executeInClient({"export", dir_path});
        ASSERT_TRUE(rExport.ok());
        EXPECT_THAT(rExport.out(), testing::HasSubstr(dir_path));
        EXPECT_THAT(rExport.out(), testing::HasSubstr("shared as exported permanent folder link"));
        EXPECT_THAT(rExport.out(), ContainsStdRegex(megaFolderLinkRegex));

        auto rDisable = executeInClient({"export", "-d", dir_path});
        ASSERT_TRUE(rDisable.ok());
        EXPECT_THAT(rDisable.out(), testing::StartsWith("Disabled export: /" + dir_path));
    }

    {
        G_SUBTEST << "File";
        const std::string file_path = "testExportFolder/subDirectoryExport/file01.txt";

        auto rCreate = executeInClient({"export", "-a", "-f", file_path});
        ASSERT_TRUE(rCreate.ok());
        EXPECT_THAT(rCreate.out(), testing::HasSubstr("Exported /" + file_path));
        EXPECT_THAT(rCreate.out(), ContainsStdRegex(megaFileLinkRegex));

        auto rExport = executeInClient({"export", file_path});
        ASSERT_TRUE(rExport.ok());
        EXPECT_THAT(rExport.out(), testing::HasSubstr(file_path));
        EXPECT_THAT(rExport.out(), testing::HasSubstr("shared as exported permanent file link"));
        EXPECT_THAT(rExport.out(), ContainsStdRegex(megaFileLinkRegex));

        auto rDisable = executeInClient({"export", "-d", file_path});
        ASSERT_TRUE(rDisable.ok());
        EXPECT_THAT(rDisable.out(), testing::StartsWith("Disabled export: /" + file_path));
    }
}

TEST_F(ExportTest, PasswordProtected)
{
    const std::string file_path = "testExportFile01.txt";
    const std::vector<std::string> createCommand{"export", "-a", "-f", "--password=SomePassword", file_path};
    const std::vector<std::string> disableCommand{"export", "-d", file_path};

    {
        G_SUBTEST << "Non-pro account";
        TestInstrumentsTestValueGuard proLevelValueGuard(TI::TestValue::AMIPRO_LEVEL, int64_t(0));

        auto rCreate = executeInClient(createCommand);
        ASSERT_TRUE(rCreate.ok());
        EXPECT_THAT(rCreate.err(), testing::HasSubstr("Only PRO users can protect links with passwords"));
        EXPECT_THAT(rCreate.err(), testing::HasSubstr("Showing UNPROTECTED link"));
        EXPECT_THAT(rCreate.out(), testing::HasSubstr("Exported /" + file_path));
        EXPECT_THAT(rCreate.out(), ContainsStdRegex(megaFileLinkRegex));

        auto rDisable = executeInClient(disableCommand);
        ASSERT_TRUE(rDisable.ok());
        EXPECT_THAT(rDisable.out(), testing::StartsWith("Disabled export: /" + file_path));
    }

    {
        G_SUBTEST << "Pro account";
        TestInstrumentsTestValueGuard proLevelValueGuard(TI::TestValue::AMIPRO_LEVEL, int64_t(1));

        auto rCreate = executeInClient(createCommand);
        ASSERT_TRUE(rCreate.ok());
        EXPECT_THAT(rCreate.out(), testing::Not(testing::HasSubstr("Only PRO users can protect links with passwords")));
        EXPECT_THAT(rCreate.out(), testing::Not(testing::HasSubstr("Showing UNPROTECTED link")));
        EXPECT_THAT(rCreate.out(), testing::HasSubstr("Exported /" + file_path));
        EXPECT_THAT(rCreate.out(), ContainsStdRegex(megaPasswordLinkRegex));

        auto rDisable = executeInClient(disableCommand);
        ASSERT_TRUE(rDisable.ok());
        EXPECT_THAT(rDisable.out(), testing::StartsWith("Disabled export: /" + file_path));
    }
}