UriSplitTest.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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,
  52. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
  53. (1 << USR_USERINFO) | (1 << USR_USER));
  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. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USERINFO, uri));
  57. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
  58. uri = "http://user:pass@aria2.sf.net/path/";
  59. memset(&res, 0, sizeof(res));
  60. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  61. CHECK_FIELD_SET(res,
  62. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
  63. (1 << USR_USERINFO) | (1 << USR_USER) |
  64. (1 << USR_PASSWD));
  65. CPPUNIT_ASSERT_EQUAL(std::string("aria2.sf.net"), mkstr(res, USR_HOST, uri));
  66. CPPUNIT_ASSERT_EQUAL(std::string("/path/"), mkstr(res, USR_PATH, uri));
  67. CPPUNIT_ASSERT_EQUAL(std::string("user:pass"), mkstr(res, USR_USERINFO, uri));
  68. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
  69. CPPUNIT_ASSERT_EQUAL(std::string("pass"), mkstr(res, USR_PASSWD, uri));
  70. // According to RFC 3986, @ in userinfo is illegal. But many people
  71. // have e-mail account as username and don't understand
  72. // percent-encoding and keep getting erros putting it in URI in
  73. // unecoded form. Because of this, we support @ in username.
  74. uri = "http://user@foo.com:pass@aria2.sf.net/path/";
  75. memset(&res, 0, sizeof(res));
  76. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  77. CHECK_FIELD_SET(res,
  78. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
  79. (1 << USR_USERINFO) | (1 << USR_USER) |
  80. (1 << USR_PASSWD));
  81. CPPUNIT_ASSERT_EQUAL(std::string("aria2.sf.net"), mkstr(res, USR_HOST, uri));
  82. CPPUNIT_ASSERT_EQUAL(std::string("/path/"), mkstr(res, USR_PATH, uri));
  83. CPPUNIT_ASSERT_EQUAL(std::string("user@foo.com:pass"),
  84. mkstr(res, USR_USERINFO, uri));
  85. CPPUNIT_ASSERT_EQUAL(std::string("user@foo.com"), mkstr(res, USR_USER, uri));
  86. CPPUNIT_ASSERT_EQUAL(std::string("pass"), mkstr(res, USR_PASSWD, uri));
  87. // Port processed in URI_MAYBE_USER -> URI_PORT
  88. uri = "https://aria2.sf.net:443/path/";
  89. memset(&res, 0, sizeof(res));
  90. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  91. CHECK_FIELD_SET(res,
  92. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT) |
  93. (1 << USR_PATH));
  94. CPPUNIT_ASSERT_EQUAL(std::string("https"), mkstr(res, USR_SCHEME, uri));
  95. CPPUNIT_ASSERT_EQUAL(std::string("aria2.sf.net"), mkstr(res, USR_HOST, uri));
  96. CPPUNIT_ASSERT_EQUAL(std::string("/path/"), mkstr(res, USR_PATH, uri));
  97. CPPUNIT_ASSERT_EQUAL((uint16_t)443, res.port);
  98. // Port processed in URI_PORT
  99. uri = "https://user:pass@aria2.sf.net:443/path/";
  100. memset(&res, 0, sizeof(res));
  101. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  102. CHECK_FIELD_SET(res,
  103. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT) |
  104. (1 << USR_PATH) | (1 << USR_USERINFO) | (1 << USR_USER) |
  105. (1 << USR_PASSWD));
  106. CPPUNIT_ASSERT_EQUAL(std::string("aria2.sf.net"), mkstr(res, USR_HOST, uri));
  107. CPPUNIT_ASSERT_EQUAL(std::string("/path/"), mkstr(res, USR_PATH, uri));
  108. CPPUNIT_ASSERT_EQUAL(std::string("user:pass"), mkstr(res, USR_USERINFO, uri));
  109. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
  110. CPPUNIT_ASSERT_EQUAL(std::string("pass"), mkstr(res, USR_PASSWD, uri));
  111. CPPUNIT_ASSERT_EQUAL((uint16_t)443, res.port);
  112. // Port processed in URI_MAYBE_PASSWD
  113. uri = "https://user@aria2.sf.net:443/path/";
  114. memset(&res, 0, sizeof(res));
  115. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  116. CHECK_FIELD_SET(res,
  117. (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,
  140. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_USERINFO) |
  141. (1 << USR_USER));
  142. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  143. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USERINFO, uri));
  144. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
  145. uri = "http://user:@aria2";
  146. memset(&res, 0, sizeof(res));
  147. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  148. CHECK_FIELD_SET(res,
  149. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_USERINFO) |
  150. (1 << USR_USER) | (1 << USR_PASSWD));
  151. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  152. CPPUNIT_ASSERT_EQUAL(std::string("user:"), mkstr(res, USR_USERINFO, uri));
  153. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
  154. CPPUNIT_ASSERT_EQUAL(std::string(""), mkstr(res, USR_PASSWD, uri));
  155. uri = "http://aria2/?foo#bar";
  156. memset(&res, 0, sizeof(res));
  157. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  158. CHECK_FIELD_SET(res,
  159. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
  160. (1 << USR_QUERY) | (1 << USR_FRAGMENT));
  161. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  162. CPPUNIT_ASSERT_EQUAL(std::string("/"), mkstr(res, USR_PATH, uri));
  163. CPPUNIT_ASSERT_EQUAL(std::string("foo"), mkstr(res, USR_QUERY, uri));
  164. CPPUNIT_ASSERT_EQUAL(std::string("bar"), mkstr(res, USR_FRAGMENT, uri));
  165. // URI_MAYBE_USER
  166. uri = "http://aria2?foo";
  167. memset(&res, 0, sizeof(res));
  168. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  169. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_QUERY));
  170. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  171. CPPUNIT_ASSERT_EQUAL(std::string("foo"), mkstr(res, USR_QUERY, uri));
  172. // URI_MAYBE_USER
  173. uri = "http://aria2#bar";
  174. memset(&res, 0, sizeof(res));
  175. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  176. CHECK_FIELD_SET(res,
  177. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_FRAGMENT));
  178. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  179. CPPUNIT_ASSERT_EQUAL(std::string("bar"), mkstr(res, USR_FRAGMENT, uri));
  180. // URI_MAYBE_PASSWD
  181. uri = "https://aria2:443?foo";
  182. memset(&res, 0, sizeof(res));
  183. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  184. CHECK_FIELD_SET(res,
  185. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT) |
  186. (1 << USR_QUERY));
  187. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  188. CPPUNIT_ASSERT_EQUAL(std::string("foo"), mkstr(res, USR_QUERY, uri));
  189. CPPUNIT_ASSERT_EQUAL((uint16_t)443, res.port);
  190. // URI_MAYBE_PASSWD
  191. uri = "https://aria2:443#bar";
  192. memset(&res, 0, sizeof(res));
  193. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  194. CHECK_FIELD_SET(res,
  195. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT) |
  196. (1 << USR_FRAGMENT));
  197. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  198. CPPUNIT_ASSERT_EQUAL(std::string("bar"), mkstr(res, USR_FRAGMENT, uri));
  199. CPPUNIT_ASSERT_EQUAL((uint16_t)443, res.port);
  200. // URI_PORT
  201. uri = "https://user:pass@aria2:443?foo";
  202. memset(&res, 0, sizeof(res));
  203. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  204. CHECK_FIELD_SET(res,
  205. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT) |
  206. (1 << USR_QUERY) | (1 << USR_USERINFO) | (1 << USR_USER) |
  207. (1 << USR_PASSWD));
  208. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  209. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
  210. CPPUNIT_ASSERT_EQUAL(std::string("pass"), mkstr(res, USR_PASSWD, uri));
  211. CPPUNIT_ASSERT_EQUAL(std::string("foo"), mkstr(res, USR_QUERY, uri));
  212. CPPUNIT_ASSERT_EQUAL((uint16_t)443, res.port);
  213. // URI_PORT
  214. uri = "https://user:pass@aria2:443#bar";
  215. memset(&res, 0, sizeof(res));
  216. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  217. CHECK_FIELD_SET(res,
  218. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT) |
  219. (1 << USR_FRAGMENT) | (1 << USR_USERINFO) |
  220. (1 << USR_USER) | (1 << USR_PASSWD));
  221. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  222. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
  223. CPPUNIT_ASSERT_EQUAL(std::string("pass"), mkstr(res, USR_PASSWD, uri));
  224. CPPUNIT_ASSERT_EQUAL(std::string("bar"), mkstr(res, USR_FRAGMENT, uri));
  225. CPPUNIT_ASSERT_EQUAL((uint16_t)443, res.port);
  226. // URI_HOST
  227. uri = "http://user:pass@aria2?foo";
  228. memset(&res, 0, sizeof(res));
  229. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  230. CHECK_FIELD_SET(res,
  231. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_QUERY) |
  232. (1 << USR_USERINFO) | (1 << USR_USER) |
  233. (1 << USR_PASSWD));
  234. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  235. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
  236. CPPUNIT_ASSERT_EQUAL(std::string("pass"), mkstr(res, USR_PASSWD, uri));
  237. CPPUNIT_ASSERT_EQUAL(std::string("foo"), mkstr(res, USR_QUERY, uri));
  238. // URI_HOST
  239. uri = "http://user:pass@aria2#bar";
  240. memset(&res, 0, sizeof(res));
  241. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  242. CHECK_FIELD_SET(res,
  243. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_FRAGMENT) |
  244. (1 << USR_USERINFO) | (1 << USR_USER) |
  245. (1 << USR_PASSWD));
  246. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  247. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
  248. CPPUNIT_ASSERT_EQUAL(std::string("pass"), mkstr(res, USR_PASSWD, uri));
  249. CPPUNIT_ASSERT_EQUAL(std::string("bar"), mkstr(res, USR_FRAGMENT, uri));
  250. // empty query
  251. uri = "http://aria2/?";
  252. memset(&res, 0, sizeof(res));
  253. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  254. CHECK_FIELD_SET(res,
  255. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
  256. (1 << USR_QUERY));
  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_QUERY, uri));
  260. // empty 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,
  265. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
  266. (1 << USR_FRAGMENT));
  267. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  268. CPPUNIT_ASSERT_EQUAL(std::string("/"), mkstr(res, USR_PATH, uri));
  269. CPPUNIT_ASSERT_EQUAL(std::string(""), mkstr(res, USR_FRAGMENT, uri));
  270. // empty query and fragment
  271. uri = "http://aria2/?#";
  272. memset(&res, 0, sizeof(res));
  273. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  274. CHECK_FIELD_SET(res,
  275. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
  276. (1 << USR_QUERY) | (1 << USR_FRAGMENT));
  277. CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
  278. CPPUNIT_ASSERT_EQUAL(std::string("/"), mkstr(res, USR_PATH, uri));
  279. CPPUNIT_ASSERT_EQUAL(std::string(""), mkstr(res, USR_QUERY, uri));
  280. CPPUNIT_ASSERT_EQUAL(std::string(""), mkstr(res, USR_FRAGMENT, uri));
  281. // IPv6 numeric address
  282. uri = "http://[::1]";
  283. memset(&res, 0, sizeof(res));
  284. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  285. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST));
  286. CPPUNIT_ASSERT_EQUAL(std::string("::1"), mkstr(res, USR_HOST, uri));
  287. CPPUNIT_ASSERT(res.flags & USF_IPV6ADDR);
  288. uri = "https://[::1]:443";
  289. memset(&res, 0, sizeof(res));
  290. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  291. CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT));
  292. CPPUNIT_ASSERT_EQUAL(std::string("::1"), mkstr(res, USR_HOST, uri));
  293. CPPUNIT_ASSERT_EQUAL((uint16_t)443, res.port);
  294. CPPUNIT_ASSERT(res.flags & USF_IPV6ADDR);
  295. // USR_MAYBE_USER
  296. uri = "https://user@[::1]";
  297. memset(&res, 0, sizeof(res));
  298. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  299. CHECK_FIELD_SET(res,
  300. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_USERINFO) |
  301. (1 << USR_USER));
  302. CPPUNIT_ASSERT_EQUAL(std::string("::1"), mkstr(res, USR_HOST, uri));
  303. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USERINFO, uri));
  304. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
  305. CPPUNIT_ASSERT(res.flags & USF_IPV6ADDR);
  306. // USR_BEFORE_HOST
  307. uri = "https://user:pass@[::1]";
  308. memset(&res, 0, sizeof(res));
  309. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  310. CHECK_FIELD_SET(res,
  311. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_USERINFO) |
  312. (1 << USR_USER) | (1 << USR_PASSWD));
  313. CPPUNIT_ASSERT_EQUAL(std::string("::1"), mkstr(res, USR_HOST, uri));
  314. CPPUNIT_ASSERT_EQUAL(std::string("user:pass"), mkstr(res, USR_USERINFO, uri));
  315. CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
  316. CPPUNIT_ASSERT_EQUAL(std::string("pass"), mkstr(res, USR_PASSWD, uri));
  317. CPPUNIT_ASSERT(res.flags & USF_IPV6ADDR);
  318. uri = "http://aria2/f";
  319. memset(&res, 0, sizeof(res));
  320. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  321. CHECK_FIELD_SET(res,
  322. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
  323. (1 << USR_BASENAME));
  324. CPPUNIT_ASSERT_EQUAL(std::string("/f"), mkstr(res, USR_PATH, uri));
  325. CPPUNIT_ASSERT_EQUAL(std::string("f"), mkstr(res, USR_BASENAME, uri));
  326. uri = "http://[::1]/f";
  327. memset(&res, 0, sizeof(res));
  328. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  329. CHECK_FIELD_SET(res,
  330. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
  331. (1 << USR_BASENAME));
  332. CPPUNIT_ASSERT_EQUAL(std::string("::1"), mkstr(res, USR_HOST, 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://[::1]:8080/f";
  336. memset(&res, 0, sizeof(res));
  337. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  338. CHECK_FIELD_SET(res,
  339. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT) |
  340. (1 << USR_PATH) | (1 << USR_BASENAME));
  341. CPPUNIT_ASSERT_EQUAL((uint16_t)8080, res.port);
  342. CPPUNIT_ASSERT_EQUAL(std::string("/f"), mkstr(res, USR_PATH, uri));
  343. CPPUNIT_ASSERT_EQUAL(std::string("f"), mkstr(res, USR_BASENAME, uri));
  344. uri = "https://user:pass@host/f";
  345. memset(&res, 0, sizeof(res));
  346. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  347. CHECK_FIELD_SET(res,
  348. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_USERINFO) |
  349. (1 << USR_USER) | (1 << USR_PASSWD) | (1 << USR_PATH) |
  350. (1 << USR_BASENAME));
  351. CPPUNIT_ASSERT_EQUAL(std::string("host"), mkstr(res, USR_HOST, uri));
  352. CPPUNIT_ASSERT_EQUAL(std::string("user:pass"), mkstr(res, USR_USERINFO, uri));
  353. CPPUNIT_ASSERT_EQUAL(std::string("/f"), mkstr(res, USR_PATH, uri));
  354. CPPUNIT_ASSERT_EQUAL(std::string("f"), mkstr(res, USR_BASENAME, uri));
  355. uri = "http://aria2/index.html?foo";
  356. memset(&res, 0, sizeof(res));
  357. CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
  358. CHECK_FIELD_SET(res,
  359. (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
  360. (1 << USR_QUERY) | (1 << USR_BASENAME));
  361. CPPUNIT_ASSERT_EQUAL(std::string("/index.html"), mkstr(res, USR_PATH, uri));
  362. CPPUNIT_ASSERT_EQUAL(std::string("index.html"),
  363. mkstr(res, USR_BASENAME, uri));
  364. CPPUNIT_ASSERT_EQUAL(std::string("foo"), mkstr(res, USR_QUERY, uri));
  365. }
  366. void UriSplitTest::testUriSplit_fail()
  367. {
  368. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, ""));
  369. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "h"));
  370. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http:"));
  371. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http:a"));
  372. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http:/"));
  373. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://"));
  374. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http:/a"));
  375. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://:host"));
  376. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://@user@host"));
  377. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user:"));
  378. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user:pass"));
  379. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user:65536"));
  380. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user:pass?"));
  381. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user:pass@host:65536"));
  382. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user:pass@host:x"));
  383. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user:pass@host:80x"));
  384. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user@"));
  385. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://[]"));
  386. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://[::"));
  387. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user[::1]"));
  388. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user[::1]x"));
  389. CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user:pass[::1]"));
  390. }
  391. } // namespace aria2