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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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, 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, actual, \
  76. sizeof ( actual ) ); \
  77. DBGC ( settings, "Fetched %s \"%s\" from:\n", \
  78. (setting)->type->name, formatted ); \
  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. /** Test generic settings block */
  84. struct generic_settings test_generic_settings = {
  85. .settings = {
  86. .refcnt = NULL,
  87. .siblings =
  88. LIST_HEAD_INIT ( test_generic_settings.settings.siblings ),
  89. .children =
  90. LIST_HEAD_INIT ( test_generic_settings.settings.children ),
  91. .op = &generic_settings_operations,
  92. },
  93. .list = LIST_HEAD_INIT ( test_generic_settings.list ),
  94. };
  95. /** Test settings block */
  96. #define test_settings test_generic_settings.settings
  97. /** Test string setting */
  98. static struct setting test_string_setting = {
  99. .name = "test_string",
  100. .type = &setting_type_string,
  101. };
  102. /** Test URI-encoded string setting */
  103. static struct setting test_uristring_setting = {
  104. .name = "test_uristring",
  105. .type = &setting_type_uristring,
  106. };
  107. /** Test IPv4 address setting type */
  108. static struct setting test_ipv4_setting = {
  109. .name = "test_ipv4",
  110. .type = &setting_type_ipv4,
  111. };
  112. /** Test signed 8-bit integer setting type */
  113. static struct setting test_int8_setting = {
  114. .name = "test_int8",
  115. .type = &setting_type_int8,
  116. };
  117. /** Test signed 16-bit integer setting type */
  118. static struct setting test_int16_setting = {
  119. .name = "test_int16",
  120. .type = &setting_type_int16,
  121. };
  122. /** Test signed 32-bit integer setting type */
  123. static struct setting test_int32_setting = {
  124. .name = "test_int32",
  125. .type = &setting_type_int32,
  126. };
  127. /** Test unsigned 8-bit integer setting type */
  128. static struct setting test_uint8_setting = {
  129. .name = "test_uint8",
  130. .type = &setting_type_uint8,
  131. };
  132. /** Test unsigned 16-bit integer setting type */
  133. static struct setting test_uint16_setting = {
  134. .name = "test_uint16",
  135. .type = &setting_type_uint16,
  136. };
  137. /** Test unsigned 32-bit integer setting type */
  138. static struct setting test_uint32_setting = {
  139. .name = "test_uint32",
  140. .type = &setting_type_uint32,
  141. };
  142. /** Test colon-separated hex string setting type */
  143. static struct setting test_hex_setting = {
  144. .name = "test_hex",
  145. .type = &setting_type_hex,
  146. };
  147. /** Test hyphen-separated hex string setting type */
  148. static struct setting test_hexhyp_setting = {
  149. .name = "test_hexhyp",
  150. .type = &setting_type_hexhyp,
  151. };
  152. /** Test raw hex string setting type */
  153. static struct setting test_hexraw_setting = {
  154. .name = "test_hexraw",
  155. .type = &setting_type_hexraw,
  156. };
  157. /** Test UUID setting type */
  158. static struct setting test_uuid_setting = {
  159. .name = "test_uuid",
  160. .type = &setting_type_uuid,
  161. };
  162. /** Test PCI bus:dev.fn setting type */
  163. static struct setting test_busdevfn_setting = {
  164. .name = "test_busdevfn",
  165. .type = &setting_type_busdevfn,
  166. };
  167. /**
  168. * Perform settings self-tests
  169. *
  170. */
  171. static void settings_test_exec ( void ) {
  172. /* Register test settings block */
  173. ok ( register_settings ( &test_settings, NULL, "test" ) == 0 );
  174. /* "string" setting type */
  175. storef_ok ( &test_settings, &test_string_setting, "hello",
  176. RAW ( 'h', 'e', 'l', 'l', 'o' ) );
  177. fetchf_ok ( &test_settings, &test_string_setting,
  178. RAW ( 'w', 'o', 'r', 'l', 'd' ), "world" );
  179. /* "uristring" setting type */
  180. storef_ok ( &test_settings, &test_uristring_setting, "hello%20world",
  181. RAW ( 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l',
  182. 'd' ) );
  183. fetchf_ok ( &test_settings, &test_uristring_setting,
  184. RAW ( 1, 2, 3, 4, 5 ), "%01%02%03%04%05" );
  185. /* "ipv4" setting type */
  186. storef_ok ( &test_settings, &test_ipv4_setting, "192.168.0.1",
  187. RAW ( 192, 168, 0, 1 ) );
  188. fetchf_ok ( &test_settings, &test_ipv4_setting,
  189. RAW ( 212, 13, 204, 60 ), "212.13.204.60" );
  190. /* Integer setting types */
  191. storef_ok ( &test_settings, &test_int8_setting,
  192. "54", RAW ( 54 ) );
  193. storef_ok ( &test_settings, &test_int8_setting,
  194. "0x7f", RAW ( 0x7f ) );
  195. storef_ok ( &test_settings, &test_int8_setting,
  196. "0x1234", RAW ( 0x34 ) );
  197. storef_ok ( &test_settings, &test_int8_setting,
  198. "-32", RAW ( -32 ) );
  199. fetchf_ok ( &test_settings, &test_int8_setting,
  200. RAW ( -9 ), "-9" );
  201. fetchf_ok ( &test_settings, &test_int8_setting,
  202. RAW ( 106 ), "106" );
  203. storef_ok ( &test_settings, &test_uint8_setting,
  204. "129", RAW ( 129 ) );
  205. storef_ok ( &test_settings, &test_uint8_setting,
  206. "0x3421", RAW ( 0x21 ) );
  207. fetchf_ok ( &test_settings, &test_uint8_setting,
  208. RAW ( 0x54 ), "0x54" );
  209. storef_ok ( &test_settings, &test_int16_setting,
  210. "29483", RAW ( 0x73, 0x2b ) );
  211. fetchf_ok ( &test_settings, &test_int16_setting,
  212. RAW ( 0x82, 0x14 ), "-32236" );
  213. fetchf_ok ( &test_settings, &test_int16_setting,
  214. RAW ( 0x12, 0x78 ), "4728" );
  215. storef_ok ( &test_settings, &test_uint16_setting,
  216. "48727", RAW ( 0xbe, 0x57 ) );
  217. fetchf_ok ( &test_settings, &test_uint16_setting,
  218. RAW ( 0x9a, 0x24 ), "0x9a24" );
  219. storef_ok ( &test_settings, &test_int32_setting,
  220. "2901274", RAW ( 0x00, 0x2c, 0x45, 0x1a ) );
  221. fetchf_ok ( &test_settings, &test_int32_setting,
  222. RAW ( 0xff, 0x34, 0x2d, 0xaf ), "-13357649" );
  223. fetchf_ok ( &test_settings, &test_int32_setting,
  224. RAW ( 0x01, 0x00, 0x34, 0xab ), "16790699" );
  225. storef_ok ( &test_settings, &test_uint32_setting,
  226. "0xb598d21", RAW ( 0x0b, 0x59, 0x8d, 0x21 ) );
  227. fetchf_ok ( &test_settings, &test_uint32_setting,
  228. RAW ( 0xf2, 0x37, 0xb2, 0x18 ), "0xf237b218" );
  229. /* "hex" setting type */
  230. storef_ok ( &test_settings, &test_hex_setting,
  231. "08:12:f5:22:90:1b:4b:47:a8:30:cb:4d:67:4c:d6:76",
  232. RAW ( 0x08, 0x12, 0xf5, 0x22, 0x90, 0x1b, 0x4b, 0x47, 0xa8,
  233. 0x30, 0xcb, 0x4d, 0x67, 0x4c, 0xd6, 0x76 ) );
  234. fetchf_ok ( &test_settings, &test_hex_setting,
  235. RAW ( 0x62, 0xd9, 0xd4, 0xc4, 0x7e, 0x3b, 0x41, 0x46, 0x91,
  236. 0xc6, 0xfd, 0x0c, 0xbf ),
  237. "62:d9:d4:c4:7e:3b:41:46:91:c6:fd:0c:bf" );
  238. /* "hexhyp" setting type */
  239. storef_ok ( &test_settings, &test_hexhyp_setting,
  240. "11-33-22", RAW ( 0x11, 0x33, 0x22 ) );
  241. fetchf_ok ( &test_settings, &test_hexhyp_setting,
  242. RAW ( 0x9f, 0xe5, 0x6d, 0xfb, 0x24, 0x3a, 0x4c, 0xbb, 0xa9,
  243. 0x09, 0x6c, 0x66, 0x13, 0xc1, 0xa8, 0xec, 0x27 ),
  244. "9f-e5-6d-fb-24-3a-4c-bb-a9-09-6c-66-13-c1-a8-ec-27" );
  245. /* "hexraw" setting type */
  246. storef_ok ( &test_settings, &test_hexraw_setting,
  247. "012345abcdef", RAW ( 0x01, 0x23, 0x45, 0xab, 0xcd, 0xef ));
  248. fetchf_ok ( &test_settings, &test_hexraw_setting,
  249. RAW ( 0x9e, 0x4b, 0x6e, 0xef, 0x36, 0xb6, 0x46, 0xfe, 0x8f,
  250. 0x17, 0x06, 0x39, 0x6b, 0xf4, 0x48, 0x4e ),
  251. "9e4b6eef36b646fe8f1706396bf4484e" );
  252. /* "uuid" setting type (no store capability) */
  253. fetchf_ok ( &test_settings, &test_uuid_setting,
  254. RAW ( 0x1a, 0x6a, 0x74, 0x9d, 0x0e, 0xda, 0x46, 0x1a,0xa8,
  255. 0x7a, 0x7c, 0xfe, 0x4f, 0xca, 0x4a, 0x57 ),
  256. "1a6a749d-0eda-461a-a87a-7cfe4fca4a57" );
  257. /* "busdevfn" setting type (no store capability) */
  258. fetchf_ok ( &test_settings, &test_busdevfn_setting,
  259. RAW ( 0x03, 0x45 ), "03:08.5" );
  260. /* Clear and unregister test settings block */
  261. clear_settings ( &test_settings );
  262. unregister_settings ( &test_settings );
  263. }
  264. /** Settings self-test */
  265. struct self_test settings_test __self_test = {
  266. .name = "settings",
  267. .exec = settings_test_exec,
  268. };