UriSplitTest.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. #include "uri_split.h"
  2. #include <cstring>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "uri_split.h"
  5. namespace aria2 {
  6. class UriSplitTest : public CppUnit::TestFixture {
  7. CPPUNIT_TEST_SUITE(UriSplitTest);
  8. CPPUNIT_TEST(testUriSplit);
  9. CPPUNIT_TEST(testUriSplit_fail);
  10. CPPUNIT_TEST_SUITE_END();
  11. public:
  12. void testUriSplit();
  13. void testUriSplit_fail();
  14. };
  15. CPPUNIT_TEST_SUITE_REGISTRATION(UriSplitTest);
  16. namespace {
  17. const char* fieldstr[] = {
  18. "USR_SCHEME", "USR_HOST", "USR_PORT", "USR_PATH", "USR_QUERY",
  19. "USR_FRAGMENT", "USR_USERINFO", "USR_USER", "USR_PASSWD", "USR_BASENAME"};
  20. } // namespace
  21. #define CHECK_FIELD_SET(RES, FLAGS) \
  22. for (int i = 0; i < USR_MAX; ++i) { \
  23. int mask = 1 << i; \
  24. if ((FLAGS)&mask) { \
  25. CPPUNIT_ASSERT_MESSAGE(fieldstr[i], RES.field_set& mask); \
  26. } \
  27. else { \
  28. CPPUNIT_ASSERT_MESSAGE(fieldstr[i], !(RES.field_set & mask)); \
  29. } \
  30. }
  31. namespace {
  32. std::string mkstr(const uri_split_result& res, int field, const char* base)
  33. {
  34. return std::string(base + res.fields[field].off, res.fields[field].len);
  35. }
  36. } // namespace
  37. void UriSplitTest::testUriSplit()
  38. {
  39. uri_split_result res;
  40. const char* uri;
  41. uri = "http://aria2.sf.net/path/";
  42. memset(&res, 0, sizeof(res));
  43. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  44. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH));
  45. CPPUNIT_ASSERT_EQUAL(std::string("http"), mkstr(res, USR_SCHEME, uri));
  46. CPPUNIT_ASSERT_EQUAL(std::string("aria2.sf.net"), mkstr(res, USR_HOST, uri));
  47. CPPUNIT_ASSERT_EQUAL(std::string("/path/"), mkstr(res, USR_PATH, uri));
  48. uri = "http://user@aria2.sf.net/path/";
  49. memset(&res, 0, sizeof(res));
  50. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  51. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
  52. (1 << USR_USERINFO) | (1 << USR_USER));
  53. CPPUNIT_ASSERT_EQUAL(std::string("aria2.sf.net"), mkstr(res, USR_HOST, uri));
  54. CPPUNIT_ASSERT_EQUAL(std::string("/path/"), mkstr(res, USR_PATH, uri));
  55. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USERINFO, uri));
  56. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
  57. uri = "http://user:pass@aria2.sf.net/path/";
  58. memset(&res, 0, sizeof(res));
  59. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  60. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
  61. (1 << USR_USERINFO) | (1 << USR_USER) |
  62. (1 << USR_PASSWD));
  63. CPPUNIT_ASSERT_EQUAL(std::string("aria2.sf.net"), mkstr(res, USR_HOST, uri));
  64. CPPUNIT_ASSERT_EQUAL(std::string("/path/"), mkstr(res, USR_PATH, uri));
  65. CPPUNIT_ASSERT_EQUAL(std::string("user:pass"), mkstr(res, USR_USERINFO, uri));
  66. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
  67. CPPUNIT_ASSERT_EQUAL(std::string("pass"), mkstr(res, USR_PASSWD, uri));
  68. // According to RFC 3986, @ in userinfo is illegal. But many people
  69. // have e-mail account as username and don't understand
  70. // percent-encoding and keep getting erros putting it in URI in
  71. // unecoded form. Because of this, we support @ in username.
  72. uri = "http://user@foo.com:pass@aria2.sf.net/path/";
  73. memset(&res, 0, sizeof(res));
  74. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  75. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
  76. (1 << USR_USERINFO) | (1 << USR_USER) |
  77. (1 << USR_PASSWD));
  78. CPPUNIT_ASSERT_EQUAL(std::string("aria2.sf.net"), mkstr(res, USR_HOST, uri));
  79. CPPUNIT_ASSERT_EQUAL(std::string("/path/"), mkstr(res, USR_PATH, uri));
  80. CPPUNIT_ASSERT_EQUAL(std::string("user@foo.com:pass"),
  81. mkstr(res, USR_USERINFO, uri));
  82. CPPUNIT_ASSERT_EQUAL(std::string("user@foo.com"), mkstr(res, USR_USER, uri));
  83. CPPUNIT_ASSERT_EQUAL(std::string("pass"), mkstr(res, USR_PASSWD, uri));
  84. // Port processed in URI_MAYBE_USER -> URI_PORT
  85. uri = "https://aria2.sf.net:443/path/";
  86. memset(&res, 0, sizeof(res));
  87. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  88. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT) |
  89. (1 << USR_PATH));
  90. CPPUNIT_ASSERT_EQUAL(std::string("https"), mkstr(res, USR_SCHEME, uri));
  91. CPPUNIT_ASSERT_EQUAL(std::string("aria2.sf.net"), mkstr(res, USR_HOST, uri));
  92. CPPUNIT_ASSERT_EQUAL(std::string("/path/"), mkstr(res, USR_PATH, uri));
  93. CPPUNIT_ASSERT_EQUAL((uint16_t)443, res.port);
  94. // Port processed in URI_PORT
  95. uri = "https://user:pass@aria2.sf.net:443/path/";
  96. memset(&res, 0, sizeof(res));
  97. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  98. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT) |
  99. (1 << USR_PATH) | (1 << USR_USERINFO) |
  100. (1 << USR_USER) | (1 << USR_PASSWD));
  101. CPPUNIT_ASSERT_EQUAL(std::string("aria2.sf.net"), mkstr(res, USR_HOST, uri));
  102. CPPUNIT_ASSERT_EQUAL(std::string("/path/"), mkstr(res, USR_PATH, uri));
  103. CPPUNIT_ASSERT_EQUAL(std::string("user:pass"), mkstr(res, USR_USERINFO, uri));
  104. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
  105. CPPUNIT_ASSERT_EQUAL(std::string("pass"), mkstr(res, USR_PASSWD, uri));
  106. CPPUNIT_ASSERT_EQUAL((uint16_t)443, res.port);
  107. // Port processed in URI_MAYBE_PASSWD
  108. uri = "https://user@aria2.sf.net:443/path/";
  109. memset(&res, 0, sizeof(res));
  110. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  111. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT) |
  112. (1 << USR_PATH) | (1 << USR_USERINFO) |
  113. (1 << USR_USER));
  114. CPPUNIT_ASSERT_EQUAL(std::string("aria2.sf.net"), mkstr(res, USR_HOST, uri));
  115. CPPUNIT_ASSERT_EQUAL(std::string("/path/"), mkstr(res, USR_PATH, uri));
  116. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USERINFO, uri));
  117. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
  118. CPPUNIT_ASSERT_EQUAL((uint16_t)443, res.port);
  119. // Port processed in URI_MAYBE_PASSWD
  120. uri = "http://aria2";
  121. memset(&res, 0, sizeof(res));
  122. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  123. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST));
  124. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  125. uri = "http://aria2:8080";
  126. memset(&res, 0, sizeof(res));
  127. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  128. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT));
  129. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  130. CPPUNIT_ASSERT_EQUAL((uint16_t)8080, res.port);
  131. uri = "http://user@aria2";
  132. memset(&res, 0, sizeof(res));
  133. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  134. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) |
  135. (1 << USR_USERINFO) | (1 << USR_USER));
  136. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  137. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USERINFO, uri));
  138. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
  139. uri = "http://user:@aria2";
  140. memset(&res, 0, sizeof(res));
  141. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  142. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) |
  143. (1 << USR_USERINFO) | (1 << USR_USER) |
  144. (1 << USR_PASSWD));
  145. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  146. CPPUNIT_ASSERT_EQUAL(std::string("user:"), mkstr(res, USR_USERINFO, uri));
  147. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
  148. CPPUNIT_ASSERT_EQUAL(std::string(""), mkstr(res, USR_PASSWD, uri));
  149. uri = "http://aria2/?foo#bar";
  150. memset(&res, 0, sizeof(res));
  151. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  152. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
  153. (1 << USR_QUERY) | (1 << USR_FRAGMENT));
  154. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  155. CPPUNIT_ASSERT_EQUAL(std::string("/"), mkstr(res, USR_PATH, uri));
  156. CPPUNIT_ASSERT_EQUAL(std::string("foo"), mkstr(res, USR_QUERY, uri));
  157. CPPUNIT_ASSERT_EQUAL(std::string("bar"), mkstr(res, USR_FRAGMENT, uri));
  158. // URI_MAYBE_USER
  159. uri = "http://aria2?foo";
  160. memset(&res, 0, sizeof(res));
  161. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  162. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_QUERY));
  163. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  164. CPPUNIT_ASSERT_EQUAL(std::string("foo"), mkstr(res, USR_QUERY, uri));
  165. // URI_MAYBE_USER
  166. uri = "http://aria2#bar";
  167. memset(&res, 0, sizeof(res));
  168. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  169. CHECK_FIELD_SET(res,
  170. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_FRAGMENT));
  171. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  172. CPPUNIT_ASSERT_EQUAL(std::string("bar"), mkstr(res, USR_FRAGMENT, uri));
  173. // URI_MAYBE_PASSWD
  174. uri = "https://aria2:443?foo";
  175. memset(&res, 0, sizeof(res));
  176. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  177. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT) |
  178. (1 << USR_QUERY));
  179. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  180. CPPUNIT_ASSERT_EQUAL(std::string("foo"), mkstr(res, USR_QUERY, uri));
  181. CPPUNIT_ASSERT_EQUAL((uint16_t)443, res.port);
  182. // URI_MAYBE_PASSWD
  183. uri = "https://aria2:443#bar";
  184. memset(&res, 0, sizeof(res));
  185. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  186. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT) |
  187. (1 << USR_FRAGMENT));
  188. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  189. CPPUNIT_ASSERT_EQUAL(std::string("bar"), mkstr(res, USR_FRAGMENT, uri));
  190. CPPUNIT_ASSERT_EQUAL((uint16_t)443, res.port);
  191. // URI_PORT
  192. uri = "https://user:pass@aria2:443?foo";
  193. memset(&res, 0, sizeof(res));
  194. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  195. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT) |
  196. (1 << USR_QUERY) | (1 << USR_USERINFO) |
  197. (1 << USR_USER) | (1 << USR_PASSWD));
  198. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  199. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
  200. CPPUNIT_ASSERT_EQUAL(std::string("pass"), mkstr(res, USR_PASSWD, uri));
  201. CPPUNIT_ASSERT_EQUAL(std::string("foo"), mkstr(res, USR_QUERY, uri));
  202. CPPUNIT_ASSERT_EQUAL((uint16_t)443, res.port);
  203. // URI_PORT
  204. uri = "https://user:pass@aria2:443#bar";
  205. memset(&res, 0, sizeof(res));
  206. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  207. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT) |
  208. (1 << USR_FRAGMENT) | (1 << USR_USERINFO) |
  209. (1 << USR_USER) | (1 << USR_PASSWD));
  210. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  211. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
  212. CPPUNIT_ASSERT_EQUAL(std::string("pass"), mkstr(res, USR_PASSWD, uri));
  213. CPPUNIT_ASSERT_EQUAL(std::string("bar"), mkstr(res, USR_FRAGMENT, uri));
  214. CPPUNIT_ASSERT_EQUAL((uint16_t)443, res.port);
  215. // URI_HOST
  216. uri = "http://user:pass@aria2?foo";
  217. memset(&res, 0, sizeof(res));
  218. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  219. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_QUERY) |
  220. (1 << USR_USERINFO) | (1 << USR_USER) |
  221. (1 << USR_PASSWD));
  222. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  223. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
  224. CPPUNIT_ASSERT_EQUAL(std::string("pass"), mkstr(res, USR_PASSWD, uri));
  225. CPPUNIT_ASSERT_EQUAL(std::string("foo"), mkstr(res, USR_QUERY, uri));
  226. // URI_HOST
  227. uri = "http://user:pass@aria2#bar";
  228. memset(&res, 0, sizeof(res));
  229. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  230. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) |
  231. (1 << USR_FRAGMENT) | (1 << USR_USERINFO) |
  232. (1 << USR_USER) | (1 << USR_PASSWD));
  233. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  234. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
  235. CPPUNIT_ASSERT_EQUAL(std::string("pass"), mkstr(res, USR_PASSWD, uri));
  236. CPPUNIT_ASSERT_EQUAL(std::string("bar"), mkstr(res, USR_FRAGMENT, uri));
  237. // empty query
  238. uri = "http://aria2/?";
  239. memset(&res, 0, sizeof(res));
  240. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  241. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
  242. (1 << USR_QUERY));
  243. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  244. CPPUNIT_ASSERT_EQUAL(std::string("/"), mkstr(res, USR_PATH, uri));
  245. CPPUNIT_ASSERT_EQUAL(std::string(""), mkstr(res, USR_QUERY, uri));
  246. // empty fragment
  247. uri = "http://aria2/#";
  248. memset(&res, 0, sizeof(res));
  249. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  250. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
  251. (1 << USR_FRAGMENT));
  252. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  253. CPPUNIT_ASSERT_EQUAL(std::string("/"), mkstr(res, USR_PATH, uri));
  254. CPPUNIT_ASSERT_EQUAL(std::string(""), mkstr(res, USR_FRAGMENT, uri));
  255. // empty query and fragment
  256. uri = "http://aria2/?#";
  257. memset(&res, 0, sizeof(res));
  258. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  259. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
  260. (1 << USR_QUERY) | (1 << USR_FRAGMENT));
  261. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  262. CPPUNIT_ASSERT_EQUAL(std::string("/"), mkstr(res, USR_PATH, uri));
  263. CPPUNIT_ASSERT_EQUAL(std::string(""), mkstr(res, USR_QUERY, uri));
  264. CPPUNIT_ASSERT_EQUAL(std::string(""), mkstr(res, USR_FRAGMENT, uri));
  265. // IPv6 numeric address
  266. uri = "http://[::1]";
  267. memset(&res, 0, sizeof(res));
  268. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  269. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST));
  270. CPPUNIT_ASSERT_EQUAL(std::string("::1"), mkstr(res, USR_HOST, uri));
  271. CPPUNIT_ASSERT(res.flags & USF_IPV6ADDR);
  272. uri = "https://[::1]:443";
  273. memset(&res, 0, sizeof(res));
  274. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  275. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT));
  276. CPPUNIT_ASSERT_EQUAL(std::string("::1"), mkstr(res, USR_HOST, uri));
  277. CPPUNIT_ASSERT_EQUAL((uint16_t)443, res.port);
  278. CPPUNIT_ASSERT(res.flags & USF_IPV6ADDR);
  279. // USR_MAYBE_USER
  280. uri = "https://user@[::1]";
  281. memset(&res, 0, sizeof(res));
  282. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  283. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) |
  284. (1 << USR_USERINFO) | (1 << USR_USER));
  285. CPPUNIT_ASSERT_EQUAL(std::string("::1"), mkstr(res, USR_HOST, uri));
  286. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USERINFO, uri));
  287. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
  288. CPPUNIT_ASSERT(res.flags & USF_IPV6ADDR);
  289. // USR_BEFORE_HOST
  290. uri = "https://user:pass@[::1]";
  291. memset(&res, 0, sizeof(res));
  292. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  293. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) |
  294. (1 << USR_USERINFO) | (1 << USR_USER) |
  295. (1 << USR_PASSWD));
  296. CPPUNIT_ASSERT_EQUAL(std::string("::1"), mkstr(res, USR_HOST, uri));
  297. CPPUNIT_ASSERT_EQUAL(std::string("user:pass"), mkstr(res, USR_USERINFO, uri));
  298. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
  299. CPPUNIT_ASSERT_EQUAL(std::string("pass"), mkstr(res, USR_PASSWD, uri));
  300. CPPUNIT_ASSERT(res.flags & USF_IPV6ADDR);
  301. uri = "http://aria2/f";
  302. memset(&res, 0, sizeof(res));
  303. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  304. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
  305. (1 << USR_BASENAME));
  306. CPPUNIT_ASSERT_EQUAL(std::string("/f"), mkstr(res, USR_PATH, uri));
  307. CPPUNIT_ASSERT_EQUAL(std::string("f"), mkstr(res, USR_BASENAME, uri));
  308. uri = "http://[::1]/f";
  309. memset(&res, 0, sizeof(res));
  310. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  311. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
  312. (1 << USR_BASENAME));
  313. CPPUNIT_ASSERT_EQUAL(std::string("::1"), mkstr(res, USR_HOST, uri));
  314. CPPUNIT_ASSERT_EQUAL(std::string("/f"), mkstr(res, USR_PATH, uri));
  315. CPPUNIT_ASSERT_EQUAL(std::string("f"), mkstr(res, USR_BASENAME, uri));
  316. uri = "http://[::1]:8080/f";
  317. memset(&res, 0, sizeof(res));
  318. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  319. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT) |
  320. (1 << USR_PATH) | (1 << USR_BASENAME));
  321. CPPUNIT_ASSERT_EQUAL((uint16_t)8080, res.port);
  322. CPPUNIT_ASSERT_EQUAL(std::string("/f"), mkstr(res, USR_PATH, uri));
  323. CPPUNIT_ASSERT_EQUAL(std::string("f"), mkstr(res, USR_BASENAME, uri));
  324. uri = "https://user:pass@host/f";
  325. memset(&res, 0, sizeof(res));
  326. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  327. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) |
  328. (1 << USR_USERINFO) | (1 << USR_USER) |
  329. (1 << USR_PASSWD) | (1 << USR_PATH) |
  330. (1 << USR_BASENAME));
  331. CPPUNIT_ASSERT_EQUAL(std::string("host"), mkstr(res, USR_HOST, uri));
  332. CPPUNIT_ASSERT_EQUAL(std::string("user:pass"), mkstr(res, USR_USERINFO, uri));
  333. CPPUNIT_ASSERT_EQUAL(std::string("/f"), mkstr(res, USR_PATH, uri));
  334. CPPUNIT_ASSERT_EQUAL(std::string("f"), mkstr(res, USR_BASENAME, uri));
  335. uri = "http://aria2/index.html?foo";
  336. memset(&res, 0, sizeof(res));
  337. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  338. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
  339. (1 << USR_QUERY) | (1 << USR_BASENAME));
  340. CPPUNIT_ASSERT_EQUAL(std::string("/index.html"), mkstr(res, USR_PATH, uri));
  341. CPPUNIT_ASSERT_EQUAL(std::string("index.html"),
  342. mkstr(res, USR_BASENAME, uri));
  343. CPPUNIT_ASSERT_EQUAL(std::string("foo"), mkstr(res, USR_QUERY, uri));
  344. }
  345. void UriSplitTest::testUriSplit_fail()
  346. {
  347. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, ""));
  348. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "h"));
  349. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http:"));
  350. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http:a"));
  351. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http:/"));
  352. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://"));
  353. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http:/a"));
  354. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://:host"));
  355. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://@user@host"));
  356. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user:"));
  357. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user:pass"));
  358. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user:65536"));
  359. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user:pass?"));
  360. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user:pass@host:65536"));
  361. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user:pass@host:x"));
  362. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user:pass@host:80x"));
  363. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user@"));
  364. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://[]"));
  365. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://[::"));
  366. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user[::1]"));
  367. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user[::1]x"));
  368. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user:pass[::1]"));
  369. }
  370. } // namespace aria2