Tests.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. /*
  14. * This header and its implementation in Tests.cpp contain assertion tests,
  15. * self-tests, cryptographic tests, and fuzzing for the ZeroTier core.
  16. *
  17. * To build these ensure that ZT_ENABLE_TESTS is defined during build time.
  18. * Otherwise they are omitted.
  19. *
  20. * The macro ZT_STANDALONE_TESTS will also build a main() function for these
  21. * tests for creating a stand-alone test executable. It will return zero if
  22. * all tests pass and non-zero if at least one test fails.
  23. *
  24. * These symbols are defined extern "C" so tests can be called from regular
  25. * C code, which is important for use via CGo or in plain C projects.
  26. *
  27. * The ZT_T_PRINTF macro defaults to printf() but if it's defined at compile
  28. * time (it must be set while building Tests.cpp) it can specify another
  29. * function to use for output. Defining it to a no-op can be used to disable
  30. * output.
  31. *
  32. * Each test function returns NULL if the tests succeeds or an error message
  33. * on test failure.
  34. *
  35. * Be aware that fuzzing tests can and will crash the program if a serious
  36. * error is discovered. This is the point. It's also beneficial to run these
  37. * in "valgrind" or a similar tool to detect marginal bad behvaior.
  38. */
  39. #ifndef ZT_TESTS_H
  40. #define ZT_TESTS_H
  41. #ifdef ZT_ENABLE_TESTS
  42. #include <stdint.h>
  43. #include <stdio.h>
  44. #ifndef ZT_T_PRINTF
  45. #define ZT_T_PRINTF(fmt,...) printf((fmt),##__VA_ARGS__),fflush(stdout)
  46. #endif
  47. #ifdef __cplusplus
  48. extern "C" {
  49. #endif
  50. /**
  51. * Test platform, compiler behavior, utility functions, and core classes
  52. */
  53. const char *ZTT_general();
  54. /**
  55. * Test crypto using test vectors and simple scenarios
  56. *
  57. * This is not an absolutely exhaustive test, just a sanity check to make sure
  58. * crypto routines are basically working.
  59. */
  60. const char *ZTT_crypto();
  61. /**
  62. * Run benchmarks of cryptographic routines and common constructions
  63. */
  64. const char *ZTT_benchmarkCrypto();
  65. #ifdef __cplusplus
  66. }
  67. #endif
  68. #endif // ZT_ENABLE_TESTS
  69. #endif