conftesting.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * @file conftesting.h
  8. * @brief Macro and type declarations for testing
  9. **/
  10. #ifndef TOR_LIB_CONF_CONFTESTING_H
  11. #define TOR_LIB_CONF_CONFTESTING_H
  12. #ifdef TOR_UNIT_TESTS
  13. /**
  14. * Union used when building in test mode typechecking the members of a type
  15. * used with confparse.c. See CONF_CHECK_VAR_TYPE for a description of how
  16. * it is used. */
  17. typedef union {
  18. char **STRING;
  19. char **FILENAME;
  20. int *POSINT; /* yes, this is really an int, and not an unsigned int. For
  21. * historical reasons, many configuration values are restricted
  22. * to the range [0,INT_MAX], and stored in signed ints.
  23. */
  24. uint64_t *UINT64;
  25. int *INT;
  26. int *INTERVAL;
  27. int *MSEC_INTERVAL;
  28. uint64_t *MEMUNIT;
  29. double *DOUBLE;
  30. int *BOOL;
  31. int *AUTOBOOL;
  32. time_t *ISOTIME;
  33. struct smartlist_t **CSV;
  34. int *CSV_INTERVAL;
  35. struct config_line_t **LINELIST;
  36. struct config_line_t **LINELIST_S;
  37. struct config_line_t **LINELIST_V;
  38. // XXXX this doesn't belong at this level of abstraction.
  39. struct routerset_t **ROUTERSET;
  40. } confparse_dummy_values_t;
  41. #endif /* defined(TOR_UNIT_TESTS) */
  42. /* Macros to define extra members inside config_var_t fields, and at the
  43. * end of a list of them.
  44. */
  45. #ifdef TOR_UNIT_TESTS
  46. /* This is a somewhat magic type-checking macro for users of confparse.c.
  47. * It initializes a union member "confparse_dummy_values_t.conftype" with
  48. * the address of a static member "tp_dummy.member". This
  49. * will give a compiler warning unless the member field is of the correct
  50. * type.
  51. *
  52. * (This warning is mandatory, because a type mismatch here violates the type
  53. * compatibility constraint for simple assignment, and requires a diagnostic,
  54. * according to the C spec.)
  55. *
  56. * For example, suppose you say:
  57. * "CONF_CHECK_VAR_TYPE(or_options_t, STRING, Address)".
  58. * Then this macro will evaluate to:
  59. * { .STRING = &or_options_t_dummy.Address }
  60. * And since confparse_dummy_values_t.STRING has type "char **", that
  61. * expression will create a warning unless or_options_t.Address also
  62. * has type "char *".
  63. */
  64. #define CONF_CHECK_VAR_TYPE(tp, conftype, member) \
  65. { . conftype = &tp ## _dummy . member }
  66. #define CONF_TEST_MEMBERS(tp, conftype, member) \
  67. , .var_ptr_dummy=CONF_CHECK_VAR_TYPE(tp, conftype, member)
  68. #define DUMMY_CONF_TEST_MEMBERS , .var_ptr_dummy={ .INT=NULL }
  69. #define DUMMY_TYPECHECK_INSTANCE(tp) \
  70. static tp tp ## _dummy
  71. #else /* !defined(TOR_UNIT_TESTS) */
  72. #define CONF_TEST_MEMBERS(tp, conftype, member)
  73. /* Repeatedly declarable incomplete struct to absorb redundant semicolons */
  74. #define DUMMY_TYPECHECK_INSTANCE(tp) \
  75. struct tor_semicolon_eater
  76. #define DUMMY_CONF_TEST_MEMBERS
  77. #endif /* defined(TOR_UNIT_TESTS) */
  78. #endif /* !defined(TOR_LIB_CONF_CONFTESTING_H) */