2014-05-22 09:26:11 +00:00
|
|
|
#include "HttpServer.h"
|
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
|
|
|
#include "SocketCore.h"
|
|
|
|
#include "a2functional.h"
|
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
class HttpServerTest : public CppUnit::TestFixture {
|
2014-05-22 09:26:11 +00:00
|
|
|
CPPUNIT_TEST_SUITE(HttpServerTest);
|
|
|
|
CPPUNIT_TEST(testHttpBasicAuth);
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
2015-12-27 09:39:47 +00:00
|
|
|
|
2014-05-22 09:26:11 +00:00
|
|
|
public:
|
|
|
|
void testHttpBasicAuth();
|
|
|
|
};
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(HttpServerTest);
|
|
|
|
|
|
|
|
namespace {
|
2015-12-27 09:39:47 +00:00
|
|
|
std::unique_ptr<HttpServer> performHttpRequest(SocketCore& server,
|
|
|
|
std::string request)
|
|
|
|
{
|
2016-01-09 09:16:08 +00:00
|
|
|
auto endpoint = server.getAddrInfo();
|
2014-05-22 09:26:11 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
SocketCore client;
|
2016-01-09 09:16:08 +00:00
|
|
|
client.establishConnection("localhost", endpoint.port);
|
2015-12-27 09:39:47 +00:00
|
|
|
while (!client.isWritable(0)) {
|
|
|
|
}
|
2014-05-22 09:26:11 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
auto inbound = server.acceptConnection();
|
|
|
|
inbound->setBlockingMode();
|
|
|
|
auto rv = make_unique<HttpServer>(inbound);
|
2014-05-22 09:26:11 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
client.writeData(request);
|
|
|
|
while (!rv->receiveRequest()) {
|
2014-05-22 09:26:11 +00:00
|
|
|
}
|
2015-12-27 09:39:47 +00:00
|
|
|
return rv;
|
|
|
|
}
|
2014-05-22 09:26:11 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void HttpServerTest::testHttpBasicAuth()
|
|
|
|
{
|
|
|
|
SocketCore server;
|
|
|
|
server.bind(0);
|
|
|
|
server.beginListen();
|
|
|
|
server.setBlockingMode();
|
|
|
|
|
|
|
|
{
|
|
|
|
// Default is no auth
|
|
|
|
auto req = performHttpRequest(
|
|
|
|
server, "GET / HTTP/1.1\r\nUser-Agent: aria2-test\r\n\r\n");
|
|
|
|
CPPUNIT_ASSERT(req->authenticate());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Empty user-name and password should come out as no auth.
|
|
|
|
auto req = performHttpRequest(
|
|
|
|
server, "GET / HTTP/1.1\r\nUser-Agent: aria2-test\r\n\r\n");
|
|
|
|
req->setUsernamePassword("", "");
|
|
|
|
CPPUNIT_ASSERT(req->authenticate());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Empty user-name but set password should also come out as no auth.
|
|
|
|
auto req = performHttpRequest(
|
|
|
|
server, "GET / HTTP/1.1\r\nUser-Agent: aria2-test\r\n\r\n");
|
|
|
|
req->setUsernamePassword("", "pass");
|
|
|
|
CPPUNIT_ASSERT(req->authenticate());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Client provided credentials should be ignored when there is no auth.
|
2015-12-27 09:39:47 +00:00
|
|
|
auto req = performHttpRequest(server, "GET / HTTP/1.1\r\nUser-Agent: "
|
|
|
|
"aria2-test\r\nAuthorization: Basic "
|
|
|
|
"dXNlcjpwYXNz\r\n\r\n");
|
2014-05-22 09:26:11 +00:00
|
|
|
req->setUsernamePassword("", "pass");
|
|
|
|
CPPUNIT_ASSERT(req->authenticate());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Correct client provided credentials should match.
|
2015-12-27 09:39:47 +00:00
|
|
|
auto req = performHttpRequest(server, "GET / HTTP/1.1\r\nUser-Agent: "
|
|
|
|
"aria2-test\r\nAuthorization: Basic "
|
|
|
|
"dXNlcjpwYXNz\r\n\r\n");
|
2014-05-22 09:26:11 +00:00
|
|
|
req->setUsernamePassword("user", "pass");
|
|
|
|
CPPUNIT_ASSERT(req->authenticate());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Correct client provided credentials should match (2).
|
|
|
|
// Embedded nulls
|
|
|
|
auto req = performHttpRequest(
|
2015-12-27 09:39:47 +00:00
|
|
|
server, "GET / HTTP/1.1\r\nUser-Agent: aria2-test\r\nAuthorization: "
|
|
|
|
"Basic dXNlcgBudWxsOnBhc3MAbnVsbA==\r\n\r\n");
|
|
|
|
req->setUsernamePassword(std::string("user\0null", 9),
|
|
|
|
std::string("pass\0null", 9));
|
2014-05-22 09:26:11 +00:00
|
|
|
CPPUNIT_ASSERT(req->authenticate());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Correct client provided credentials should match (3).
|
|
|
|
// Embedded, leading nulls
|
2015-12-27 09:39:47 +00:00
|
|
|
auto req = performHttpRequest(server, "GET / HTTP/1.1\r\nUser-Agent: "
|
|
|
|
"aria2-test\r\nAuthorization: Basic "
|
|
|
|
"AHVzZXI6AHBhc3M=\r\n\r\n");
|
|
|
|
req->setUsernamePassword(std::string("\0user", 5),
|
|
|
|
std::string("\0pass", 5));
|
2014-05-22 09:26:11 +00:00
|
|
|
CPPUNIT_ASSERT(req->authenticate());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Correct client provided credentials should match (3).
|
|
|
|
// Whitespace
|
2015-12-27 09:39:47 +00:00
|
|
|
auto req = performHttpRequest(server, "GET / HTTP/1.1\r\nUser-Agent: "
|
|
|
|
"aria2-test\r\nAuthorization: Basic "
|
|
|
|
"IHVzZXIJOgpwYXNzDQ==\r\n\r\n");
|
2014-05-22 09:26:11 +00:00
|
|
|
req->setUsernamePassword(" user\t", "\npass\r");
|
|
|
|
CPPUNIT_ASSERT(req->authenticate());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Wrong client provided credentials should NOT match.
|
2015-12-27 09:39:47 +00:00
|
|
|
auto req = performHttpRequest(server, "GET / HTTP/1.1\r\nUser-Agent: "
|
|
|
|
"aria2-test\r\nAuthorization: Basic "
|
|
|
|
"dXNlcjpwYXNz\r\n\r\n");
|
2014-05-22 09:26:11 +00:00
|
|
|
req->setUsernamePassword("user", "pass2");
|
|
|
|
CPPUNIT_ASSERT(!req->authenticate());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Wrong client provided credentials should NOT match (2).
|
2015-12-27 09:39:47 +00:00
|
|
|
auto req = performHttpRequest(server, "GET / HTTP/1.1\r\nUser-Agent: "
|
|
|
|
"aria2-test\r\nAuthorization: Basic "
|
|
|
|
"dXNlcjpwYXNz\r\n\r\n");
|
2014-05-22 09:26:11 +00:00
|
|
|
req->setUsernamePassword("user2", "pass");
|
|
|
|
CPPUNIT_ASSERT(!req->authenticate());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Wrong client provided credentials should NOT match (3).
|
|
|
|
// Embedded null in pass config.
|
2015-12-27 09:39:47 +00:00
|
|
|
auto req = performHttpRequest(server, "GET / HTTP/1.1\r\nUser-Agent: "
|
|
|
|
"aria2-test\r\nAuthorization: Basic "
|
|
|
|
"dXNlcjpwYXNz\r\n\r\n");
|
2014-05-22 09:26:11 +00:00
|
|
|
req->setUsernamePassword("user", std::string("pass\0three", 10));
|
|
|
|
CPPUNIT_ASSERT(!req->authenticate());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Wrong client provided credentials should NOT match (4).
|
|
|
|
// Embedded null in user config.
|
2015-12-27 09:39:47 +00:00
|
|
|
auto req = performHttpRequest(server, "GET / HTTP/1.1\r\nUser-Agent: "
|
|
|
|
"aria2-test\r\nAuthorization: Basic "
|
|
|
|
"dXNlcjpwYXNz\r\n\r\n");
|
2014-05-22 09:26:11 +00:00
|
|
|
req->setUsernamePassword(std::string("user\0four", 9), "pass");
|
|
|
|
CPPUNIT_ASSERT(!req->authenticate());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Wrong client provided credentials should NOT match (5).
|
|
|
|
// Embedded null in http auth.
|
2015-12-27 09:39:47 +00:00
|
|
|
auto req = performHttpRequest(server, "GET / HTTP/1.1\r\nUser-Agent: "
|
|
|
|
"aria2-test\r\nAuthorization: Basic "
|
|
|
|
"dXNlcjpwYXNzAHRocmVl\r\n\r\n");
|
2014-05-22 09:26:11 +00:00
|
|
|
req->setUsernamePassword("user", "pass");
|
|
|
|
CPPUNIT_ASSERT(!req->authenticate());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Wrong client provided credentials should NOT match (6).
|
|
|
|
// Embedded null in http auth.
|
|
|
|
// Embedded, leading nulls
|
2015-12-27 09:39:47 +00:00
|
|
|
auto req = performHttpRequest(server, "GET / HTTP/1.1\r\nUser-Agent: "
|
|
|
|
"aria2-test\r\nAuthorization: Basic "
|
|
|
|
"AHVzZXI6AHBhc3M=\r\n\r\n");
|
|
|
|
req->setUsernamePassword(std::string("\0user5", 6),
|
|
|
|
std::string("\0pass", 5));
|
2014-05-22 09:26:11 +00:00
|
|
|
CPPUNIT_ASSERT(!req->authenticate());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// When there is a user and password, the client must actually provide auth.
|
|
|
|
auto req = performHttpRequest(
|
2015-12-27 09:39:47 +00:00
|
|
|
server, "GET / HTTP/1.1\r\nUser-Agent: aria2-test\r\n\r\n");
|
2014-05-22 09:26:11 +00:00
|
|
|
req->setUsernamePassword("user", "pass");
|
|
|
|
CPPUNIT_ASSERT(!req->authenticate());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace aria2
|