HttpServerTest.cc 6.6 KB

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