PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/libUPnP/Neptune/Source/Tests/Url1/UrlTest1.cpp

https://github.com/koying/xbmc
C++ | 278 lines | 221 code | 28 blank | 29 comment | 72 complexity | 123cedecde0e2577732c556521f2450e MD5 | raw file
  1. /*****************************************************************
  2. |
  3. | URL Test Program 1
  4. |
  5. | (c) 2001-2006 Gilles Boccon-Gibod
  6. | Author: Gilles Boccon-Gibod (bok@bok.net)
  7. |
  8. ****************************************************************/
  9. /*----------------------------------------------------------------------
  10. | includes
  11. +---------------------------------------------------------------------*/
  12. #include "Neptune.h"
  13. #include "NptDebug.h"
  14. #if defined(WIN32) && defined(_DEBUG)
  15. #include <crtdbg.h>
  16. #endif
  17. #define CHECK(x) \
  18. do { \
  19. if (!(x)) { \
  20. fprintf(stderr, "ERROR line %d \n", __LINE__); \
  21. } \
  22. } while(0)
  23. /*----------------------------------------------------------------------
  24. | Parse Test Vectors
  25. +---------------------------------------------------------------------*/
  26. typedef struct {
  27. const char* url;
  28. bool expected_to_be_valid;
  29. const char* expected_scheme;
  30. const char* expected_host;
  31. int expected_port;
  32. const char* expected_path;
  33. const char* expected_query;
  34. const char* expected_fragment;
  35. const char* expected_string;
  36. } ParseTestVector;
  37. static ParseTestVector ParseTestVectors[] = {
  38. {"", false, NULL, NULL, 0, NULL, NULL, NULL, NULL},
  39. {"http", false, NULL, NULL, 0, NULL, NULL, NULL, NULL},
  40. {"http:", false, NULL, NULL, 0, NULL, NULL, NULL, NULL},
  41. {"http:/", false, NULL, NULL, 0, NULL, NULL, NULL, NULL},
  42. {"http://", false, NULL, NULL, 0, NULL, NULL, NULL, NULL},
  43. {"http://a", true, "http", "a", 80, "/", NULL, NULL, "http://a/"},
  44. {"http://foo.bar", true, "http", "foo.bar", 80, "/", NULL, NULL, "http://foo.bar/"},
  45. {"http://foo.bar:", false, NULL, NULL, 0, NULL, NULL, NULL, NULL},
  46. {"http://foo.bar:156", true, "http", "foo.bar", 156, "/", NULL, NULL, "http://foo.bar:156/"},
  47. {"http://foo.bar:176899", false, NULL, NULL, 0, NULL, NULL, NULL, NULL},
  48. {"http://foo.bar:176a", false, NULL, NULL, 0, NULL, NULL, NULL, NULL},
  49. {"http://foo.bar:176/", true, "http", "foo.bar", 176, "/", NULL, NULL, "http://foo.bar:176/"},
  50. {"http://foo.bar:176/blabla", true, "http", "foo.bar", 176, "/blabla", NULL, NULL, "http://foo.bar:176/blabla"},
  51. {"http://foo.bar/blabla/blibli", true, "http", "foo.bar", 80, "/blabla/blibli", NULL, NULL, "http://foo.bar/blabla/blibli"},
  52. {"http://foo.bar/blabla/blibli", true, "http", "foo.bar", 80, "/blabla/blibli", NULL, NULL, "http://foo.bar/blabla/blibli"},
  53. {"http://foo.bar:176/blabla/blibli/", true, "http", "foo.bar", 176, "/blabla/blibli/", NULL, NULL, "http://foo.bar:176/blabla/blibli/"},
  54. {"http://foo.bar/", true, "http", "foo.bar", 80, "/", NULL, NULL, "http://foo.bar/"},
  55. {"http://foo.bar/blabla/blibli/?query", true, "http", "foo.bar", 80, "/blabla/blibli/", "query", NULL, "http://foo.bar/blabla/blibli/?query"},
  56. {"http://foo.bar/blabla/blibli/?query=1&bla=%20&slash=/&foo=a#fragment", true, "http", "foo.bar", 80, "/blabla/blibli/", "query=1&bla=%20&slash=/&foo=a", "fragment", "http://foo.bar/blabla/blibli/?query=1&bla=%20&slash=/&foo=a#fragment"},
  57. {"http://foo.bar/blabla%20foo/blibli/?query=1&bla=2&slash=/&foo=a#fragment", true, "http", "foo.bar", 80, "/blabla%20foo/blibli/", "query=1&bla=2&slash=/&foo=a","fragment", "http://foo.bar/blabla%20foo/blibli/?query=1&bla=2&slash=/&foo=a#fragment"},
  58. {"http://foo.bar?query", true, "http", "foo.bar", 80, NULL, "query", NULL, "http://foo.bar/?query"},
  59. {"http://foo.bar#fragment", true, "http", "foo.bar", 80, NULL, NULL, "fragment", "http://foo.bar/#fragment"}
  60. };
  61. typedef struct {
  62. char* scheme;
  63. char* host;
  64. int port;
  65. char* qery;
  66. char* fragment;
  67. char* expected_uri;
  68. } ConstructTestVector;
  69. typedef struct {
  70. char* in;
  71. char* out;
  72. bool do_percent;
  73. } EncodeTestVector;
  74. typedef struct {
  75. char* in;
  76. char* out;
  77. } DecodeTestVector;
  78. /*----------------------------------------------------------------------
  79. | TestParse
  80. +---------------------------------------------------------------------*/
  81. static void
  82. TestParse(ParseTestVector* vector, int test_index)
  83. {
  84. NPT_HttpUrl url(vector->url);
  85. if (url.IsValid() != vector->expected_to_be_valid) {
  86. fprintf(stderr, "TEST %02d: expected IsValid() to return %s, got %s\n", test_index, vector->expected_to_be_valid?"true":"false", url.IsValid()?"true":"false");
  87. return;
  88. }
  89. if (!vector->expected_to_be_valid) return;
  90. if (vector->expected_scheme) {
  91. if (url.GetScheme() != vector->expected_scheme) {
  92. fprintf(stderr, "TEST %02d: expected GetScheme() to return %s, got %s\n", test_index, vector->expected_scheme, url.GetScheme().GetChars());
  93. return;
  94. }
  95. }
  96. if (vector->expected_host) {
  97. if (url.GetHost() != vector->expected_host) {
  98. fprintf(stderr, "TEST %02d: expected GetHost() to return %s, got %s\n", test_index, vector->expected_host, url.GetHost().GetChars());
  99. return;
  100. }
  101. }
  102. if (url.GetPort() != vector->expected_port) {
  103. fprintf(stderr, "TEST %02d: expected GetPort() to return %d, got %d\n", test_index, vector->expected_port, url.GetPort());
  104. return;
  105. }
  106. if (vector->expected_path) {
  107. if (url.GetPath() != vector->expected_path) {
  108. fprintf(stderr, "TEST %02d: expected GetPath() to return %s, got %s\n", test_index, vector->expected_path, url.GetPath().GetChars());
  109. return;
  110. }
  111. }
  112. if (url.HasQuery() != (vector->expected_query != NULL)) {
  113. fprintf(stderr, "TEST %02d: expected a query, did not get one\n", test_index);
  114. return;
  115. }
  116. if (vector->expected_query) {
  117. if (url.GetQuery() != vector->expected_query) {
  118. fprintf(stderr, "TEST %02d: expected GetQuery() to return %s, got %s\n", test_index, vector->expected_query, url.GetQuery().GetChars());
  119. return;
  120. }
  121. }
  122. if (url.HasFragment() != (vector->expected_fragment != NULL)) {
  123. fprintf(stderr, "TEST %02d: expected a fragment, did not get one\n", test_index);
  124. return;
  125. }
  126. if (vector->expected_fragment) {
  127. if (url.GetFragment() != vector->expected_fragment) {
  128. fprintf(stderr, "TEST %02d: expected GetFragment() to return %s, got %s\n", test_index, vector->expected_fragment, url.GetFragment().GetChars());
  129. return;
  130. }
  131. }
  132. NPT_String url_string = url.ToString();
  133. if (url_string != vector->expected_string) {
  134. fprintf(stderr, "TEST %02d: expected ToString() to return %s, got %s\n", test_index, vector->expected_string, url_string.GetChars());
  135. return;
  136. }
  137. NPT_HttpUrl url2(url_string);
  138. if (url2.ToString() != url_string) {
  139. fprintf(stderr, "TEST %02d: url ToString() does not parse to same url\n", test_index);
  140. return;
  141. }
  142. }
  143. /*----------------------------------------------------------------------
  144. | main
  145. +---------------------------------------------------------------------*/
  146. int
  147. main(int /*argc*/, char** /*argv*/)
  148. {
  149. // setup debugging
  150. #if defined(WIN32) && defined(_DEBUG)
  151. int flags = _crtDbgFlag |
  152. _CRTDBG_ALLOC_MEM_DF |
  153. _CRTDBG_DELAY_FREE_MEM_DF |
  154. _CRTDBG_CHECK_ALWAYS_DF;
  155. _CrtSetDbgFlag(flags);
  156. //AllocConsole();
  157. //freopen("CONOUT$", "w", stdout);
  158. #endif
  159. printf("--- test starting\n");
  160. // parsing test vectors
  161. for (unsigned int i=0; i<sizeof(ParseTestVectors)/sizeof(ParseTestVectors[0]); i++) {
  162. ParseTestVector* vector = &ParseTestVectors[i];
  163. TestParse(vector, i);
  164. }
  165. // test URL parsing, special cases
  166. NPT_HttpUrl url;
  167. CHECK(!url.IsValid());
  168. url = "http://foo.bar/blabla%20foo/blibli/?query=1&bla=2&slash=/&foo=a#fragment";
  169. CHECK(url.IsValid());
  170. CHECK(url.GetHost() == "foo.bar");
  171. CHECK(url.GetPort() == 80);
  172. CHECK(url.GetPath() == "/blabla%20foo/blibli/");
  173. CHECK(url.GetQuery() == "query=1&bla=2&slash=/&foo=a");
  174. CHECK(url.GetFragment() == "fragment");
  175. CHECK(url.ToString(false) == "http://foo.bar/blabla%20foo/blibli/?query=1&bla=2&slash=/&foo=a");
  176. url = NPT_HttpUrl("http://foo.bar/blabla%20foo/blibli/?query=1&bla=2&slash=/&foo=a#fragment");
  177. CHECK(url.IsValid());
  178. CHECK(url.GetHost() == "foo.bar");
  179. CHECK(url.GetPort() == 80);
  180. CHECK(url.GetPath() == "/blabla%20foo/blibli/");
  181. CHECK(url.GetQuery() == "query=1&bla=2&slash=/&foo=a");
  182. CHECK(url.GetFragment() == "fragment");
  183. CHECK(url.ToRequestString() == "/blabla%20foo/blibli/?query=1&bla=2&slash=/&foo=a");
  184. url.ParsePathPlus("/bla/foo?query=bar");
  185. url.SetHost("bar.com:8080");
  186. CHECK(url.IsValid());
  187. CHECK(url.GetHost() == "bar.com");
  188. CHECK(url.GetPort() == 8080);
  189. CHECK(url.GetPath() == "/bla/foo");
  190. CHECK(url.GetQuery() == "query=bar");
  191. url.ParsePathPlus("bla/foo?query=bar");
  192. url.SetHost("bar.com:8080");
  193. CHECK(url.IsValid());
  194. CHECK(url.GetHost() == "bar.com");
  195. CHECK(url.GetPort() == 8080);
  196. CHECK(url.GetPath() == "bla/foo");
  197. CHECK(url.GetQuery() == "query=bar");
  198. url.ParsePathPlus("*");
  199. CHECK(url.IsValid());
  200. CHECK(url.GetPath() == "*");
  201. url = NPT_HttpUrl("http://foo/?query=1&bla=2&slash=/&foo=a#fragment");
  202. CHECK(url.IsValid());
  203. CHECK(url.GetHost() == "foo");
  204. CHECK(url.GetPort() == 80);
  205. CHECK(url.GetPath() == "/");
  206. CHECK(url.GetQuery() == "query=1&bla=2&slash=/&foo=a");
  207. CHECK(url.GetFragment() == "fragment");
  208. CHECK(url.ToRequestString() == "/?query=1&bla=2&slash=/&foo=a");
  209. // url form encoding
  210. NPT_UrlQuery query;
  211. query.AddField("url1","http://foo.bar/foo?q=3&bar=+7/3&boo=a%3Db&bli=a b");
  212. query.AddField("url2","(1234+456 789)");
  213. CHECK(query.ToString() == "url1=http%3A%2F%2Ffoo.bar%2Ffoo%3Fq%3D3%26bar%3D%2B7%2F3%26boo%3Da%253Db%26bli%3Da+b&url2=(1234%2B456+789)");
  214. query = "url1=http%3A%2F%2Ffoo.bar%2Ffoo%3Fq%3D3%26bar%3D%2B7%2F3&url2=12+34";
  215. CHECK(query.ToString() == "url1=http%3A%2F%2Ffoo.bar%2Ffoo%3Fq%3D3%26bar%3D%2B7%2F3&url2=12+34");
  216. // url query decoding
  217. NPT_UrlQuery query2("a=1+2+3&b=http%3A%2F%2Ffoo.bar%2Ffoo%3Fq%3D3%26bar%3D%2B7%2F3%26boo%3Da%3Db%26bli%3Da+b");
  218. const char* a_field = query2.GetField("a");
  219. const char* b_field = query2.GetField("b");
  220. const char* c_field = query2.GetField("c");
  221. CHECK(a_field != NULL);
  222. CHECK(NPT_StringsEqual(a_field, "1+2+3"));
  223. CHECK(NPT_UrlQuery::UrlDecode(a_field) == "1 2 3");
  224. CHECK(b_field != NULL);
  225. CHECK(NPT_StringsEqual(b_field, "http%3A%2F%2Ffoo.bar%2Ffoo%3Fq%3D3%26bar%3D%2B7%2F3%26boo%3Da%3Db%26bli%3Da+b"));
  226. CHECK(NPT_UrlQuery::UrlDecode(b_field) == "http://foo.bar/foo?q=3&bar= 7/3&boo=a=b&bli=a b");
  227. CHECK(c_field == NULL);
  228. // url query misc
  229. NPT_UrlQuery query3;
  230. query3.SetField("a b", "c&3", false);
  231. query3.AddField("a b", "c&4 b&6", false);
  232. query3.SetField("c d", "c&5", false);
  233. query3.SetField("a+b", "c_3", true);
  234. const char* field1 = query3.GetField("a b");
  235. const char* field2 = query3.GetField("c d");
  236. CHECK(field1 != NULL);
  237. CHECK(NPT_UrlQuery::UrlDecode(field1) == "c_3");
  238. CHECK(field2 != NULL);
  239. CHECK(NPT_UrlQuery::UrlDecode(field2) == "c&5");
  240. // url query with empty values
  241. NPT_UrlQuery query4("a=1&b&c=");
  242. a_field = query4.GetField("a");
  243. b_field = query4.GetField("b");
  244. c_field = query4.GetField("c");
  245. CHECK(NPT_StringsEqual(a_field, "1"));
  246. CHECK(NPT_StringsEqual(b_field, ""));
  247. CHECK(NPT_StringsEqual(c_field, ""));
  248. printf("--- test done\n");
  249. return 0;
  250. }