HttpServerTest.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include "HttpServer.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. #include "SocketCore.h"
  4. #include "a2functional.h"
  5. namespace aria2 {
  6. class HttpServerTest : public CppUnit::TestFixture {
  7. CPPUNIT_TEST_SUITE(HttpServerTest);
  8. CPPUNIT_TEST(testHttpBasicAuth);
  9. CPPUNIT_TEST_SUITE_END();
  10. public:
  11. void testHttpBasicAuth();
  12. };
  13. CPPUNIT_TEST_SUITE_REGISTRATION(HttpServerTest);
  14. namespace {
  15. std::unique_ptr<HttpServer> performHttpRequest(SocketCore& server,
  16. std::string request)
  17. {
  18. std::pair<std::string, uint16_t> addr;
  19. server.getAddrInfo(addr);
  20. SocketCore client;
  21. client.establishConnection("localhost", addr.second);
  22. while (!client.isWritable(0)) {
  23. }
  24. auto inbound = server.acceptConnection();
  25. inbound->setBlockingMode();
  26. auto rv = make_unique<HttpServer>(inbound);
  27. client.writeData(request);
  28. while (!rv->receiveRequest()) {
  29. }
  30. return rv;
  31. }
  32. } // namespace
  33. void HttpServerTest::testHttpBasicAuth()
  34. {
  35. SocketCore server;
  36. server.bind(0);
  37. server.beginListen();
  38. server.setBlockingMode();
  39. {
  40. // Default is no auth
  41. auto req = performHttpRequest(
  42. server, "GET / HTTP/1.1\r\nUser-Agent: aria2-test\r\n\r\n");
  43. CPPUNIT_ASSERT(req->authenticate());
  44. }
  45. {
  46. // Empty user-name and password should come out as no auth.
  47. auto req = performHttpRequest(
  48. server, "GET / HTTP/1.1\r\nUser-Agent: aria2-test\r\n\r\n");
  49. req->setUsernamePassword("", "");
  50. CPPUNIT_ASSERT(req->authenticate());
  51. }
  52. {
  53. // Empty user-name but set password should also come out as no auth.
  54. auto req = performHttpRequest(
  55. server, "GET / HTTP/1.1\r\nUser-Agent: aria2-test\r\n\r\n");
  56. req->setUsernamePassword("", "pass");
  57. CPPUNIT_ASSERT(req->authenticate());
  58. }
  59. {
  60. // Client provided credentials should be ignored when there is no auth.
  61. auto req = performHttpRequest(server, "GET / HTTP/1.1\r\nUser-Agent: "
  62. "aria2-test\r\nAuthorization: Basic "
  63. "dXNlcjpwYXNz\r\n\r\n");
  64. req->setUsernamePassword("", "pass");
  65. CPPUNIT_ASSERT(req->authenticate());
  66. }
  67. {
  68. // Correct client provided credentials should match.
  69. auto req = performHttpRequest(server, "GET / HTTP/1.1\r\nUser-Agent: "
  70. "aria2-test\r\nAuthorization: Basic "
  71. "dXNlcjpwYXNz\r\n\r\n");
  72. req->setUsernamePassword("user", "pass");
  73. CPPUNIT_ASSERT(req->authenticate());
  74. }
  75. {
  76. // Correct client provided credentials should match (2).
  77. // Embedded nulls
  78. auto req = performHttpRequest(
  79. server, "GET / HTTP/1.1\r\nUser-Agent: aria2-test\r\nAuthorization: "
  80. "Basic dXNlcgBudWxsOnBhc3MAbnVsbA==\r\n\r\n");
  81. req->setUsernamePassword(std::string("user\0null", 9),
  82. std::string("pass\0null", 9));
  83. CPPUNIT_ASSERT(req->authenticate());
  84. }
  85. {
  86. // Correct client provided credentials should match (3).
  87. // Embedded, leading nulls
  88. auto req = performHttpRequest(server, "GET / HTTP/1.1\r\nUser-Agent: "
  89. "aria2-test\r\nAuthorization: Basic "
  90. "AHVzZXI6AHBhc3M=\r\n\r\n");
  91. req->setUsernamePassword(std::string("\0user", 5),
  92. std::string("\0pass", 5));
  93. CPPUNIT_ASSERT(req->authenticate());
  94. }
  95. {
  96. // Correct client provided credentials should match (3).
  97. // Whitespace
  98. auto req = performHttpRequest(server, "GET / HTTP/1.1\r\nUser-Agent: "
  99. "aria2-test\r\nAuthorization: Basic "
  100. "IHVzZXIJOgpwYXNzDQ==\r\n\r\n");
  101. req->setUsernamePassword(" user\t", "\npass\r");
  102. CPPUNIT_ASSERT(req->authenticate());
  103. }
  104. {
  105. // Wrong client provided credentials should NOT match.
  106. auto req = performHttpRequest(server, "GET / HTTP/1.1\r\nUser-Agent: "
  107. "aria2-test\r\nAuthorization: Basic "
  108. "dXNlcjpwYXNz\r\n\r\n");
  109. req->setUsernamePassword("user", "pass2");
  110. CPPUNIT_ASSERT(!req->authenticate());
  111. }
  112. {
  113. // Wrong client provided credentials should NOT match (2).
  114. auto req = performHttpRequest(server, "GET / HTTP/1.1\r\nUser-Agent: "
  115. "aria2-test\r\nAuthorization: Basic "
  116. "dXNlcjpwYXNz\r\n\r\n");
  117. req->setUsernamePassword("user2", "pass");
  118. CPPUNIT_ASSERT(!req->authenticate());
  119. }
  120. {
  121. // Wrong client provided credentials should NOT match (3).
  122. // Embedded null in pass config.
  123. auto req = performHttpRequest(server, "GET / HTTP/1.1\r\nUser-Agent: "
  124. "aria2-test\r\nAuthorization: Basic "
  125. "dXNlcjpwYXNz\r\n\r\n");
  126. req->setUsernamePassword("user", std::string("pass\0three", 10));
  127. CPPUNIT_ASSERT(!req->authenticate());
  128. }
  129. {
  130. // Wrong client provided credentials should NOT match (4).
  131. // Embedded null in user config.
  132. auto req = performHttpRequest(server, "GET / HTTP/1.1\r\nUser-Agent: "
  133. "aria2-test\r\nAuthorization: Basic "
  134. "dXNlcjpwYXNz\r\n\r\n");
  135. req->setUsernamePassword(std::string("user\0four", 9), "pass");
  136. CPPUNIT_ASSERT(!req->authenticate());
  137. }
  138. {
  139. // Wrong client provided credentials should NOT match (5).
  140. // Embedded null in http auth.
  141. auto req = performHttpRequest(server, "GET / HTTP/1.1\r\nUser-Agent: "
  142. "aria2-test\r\nAuthorization: Basic "
  143. "dXNlcjpwYXNzAHRocmVl\r\n\r\n");
  144. req->setUsernamePassword("user", "pass");
  145. CPPUNIT_ASSERT(!req->authenticate());
  146. }
  147. {
  148. // Wrong client provided credentials should NOT match (6).
  149. // Embedded null in http auth.
  150. // Embedded, leading nulls
  151. auto req = performHttpRequest(server, "GET / HTTP/1.1\r\nUser-Agent: "
  152. "aria2-test\r\nAuthorization: Basic "
  153. "AHVzZXI6AHBhc3M=\r\n\r\n");
  154. req->setUsernamePassword(std::string("\0user5", 6),
  155. std::string("\0pass", 5));
  156. CPPUNIT_ASSERT(!req->authenticate());
  157. }
  158. {
  159. // When there is a user and password, the client must actually provide auth.
  160. auto req = performHttpRequest(
  161. server, "GET / HTTP/1.1\r\nUser-Agent: aria2-test\r\n\r\n");
  162. req->setUsernamePassword("user", "pass");
  163. CPPUNIT_ASSERT(!req->authenticate());
  164. }
  165. }
  166. } // namespace aria2