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

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