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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. struct uri *uri = NULL;
  34. int rc;
  35. /* Parse and unparse URI */
  36. uri = parse_uri ( uri_string );
  37. if ( ! uri ) {
  38. rc = -ENOMEM;
  39. goto done;
  40. }
  41. unparse_uri ( buf, sizeof ( buf ), uri, URI_ALL );
  42. /* Compare result */
  43. if ( strcmp ( buf, uri_string ) != 0 ) {
  44. printf ( "Unparse of \"%s\" produced \"%s\"\n",
  45. uri_string, buf );
  46. rc = -EINVAL;
  47. goto done;
  48. }
  49. rc = 0;
  50. done:
  51. uri_put ( uri );
  52. if ( rc ) {
  53. printf ( "URI parse-unparse of \"%s\" failed: %s\n",
  54. uri_string, strerror ( rc ) );
  55. }
  56. return rc;
  57. }
  58. static int test_resolve ( const char *base_uri_string,
  59. const char *relative_uri_string,
  60. const char *resolved_uri_string ) {
  61. struct uri *base_uri = NULL;
  62. struct uri *relative_uri = NULL;
  63. struct uri *resolved_uri = NULL;
  64. char buf[URI_MAX_LEN];
  65. int rc;
  66. /* Parse URIs */
  67. base_uri = parse_uri ( base_uri_string );
  68. if ( ! base_uri ) {
  69. rc = -ENOMEM;
  70. goto done;
  71. }
  72. relative_uri = parse_uri ( relative_uri_string );
  73. if ( ! relative_uri ) {
  74. rc = -ENOMEM;
  75. goto done;
  76. }
  77. /* Resolve URI */
  78. resolved_uri = resolve_uri ( base_uri, relative_uri );
  79. if ( ! resolved_uri ) {
  80. rc = -ENOMEM;
  81. goto done;
  82. }
  83. /* Compare result */
  84. unparse_uri ( buf, sizeof ( buf ), resolved_uri, URI_ALL );
  85. if ( strcmp ( buf, resolved_uri_string ) != 0 ) {
  86. printf ( "Resolution of \"%s\"+\"%s\" produced \"%s\"\n",
  87. base_uri_string, relative_uri_string, buf );
  88. rc = -EINVAL;
  89. goto done;
  90. }
  91. rc = 0;
  92. done:
  93. uri_put ( base_uri );
  94. uri_put ( relative_uri );
  95. uri_put ( resolved_uri );
  96. if ( rc ) {
  97. printf ( "URI resolution of \"%s\"+\"%s\" failed: %s\n",
  98. base_uri_string, relative_uri_string,
  99. strerror ( rc ) );
  100. }
  101. return rc;
  102. }
  103. int uri_test ( void ) {
  104. unsigned int i;
  105. struct uri_test *uri_test;
  106. int rc;
  107. int overall_rc = 0;
  108. for ( i = 0 ; i < ( sizeof ( uri_tests ) /
  109. sizeof ( uri_tests[0] ) ) ; i++ ) {
  110. uri_test = &uri_tests[i];
  111. rc = test_parse_unparse ( uri_test->base_uri_string );
  112. if ( rc != 0 )
  113. overall_rc = rc;
  114. rc = test_parse_unparse ( uri_test->relative_uri_string );
  115. if ( rc != 0 )
  116. overall_rc = rc;
  117. rc = test_parse_unparse ( uri_test->resolved_uri_string );
  118. if ( rc != 0 )
  119. overall_rc = rc;
  120. rc = test_resolve ( uri_test->base_uri_string,
  121. uri_test->relative_uri_string,
  122. uri_test->resolved_uri_string );
  123. if ( rc != 0 )
  124. overall_rc = rc;
  125. }
  126. if ( overall_rc )
  127. printf ( "URI tests failed: %s\n", strerror ( overall_rc ) );
  128. return overall_rc;
  129. }