Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

settings_test.c 13KB

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