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
|
/**
* (c) 2019 by Mega Limited, Wellsford, New Zealand
*
* This file is part of the MEGA SDK - Client Access Engine.
*
* Applications using the MEGA API must present a valid application key
* and comply with the the rules set forth in the Terms of Service.
*
* The MEGA SDK 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 <memory>
#include <gtest/gtest.h>
#include <mega/command.h>
#include <mega/json.h>
#include <mega/megaapp.h>
#include <mega/megaclient.h>
#include <mega/types.h>
using namespace std;
using namespace mega;
namespace {
class MockApp_CommandGetCountryCallingCodes : public MegaApp
{
public:
using DataType = map<string, vector<string>>;
int mCallCount = 0;
ErrorCodes mLastError = ErrorCodes::API_EINTERNAL;
std::unique_ptr<DataType> mCountryCallingCodes;
void getcountrycallingcodes_result(const ErrorCodes e, DataType* const data) override
{
++mCallCount;
mLastError = e;
if (data)
{
mCountryCallingCodes = std::unique_ptr<DataType>{new DataType{*data}};
}
else
{
assert(e != ErrorCodes::API_OK);
}
}
};
} // anonymous
/*TEST(Commands, CommandGetCountryCallingCodes_processResult_happyPath)
{
MockApp_CommandGetCountryCallingCodes app;
JSON json;
json.pos = R"({"cc":"AD","l":[376]},{"cc":"AE","l":[971,13]},{"cc":"AF","l":[93,13,42]})";
const auto jsonBegin = json.pos;
const auto jsonLength = strlen(json.pos);
CommandGetCountryCallingCodes::processResult(app, json);
const map<string, vector<string>> expected{
{"AD", {"376"}},
{"AE", {"971", "13"}},
{"AF", {"93", "13", "42"}},
};
ASSERT_EQ(1, app.mCallCount);
ASSERT_EQ(API_OK, app.mLastError);
ASSERT_NE(nullptr, app.mCountryCallingCodes);
ASSERT_EQ(expected, *app.mCountryCallingCodes);
ASSERT_EQ(ptrdiff_t(jsonLength), std::distance(jsonBegin, json.pos)); // assert json has been parsed all the way
}
TEST(Commands, CommandGetCountryCallingCodes_processResult_onlyOneCountry)
{
MockApp_CommandGetCountryCallingCodes app;
JSON json;
json.pos = R"({"cc":"AD","l":[12,376]})";
const auto jsonBegin = json.pos;
const auto jsonLength = strlen(json.pos);
CommandGetCountryCallingCodes::processResult(app, json);
const map<string, vector<string>> expected{
{"AD", {"12", "376"}},
};
ASSERT_EQ(1, app.mCallCount);
ASSERT_EQ(API_OK, app.mLastError);
ASSERT_NE(nullptr, app.mCountryCallingCodes);
ASSERT_EQ(expected, *app.mCountryCallingCodes);
ASSERT_EQ(ptrdiff_t(jsonLength), std::distance(jsonBegin, json.pos)); // assert json has been parsed all the way
}
TEST(Commands, CommandGetCountryCallingCodes_processResult_extraFieldShouldBeIgnored)
{
MockApp_CommandGetCountryCallingCodes app;
JSON json;
json.pos = R"({"cc":"AD","l":[12,376],"blah":"42"})";
const auto jsonBegin = json.pos;
const auto jsonLength = strlen(json.pos);
CommandGetCountryCallingCodes::processResult(app, json);
const map<string, vector<string>> expected{
{"AD", {"12", "376"}},
};
ASSERT_EQ(1, app.mCallCount);
ASSERT_EQ(API_OK, app.mLastError);
ASSERT_NE(nullptr, app.mCountryCallingCodes);
ASSERT_EQ(expected, *app.mCountryCallingCodes);
ASSERT_EQ(ptrdiff_t(jsonLength), std::distance(jsonBegin, json.pos)); // assert json has been parsed all the way
}
TEST(Commands, CommandGetCountryCallingCodes_processResult_invalidResponse)
{
MockApp_CommandGetCountryCallingCodes app;
JSON json;
json.pos = R"({"cc":"AD","blah":[12,376]})";
const auto jsonBegin = json.pos;
const auto jsonLength = strlen(json.pos);
CommandGetCountryCallingCodes::processResult(app, json);
ASSERT_EQ(1, app.mCallCount);
ASSERT_EQ(API_EINTERNAL, app.mLastError);
ASSERT_EQ(nullptr, app.mCountryCallingCodes);
ASSERT_EQ(ptrdiff_t(jsonLength), std::distance(jsonBegin, json.pos)); // assert json has been parsed all the way
}
class FileSystemAccessMockup : public ::mega::FileSystemAccess
{
public:
FileSystemAccessMockup()
{}
std::unique_ptr<FileAccess> newfileaccess(bool = true) override{ return std::unique_ptr<FileAccess>(); }
DirAccess* newdiraccess() override {return nullptr;}
bool getlocalfstype(const ::mega::LocalPath&, ::mega::FileSystemType&) const override { return false; }
void path2local(const string*, string*) const override {}
void local2path(const string*, string*) const override {}
#if defined(_WIN32)
void path2local(const string*, std::wstring*) const override {}
void local2path(const std::wstring*, string*) const override {}
#endif
void tmpnamelocal(LocalPath&) const override {}
bool getsname(const LocalPath& , LocalPath& ) const override { return false; }
bool renamelocal(LocalPath&, LocalPath&, bool = true) override { return false; }
bool copylocal(LocalPath&, LocalPath&, m_time_t) override { return false; }
bool unlinklocal(LocalPath&) override { return false; }
bool rmdirlocal(LocalPath&) override { return false; }
bool mkdirlocal(LocalPath&, bool = false) override { return false; }
bool setmtimelocal(LocalPath&, m_time_t) override { return false; }
bool chdirlocal(LocalPath&) const override { return false; }
bool getextension(const LocalPath&, string&) const override { return false; }
bool expanselocalpath(LocalPath& , LocalPath& ) override { return false; }
bool cwd(LocalPath&) const { return false; }
void addevents(Waiter*, int) override {}
virtual bool issyncsupported(const LocalPath&, bool& b, SyncError& se, SyncWarning& sw) { b = false; se = NO_SYNC_ERROR; sw = NO_SYNC_WARNING; return true;}
};
class HttpIOMockup : public ::mega::HttpIO
{
public:
HttpIOMockup(){}
void post(struct HttpReq*, const char* = NULL, unsigned = 0) override{};
void cancel(HttpReq*) override{}
m_off_t postpos(void*) override{ return 0; }
bool doio(void) override{ return false; }
void setuseragent(string*) override{}
void addevents(Waiter*, int) override {}
};
class MegaAppMockup : public ::mega::MegaApp
{
public:
MegaAppMockup(){}
};
class ClientMockup : public ::mega::MegaClient
{
public:
ClientMockup(MegaAppMockup& megaApp, HttpIOMockup& httpIO, FileSystemAccessMockup& fileSystem)
: MegaClient(&megaApp, nullptr, &httpIO, &fileSystem, nullptr, nullptr, nullptr, "UserAgent", 1)
{
}
};
TEST(Commands, CommandFetchAds)
{
FileSystemAccessMockup fileSystem;
HttpIOMockup httpIO;
MegaAppMockup megaApp;
ClientMockup client(megaApp, httpIO, fileSystem);
client.json.pos = R"({"id": "wphl","iu": "/22060108601/wph/wph_l"},{"id":"wphr","iu": "/22060108601/wph/wph_r"},{"id":"wpht","iu": "/22060108601/wph/wph_t"})";
std::vector<std::string> v;
handle h = UNDEF;
int adFlags = 512;
::mega::CommandFetchAds command(&client, adFlags, v, h, [](::mega::Error e, ::mega::string_map value)
{
ASSERT_EQ(e, API_OK);
ASSERT_EQ(value.size(), 3);
ASSERT_NE(value.find("wphl"), value.end());
ASSERT_EQ(value["wphl"], "/22060108601/wph/wph_l");
ASSERT_NE(value.find("wphr"), value.end());
ASSERT_EQ(value["wphr"], "/22060108601/wph/wph_r");
ASSERT_NE(value.find("wpht"), value.end());
ASSERT_EQ(value["wpht"], "/22060108601/wph/wph_t");
});
command.client = &client;
::mega::Command::Result r(::mega::Command::Outcome::CmdArray);
command.procresult(r);
}
TEST(Commands, CommandQueryAds)
{
FileSystemAccessMockup fileSystem;
HttpIOMockup httpIO;
MegaAppMockup megaApp;
ClientMockup client(megaApp, httpIO, fileSystem);
client.json.pos = R"(1)";
handle h = UNDEF;
int adFlags = 512;
::mega::CommandQueryAds command(&client, adFlags, h, [](::mega::Error e, int value)
{
ASSERT_EQ(e, API_OK);
ASSERT_EQ(value, 1);
});
command.client = &client;
::mega::Command::Result r(::mega::Command::Outcome::CmdArray);
command.procresult(r);
}
*/
|