選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

uri_test.c 3.3KB

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