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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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. /** Basic path-only URI */
  443. static struct uri_test uri_path = {
  444. "/var/lib/tftpboot/pxelinux.0",
  445. { .path = "/var/lib/tftpboot/pxelinux.0" },
  446. };
  447. /** Path-only URI with escaped characters */
  448. static struct uri_test uri_path_escaped = {
  449. "/hello%20world%3F",
  450. { .path = "/hello world?" },
  451. };
  452. /** HTTP URI with all the trimmings */
  453. static struct uri_test uri_http_all = {
  454. "http://anon:password@example.com:3001/~foo/cgi-bin/foo.pl?a=b&c=d#bit",
  455. {
  456. .scheme = "http",
  457. .user = "anon",
  458. .password = "password",
  459. .host = "example.com",
  460. .port = "3001",
  461. .path = "/~foo/cgi-bin/foo.pl",
  462. .query = "a=b&c=d",
  463. .fragment = "bit",
  464. },
  465. };
  466. /** HTTP URI with escaped characters */
  467. static struct uri_test uri_http_escaped = {
  468. "https://test.ipxe.org/wtf%3F%0A?kind%23of/uri%20is#this%3F",
  469. {
  470. .scheme = "https",
  471. .host = "test.ipxe.org",
  472. .path = "/wtf?\n",
  473. .query = "kind#of/uri is",
  474. .fragment = "this?",
  475. },
  476. };
  477. /** HTTP URI with improperly escaped characters */
  478. static struct uri_test uri_http_escaped_improper = {
  479. /* We accept for parsing improperly escaped characters.
  480. * (Formatting the parsed URI would produce the properly
  481. * encoded form, and so would not exactly match the original
  482. * URI string.)
  483. */
  484. "https://test%2eipxe.org/wt%66%3f\n?kind%23of/uri is#this?",
  485. {
  486. .scheme = "https",
  487. .host = "test.ipxe.org",
  488. .path = "/wtf?\n",
  489. .query = "kind#of/uri is",
  490. .fragment = "this?",
  491. },
  492. };
  493. /** IPv6 URI */
  494. static struct uri_test uri_ipv6 = {
  495. "http://[2001:ba8:0:1d4::6950:5845]/",
  496. {
  497. .scheme = "http",
  498. .host = "[2001:ba8:0:1d4::6950:5845]",
  499. .path = "/",
  500. },
  501. };
  502. /** IPv6 URI with port */
  503. static struct uri_test uri_ipv6_port = {
  504. "http://[2001:ba8:0:1d4::6950:5845]:8001/boot",
  505. {
  506. .scheme = "http",
  507. .host = "[2001:ba8:0:1d4::6950:5845]",
  508. .port = "8001",
  509. .path = "/boot",
  510. },
  511. };
  512. /** IPv6 URI with link-local address */
  513. static struct uri_test uri_ipv6_local = {
  514. "http://[fe80::69ff:fe50:5845%25net0]/ipxe",
  515. {
  516. .scheme = "http",
  517. .host = "[fe80::69ff:fe50:5845%net0]",
  518. .path = "/ipxe",
  519. },
  520. };
  521. /** IPv6 URI with link-local address not conforming to RFC 6874 */
  522. static struct uri_test uri_ipv6_local_non_conforming = {
  523. /* We accept for parsing a single "%" in "%net0" (rather than
  524. * the properly encoded form "%25net0"). (Formatting the
  525. * parsed URI would produce the properly encoded form, and so
  526. * would not exactly match the original URI string.)
  527. */
  528. "http://[fe80::69ff:fe50:5845%net0]/ipxe",
  529. {
  530. .scheme = "http",
  531. .host = "[fe80::69ff:fe50:5845%net0]",
  532. .path = "/ipxe",
  533. },
  534. };
  535. /** iSCSI URI */
  536. static struct uri_test uri_iscsi = {
  537. "iscsi:10.253.253.1::::iqn.2010-04.org.ipxe:rabbit",
  538. {
  539. .scheme = "iscsi",
  540. .opaque = "10.253.253.1::::iqn.2010-04.org.ipxe:rabbit",
  541. },
  542. };
  543. /** File URI with relative (opaque) path */
  544. static struct uri_test uri_file_relative = {
  545. "file:boot/script.ipxe",
  546. {
  547. .scheme = "file",
  548. .opaque = "boot/script.ipxe",
  549. },
  550. };
  551. /** File URI with absolute path */
  552. static struct uri_test uri_file_absolute = {
  553. "file:/boot/script.ipxe",
  554. {
  555. .scheme = "file",
  556. .path = "/boot/script.ipxe",
  557. },
  558. };
  559. /** File URI with volume name */
  560. static struct uri_test uri_file_volume = {
  561. "file://hpilo/boot/script.ipxe",
  562. {
  563. .scheme = "file",
  564. .host = "hpilo",
  565. .path = "/boot/script.ipxe",
  566. },
  567. };
  568. /** URI with port number */
  569. static struct uri_port_test uri_explicit_port = {
  570. "http://192.168.0.1:8080/boot.php",
  571. 80,
  572. 8080,
  573. };
  574. /** URI without port number */
  575. static struct uri_port_test uri_default_port = {
  576. "http://192.168.0.1/boot.php",
  577. 80,
  578. 80,
  579. };
  580. /** Simple path resolution test */
  581. static struct uri_resolve_test uri_simple_path = {
  582. "/etc/passwd",
  583. "group",
  584. "/etc/group",
  585. };
  586. /** Path resolution test with "." and ".." elements */
  587. static struct uri_resolve_test uri_relative_path = {
  588. "/var/lib/tftpboot/pxe/pxelinux.0",
  589. "./../ipxe/undionly.kpxe",
  590. "/var/lib/tftpboot/ipxe/undionly.kpxe",
  591. };
  592. /** Path resolution test terminating with directory */
  593. static struct uri_resolve_test uri_directory_path = {
  594. "/test/cgi-bin.pl/boot.ipxe",
  595. "..",
  596. "/test/",
  597. };
  598. /** Path resolution test with excessive ".." elements */
  599. static struct uri_resolve_test uri_excessive_path = {
  600. "/var/lib/tftpboot/ipxe.pxe",
  601. "../../../../../../../foo",
  602. "/foo",
  603. };
  604. /** Path resolution test with absolute path */
  605. static struct uri_resolve_test uri_absolute_path = {
  606. "/var/lib/tftpboot",
  607. "/etc/hostname",
  608. "/etc/hostname",
  609. };
  610. /** Relative URI resolution test */
  611. static struct uri_resolve_test uri_relative = {
  612. "http://boot.ipxe.org/demo/boot.php?vendor=10ec&device=8139",
  613. "initrd.img",
  614. "http://boot.ipxe.org/demo/initrd.img",
  615. };
  616. /** Absolute URI resolution test */
  617. static struct uri_resolve_test uri_absolute = {
  618. "http://boot.ipxe.org/demo/boot.php",
  619. "ftp://192.168.0.1/boot.ipxe",
  620. "ftp://192.168.0.1/boot.ipxe",
  621. };
  622. /** Absolute path URI resolution test */
  623. static struct uri_resolve_test uri_absolute_uri_path = {
  624. "http://boot.ipxe.org/demo/boot.php#test",
  625. "/demo/vmlinuz",
  626. "http://boot.ipxe.org/demo/vmlinuz",
  627. };
  628. /** Query URI resolution test */
  629. static struct uri_resolve_test uri_query = {
  630. "http://10.253.253.1/test.pl?mac=02-00-69-50-58-45",
  631. "?mac=00-1f-16-bc-fe-2f",
  632. "http://10.253.253.1/test.pl?mac=00-1f-16-bc-fe-2f",
  633. };
  634. /** Fragment URI resolution test */
  635. static struct uri_resolve_test uri_fragment = {
  636. "http://192.168.0.254/test#foo",
  637. "#bar",
  638. "http://192.168.0.254/test#bar",
  639. };
  640. /** PXE URI with absolute URI */
  641. static struct uri_pxe_test uri_pxe_absolute = {
  642. {
  643. /* 192.168.0.3 */
  644. .sin = {
  645. .sin_family = AF_INET,
  646. .sin_addr = { .s_addr = htonl ( 0xc0a80003 ) },
  647. },
  648. },
  649. "http://not.a.tftp/uri",
  650. {
  651. .scheme = "http",
  652. .host = "not.a.tftp",
  653. .path = "/uri",
  654. },
  655. "http://not.a.tftp/uri",
  656. };
  657. /** PXE URI with absolute path */
  658. static struct uri_pxe_test uri_pxe_absolute_path = {
  659. {
  660. /* 192.168.0.2 */
  661. .sin = {
  662. .sin_family = AF_INET,
  663. .sin_addr = { .s_addr = htonl ( 0xc0a80002 ) },
  664. },
  665. },
  666. "/absolute/path",
  667. {
  668. .scheme = "tftp",
  669. .host = "192.168.0.2",
  670. .path = "//absolute/path",
  671. },
  672. "tftp://192.168.0.2//absolute/path",
  673. };
  674. /** PXE URI with relative path */
  675. static struct uri_pxe_test uri_pxe_relative_path = {
  676. {
  677. /* 192.168.0.3 */
  678. .sin = {
  679. .sin_family = AF_INET,
  680. .sin_addr = { .s_addr = htonl ( 0xc0a80003 ) },
  681. },
  682. },
  683. "relative/path",
  684. {
  685. .scheme = "tftp",
  686. .host = "192.168.0.3",
  687. .path = "/relative/path",
  688. },
  689. "tftp://192.168.0.3/relative/path",
  690. };
  691. /** PXE URI with path containing special characters */
  692. static struct uri_pxe_test uri_pxe_icky = {
  693. {
  694. /* 10.0.0.6 */
  695. .sin = {
  696. .sin_family = AF_INET,
  697. .sin_addr = { .s_addr = htonl ( 0x0a000006 ) },
  698. },
  699. },
  700. "C:\\tftpboot\\icky#path",
  701. {
  702. .scheme = "tftp",
  703. .host = "10.0.0.6",
  704. .path = "/C:\\tftpboot\\icky#path",
  705. },
  706. "tftp://10.0.0.6/C%3A\\tftpboot\\icky%23path",
  707. };
  708. /** PXE URI with custom port */
  709. static struct uri_pxe_test uri_pxe_port = {
  710. {
  711. /* 192.168.0.1:4069 */
  712. .sin = {
  713. .sin_family = AF_INET,
  714. .sin_addr = { .s_addr = htonl ( 0xc0a80001 ) },
  715. .sin_port = htons ( 4069 ),
  716. },
  717. },
  718. "/another/path",
  719. {
  720. .scheme = "tftp",
  721. .host = "192.168.0.1",
  722. .port = "4069",
  723. .path = "//another/path",
  724. },
  725. "tftp://192.168.0.1:4069//another/path",
  726. };
  727. /** Current working URI test */
  728. static struct uri_churi_test uri_churi[] = {
  729. {
  730. "http://boot.ipxe.org/demo/boot.php",
  731. "http://boot.ipxe.org/demo/boot.php",
  732. },
  733. {
  734. "?vendor=10ec&device=8139",
  735. "http://boot.ipxe.org/demo/boot.php?vendor=10ec&device=8139",
  736. },
  737. {
  738. "fedora/fedora.ipxe",
  739. "http://boot.ipxe.org/demo/fedora/fedora.ipxe",
  740. },
  741. {
  742. "vmlinuz",
  743. "http://boot.ipxe.org/demo/fedora/vmlinuz",
  744. },
  745. {
  746. "http://local/boot/initrd.img",
  747. "http://local/boot/initrd.img",
  748. },
  749. {
  750. "modules/8139too.ko",
  751. "http://local/boot/modules/8139too.ko",
  752. },
  753. {
  754. NULL,
  755. NULL,
  756. }
  757. };
  758. /** Form parameter URI test list */
  759. static struct uri_params_test_list uri_params_list[] = {
  760. {
  761. "vendor",
  762. "10ec",
  763. },
  764. {
  765. "device",
  766. "8139",
  767. },
  768. {
  769. "uuid",
  770. "f59fac00-758f-498f-9fe5-87d790045d94",
  771. },
  772. {
  773. NULL,
  774. NULL,
  775. }
  776. };
  777. /** Form parameter URI test */
  778. static struct uri_params_test uri_params = {
  779. "http://boot.ipxe.org/demo/boot.php##params",
  780. {
  781. .scheme = "http",
  782. .host = "boot.ipxe.org",
  783. .path = "/demo/boot.php",
  784. },
  785. NULL,
  786. uri_params_list,
  787. };
  788. /** Named form parameter URI test list */
  789. static struct uri_params_test_list uri_named_params_list[] = {
  790. {
  791. "mac",
  792. "00:1e:65:80:d3:b6",
  793. },
  794. {
  795. "serial",
  796. "LXTQ20Z1139322762F2000",
  797. },
  798. {
  799. NULL,
  800. NULL,
  801. }
  802. };
  803. /** Named form parameter URI test */
  804. static struct uri_params_test uri_named_params = {
  805. "http://192.168.100.4:3001/register##params=foo",
  806. {
  807. .scheme = "http",
  808. .host = "192.168.100.4",
  809. .port = "3001",
  810. .path = "/register",
  811. },
  812. "foo",
  813. uri_named_params_list,
  814. };
  815. /**
  816. * Perform URI self-test
  817. *
  818. */
  819. static void uri_test_exec ( void ) {
  820. /* URI parsing, formatting, and duplication tests */
  821. uri_parse_format_dup_ok ( &uri_empty );
  822. uri_parse_format_dup_ok ( &uri_boot_ipxe_org );
  823. uri_parse_format_dup_ok ( &uri_mailto );
  824. uri_parse_format_dup_ok ( &uri_path );
  825. uri_parse_format_dup_ok ( &uri_path_escaped );
  826. uri_parse_format_dup_ok ( &uri_http_all );
  827. uri_parse_format_dup_ok ( &uri_http_escaped );
  828. uri_parse_ok ( &uri_http_escaped_improper ); /* Parse only */
  829. uri_parse_format_dup_ok ( &uri_ipv6 );
  830. uri_parse_format_dup_ok ( &uri_ipv6_port );
  831. uri_parse_format_dup_ok ( &uri_ipv6_local );
  832. uri_parse_ok ( &uri_ipv6_local_non_conforming ); /* Parse only */
  833. uri_parse_format_dup_ok ( &uri_iscsi );
  834. uri_parse_format_dup_ok ( &uri_file_relative );
  835. uri_parse_format_dup_ok ( &uri_file_absolute );
  836. uri_parse_format_dup_ok ( &uri_file_volume );
  837. /** URI port number tests */
  838. uri_port_ok ( &uri_explicit_port );
  839. uri_port_ok ( &uri_default_port );
  840. /** Path resolution tests */
  841. uri_resolve_path_ok ( &uri_simple_path );
  842. uri_resolve_path_ok ( &uri_relative_path );
  843. uri_resolve_path_ok ( &uri_directory_path );
  844. uri_resolve_path_ok ( &uri_excessive_path );
  845. uri_resolve_path_ok ( &uri_absolute_path );
  846. /** URI resolution tests */
  847. uri_resolve_ok ( &uri_relative );
  848. uri_resolve_ok ( &uri_absolute );
  849. uri_resolve_ok ( &uri_absolute_uri_path );
  850. uri_resolve_ok ( &uri_query );
  851. uri_resolve_ok ( &uri_fragment );
  852. /* PXE URI construction tests */
  853. uri_pxe_ok ( &uri_pxe_absolute );
  854. uri_pxe_ok ( &uri_pxe_absolute_path );
  855. uri_pxe_ok ( &uri_pxe_relative_path );
  856. uri_pxe_ok ( &uri_pxe_icky );
  857. uri_pxe_ok ( &uri_pxe_port );
  858. /* Current working URI tests */
  859. uri_churi_ok ( uri_churi );
  860. /* Form parameter URI tests */
  861. uri_params_ok ( &uri_params );
  862. uri_params_ok ( &uri_named_params );
  863. }
  864. /** URI self-test */
  865. struct self_test uri_test __self_test = {
  866. .name = "uri",
  867. .exec = uri_test_exec,
  868. };