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 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /*
  2. * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * URI self-tests
  27. *
  28. */
  29. /* Forcibly enable assertions */
  30. #undef NDEBUG
  31. #include <string.h>
  32. #include <byteswap.h>
  33. #include <ipxe/uri.h>
  34. #include <ipxe/params.h>
  35. #include <ipxe/test.h>
  36. /** A URI parsing/formatting test */
  37. struct uri_test {
  38. /** URI string */
  39. const char *string;
  40. /** URI */
  41. struct uri uri;
  42. };
  43. /** A URI port number test */
  44. struct uri_port_test {
  45. /** URI string */
  46. const char *string;
  47. /** Default port number */
  48. unsigned int default_port;
  49. /** Expected port number */
  50. unsigned int port;
  51. };
  52. /** A URI or path resolution test */
  53. struct uri_resolve_test {
  54. /** Base path or URI */
  55. const char *base;
  56. /** Relative path or URI */
  57. const char *relative;
  58. /** Expected resolved path or URI */
  59. const char *resolved;
  60. };
  61. /** A TFTP URI test */
  62. struct uri_tftp_test {
  63. /** Next-server address */
  64. struct in_addr next_server;
  65. /** Port number */
  66. unsigned int port;
  67. /** Filename */
  68. const char *filename;
  69. /** URI */
  70. struct uri uri;
  71. /** URI string (for display only; cannot be reparsed) */
  72. const char *string;
  73. };
  74. /** A current working URI test */
  75. struct uri_churi_test {
  76. /** Relative URI */
  77. const char *relative;
  78. /** Expected new working URI */
  79. const char *expected;
  80. };
  81. /** A form parameter URI test list */
  82. struct uri_params_test_list {
  83. /** Key */
  84. const char *key;
  85. /** Value */
  86. const char *value;
  87. };
  88. /** A form parameter URI test */
  89. struct uri_params_test {
  90. /** URI string */
  91. const char *string;
  92. /** URI */
  93. struct uri uri;
  94. /** Parameter list name */
  95. const char *name;
  96. /** Parameter list */
  97. struct uri_params_test_list *list;
  98. };
  99. /**
  100. * Compare two URI component strings
  101. *
  102. * @v first First string, or NULL
  103. * @v second Second string, or NULL
  104. * @v difference Difference
  105. */
  106. static int uristrcmp ( const char *first, const char *second ) {
  107. /* Compare strings, allowing for either to be NULL */
  108. if ( first == second ) {
  109. return 0;
  110. } else if ( ( first == NULL ) || ( second == NULL ) ) {
  111. return -1;
  112. } else {
  113. return strcmp ( first, second );
  114. }
  115. }
  116. /**
  117. * Report URI equality test result
  118. *
  119. * @v uri URI
  120. * @v expected Expected URI
  121. * @v file Test code file
  122. * @v line Test code line
  123. */
  124. static void uri_okx ( struct uri *uri, struct uri *expected, const char *file,
  125. unsigned int line ) {
  126. okx ( uristrcmp ( uri->scheme, expected->scheme ) == 0, file, line );
  127. okx ( uristrcmp ( uri->opaque, expected->opaque ) == 0, file, line );
  128. okx ( uristrcmp ( uri->user, expected->user ) == 0, file, line );
  129. okx ( uristrcmp ( uri->password, expected->password ) == 0, file, line);
  130. okx ( uristrcmp ( uri->host, expected->host ) == 0, file, line );
  131. okx ( uristrcmp ( uri->port, expected->port ) == 0, file, line );
  132. okx ( uristrcmp ( uri->path, expected->path ) == 0, file, line );
  133. okx ( uristrcmp ( uri->query, expected->query ) == 0, file, line );
  134. okx ( uristrcmp ( uri->fragment, expected->fragment ) == 0, file, line);
  135. okx ( uri->params == expected->params, file, line );
  136. }
  137. #define uri_ok( uri, expected ) uri_okx ( uri, expected, __FILE__, __LINE__ )
  138. /**
  139. * Report URI parsing test result
  140. *
  141. * @v test URI test
  142. * @v file Test code file
  143. * @v line Test code line
  144. */
  145. static void uri_parse_okx ( struct uri_test *test, const char *file,
  146. unsigned int line ) {
  147. struct uri *uri;
  148. /* Parse URI */
  149. uri = parse_uri ( test->string );
  150. okx ( uri != NULL, file, line );
  151. if ( uri )
  152. uri_okx ( uri, &test->uri, file, line );
  153. uri_put ( uri );
  154. }
  155. #define uri_parse_ok( test ) uri_parse_okx ( test, __FILE__, __LINE__ )
  156. /**
  157. * Report URI formatting test result
  158. *
  159. * @v test URI test
  160. * @v file Test code file
  161. * @v line Test code line
  162. */
  163. static void uri_format_okx ( struct uri_test *test, const char *file,
  164. unsigned int line ) {
  165. char buf[ strlen ( test->string ) + 1 /* NUL */ ];
  166. char *tmp;
  167. size_t len;
  168. /* Format into fixed-size buffer */
  169. len = format_uri ( &test->uri, buf, sizeof ( buf ) );
  170. okx ( len == ( sizeof ( buf ) - 1 /* NUL */ ), file, line );
  171. okx ( strcmp ( buf, test->string ) == 0, file, line );
  172. /* Format into temporarily allocated buffer */
  173. tmp = format_uri_alloc ( &test->uri );
  174. okx ( tmp != NULL, file, line );
  175. if ( tmp )
  176. okx ( strcmp ( tmp, test->string ) == 0, file, line );
  177. free ( tmp );
  178. }
  179. #define uri_format_ok( test ) uri_format_okx ( test, __FILE__, __LINE__ )
  180. /**
  181. * Report URI duplication test result
  182. *
  183. * @v test URI
  184. * @v file Test code file
  185. * @v line Test code line
  186. */
  187. static void uri_dup_okx ( struct uri *uri, const char *file,
  188. unsigned int line ) {
  189. struct uri *dup;
  190. dup = uri_dup ( uri );
  191. okx ( dup != NULL, file, line );
  192. if ( dup )
  193. uri_okx ( dup, uri, file, line );
  194. uri_put ( dup );
  195. }
  196. #define uri_dup_ok( test ) uri_dup_okx ( test, __FILE__, __LINE__ )
  197. /**
  198. * Report URI combined parsing and formatting test result
  199. *
  200. * @v test URI test
  201. * @v file Test code file
  202. * @v line Test code line
  203. */
  204. static void uri_parse_format_dup_okx ( struct uri_test *test, const char *file,
  205. unsigned int line ) {
  206. uri_parse_okx ( test, file, line );
  207. uri_format_okx ( test, file, line );
  208. uri_dup_okx ( &test->uri, file, line );
  209. }
  210. #define uri_parse_format_dup_ok( test ) \
  211. uri_parse_format_dup_okx ( test, __FILE__, __LINE__ )
  212. /**
  213. * Report URI port number test result
  214. *
  215. * @v test URI port number test
  216. * @v file Test code file
  217. * @v line Test code line
  218. */
  219. static void uri_port_okx ( struct uri_port_test *test, const char *file,
  220. unsigned int line ) {
  221. struct uri *uri;
  222. unsigned int port;
  223. /* Parse URI */
  224. uri = parse_uri ( test->string );
  225. okx ( uri != NULL, file, line );
  226. if ( uri ) {
  227. port = uri_port ( uri, test->default_port );
  228. okx ( port == test->port, file, line );
  229. }
  230. uri_put ( uri );
  231. }
  232. #define uri_port_ok( test ) uri_port_okx ( test, __FILE__, __LINE__ )
  233. /**
  234. * Report URI resolution test result
  235. *
  236. * @v test Path resolution test
  237. * @v file Test code file
  238. * @v line Test code line
  239. */
  240. static void uri_resolve_okx ( struct uri_resolve_test *test,
  241. const char *file, unsigned int line ) {
  242. struct uri *base;
  243. struct uri *relative;
  244. struct uri *resolved = NULL;
  245. char *formatted;
  246. /* Parse URIs */
  247. base = parse_uri ( test->base );
  248. okx ( base != NULL, file, line );
  249. relative = parse_uri ( test->relative );
  250. okx ( relative != NULL, file, line );
  251. /* Resolve URI */
  252. if ( base && relative ) {
  253. resolved = resolve_uri ( base, relative );
  254. okx ( resolved != NULL, file, line );
  255. }
  256. /* Format resolved URI */
  257. formatted = format_uri_alloc ( resolved );
  258. okx ( formatted != NULL, file, line );
  259. /* Check resolved URI */
  260. if ( formatted )
  261. okx ( strcmp ( formatted, test->resolved ) == 0, file, line );
  262. free ( formatted );
  263. uri_put ( resolved );
  264. uri_put ( relative );
  265. uri_put ( base );
  266. }
  267. #define uri_resolve_ok( test ) uri_resolve_okx ( test, __FILE__, __LINE__ )
  268. /**
  269. * Report path resolution test result
  270. *
  271. * @v test Path resolution test
  272. * @v file Test code file
  273. * @v line Test code line
  274. */
  275. static void uri_resolve_path_okx ( struct uri_resolve_test *test,
  276. const char *file, unsigned int line ) {
  277. char *resolved;
  278. /* Resolve paths using resolve_path() directly */
  279. resolved = resolve_path ( test->base, test->relative );
  280. okx ( resolved != NULL, file, line );
  281. if ( resolved )
  282. okx ( strcmp ( resolved, test->resolved ) == 0, file, line );
  283. free ( resolved );
  284. /* Resolve paths as URIs (since all paths are valid URIs) */
  285. uri_resolve_okx ( test, file, line );
  286. }
  287. #define uri_resolve_path_ok( test ) \
  288. uri_resolve_path_okx ( test, __FILE__, __LINE__ )
  289. /**
  290. * Report URI TFTP test result
  291. *
  292. * @v test URI TFTP test
  293. * @v file Test code file
  294. * @v line Test code line
  295. */
  296. static void uri_tftp_okx ( struct uri_tftp_test *test, const char *file,
  297. unsigned int line ) {
  298. char buf[ strlen ( test->string ) + 1 /* NUL */ ];
  299. struct uri *uri;
  300. size_t len;
  301. /* Construct URI */
  302. uri = tftp_uri ( test->next_server, test->port, test->filename );
  303. okx ( uri != NULL, file, line );
  304. if ( uri ) {
  305. uri_okx ( uri, &test->uri, file, line );
  306. len = format_uri ( uri, buf, sizeof ( buf ) );
  307. okx ( len == ( sizeof ( buf ) - 1 /* NUL */ ), file, line );
  308. okx ( strcmp ( buf, test->string ) == 0, file, line );
  309. }
  310. uri_put ( uri );
  311. }
  312. #define uri_tftp_ok( test ) uri_tftp_okx ( test, __FILE__, __LINE__ )
  313. /**
  314. * Report current working URI test result
  315. *
  316. * @v tests List of current working URI tests
  317. * @v file Test code file
  318. * @v line Test code line
  319. */
  320. static void uri_churi_okx ( struct uri_churi_test *test, const char *file,
  321. unsigned int line ) {
  322. struct uri *old_cwuri;
  323. struct uri *uri;
  324. char *formatted;
  325. /* Preserve original current working URI */
  326. old_cwuri = uri_get ( cwuri );
  327. /* Perform sequence of current working URI changes */
  328. do {
  329. /* Parse relative URI */
  330. uri = parse_uri ( test->relative );
  331. okx ( uri != NULL, file, line );
  332. /* Move to this URI */
  333. churi ( uri );
  334. /* Format new current working URI */
  335. formatted = format_uri_alloc ( cwuri );
  336. okx ( formatted != NULL, file, line );
  337. if ( formatted ) {
  338. okx ( strcmp ( formatted, test->expected ) == 0,
  339. file, line );
  340. }
  341. /* Free temporary storage */
  342. free ( formatted );
  343. uri_put ( uri );
  344. /* Move to next current working URI test */
  345. test++;
  346. } while ( test->relative != NULL );
  347. /* Restore original current working URI */
  348. churi ( old_cwuri );
  349. uri_put ( old_cwuri );
  350. }
  351. #define uri_churi_ok( test ) uri_churi_okx ( test, __FILE__, __LINE__ )
  352. /**
  353. * Report form parameter URI test list result
  354. *
  355. * @v test Form parameter URI test
  356. * @v uri URI
  357. * @v file Test code file
  358. * @v line Test code line
  359. */
  360. static void uri_params_list_okx ( struct uri_params_test *test,
  361. struct uri *uri, const char *file,
  362. unsigned int line ) {
  363. struct uri_params_test_list *list;
  364. struct parameter *param;
  365. /* Check URI */
  366. uri_okx ( uri, &test->uri, file, line );
  367. /* Check URI parameters */
  368. okx ( uri->params != NULL, file, line );
  369. if ( uri->params ) {
  370. list = test->list;
  371. for_each_param ( param, uri->params ) {
  372. okx ( strcmp ( param->key, list->key ) == 0,
  373. file, line );
  374. okx ( strcmp ( param->value, list->value ) == 0,
  375. file, line );
  376. list++;
  377. }
  378. okx ( list->key == NULL, file, line );
  379. }
  380. }
  381. #define uri_params_list_ok( test ) \
  382. uri_params_list_okx ( test, __FILE__, __LINE__ )
  383. /**
  384. * Report form parameter URI test result
  385. *
  386. * @v test Form parameter URI test
  387. * @v file Test code file
  388. * @v line Test code line
  389. */
  390. static void uri_params_okx ( struct uri_params_test *test, const char *file,
  391. unsigned int line ) {
  392. struct uri_params_test_list *list;
  393. struct parameters *params;
  394. struct parameter *param;
  395. struct uri *uri;
  396. struct uri *dup;
  397. /* Create parameter list */
  398. params = create_parameters ( test->name );
  399. okx ( params != NULL, file, line );
  400. if ( params ) {
  401. for ( list = test->list ; list->key ; list++ ) {
  402. param = add_parameter ( params, list->key, list->value);
  403. okx ( param != NULL, file, line );
  404. }
  405. }
  406. /* Record parameter list as part of expected URI */
  407. test->uri.params = params;
  408. /* Parse URI */
  409. uri = parse_uri ( test->string );
  410. okx ( uri != NULL, file, line );
  411. if ( uri )
  412. uri_params_list_okx ( test, uri, file, line );
  413. /* Duplicate URI */
  414. dup = uri_dup ( uri );
  415. okx ( dup != NULL, file, line );
  416. if ( dup )
  417. uri_params_list_okx ( test, dup, file, line );
  418. /* Clear parameter list in expected URI */
  419. test->uri.params = NULL;
  420. uri_put ( uri );
  421. uri_put ( dup );
  422. }
  423. #define uri_params_ok( test ) uri_params_okx ( test, __FILE__, __LINE__ )
  424. /** Empty URI */
  425. static struct uri_test uri_empty = {
  426. .string = "",
  427. };
  428. /** Basic HTTP URI */
  429. static struct uri_test uri_boot_ipxe_org = {
  430. "http://boot.ipxe.org/demo/boot.php",
  431. { .scheme = "http", .host = "boot.ipxe.org", .path = "/demo/boot.php" }
  432. };
  433. /** Basic opaque URI */
  434. static struct uri_test uri_mailto = {
  435. "mailto:ipxe-devel@lists.ipxe.org",
  436. { .scheme = "mailto", .opaque = "ipxe-devel@lists.ipxe.org" }
  437. };
  438. /** HTTP URI with all the trimmings */
  439. static struct uri_test uri_http_all = {
  440. "http://anon:password@example.com:3001/~foo/cgi-bin/foo.pl?a=b&c=d#bit",
  441. {
  442. .scheme = "http",
  443. .user = "anon",
  444. .password = "password",
  445. .host = "example.com",
  446. .port = "3001",
  447. .path = "/~foo/cgi-bin/foo.pl",
  448. .query = "a=b&c=d",
  449. .fragment = "bit",
  450. },
  451. };
  452. /** HTTP URI with escaped characters */
  453. static struct uri_test uri_http_escaped = {
  454. "https://test.ipxe.org/wtf%3F%0A?kind%23of/uri%20is#this%3F",
  455. {
  456. .scheme = "https",
  457. .host = "test.ipxe.org",
  458. .path = "/wtf?\n",
  459. .query = "kind#of/uri is",
  460. .fragment = "this?",
  461. },
  462. };
  463. /** HTTP URI with improperly escaped characters */
  464. static struct uri_test uri_http_escaped_improper = {
  465. /* We accept for parsing improperly escaped characters.
  466. * (Formatting the parsed URI would produce the properly
  467. * encoded form, and so would not exactly match the original
  468. * URI string.)
  469. */
  470. "https://test%2eipxe.org/wt%66%3f\n?kind%23of/uri is#this?",
  471. {
  472. .scheme = "https",
  473. .host = "test.ipxe.org",
  474. .path = "/wtf?\n",
  475. .query = "kind#of/uri is",
  476. .fragment = "this?",
  477. },
  478. };
  479. /** IPv6 URI */
  480. static struct uri_test uri_ipv6 = {
  481. "http://[2001:ba8:0:1d4::6950:5845]/",
  482. {
  483. .scheme = "http",
  484. .host = "[2001:ba8:0:1d4::6950:5845]",
  485. .path = "/",
  486. },
  487. };
  488. /** IPv6 URI with port */
  489. static struct uri_test uri_ipv6_port = {
  490. "http://[2001:ba8:0:1d4::6950:5845]:8001/boot",
  491. {
  492. .scheme = "http",
  493. .host = "[2001:ba8:0:1d4::6950:5845]",
  494. .port = "8001",
  495. .path = "/boot",
  496. },
  497. };
  498. /** IPv6 URI with link-local address */
  499. static struct uri_test uri_ipv6_local = {
  500. "http://[fe80::69ff:fe50:5845%25net0]/ipxe",
  501. {
  502. .scheme = "http",
  503. .host = "[fe80::69ff:fe50:5845%net0]",
  504. .path = "/ipxe",
  505. },
  506. };
  507. /** IPv6 URI with link-local address not conforming to RFC 6874 */
  508. static struct uri_test uri_ipv6_local_non_conforming = {
  509. /* We accept for parsing a single "%" in "%net0" (rather than
  510. * the properly encoded form "%25net0"). (Formatting the
  511. * parsed URI would produce the properly encoded form, and so
  512. * would not exactly match the original URI string.)
  513. */
  514. "http://[fe80::69ff:fe50:5845%net0]/ipxe",
  515. {
  516. .scheme = "http",
  517. .host = "[fe80::69ff:fe50:5845%net0]",
  518. .path = "/ipxe",
  519. },
  520. };
  521. /** iSCSI URI */
  522. static struct uri_test uri_iscsi = {
  523. "iscsi:10.253.253.1::::iqn.2010-04.org.ipxe:rabbit",
  524. {
  525. .scheme = "iscsi",
  526. .opaque = "10.253.253.1::::iqn.2010-04.org.ipxe:rabbit",
  527. },
  528. };
  529. /** URI with port number */
  530. static struct uri_port_test uri_explicit_port = {
  531. "http://192.168.0.1:8080/boot.php",
  532. 80,
  533. 8080,
  534. };
  535. /** URI without port number */
  536. static struct uri_port_test uri_default_port = {
  537. "http://192.168.0.1/boot.php",
  538. 80,
  539. 80,
  540. };
  541. /** Simple path resolution test */
  542. static struct uri_resolve_test uri_simple_path = {
  543. "/etc/passwd",
  544. "group",
  545. "/etc/group",
  546. };
  547. /** Path resolution test with "." and ".." elements */
  548. static struct uri_resolve_test uri_relative_path = {
  549. "/var/lib/tftpboot/pxe/pxelinux.0",
  550. "./../ipxe/undionly.kpxe",
  551. "/var/lib/tftpboot/ipxe/undionly.kpxe",
  552. };
  553. /** Path resolution test terminating with directory */
  554. static struct uri_resolve_test uri_directory_path = {
  555. "/test/cgi-bin.pl/boot.ipxe",
  556. "..",
  557. "/test/",
  558. };
  559. /** Path resolution test with excessive ".." elements */
  560. static struct uri_resolve_test uri_excessive_path = {
  561. "/var/lib/tftpboot/ipxe.pxe",
  562. "../../../../../../../foo",
  563. "/foo",
  564. };
  565. /** Path resolution test with absolute path */
  566. static struct uri_resolve_test uri_absolute_path = {
  567. "/var/lib/tftpboot",
  568. "/etc/hostname",
  569. "/etc/hostname",
  570. };
  571. /** Relative URI resolution test */
  572. static struct uri_resolve_test uri_relative = {
  573. "http://boot.ipxe.org/demo/boot.php?vendor=10ec&device=8139",
  574. "initrd.img",
  575. "http://boot.ipxe.org/demo/initrd.img",
  576. };
  577. /** Absolute URI resolution test */
  578. static struct uri_resolve_test uri_absolute = {
  579. "http://boot.ipxe.org/demo/boot.php",
  580. "ftp://192.168.0.1/boot.ipxe",
  581. "ftp://192.168.0.1/boot.ipxe",
  582. };
  583. /** Absolute path URI resolution test */
  584. static struct uri_resolve_test uri_absolute_uri_path = {
  585. "http://boot.ipxe.org/demo/boot.php#test",
  586. "/demo/vmlinuz",
  587. "http://boot.ipxe.org/demo/vmlinuz",
  588. };
  589. /** Query URI resolution test */
  590. static struct uri_resolve_test uri_query = {
  591. "http://10.253.253.1/test.pl?mac=02-00-69-50-58-45",
  592. "?mac=00-1f-16-bc-fe-2f",
  593. "http://10.253.253.1/test.pl?mac=00-1f-16-bc-fe-2f",
  594. };
  595. /** Fragment URI resolution test */
  596. static struct uri_resolve_test uri_fragment = {
  597. "http://192.168.0.254/test#foo",
  598. "#bar",
  599. "http://192.168.0.254/test#bar",
  600. };
  601. /** TFTP URI with absolute path */
  602. static struct uri_tftp_test uri_tftp_absolute = {
  603. { .s_addr = htonl ( 0xc0a80002 ) /* 192.168.0.2 */ }, 0,
  604. "/absolute/path",
  605. {
  606. .scheme = "tftp",
  607. .host = "192.168.0.2",
  608. .path = "/absolute/path",
  609. },
  610. "tftp://192.168.0.2/absolute/path",
  611. };
  612. /** TFTP URI with relative path */
  613. static struct uri_tftp_test uri_tftp_relative = {
  614. { .s_addr = htonl ( 0xc0a80003 ) /* 192.168.0.3 */ }, 0,
  615. "relative/path",
  616. {
  617. .scheme = "tftp",
  618. .host = "192.168.0.3",
  619. .path = "relative/path",
  620. },
  621. "tftp://192.168.0.3/relative/path",
  622. };
  623. /** TFTP URI with path containing special characters */
  624. static struct uri_tftp_test uri_tftp_icky = {
  625. { .s_addr = htonl ( 0x0a000006 ) /* 10.0.0.6 */ }, 0,
  626. "C:\\tftpboot\\icky#path",
  627. {
  628. .scheme = "tftp",
  629. .host = "10.0.0.6",
  630. .path = "C:\\tftpboot\\icky#path",
  631. },
  632. "tftp://10.0.0.6/C%3A\\tftpboot\\icky%23path",
  633. };
  634. /** TFTP URI with custom port */
  635. static struct uri_tftp_test uri_tftp_port = {
  636. { .s_addr = htonl ( 0xc0a80001 ) /* 192.168.0.1 */ }, 4069,
  637. "/another/path",
  638. {
  639. .scheme = "tftp",
  640. .host = "192.168.0.1",
  641. .port = "4069",
  642. .path = "/another/path",
  643. },
  644. "tftp://192.168.0.1:4069/another/path",
  645. };
  646. /** Current working URI test */
  647. static struct uri_churi_test uri_churi[] = {
  648. {
  649. "http://boot.ipxe.org/demo/boot.php",
  650. "http://boot.ipxe.org/demo/boot.php",
  651. },
  652. {
  653. "?vendor=10ec&device=8139",
  654. "http://boot.ipxe.org/demo/boot.php?vendor=10ec&device=8139",
  655. },
  656. {
  657. "fedora/fedora.ipxe",
  658. "http://boot.ipxe.org/demo/fedora/fedora.ipxe",
  659. },
  660. {
  661. "vmlinuz",
  662. "http://boot.ipxe.org/demo/fedora/vmlinuz",
  663. },
  664. {
  665. "http://local/boot/initrd.img",
  666. "http://local/boot/initrd.img",
  667. },
  668. {
  669. "modules/8139too.ko",
  670. "http://local/boot/modules/8139too.ko",
  671. },
  672. {
  673. NULL,
  674. NULL,
  675. }
  676. };
  677. /** Form parameter URI test list */
  678. static struct uri_params_test_list uri_params_list[] = {
  679. {
  680. "vendor",
  681. "10ec",
  682. },
  683. {
  684. "device",
  685. "8139",
  686. },
  687. {
  688. "uuid",
  689. "f59fac00-758f-498f-9fe5-87d790045d94",
  690. },
  691. {
  692. NULL,
  693. NULL,
  694. }
  695. };
  696. /** Form parameter URI test */
  697. static struct uri_params_test uri_params = {
  698. "http://boot.ipxe.org/demo/boot.php##params",
  699. {
  700. .scheme = "http",
  701. .host = "boot.ipxe.org",
  702. .path = "/demo/boot.php",
  703. },
  704. NULL,
  705. uri_params_list,
  706. };
  707. /** Named form parameter URI test list */
  708. static struct uri_params_test_list uri_named_params_list[] = {
  709. {
  710. "mac",
  711. "00:1e:65:80:d3:b6",
  712. },
  713. {
  714. "serial",
  715. "LXTQ20Z1139322762F2000",
  716. },
  717. {
  718. NULL,
  719. NULL,
  720. }
  721. };
  722. /** Named form parameter URI test */
  723. static struct uri_params_test uri_named_params = {
  724. "http://192.168.100.4:3001/register##params=foo",
  725. {
  726. .scheme = "http",
  727. .host = "192.168.100.4",
  728. .port = "3001",
  729. .path = "/register",
  730. },
  731. "foo",
  732. uri_named_params_list,
  733. };
  734. /**
  735. * Perform URI self-test
  736. *
  737. */
  738. static void uri_test_exec ( void ) {
  739. /* URI parsing, formatting, and duplication tests */
  740. uri_parse_format_dup_ok ( &uri_empty );
  741. uri_parse_format_dup_ok ( &uri_boot_ipxe_org );
  742. uri_parse_format_dup_ok ( &uri_mailto );
  743. uri_parse_format_dup_ok ( &uri_http_all );
  744. uri_parse_format_dup_ok ( &uri_http_escaped );
  745. uri_parse_ok ( &uri_http_escaped_improper ); /* Parse only */
  746. uri_parse_format_dup_ok ( &uri_ipv6 );
  747. uri_parse_format_dup_ok ( &uri_ipv6_port );
  748. uri_parse_format_dup_ok ( &uri_ipv6_local );
  749. uri_parse_ok ( &uri_ipv6_local_non_conforming ); /* Parse only */
  750. uri_parse_format_dup_ok ( &uri_iscsi );
  751. /** URI port number tests */
  752. uri_port_ok ( &uri_explicit_port );
  753. uri_port_ok ( &uri_default_port );
  754. /** Path resolution tests */
  755. uri_resolve_path_ok ( &uri_simple_path );
  756. uri_resolve_path_ok ( &uri_relative_path );
  757. uri_resolve_path_ok ( &uri_directory_path );
  758. uri_resolve_path_ok ( &uri_excessive_path );
  759. uri_resolve_path_ok ( &uri_absolute_path );
  760. /** URI resolution tests */
  761. uri_resolve_ok ( &uri_relative );
  762. uri_resolve_ok ( &uri_absolute );
  763. uri_resolve_ok ( &uri_absolute_uri_path );
  764. uri_resolve_ok ( &uri_query );
  765. uri_resolve_ok ( &uri_fragment );
  766. /* TFTP URI construction tests */
  767. uri_tftp_ok ( &uri_tftp_absolute );
  768. uri_tftp_ok ( &uri_tftp_relative );
  769. uri_tftp_ok ( &uri_tftp_icky );
  770. uri_tftp_ok ( &uri_tftp_port );
  771. /* Current working URI tests */
  772. uri_churi_ok ( uri_churi );
  773. /* Form parameter URI tests */
  774. uri_params_ok ( &uri_params );
  775. uri_params_ok ( &uri_named_params );
  776. }
  777. /** URI self-test */
  778. struct self_test uri_test __self_test = {
  779. .name = "uri",
  780. .exec = uri_test_exec,
  781. };