uri_test.c

Go to the documentation of this file.
00001 #include <stdint.h>
00002 #include <stddef.h>
00003 #include <stdio.h>
00004 #include <string.h>
00005 #include <errno.h>
00006 #include <gpxe/uri.h>
00007 
00008 #define URI_MAX_LEN 1024
00009 
00010 struct uri_test {
00011         const char *base_uri_string;
00012         const char *relative_uri_string;
00013         const char *resolved_uri_string;
00014 };
00015 
00016 static struct uri_test uri_tests[] = {
00017         { "http://www.fensystems.co.uk", "",
00018           "http://www.fensystems.co.uk/" },
00019         { "http://etherboot.org/wiki/page1", "page2",
00020           "http://etherboot.org/wiki/page2" },
00021         { "http://etherboot.org/wiki/page1", "../page3",
00022           "http://etherboot.org/page3" },
00023         { "tftp://192.168.0.1/", "/tftpboot/vmlinuz",
00024           "tftp://192.168.0.1/tftpboot/vmlinuz" },
00025         { "ftp://the%41nswer%3d:%34ty%32wo@ether%62oot.org:8080/p%41th/foo",
00026           "to?%41=b#%43d",
00027           "ftp://theAnswer%3d:4ty2wo@etherboot.org:8080/path/to?a=b#cd" },
00028 #if 0
00029         "http://www.etherboot.org/wiki",
00030         "mailto:bob@nowhere.com",
00031         "ftp://joe:secret@insecure.org:8081/hidden/path/to?what=is#this",
00032 #endif
00033 };
00034 
00035 static int test_parse_unparse ( const char *uri_string ) {
00036         char buf[URI_MAX_LEN];
00037         size_t len;
00038         struct uri *uri = NULL;
00039         int rc;
00040 
00041         /* Parse and unparse URI */
00042         uri = parse_uri ( uri_string );
00043         if ( ! uri ) {
00044                 rc = -ENOMEM;
00045                 goto done;
00046         }
00047         len = unparse_uri ( buf, sizeof ( buf ), uri, URI_ALL );
00048 
00049         /* Compare result */
00050         if ( strcmp ( buf, uri_string ) != 0 ) {
00051                 printf ( "Unparse of \"%s\" produced \"%s\"\n",
00052                          uri_string, buf );
00053                 rc = -EINVAL;
00054                 goto done;
00055         }
00056 
00057         rc = 0;
00058 
00059  done:
00060         uri_put ( uri );
00061         if ( rc ) {
00062                 printf ( "URI parse-unparse of \"%s\" failed: %s\n",
00063                          uri_string, strerror ( rc ) );
00064         }
00065         return rc;
00066 }
00067 
00068 static int test_resolve ( const char *base_uri_string,
00069                           const char *relative_uri_string,
00070                           const char *resolved_uri_string ) {
00071         struct uri *base_uri = NULL;
00072         struct uri *relative_uri = NULL;
00073         struct uri *resolved_uri = NULL;
00074         char buf[URI_MAX_LEN];
00075         size_t len;
00076         int rc;
00077 
00078         /* Parse URIs */
00079         base_uri = parse_uri ( base_uri_string );
00080         if ( ! base_uri ) {
00081                 rc = -ENOMEM;
00082                 goto done;
00083         }
00084         relative_uri = parse_uri ( relative_uri_string );
00085         if ( ! relative_uri ) {
00086                 rc = -ENOMEM;
00087                 goto done;
00088         }
00089 
00090         /* Resolve URI */
00091         resolved_uri = resolve_uri ( base_uri, relative_uri );
00092         if ( ! resolved_uri ) {
00093                 rc = -ENOMEM;
00094                 goto done;
00095         }
00096 
00097         /* Compare result */
00098         len = unparse_uri ( buf, sizeof ( buf ), resolved_uri, URI_ALL );
00099         if ( strcmp ( buf, resolved_uri_string ) != 0 ) {
00100                 printf ( "Resolution of \"%s\"+\"%s\" produced \"%s\"\n",
00101                          base_uri_string, relative_uri_string, buf );
00102                 rc = -EINVAL;
00103                 goto done;
00104         }
00105 
00106         rc = 0;
00107 
00108  done:
00109         uri_put ( base_uri );
00110         uri_put ( relative_uri );
00111         uri_put ( resolved_uri );
00112         if ( rc ) {
00113                 printf ( "URI resolution of \"%s\"+\"%s\" failed: %s\n",
00114                          base_uri_string, relative_uri_string,
00115                          strerror ( rc ) );
00116         }
00117         return rc;
00118 }
00119 
00120 int uri_test ( void ) {
00121         unsigned int i;
00122         struct uri_test *uri_test;
00123         int rc;
00124         int overall_rc = 0;
00125 
00126         for ( i = 0 ; i < ( sizeof ( uri_tests ) /
00127                             sizeof ( uri_tests[0] ) ) ; i++ ) {
00128                 uri_test = &uri_tests[i];
00129                 rc = test_parse_unparse ( uri_test->base_uri_string );
00130                 if ( rc != 0 )
00131                         overall_rc = rc;
00132                 rc = test_parse_unparse ( uri_test->relative_uri_string );
00133                 if ( rc != 0 )
00134                         overall_rc = rc;
00135                 rc = test_parse_unparse ( uri_test->resolved_uri_string );
00136                 if ( rc != 0 )
00137                         overall_rc = rc;
00138                 rc = test_resolve ( uri_test->base_uri_string,
00139                                     uri_test->relative_uri_string,
00140                                     uri_test->resolved_uri_string );
00141                 if ( rc != 0 )
00142                         overall_rc = rc;
00143         }
00144 
00145         if ( overall_rc )
00146                 printf ( "URI tests failed: %s\n", strerror ( overall_rc ) );
00147         return overall_rc;
00148 }

Generated on Tue Apr 6 20:01:11 2010 for gPXE by  doxygen 1.5.7.1