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
|
#include "mega/proxy.h"
#include "mega/utils.h"
#include <gtest/gtest.h>
using ::mega::Proxy;
namespace mega
{
std::ostream& operator<<(std::ostream& os, const ::mega::Proxy& proxy)
{
os << "["
<< "type=" << proxy.getProxyType() << ", url=" << proxy.getProxyURL()
<< ", user=" << proxy.getUsername() << ", password=" << proxy.getPassword() << "]";
return os;
}
}
TEST(Proxy, NoHostURLReturnsDefaultProxy)
{
ASSERT_EQ(Proxy{}, Proxy::parseFromURL("http://:122"));
}
TEST(Proxy, EmptyStringReturnsDefaultProxy)
{
ASSERT_EQ(Proxy{}, Proxy::parseFromURL(""));
}
TEST(Proxy, ParseURLWithoutCredential)
{
Proxy expected;
{
expected.setProxyType(Proxy::CUSTOM);
expected.setProxyURL("https://example.com");
}
ASSERT_EQ(expected, Proxy::parseFromURL("https://example.com"));
}
TEST(Proxy, ParseURLWithPort)
{
Proxy expected;
{
expected.setProxyType(Proxy::CUSTOM);
expected.setProxyURL("https://example.com:1010");
}
ASSERT_EQ(expected, Proxy::parseFromURL("https://example.com:1010"));
}
TEST(Proxy, ParseURLWithCrendential)
{
Proxy expected;
{
expected.setProxyType(Proxy::CUSTOM);
expected.setCredentials("user", "pass");
expected.setProxyURL("https://example.com:1010");
}
ASSERT_EQ(expected, Proxy::parseFromURL("https://user:pass@example.com:1010"));
}
TEST(Proxy, SocksSchemeIsSupported)
{
Proxy expected;
{
expected.setProxyType(Proxy::CUSTOM);
expected.setProxyURL("socks5h://example.com");
}
ASSERT_EQ(expected, Proxy::parseFromURL("socks5h://example.com"));
}
TEST(Proxy, SchemeIsGuessed)
{
Proxy expected;
{
expected.setProxyType(Proxy::CUSTOM);
expected.setProxyURL("http://example.com");
}
ASSERT_EQ(expected, Proxy::parseFromURL("example.com"));
}
TEST(Proxy, IncompleteCrendentialIsNeglected)
{
Proxy expected;
{
expected.setProxyType(Proxy::CUSTOM);
expected.setProxyURL("https://example.com:1010");
}
ASSERT_EQ(expected, Proxy::parseFromURL("https://user@example.com:1010"));
}
#if !defined(WIN32) && !defined(__APPLE__) && !(__ANDROID__)
using mega::Utils;
class EnvRestorer
{
public:
EnvRestorer(const std::string& name);
~EnvRestorer();
private:
std::string mName;
std::string mValue;
bool mHasValue{false};
};
EnvRestorer::EnvRestorer(const std::string& name):
mName{name}
{
std::tie(mValue, mHasValue) = Utils::getenv(mName);
}
EnvRestorer::~EnvRestorer()
{
if (!mHasValue)
Utils::unsetenv(mName);
else
Utils::setenv(mName, mValue);
}
TEST(Proxy, GetProxyFromEnv)
{
EnvRestorer http_proxy_restorer{"http_proxy"};
EnvRestorer HTTP_PROXY_restorer{"HTTP_PROXY"};
EnvRestorer https_proxy_restorer{"https_proxy"};
EnvRestorer HTTPS_PROXY_restorer{"HTTPS_PROXY"};
// unset
Utils::unsetenv("http_proxy");
Utils::unsetenv("HTTP_PROXY");
Utils::unsetenv("https_proxy");
Utils::unsetenv("HTTPS_PROXY");
// No environment is set, default is returned
Proxy expected{};
Proxy proxy{};
mega::getEnvProxy(&proxy);
ASSERT_EQ(expected, proxy);
// https_proxy is set, get from https_proxy
Utils::setenv("https_proxy", "https://example3.com");
expected.setProxyType(Proxy::CUSTOM);
expected.setProxyURL("https://example3.com");
mega::getEnvProxy(&proxy);
ASSERT_EQ(expected, proxy);
// https_proxy, HTTP_PROXY is set. HTTP_PROXY takes priority
Utils::setenv("HTTP_PROXY", "http://example2.com");
expected.setProxyType(Proxy::CUSTOM);
expected.setProxyURL("http://example2.com");
mega::getEnvProxy(&proxy);
ASSERT_EQ(expected, proxy);
// https_proxy, HTTP_PROXY and http_proxy is set. http_proxy takes prority
Utils::setenv("http_proxy", "http://example1.com");
expected.setProxyType(Proxy::CUSTOM);
expected.setProxyURL("http://example1.com");
mega::getEnvProxy(&proxy);
ASSERT_EQ(expected, proxy);
}
#endif
|