[release] Update version to 1.0.0 for release
[gpxe.git] / src / tests / uri_test.c
blobca3aed9fdd0924c395db3f795879a34debafcb7d
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>
8 #define URI_MAX_LEN 1024
10 struct uri_test {
11 const char *base_uri_string;
12 const char *relative_uri_string;
13 const char *resolved_uri_string;
16 static struct uri_test uri_tests[] = {
17 { "http://www.fensystems.co.uk", "",
18 "http://www.fensystems.co.uk/" },
19 { "http://etherboot.org/wiki/page1", "page2",
20 "http://etherboot.org/wiki/page2" },
21 { "http://etherboot.org/wiki/page1", "../page3",
22 "http://etherboot.org/page3" },
23 { "tftp://192.168.0.1/", "/tftpboot/vmlinuz",
24 "tftp://192.168.0.1/tftpboot/vmlinuz" },
25 { "ftp://the%41nswer%3d:%34ty%32wo@ether%62oot.org:8080/p%41th/foo",
26 "to?%41=b#%43d",
27 "ftp://theAnswer%3d:4ty2wo@etherboot.org:8080/path/to?a=b#cd" },
28 #if 0
29 "http://www.etherboot.org/wiki",
30 "mailto:bob@nowhere.com",
31 "ftp://joe:secret@insecure.org:8081/hidden/path/to?what=is#this",
32 #endif
35 static int test_parse_unparse ( const char *uri_string ) {
36 char buf[URI_MAX_LEN];
37 size_t len;
38 struct uri *uri = NULL;
39 int rc;
41 /* Parse and unparse URI */
42 uri = parse_uri ( uri_string );
43 if ( ! uri ) {
44 rc = -ENOMEM;
45 goto done;
47 len = unparse_uri ( buf, sizeof ( buf ), uri, URI_ALL );
49 /* Compare result */
50 if ( strcmp ( buf, uri_string ) != 0 ) {
51 printf ( "Unparse of \"%s\" produced \"%s\"\n",
52 uri_string, buf );
53 rc = -EINVAL;
54 goto done;
57 rc = 0;
59 done:
60 uri_put ( uri );
61 if ( rc ) {
62 printf ( "URI parse-unparse of \"%s\" failed: %s\n",
63 uri_string, strerror ( rc ) );
65 return rc;
68 static int test_resolve ( const char *base_uri_string,
69 const char *relative_uri_string,
70 const char *resolved_uri_string ) {
71 struct uri *base_uri = NULL;
72 struct uri *relative_uri = NULL;
73 struct uri *resolved_uri = NULL;
74 char buf[URI_MAX_LEN];
75 size_t len;
76 int rc;
78 /* Parse URIs */
79 base_uri = parse_uri ( base_uri_string );
80 if ( ! base_uri ) {
81 rc = -ENOMEM;
82 goto done;
84 relative_uri = parse_uri ( relative_uri_string );
85 if ( ! relative_uri ) {
86 rc = -ENOMEM;
87 goto done;
90 /* Resolve URI */
91 resolved_uri = resolve_uri ( base_uri, relative_uri );
92 if ( ! resolved_uri ) {
93 rc = -ENOMEM;
94 goto done;
97 /* Compare result */
98 len = unparse_uri ( buf, sizeof ( buf ), resolved_uri, URI_ALL );
99 if ( strcmp ( buf, resolved_uri_string ) != 0 ) {
100 printf ( "Resolution of \"%s\"+\"%s\" produced \"%s\"\n",
101 base_uri_string, relative_uri_string, buf );
102 rc = -EINVAL;
103 goto done;
106 rc = 0;
108 done:
109 uri_put ( base_uri );
110 uri_put ( relative_uri );
111 uri_put ( resolved_uri );
112 if ( rc ) {
113 printf ( "URI resolution of \"%s\"+\"%s\" failed: %s\n",
114 base_uri_string, relative_uri_string,
115 strerror ( rc ) );
117 return rc;
120 int uri_test ( void ) {
121 unsigned int i;
122 struct uri_test *uri_test;
123 int rc;
124 int overall_rc = 0;
126 for ( i = 0 ; i < ( sizeof ( uri_tests ) /
127 sizeof ( uri_tests[0] ) ) ; i++ ) {
128 uri_test = &uri_tests[i];
129 rc = test_parse_unparse ( uri_test->base_uri_string );
130 if ( rc != 0 )
131 overall_rc = rc;
132 rc = test_parse_unparse ( uri_test->relative_uri_string );
133 if ( rc != 0 )
134 overall_rc = rc;
135 rc = test_parse_unparse ( uri_test->resolved_uri_string );
136 if ( rc != 0 )
137 overall_rc = rc;
138 rc = test_resolve ( uri_test->base_uri_string,
139 uri_test->relative_uri_string,
140 uri_test->resolved_uri_string );
141 if ( rc != 0 )
142 overall_rc = rc;
145 if ( overall_rc )
146 printf ( "URI tests failed: %s\n", strerror ( overall_rc ) );
147 return overall_rc;