|
|
1.1 ! root 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: ! 8: #define URI_MAX_LEN 1024 ! 9: ! 10: struct uri_test { ! 11: const char *base_uri_string; ! 12: const char *relative_uri_string; ! 13: const char *resolved_uri_string; ! 14: }; ! 15: ! 16: static struct uri_test uri_tests[] = { ! 17: { "http://www.fensystems.co.uk", "", ! 18: "http://www.fensystems.co.uk/" }, ! 19: { "http://ipxe.org/wiki/page1", "page2", ! 20: "http://ipxe.org/wiki/page2" }, ! 21: { "http://ipxe.org/wiki/page1", "../page3", ! 22: "http://ipxe.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:[email protected]:8080/path/to?a=b#cd" }, ! 28: #if 0 ! 29: "http://www.ipxe.org/wiki", ! 30: "mailto:[email protected]", ! 31: "ftp://joe:[email protected]:8081/hidden/path/to?what=is#this", ! 32: #endif ! 33: }; ! 34: ! 35: static int test_parse_unparse ( const char *uri_string ) { ! 36: char buf[URI_MAX_LEN]; ! 37: struct uri *uri = NULL; ! 38: int rc; ! 39: ! 40: /* Parse and unparse URI */ ! 41: uri = parse_uri ( uri_string ); ! 42: if ( ! uri ) { ! 43: rc = -ENOMEM; ! 44: goto done; ! 45: } ! 46: unparse_uri ( buf, sizeof ( buf ), uri, URI_ALL ); ! 47: ! 48: /* Compare result */ ! 49: if ( strcmp ( buf, uri_string ) != 0 ) { ! 50: printf ( "Unparse of \"%s\" produced \"%s\"\n", ! 51: uri_string, buf ); ! 52: rc = -EINVAL; ! 53: goto done; ! 54: } ! 55: ! 56: rc = 0; ! 57: ! 58: done: ! 59: uri_put ( uri ); ! 60: if ( rc ) { ! 61: printf ( "URI parse-unparse of \"%s\" failed: %s\n", ! 62: uri_string, strerror ( rc ) ); ! 63: } ! 64: return rc; ! 65: } ! 66: ! 67: static int test_resolve ( const char *base_uri_string, ! 68: const char *relative_uri_string, ! 69: const char *resolved_uri_string ) { ! 70: struct uri *base_uri = NULL; ! 71: struct uri *relative_uri = NULL; ! 72: struct uri *resolved_uri = NULL; ! 73: char buf[URI_MAX_LEN]; ! 74: int rc; ! 75: ! 76: /* Parse URIs */ ! 77: base_uri = parse_uri ( base_uri_string ); ! 78: if ( ! base_uri ) { ! 79: rc = -ENOMEM; ! 80: goto done; ! 81: } ! 82: relative_uri = parse_uri ( relative_uri_string ); ! 83: if ( ! relative_uri ) { ! 84: rc = -ENOMEM; ! 85: goto done; ! 86: } ! 87: ! 88: /* Resolve URI */ ! 89: resolved_uri = resolve_uri ( base_uri, relative_uri ); ! 90: if ( ! resolved_uri ) { ! 91: rc = -ENOMEM; ! 92: goto done; ! 93: } ! 94: ! 95: /* Compare result */ ! 96: unparse_uri ( buf, sizeof ( buf ), resolved_uri, URI_ALL ); ! 97: if ( strcmp ( buf, resolved_uri_string ) != 0 ) { ! 98: printf ( "Resolution of \"%s\"+\"%s\" produced \"%s\"\n", ! 99: base_uri_string, relative_uri_string, buf ); ! 100: rc = -EINVAL; ! 101: goto done; ! 102: } ! 103: ! 104: rc = 0; ! 105: ! 106: done: ! 107: uri_put ( base_uri ); ! 108: uri_put ( relative_uri ); ! 109: uri_put ( resolved_uri ); ! 110: if ( rc ) { ! 111: printf ( "URI resolution of \"%s\"+\"%s\" failed: %s\n", ! 112: base_uri_string, relative_uri_string, ! 113: strerror ( rc ) ); ! 114: } ! 115: return rc; ! 116: } ! 117: ! 118: int uri_test ( void ) { ! 119: unsigned int i; ! 120: struct uri_test *uri_test; ! 121: int rc; ! 122: int overall_rc = 0; ! 123: ! 124: for ( i = 0 ; i < ( sizeof ( uri_tests ) / ! 125: sizeof ( uri_tests[0] ) ) ; i++ ) { ! 126: uri_test = &uri_tests[i]; ! 127: rc = test_parse_unparse ( uri_test->base_uri_string ); ! 128: if ( rc != 0 ) ! 129: overall_rc = rc; ! 130: rc = test_parse_unparse ( uri_test->relative_uri_string ); ! 131: if ( rc != 0 ) ! 132: overall_rc = rc; ! 133: rc = test_parse_unparse ( uri_test->resolved_uri_string ); ! 134: if ( rc != 0 ) ! 135: overall_rc = rc; ! 136: rc = test_resolve ( uri_test->base_uri_string, ! 137: uri_test->relative_uri_string, ! 138: uri_test->resolved_uri_string ); ! 139: if ( rc != 0 ) ! 140: overall_rc = rc; ! 141: } ! 142: ! 143: if ( overall_rc ) ! 144: printf ( "URI tests failed: %s\n", strerror ( overall_rc ) ); ! 145: return overall_rc; ! 146: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.