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.

settings_test.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * Copyright (C) 2012 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. * Settings self-tests
  27. *
  28. */
  29. /* Forcibly enable assertions */
  30. #undef NDEBUG
  31. #include <string.h>
  32. #include <ipxe/settings.h>
  33. #include <ipxe/test.h>
  34. /** Define inline raw data */
  35. #define RAW(...) { __VA_ARGS__ }
  36. /**
  37. * Report a formatted-store test result
  38. *
  39. * @v _settings Settings block
  40. * @v _setting Setting
  41. * @v _formatted Formatted value
  42. * @v _raw_array Expected raw value
  43. */
  44. #define storef_ok( _settings, _setting, _formatted, _raw_array ) do { \
  45. const uint8_t expected[] = _raw_array; \
  46. uint8_t actual[ sizeof ( expected ) ]; \
  47. int len; \
  48. \
  49. ok ( storef_setting ( _settings, _setting, _formatted ) == 0 ); \
  50. len = fetch_setting ( _settings, _setting, NULL, NULL, actual, \
  51. sizeof ( actual ) ); \
  52. if ( len >= 0 ) { \
  53. DBGC ( _settings, "Stored %s \"%s\", got:\n", \
  54. (_setting)->type->name, _formatted ); \
  55. DBGC_HDA ( _settings, 0, actual, len ); \
  56. } else { \
  57. DBGC ( _settings, "Stored %s \"%s\", got error %s\n", \
  58. (_setting)->type->name, _formatted, \
  59. strerror ( len ) ); \
  60. } \
  61. ok ( len == ( int ) sizeof ( actual ) ); \
  62. ok ( memcmp ( actual, expected, sizeof ( actual ) ) == 0 ); \
  63. } while ( 0 )
  64. /**
  65. * Report a formatted-fetch test result
  66. *
  67. * @v _settings Settings block
  68. * @v _setting Setting
  69. * @v _raw_array Raw value
  70. * @v _formatted Expected formatted value
  71. */
  72. #define fetchf_ok( _settings, _setting, _raw_array, _formatted ) do { \
  73. const uint8_t raw[] = _raw_array; \
  74. char actual[ strlen ( _formatted ) + 1 ]; \
  75. int len; \
  76. \
  77. ok ( store_setting ( _settings, _setting, raw, \
  78. sizeof ( raw ) ) == 0 ); \
  79. len = fetchf_setting ( _settings, _setting, NULL, NULL, actual, \
  80. sizeof ( actual ) ); \
  81. DBGC ( _settings, "Fetched %s \"%s\" from:\n", \
  82. (_setting)->type->name, actual ); \
  83. DBGC_HDA ( _settings, 0, raw, sizeof ( raw ) ); \
  84. ok ( len == ( int ) ( sizeof ( actual ) - 1 ) ); \
  85. ok ( strcmp ( actual, _formatted ) == 0 ); \
  86. } while ( 0 )
  87. /**
  88. * Report a numeric-store test result
  89. *
  90. * @v _settings Settings block
  91. * @v _setting Setting
  92. * @v _numeric Numeric value
  93. * @v _raw_array Expected raw value
  94. */
  95. #define storen_ok( _settings, _setting, _numeric, _raw_array ) do { \
  96. const uint8_t expected[] = _raw_array; \
  97. uint8_t actual[ sizeof ( expected ) ]; \
  98. int len; \
  99. \
  100. ok ( storen_setting ( _settings, _setting, _numeric ) == 0 ); \
  101. len = fetch_setting ( _settings, _setting, NULL, NULL, actual, \
  102. sizeof ( actual ) ); \
  103. if ( len >= 0 ) { \
  104. DBGC ( _settings, "Stored %s %#lx, got:\n", \
  105. (_setting)->type->name, \
  106. ( unsigned long ) _numeric ); \
  107. DBGC_HDA ( _settings, 0, actual, len ); \
  108. } else { \
  109. DBGC ( _settings, "Stored %s %#lx, got error %s\n", \
  110. (_setting)->type->name, \
  111. ( unsigned long ) _numeric, strerror ( len ) ); \
  112. } \
  113. ok ( len == ( int ) sizeof ( actual ) ); \
  114. ok ( memcmp ( actual, expected, sizeof ( actual ) ) == 0 ); \
  115. } while ( 0 )
  116. /**
  117. * Report a numeric-fetch test result
  118. *
  119. * @v _settings Settings block
  120. * @v _setting Setting
  121. * @v _raw_array Raw array
  122. * @v _numeric Expected numeric value
  123. */
  124. #define fetchn_ok( _settings, _setting, _raw_array, _numeric ) do { \
  125. const uint8_t raw[] = _raw_array; \
  126. unsigned long actual; \
  127. \
  128. ok ( store_setting ( _settings, _setting, raw, \
  129. sizeof ( raw ) ) == 0 ); \
  130. ok ( fetchn_setting ( _settings, _setting, NULL, NULL, \
  131. &actual ) == 0 ); \
  132. DBGC ( _settings, "Fetched %s %#lx from:\n", \
  133. (_setting)->type->name, actual ); \
  134. DBGC_HDA ( _settings, 0, raw, sizeof ( raw ) ); \
  135. ok ( actual == ( unsigned long ) _numeric ); \
  136. } while ( 0 )
  137. /** Test generic settings block */
  138. struct generic_settings test_generic_settings = {
  139. .settings = {
  140. .refcnt = NULL,
  141. .siblings =
  142. LIST_HEAD_INIT ( test_generic_settings.settings.siblings ),
  143. .children =
  144. LIST_HEAD_INIT ( test_generic_settings.settings.children ),
  145. .op = &generic_settings_operations,
  146. },
  147. .list = LIST_HEAD_INIT ( test_generic_settings.list ),
  148. };
  149. /** Test settings block */
  150. #define test_settings test_generic_settings.settings
  151. /** Test string setting */
  152. static struct setting test_string_setting = {
  153. .name = "test_string",
  154. .type = &setting_type_string,
  155. };
  156. /** Test URI-encoded string setting */
  157. static struct setting test_uristring_setting = {
  158. .name = "test_uristring",
  159. .type = &setting_type_uristring,
  160. };
  161. /** Test IPv4 address setting type */
  162. static struct setting test_ipv4_setting = {
  163. .name = "test_ipv4",
  164. .type = &setting_type_ipv4,
  165. };
  166. /** Test IPv6 address setting type */
  167. static struct setting test_ipv6_setting = {
  168. .name = "test_ipv6",
  169. .type = &setting_type_ipv6,
  170. };
  171. /** Test signed 8-bit integer setting type */
  172. static struct setting test_int8_setting = {
  173. .name = "test_int8",
  174. .type = &setting_type_int8,
  175. };
  176. /** Test signed 16-bit integer setting type */
  177. static struct setting test_int16_setting = {
  178. .name = "test_int16",
  179. .type = &setting_type_int16,
  180. };
  181. /** Test signed 32-bit integer setting type */
  182. static struct setting test_int32_setting = {
  183. .name = "test_int32",
  184. .type = &setting_type_int32,
  185. };
  186. /** Test unsigned 8-bit integer setting type */
  187. static struct setting test_uint8_setting = {
  188. .name = "test_uint8",
  189. .type = &setting_type_uint8,
  190. };
  191. /** Test unsigned 16-bit integer setting type */
  192. static struct setting test_uint16_setting = {
  193. .name = "test_uint16",
  194. .type = &setting_type_uint16,
  195. };
  196. /** Test unsigned 32-bit integer setting type */
  197. static struct setting test_uint32_setting = {
  198. .name = "test_uint32",
  199. .type = &setting_type_uint32,
  200. };
  201. /** Test colon-separated hex string setting type */
  202. static struct setting test_hex_setting = {
  203. .name = "test_hex",
  204. .type = &setting_type_hex,
  205. };
  206. /** Test hyphen-separated hex string setting type */
  207. static struct setting test_hexhyp_setting = {
  208. .name = "test_hexhyp",
  209. .type = &setting_type_hexhyp,
  210. };
  211. /** Test raw hex string setting type */
  212. static struct setting test_hexraw_setting = {
  213. .name = "test_hexraw",
  214. .type = &setting_type_hexraw,
  215. };
  216. /** Test Base64 setting type */
  217. static struct setting test_base64_setting = {
  218. .name = "test_base64",
  219. .type = &setting_type_base64,
  220. };
  221. /** Test UUID setting type */
  222. static struct setting test_uuid_setting = {
  223. .name = "test_uuid",
  224. .type = &setting_type_uuid,
  225. };
  226. /** Test PCI bus:dev.fn setting type */
  227. static struct setting test_busdevfn_setting = {
  228. .name = "test_busdevfn",
  229. .type = &setting_type_busdevfn,
  230. };
  231. /**
  232. * Perform settings self-tests
  233. *
  234. */
  235. static void settings_test_exec ( void ) {
  236. /* Register test settings block */
  237. ok ( register_settings ( &test_settings, NULL, "test" ) == 0 );
  238. /* "string" setting type */
  239. storef_ok ( &test_settings, &test_string_setting, "hello",
  240. RAW ( 'h', 'e', 'l', 'l', 'o' ) );
  241. fetchf_ok ( &test_settings, &test_string_setting,
  242. RAW ( 'w', 'o', 'r', 'l', 'd' ), "world" );
  243. /* "uristring" setting type */
  244. storef_ok ( &test_settings, &test_uristring_setting, "hello%20world",
  245. RAW ( 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l',
  246. 'd' ) );
  247. fetchf_ok ( &test_settings, &test_uristring_setting,
  248. RAW ( 1, 2, 3, 4, 5 ), "%01%02%03%04%05" );
  249. fetchf_ok ( &test_settings, &test_uristring_setting,
  250. RAW ( 0, ' ', '%', '/', '#', ':', '@', '?', '=', '&' ),
  251. "%00%20%25%2F%23%3A%40%3F%3D%26" );
  252. /* "ipv4" setting type */
  253. storef_ok ( &test_settings, &test_ipv4_setting, "192.168.0.1",
  254. RAW ( 192, 168, 0, 1 ) );
  255. fetchf_ok ( &test_settings, &test_ipv4_setting,
  256. RAW ( 212, 13, 204, 60 ), "212.13.204.60" );
  257. /* "ipv6" setting type */
  258. storef_ok ( &test_settings, &test_ipv6_setting,
  259. "2001:ba8:0:1d4::6950:5845",
  260. RAW ( 0x20, 0x01, 0x0b, 0xa8, 0x00, 0x00, 0x01, 0xd4,
  261. 0x00, 0x00, 0x00, 0x00, 0x69, 0x50, 0x58, 0x45 ) );
  262. fetchf_ok ( &test_settings, &test_ipv6_setting,
  263. RAW ( 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  264. 0x02, 0x0c, 0x29, 0xff, 0xfe, 0xc5, 0x39, 0xa1 ),
  265. "fe80::20c:29ff:fec5:39a1" );
  266. /* Integer setting types (as formatted strings) */
  267. storef_ok ( &test_settings, &test_int8_setting,
  268. "54", RAW ( 54 ) );
  269. storef_ok ( &test_settings, &test_int8_setting,
  270. "0x7f", RAW ( 0x7f ) );
  271. storef_ok ( &test_settings, &test_int8_setting,
  272. "0x1234", RAW ( 0x34 ) );
  273. storef_ok ( &test_settings, &test_int8_setting,
  274. "-32", RAW ( -32 ) );
  275. fetchf_ok ( &test_settings, &test_int8_setting,
  276. RAW ( -9 ), "-9" );
  277. fetchf_ok ( &test_settings, &test_int8_setting,
  278. RAW ( 106 ), "106" );
  279. storef_ok ( &test_settings, &test_uint8_setting,
  280. "129", RAW ( 129 ) );
  281. storef_ok ( &test_settings, &test_uint8_setting,
  282. "0x3421", RAW ( 0x21 ) );
  283. fetchf_ok ( &test_settings, &test_uint8_setting,
  284. RAW ( 0x54 ), "0x54" );
  285. storef_ok ( &test_settings, &test_int16_setting,
  286. "29483", RAW ( 0x73, 0x2b ) );
  287. fetchf_ok ( &test_settings, &test_int16_setting,
  288. RAW ( 0x82, 0x14 ), "-32236" );
  289. fetchf_ok ( &test_settings, &test_int16_setting,
  290. RAW ( 0x12, 0x78 ), "4728" );
  291. storef_ok ( &test_settings, &test_uint16_setting,
  292. "48727", RAW ( 0xbe, 0x57 ) );
  293. fetchf_ok ( &test_settings, &test_uint16_setting,
  294. RAW ( 0x9a, 0x24 ), "0x9a24" );
  295. storef_ok ( &test_settings, &test_int32_setting,
  296. "2901274", RAW ( 0x00, 0x2c, 0x45, 0x1a ) );
  297. fetchf_ok ( &test_settings, &test_int32_setting,
  298. RAW ( 0xff, 0x34, 0x2d, 0xaf ), "-13357649" );
  299. fetchf_ok ( &test_settings, &test_int32_setting,
  300. RAW ( 0x01, 0x00, 0x34, 0xab ), "16790699" );
  301. storef_ok ( &test_settings, &test_uint32_setting,
  302. "0xb598d21", RAW ( 0x0b, 0x59, 0x8d, 0x21 ) );
  303. fetchf_ok ( &test_settings, &test_uint32_setting,
  304. RAW ( 0xf2, 0x37, 0xb2, 0x18 ), "0xf237b218" );
  305. /* Integer setting types (as numeric values) */
  306. storen_ok ( &test_settings, &test_int8_setting,
  307. 72, RAW ( 72 ) );
  308. storen_ok ( &test_settings, &test_int8_setting,
  309. 0xabcd, RAW ( 0xcd ) );
  310. fetchn_ok ( &test_settings, &test_int8_setting,
  311. RAW ( 0xfe ), -2 );
  312. storen_ok ( &test_settings, &test_uint8_setting,
  313. 84, RAW ( 84 ) );
  314. fetchn_ok ( &test_settings, &test_uint8_setting,
  315. RAW ( 0xfe ), 0xfe );
  316. storen_ok ( &test_settings, &test_int16_setting,
  317. 0x87bd, RAW ( 0x87, 0xbd ) );
  318. fetchn_ok ( &test_settings, &test_int16_setting,
  319. RAW ( 0x3d, 0x14 ), 0x3d14 );
  320. fetchn_ok ( &test_settings, &test_int16_setting,
  321. RAW ( 0x80 ), -128 );
  322. storen_ok ( &test_settings, &test_uint16_setting,
  323. 1, RAW ( 0x00, 0x01 ) );
  324. fetchn_ok ( &test_settings, &test_uint16_setting,
  325. RAW ( 0xbd, 0x87 ), 0xbd87 );
  326. fetchn_ok ( &test_settings, &test_uint16_setting,
  327. RAW ( 0x80 ), 0x0080 );
  328. storen_ok ( &test_settings, &test_int32_setting,
  329. 0x0812bfd2, RAW ( 0x08, 0x12, 0xbf, 0xd2 ) );
  330. fetchn_ok ( &test_settings, &test_int32_setting,
  331. RAW ( 0x43, 0x87, 0x91, 0xb4 ), 0x438791b4 );
  332. fetchn_ok ( &test_settings, &test_int32_setting,
  333. RAW ( 0xff, 0xff, 0xfe ), -2 );
  334. storen_ok ( &test_settings, &test_uint32_setting,
  335. 0xb5927ab8, RAW ( 0xb5, 0x92, 0x7a, 0xb8 ) );
  336. fetchn_ok ( &test_settings, &test_uint32_setting,
  337. RAW ( 0x98, 0xab, 0x41, 0x81 ), 0x98ab4181 );
  338. fetchn_ok ( &test_settings, &test_uint32_setting,
  339. RAW ( 0xff, 0xff, 0xfe ), 0x00fffffe );
  340. fetchn_ok ( &test_settings, &test_uint32_setting,
  341. RAW ( 0, 0, 0, 0x12, 0x34, 0x56, 0x78 ), 0x12345678 );
  342. fetchn_ok ( &test_settings, &test_int32_setting,
  343. RAW ( 0, 0, 0, 0x12, 0x34, 0x56, 0x78 ), 0x12345678 );
  344. fetchn_ok ( &test_settings, &test_int32_setting,
  345. RAW ( 0xff, 0xff, 0x87, 0x65, 0x43, 0x21 ), -0x789abcdf );
  346. /* "hex" setting type */
  347. storef_ok ( &test_settings, &test_hex_setting,
  348. "08:12:f5:22:90:1b:4b:47:a8:30:cb:4d:67:4c:d6:76",
  349. RAW ( 0x08, 0x12, 0xf5, 0x22, 0x90, 0x1b, 0x4b, 0x47, 0xa8,
  350. 0x30, 0xcb, 0x4d, 0x67, 0x4c, 0xd6, 0x76 ) );
  351. fetchf_ok ( &test_settings, &test_hex_setting,
  352. RAW ( 0x62, 0xd9, 0xd4, 0xc4, 0x7e, 0x3b, 0x41, 0x46, 0x91,
  353. 0xc6, 0xfd, 0x0c, 0xbf ),
  354. "62:d9:d4:c4:7e:3b:41:46:91:c6:fd:0c:bf" );
  355. /* "hexhyp" setting type */
  356. storef_ok ( &test_settings, &test_hexhyp_setting,
  357. "11-33-22", RAW ( 0x11, 0x33, 0x22 ) );
  358. fetchf_ok ( &test_settings, &test_hexhyp_setting,
  359. RAW ( 0x9f, 0xe5, 0x6d, 0xfb, 0x24, 0x3a, 0x4c, 0xbb, 0xa9,
  360. 0x09, 0x6c, 0x66, 0x13, 0xc1, 0xa8, 0xec, 0x27 ),
  361. "9f-e5-6d-fb-24-3a-4c-bb-a9-09-6c-66-13-c1-a8-ec-27" );
  362. /* "hexraw" setting type */
  363. storef_ok ( &test_settings, &test_hexraw_setting,
  364. "012345abcdef", RAW ( 0x01, 0x23, 0x45, 0xab, 0xcd, 0xef ));
  365. fetchf_ok ( &test_settings, &test_hexraw_setting,
  366. RAW ( 0x9e, 0x4b, 0x6e, 0xef, 0x36, 0xb6, 0x46, 0xfe, 0x8f,
  367. 0x17, 0x06, 0x39, 0x6b, 0xf4, 0x48, 0x4e ),
  368. "9e4b6eef36b646fe8f1706396bf4484e" );
  369. /* "base64" setting type */
  370. storef_ok ( &test_settings, &test_base64_setting,
  371. "cGFzc6\nNwaHJhc2U= ",
  372. RAW ( 0x70, 0x61, 0x73, 0x73, 0xa3, 0x70, 0x68, 0x72, 0x61,
  373. 0x73, 0x65 ) );
  374. fetchf_ok ( &test_settings, &test_base64_setting,
  375. RAW ( 0x80, 0x81, 0x82, 0x83, 0x84, 0x00, 0xff ),
  376. "gIGCg4QA/w==" );
  377. /* "uuid" setting type (no store capability) */
  378. fetchf_ok ( &test_settings, &test_uuid_setting,
  379. RAW ( 0x1a, 0x6a, 0x74, 0x9d, 0x0e, 0xda, 0x46, 0x1a,0xa8,
  380. 0x7a, 0x7c, 0xfe, 0x4f, 0xca, 0x4a, 0x57 ),
  381. "1a6a749d-0eda-461a-a87a-7cfe4fca4a57" );
  382. /* "busdevfn" setting type (no store capability) */
  383. fetchf_ok ( &test_settings, &test_busdevfn_setting,
  384. RAW ( 0x03, 0x45 ), "0000:03:08.5" );
  385. fetchf_ok ( &test_settings, &test_busdevfn_setting,
  386. RAW ( 0x00, 0x02, 0x0a, 0x21 ), "0002:0a:04.1" );
  387. /* Clear and unregister test settings block */
  388. clear_settings ( &test_settings );
  389. unregister_settings ( &test_settings );
  390. }
  391. /** Settings self-test */
  392. struct self_test settings_test __self_test = {
  393. .name = "settings",
  394. .exec = settings_test_exec,
  395. };
  396. /* Include real IPv6 setting type */
  397. REQUIRING_SYMBOL ( settings_test );
  398. REQUIRE_OBJECT ( ipv6 );