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

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