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
|
/**
* (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 "MegaCmdTestingTools.h"
#include "TestUtils.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <filesystem>
#include <fstream>
#include <string>
#include <vector>
namespace fs = std::filesystem;
namespace
{
std::string stripTrailingNewlines(std::string s)
{
while (!s.empty() && (s.back() == '\n' || s.back() == '\r'))
{
s.pop_back();
}
return s;
}
std::string makeUniqueRemoteBase()
{
const auto* ti = ::testing::UnitTest::GetInstance()->current_test_info();
std::string name = std::string(ti->test_suite_name()) + "_" + std::string(ti->name());
// Keep it filesystem/MEGA-path friendly
for (auto& ch: name)
{
if (!(std::isalnum(static_cast<unsigned char>(ch)) || ch == '_' || ch == '-'))
{
ch = '_';
}
}
return "put_test_dir_" + name;
}
std::string readLocalFile(const fs::path& p)
{
std::ifstream in(p, std::ios::binary);
return std::string((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
}
} // namespace
class PutTests: public NOINTERACTIVELoggedInTest
{
SelfDeletingTmpFolder mTmpDir;
std::string mRemoteBase;
void SetUp() override
{
NOINTERACTIVELoggedInTest::SetUp();
mRemoteBase = makeUniqueRemoteBase();
// Ensure a clean remote sandbox
(void)executeInClient({"cd", "/"});
(void)executeInClient({"rm", "-rf", mRemoteBase});
auto r = executeInClient({"mkdir", mRemoteBase});
ASSERT_TRUE(r.ok()) << r.err();
r = executeInClient({"cd", mRemoteBase});
ASSERT_TRUE(r.ok()) << r.err();
}
void TearDown() override
{
(void)executeInClient({"cd", "/"});
(void)executeInClient({"rm", "-rf", mRemoteBase});
NOINTERACTIVELoggedInTest::TearDown();
}
protected:
fs::path localPath() const
{
return mTmpDir.path();
}
void createLocalFile(const std::string& filename, const std::string& content = "")
{
fs::create_directories((localPath() / filename).parent_path());
std::ofstream file(localPath() / filename, std::ios::binary);
file << content;
}
void createLocalDir(const std::string& dirname)
{
fs::create_directories(localPath() / dirname);
}
std::string remoteBase() const
{
return mRemoteBase;
}
};
TEST_F(PutTests, SingleFileUpload)
{
const std::string filename = "test_file.txt";
const std::string content = "Hello, MEGAcmd!";
createLocalFile(filename, content);
// Upload into current remote cwd (mRemoteBase)
auto result = executeInClient({"put", (localPath() / filename).string()});
ASSERT_TRUE(result.ok()) << result.err();
// Verify file was uploaded + content matches (normalize trailing newline)
result = executeInClient({"cat", filename});
ASSERT_TRUE(result.ok()) << result.err();
EXPECT_EQ(content, stripTrailingNewlines(result.out()));
}
TEST_F(PutTests, SingleFileUploadToDestination)
{
const std::string filename = "test_file.txt";
const std::string content = "Hello, MEGAcmd!";
const std::string remoteDir = "dest_dir";
createLocalFile(filename, content);
auto result = executeInClient({"mkdir", remoteDir});
ASSERT_TRUE(result.ok()) << result.err();
result = executeInClient({"put", (localPath() / filename).string(), remoteDir});
ASSERT_TRUE(result.ok()) << result.err();
result = executeInClient({"cat", remoteDir + "/" + filename});
ASSERT_TRUE(result.ok()) << result.err();
EXPECT_EQ(content, stripTrailingNewlines(result.out()));
}
TEST_F(PutTests, EmptyFileUpload)
{
const std::string filename = "empty_file.txt";
createLocalFile(filename);
auto result = executeInClient({"put", (localPath() / filename).string()});
ASSERT_TRUE(result.ok()) << result.err();
result = executeInClient({"ls", filename});
ASSERT_TRUE(result.ok()) << result.err();
EXPECT_THAT(result.out(), testing::HasSubstr(filename));
}
TEST_F(PutTests, MultipleFileUpload)
{
const std::string remoteDir = "multi_dest";
auto result = executeInClient({"mkdir", remoteDir});
ASSERT_TRUE(result.ok()) << result.err();
const std::vector<std::string> files = {"file1.txt", "file2.txt", "file3.txt"};
for (const auto& f: files)
{
createLocalFile(f, "Content of " + f);
}
std::vector<std::string> command = {"put"};
for (const auto& f: files)
{
command.push_back((localPath() / f).string());
}
command.push_back(remoteDir);
result = executeInClient(command);
ASSERT_TRUE(result.ok()) << " command = " << joinString(command) << " err=" << result.err();
for (const auto& f: files)
{
result = executeInClient({"cat", remoteDir + "/" + f});
ASSERT_TRUE(result.ok()) << "Failed to verify file: " << f << " err=" << result.err();
EXPECT_EQ("Content of " + f, stripTrailingNewlines(result.out()));
}
}
TEST_F(PutTests, DirectoryUpload)
{
const std::string dirname = "test_dir";
const std::string subfile = "subfile.txt";
const std::string content = "Content in subdirectory";
createLocalDir(dirname);
createLocalFile(dirname + "/" + subfile, content);
auto result = executeInClient({"put", (localPath() / dirname).string()});
ASSERT_TRUE(result.ok()) << result.err();
result = executeInClient({"ls", dirname});
ASSERT_TRUE(result.ok()) << result.err();
EXPECT_THAT(result.out(), testing::HasSubstr(subfile));
result = executeInClient({"cat", dirname + "/" + subfile});
ASSERT_TRUE(result.ok()) << result.err();
EXPECT_EQ(content, stripTrailingNewlines(result.out()));
}
TEST_F(PutTests, DirectoryUploadToDestination)
{
const std::string dirname = "test_dir";
const std::string subfile = "subfile.txt";
const std::string content = "Content in subdirectory";
const std::string remoteDest = "dest_dir";
createLocalDir(dirname);
createLocalFile(dirname + "/" + subfile, content);
auto result = executeInClient({"mkdir", remoteDest});
ASSERT_TRUE(result.ok()) << result.err();
result = executeInClient({"put", (localPath() / dirname).string(), remoteDest});
ASSERT_TRUE(result.ok()) << result.err();
result = executeInClient({"ls", remoteDest + "/" + dirname});
ASSERT_TRUE(result.ok()) << result.err();
EXPECT_THAT(result.out(), testing::HasSubstr(subfile));
result = executeInClient({"cat", remoteDest + "/" + dirname + "/" + subfile});
ASSERT_TRUE(result.ok()) << result.err();
EXPECT_EQ(content, stripTrailingNewlines(result.out()));
}
TEST_F(PutTests, UploadWithCreateFlag)
{
const std::string filename = "test_file.txt";
const std::string content = "Hello, MEGAcmd!";
const std::string remotePath = "new_dir/test_file.txt";
createLocalFile(filename, content);
auto result = executeInClient({"put", "-c", (localPath() / filename).string(), remotePath});
ASSERT_TRUE(result.ok()) << result.err();
result = executeInClient({"cat", remotePath});
ASSERT_TRUE(result.ok()) << result.err();
EXPECT_EQ(content, stripTrailingNewlines(result.out()));
}
TEST_F(PutTests, UploadWithCreateFlagAndAbsoluteDestinationPath)
{
// When cwd is in a subfolder (e.g. /put_test_xxx) and put -c uses an absolute
// destination path, the path must be resolved from root, not from cwd.
const std::string filename = "file01.txt";
createLocalFile(filename);
const std::string absoluteDestFolder = "/" + remoteBase() + "/newfile/";
auto result = executeInClient({"put", "-c", (localPath() / filename).string(), absoluteDestFolder});
ASSERT_TRUE(result.ok()) << result.err();
const std::string absoluteRemotePath = absoluteDestFolder + filename;
result = executeInClient({"cat", absoluteRemotePath});
ASSERT_TRUE(result.ok()) << result.err();
EXPECT_EQ("", stripTrailingNewlines(result.out()));
}
TEST_F(PutTests, UploadNonAsciiFilename)
{
#ifdef _WIN32
// Skip on Windows due to encoding issues in test environment
GTEST_SKIP() << "Skipping non-ASCII filename test on Windows due to encoding limitations";
return;
#endif
const std::string filename = "\xE3\x83\x86\xE3\x82\xB9\xE3\x83\x88\xE3\x83\x95\xE3\x82\xA1"
"\xE3\x82\xA4\xE3\x83\xAB.txt";
const std::string content = "Content with non-ASCII filename";
createLocalFile(filename, content);
auto result = executeInClient({"put", (localPath() / filename).string()});
ASSERT_TRUE(result.ok()) << result.err();
result = executeInClient({"ls"});
ASSERT_TRUE(result.ok()) << result.err();
EXPECT_THAT(result.out(), testing::HasSubstr(filename));
}
TEST_F(PutTests, UploadFileWithSpaces)
{
const std::string filename = "test file with spaces.txt";
const std::string content = "Content in file with spaces";
createLocalFile(filename, content);
auto result = executeInClient({"put", (localPath() / filename).string()});
ASSERT_TRUE(result.ok()) << result.err();
result = executeInClient({"ls"});
ASSERT_TRUE(result.ok()) << result.err();
EXPECT_THAT(result.out(), testing::HasSubstr(filename));
}
TEST_F(PutTests, ReplaceExistingFile)
{
const std::string filename = "test_file.txt";
const std::string originalContent = "Original content";
const std::string newContent = "New content replacing original";
createLocalFile(filename, originalContent);
auto result = executeInClient({"put", (localPath() / filename).string()});
ASSERT_TRUE(result.ok()) << result.err();
result = executeInClient({"cat", filename});
ASSERT_TRUE(result.ok()) << result.err();
EXPECT_EQ(originalContent, stripTrailingNewlines(result.out()));
createLocalFile(filename, newContent);
result = executeInClient({"put", (localPath() / filename).string(), filename});
ASSERT_TRUE(result.ok()) << result.err();
result = executeInClient({"cat", filename});
ASSERT_TRUE(result.ok()) << result.err();
EXPECT_EQ(newContent, stripTrailingNewlines(result.out()));
}
TEST_F(PutTests, ReplaceFileInSubdirectory)
{
const std::string dirname = "test_dir";
const std::string filename = "test_file.txt";
const std::string originalContent = "Original content";
const std::string newContent = "New content in subdirectory";
ASSERT_TRUE(executeInClient({"mkdir", dirname}).ok());
createLocalFile(filename, originalContent);
auto result = executeInClient({"put", (localPath() / filename).string(), dirname});
ASSERT_TRUE(result.ok()) << result.err();
createLocalFile(filename, newContent);
result = executeInClient({"put", (localPath() / filename).string(), dirname + "/" + filename});
ASSERT_TRUE(result.ok()) << result.err();
result = executeInClient({"cat", dirname + "/" + filename});
ASSERT_TRUE(result.ok()) << result.err();
EXPECT_EQ(newContent, stripTrailingNewlines(result.out()));
}
TEST_F(PutTests, MultipleFilesToFileTargetShouldFail)
{
const std::string file1 = "file1.txt";
const std::string file2 = "file2.txt";
const std::string targetFile = "target.txt";
createLocalFile(file1, "Content 1");
createLocalFile(file2, "Content 2");
auto result = executeInClient({"put", (localPath() / file1).string(), targetFile});
ASSERT_TRUE(result.ok()) << result.err();
result = executeInClient({"put", (localPath() / file1).string(), (localPath() / file2).string(), targetFile});
ASSERT_FALSE(result.ok());
EXPECT_THAT(result.err(), testing::HasSubstr("Destination"));
}
TEST_F(PutTests, UploadToNonExistentPathWithoutCreateFlag)
{
const std::string filename = "test_file.txt";
const std::string nonExistentPath = "non_existent_dir/file.txt";
createLocalFile(filename, "Test content");
auto result = executeInClient({"put", (localPath() / filename).string(), nonExistentPath});
ASSERT_FALSE(result.ok());
EXPECT_THAT(result.err(),
testing::AnyOf(testing::HasSubstr("Couldn't find destination"), testing::HasSubstr("destination")));
}
TEST_F(PutTests, UploadDirectoryToFileTargetShouldFail)
{
const std::string dirname = "test_dir";
const std::string targetFile = "target.txt";
createLocalDir(dirname);
createLocalFile("temp.txt", "temp");
auto result = executeInClient({"put", (localPath() / "temp.txt").string(), targetFile});
ASSERT_TRUE(result.ok()) << result.err();
result = executeInClient({"put", (localPath() / dirname).string(), targetFile});
ASSERT_FALSE(result.ok());
EXPECT_THAT(result.err(), testing::HasSubstr("Destination"));
}
TEST_F(PutTests, PutSingleFileRoundTripViaGet)
{
const std::string filename = "roundtrip.txt";
const std::string content = "roundtrip-content\nline2\n";
createLocalFile(filename, content);
auto r = executeInClient({"put", (localPath() / filename).string()});
ASSERT_TRUE(r.ok()) << r.err();
// Download back and compare bytes (stronger than `cat`)
SelfDeletingTmpFolder dlTmp;
r = executeInClient({"get", filename, dlTmp.path().string()});
ASSERT_TRUE(r.ok()) << r.err();
const fs::path downloaded = dlTmp.path() / filename;
ASSERT_TRUE(fs::exists(downloaded));
EXPECT_EQ(readLocalFile(downloaded), content);
}
TEST_F(PutTests, PutCanRenameRemoteFile)
{
const std::string localName = "src.txt";
const std::string remoteName = "dst.txt";
const std::string content = "rename me\n";
createLocalFile(localName, content);
auto r = executeInClient({"put", (localPath() / localName).string(), remoteName});
ASSERT_TRUE(r.ok()) << r.err();
r = executeInClient({"ls"});
ASSERT_TRUE(r.ok()) << r.err();
EXPECT_THAT(r.out(), testing::HasSubstr(remoteName));
SelfDeletingTmpFolder dlTmp;
r = executeInClient({"get", remoteName, dlTmp.path().string()});
ASSERT_TRUE(r.ok()) << r.err();
const fs::path downloaded = dlTmp.path() / remoteName;
ASSERT_TRUE(fs::exists(downloaded));
EXPECT_EQ(readLocalFile(downloaded), content);
}
TEST_F(PutTests, PutDirectoryTreeShowsUp)
{
// Local tree:
// tree/
// a.txt
// sub/b.txt
const std::string treeDir = "tree";
createLocalDir(treeDir + "/sub");
createLocalFile(treeDir + "/a.txt", "A\n");
createLocalFile(treeDir + "/sub/b.txt", "B\n");
auto r = executeInClient({"put", (localPath() / treeDir).string()});
ASSERT_TRUE(r.ok()) << r.err();
r = executeInClient({"ls", treeDir});
ASSERT_TRUE(r.ok()) << r.err();
EXPECT_THAT(r.out(), testing::HasSubstr("a.txt"));
EXPECT_THAT(r.out(), testing::HasSubstr("sub"));
r = executeInClient({"ls", treeDir + "/sub"});
ASSERT_TRUE(r.ok()) << r.err();
EXPECT_THAT(r.out(), testing::HasSubstr("b.txt"));
}
TEST_F(PutTests, PutPrintTagAtStartPrintsDecimalTag)
{
const std::string filename = "tag_test.txt";
createLocalFile(filename, "tag\n");
auto r = executeInClient({"put", "--print-tag-at-start", (localPath() / filename).string()});
ASSERT_TRUE(r.ok()) << r.err();
const std::string out = r.out();
// Must contain the prefix
const std::string prefix = "Upload started: Tag = ";
auto pos = out.find(prefix);
ASSERT_NE(pos, std::string::npos) << "Output did not contain expected prefix. out=\n" << out;
pos += prefix.size();
ASSERT_LT(pos, out.size()) << "No characters after tag prefix. out=\n" << out;
// Parse consecutive decimal digits after the prefix
std::size_t end = pos;
while (end < out.size() && std::isdigit(static_cast<unsigned char>(out[end])))
{
++end;
}
ASSERT_GT(end, pos) << "Tag was not a decimal number. out=\n" << out;
// Optional: sanity check it is followed by "."
// (keeps it aligned with the format, but still doesn't validate path)
ASSERT_TRUE(end < out.size() && out[end] == '.') << "Expected '.' after decimal tag. out=\n" << out;
}
|