File: MegaCmdTestingTools.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 (104 lines) | stat: -rw-r--r-- 2,948 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
/**
 * (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 <gmock/gmock.h>
#include <gtest/gtest.h>

#include "MegaCmdTestingTools.h"

std::vector<std::string> splitByNewline(const std::string& str)
{
    std::vector<std::string> result;
    std::string line;
    std::istringstream iss(str);

    while (std::getline(iss, line))
    {
        result.push_back(line);
    }
    return result;
}

std::string joinString(const std::vector<std::string> &vec, std::string_view sep)
{
    std::ostringstream oss;
    for (size_t i = 0; i < vec.size(); ++i)
    {
        if (i)
            oss << sep;
        oss << vec[i];
    }
    return oss.str();
}

ClientResponse executeInClient(const std::vector<std::string>& command,
                               bool /*nonInteractive: TODO: give support to shell execs*/)
{
    // To manage the memory of the first arg
    const std::string firstArg("args0_test_client");

    std::vector<char*> args{const_cast<char*>(firstArg.c_str())};
    for (const auto& word : command)
    {
        args.push_back(const_cast<char*>(word.c_str()));
    }
    args.push_back(nullptr);

    OUTSTRINGSTREAM stream;
    OUTSTRINGSTREAM streamErr;
    auto code = megacmd::executeClient(static_cast<int>(args.size() - 1), args.data(), stream, streamErr);
    return {code, stream, streamErr};
}

bool isServerLogged()
{
    return executeInClient({"whoami"}).ok();
}

void ensureLoggedIn()
{
    if (!isServerLogged())
    {
        const char* user = getenv("MEGACMD_TEST_USER");
        const char* pass = getenv("MEGACMD_TEST_PASS");

        ASSERT_THAT(user, testing::NotNull()) << "Missing testing user env variable. Ensure that MEGACMD_TEST_USER is set!";
        ASSERT_STRNE(user, "") << "Missing testing user env variable. Ensure that MEGACMD_TEST_USER is set!";

        ASSERT_THAT(pass, testing::NotNull()) << "Missing testing user password env variable. Ensure that MEGACMD_TEST_PASS is set!";
        ASSERT_STRNE(pass, "") << "Missing testing user password env variable. Ensure that MEGACMD_TEST_PASS is set!";

        auto result = executeInClient({"login", user, pass});
        ASSERT_TRUE(result.ok());

        result = executeInClient({"whoami"});
        ASSERT_TRUE(result.ok());
        ASSERT_THAT(result.out(), testing::HasSubstr(user));
    }
}

bool hasReadStructure()
{
    return executeInClient({"ls", "testReadingFolder01"}).ok();
}

void ensureReadStructure()
{
    if (!hasReadStructure())
    {
        auto result = executeInClient({"import", LINK_TESTREADINGFOLDER01});
        ASSERT_TRUE(result.ok());
    }
}