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.8KB

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