You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uri_test.c 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include <stdint.h>
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #include <ipxe/uri.h>
  7. #define URI_MAX_LEN 1024
  8. struct uri_test {
  9. const char *base_uri_string;
  10. const char *relative_uri_string;
  11. const char *resolved_uri_string;
  12. };
  13. static struct uri_test uri_tests[] = {
  14. { "http://www.fensystems.co.uk", "",
  15. "http://www.fensystems.co.uk/" },
  16. { "http://ipxe.org/wiki/page1", "page2",
  17. "http://ipxe.org/wiki/page2" },
  18. { "http://ipxe.org/wiki/page1", "../page3",
  19. "http://ipxe.org/page3" },
  20. { "tftp://192.168.0.1/", "/tftpboot/vmlinuz",
  21. "tftp://192.168.0.1/tftpboot/vmlinuz" },
  22. { "ftp://the%41nswer%3d:%34ty%32wo@ether%62oot.org:8080/p%41th/foo",
  23. "to?%41=b#%43d",
  24. "ftp://theAnswer%3d:4ty2wo@ipxe.org:8080/path/to?a=b#cd" },
  25. #if 0
  26. "http://www.ipxe.org/wiki",
  27. "mailto:bob@nowhere.com",
  28. "ftp://joe:secret@insecure.org:8081/hidden/path/to?what=is#this",
  29. #endif
  30. };
  31. static int test_parse_unparse ( const char *uri_string ) {
  32. char buf[URI_MAX_LEN];
  33. size_t len;
  34. struct uri *uri = NULL;
  35. int rc;
  36. /* Parse and unparse URI */
  37. uri = parse_uri ( uri_string );
  38. if ( ! uri ) {
  39. rc = -ENOMEM;
  40. goto done;
  41. }
  42. len = unparse_uri ( buf, sizeof ( buf ), uri, URI_ALL );
  43. /* Compare result */
  44. if ( strcmp ( buf, uri_string ) != 0 ) {
  45. printf ( "Unparse of \"%s\" produced \"%s\"\n",
  46. uri_string, buf );
  47. rc = -EINVAL;
  48. goto done;
  49. }
  50. rc = 0;
  51. done:
  52. uri_put ( uri );
  53. if ( rc ) {
  54. printf ( "URI parse-unparse of \"%s\" failed: %s\n",
  55. uri_string, strerror ( rc ) );
  56. }
  57. return rc;
  58. }
  59. static int test_resolve ( const char *base_uri_string,
  60. const char *relative_uri_string,
  61. const char *resolved_uri_string ) {
  62. struct uri *base_uri = NULL;
  63. struct uri *relative_uri = NULL;
  64. struct uri *resolved_uri = NULL;
  65. char buf[URI_MAX_LEN];
  66. size_t len;
  67. int rc;
  68. /* Parse URIs */
  69. base_uri = parse_uri ( base_uri_string );
  70. if ( ! base_uri ) {
  71. rc = -ENOMEM;
  72. goto done;
  73. }
  74. relative_uri = parse_uri ( relative_uri_string );
  75. if ( ! relative_uri ) {
  76. rc = -ENOMEM;
  77. goto done;
  78. }
  79. /* Resolve URI */
  80. resolved_uri = resolve_uri ( base_uri, relative_uri );
  81. if ( ! resolved_uri ) {
  82. rc = -ENOMEM;
  83. goto done;
  84. }
  85. /* Compare result */
  86. len = unparse_uri ( buf, sizeof ( buf ), resolved_uri, URI_ALL );
  87. if ( strcmp ( buf, resolved_uri_string ) != 0 ) {
  88. printf ( "Resolution of \"%s\"+\"%s\" produced \"%s\"\n",
  89. base_uri_string, relative_uri_string, buf );
  90. rc = -EINVAL;
  91. goto done;
  92. }
  93. rc = 0;
  94. done:
  95. uri_put ( base_uri );
  96. uri_put ( relative_uri );
  97. uri_put ( resolved_uri );
  98. if ( rc ) {
  99. printf ( "URI resolution of \"%s\"+\"%s\" failed: %s\n",
  100. base_uri_string, relative_uri_string,
  101. strerror ( rc ) );
  102. }
  103. return rc;
  104. }
  105. int uri_test ( void ) {
  106. unsigned int i;
  107. struct uri_test *uri_test;
  108. int rc;
  109. int overall_rc = 0;
  110. for ( i = 0 ; i < ( sizeof ( uri_tests ) /
  111. sizeof ( uri_tests[0] ) ) ; i++ ) {
  112. uri_test = &uri_tests[i];
  113. rc = test_parse_unparse ( uri_test->base_uri_string );
  114. if ( rc != 0 )
  115. overall_rc = rc;
  116. rc = test_parse_unparse ( uri_test->relative_uri_string );
  117. if ( rc != 0 )
  118. overall_rc = rc;
  119. rc = test_parse_unparse ( uri_test->resolved_uri_string );
  120. if ( rc != 0 )
  121. overall_rc = rc;
  122. rc = test_resolve ( uri_test->base_uri_string,
  123. uri_test->relative_uri_string,
  124. uri_test->resolved_uri_string );
  125. if ( rc != 0 )
  126. overall_rc = rc;
  127. }
  128. if ( overall_rc )
  129. printf ( "URI tests failed: %s\n", strerror ( overall_rc ) );
  130. return overall_rc;
  131. }