UriSplitTest.cc 19 KB

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