Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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