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.

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