Tests.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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: 2024-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_HPP
  40. #define ZT_TESTS_HPP
  41. #ifdef ZT_ENABLE_TESTS
  42. #ifdef __cplusplus
  43. #include <cstdint>
  44. #include <cstdio>
  45. extern "C" {
  46. #else
  47. #include <stdint.h>
  48. #include <stdio.h>
  49. #endif
  50. #ifndef ZT_T_PRINTF
  51. #define ZT_T_PRINTF(fmt,...) printf((fmt),##__VA_ARGS__),fflush(stdout)
  52. #endif
  53. /**
  54. * Test platform, compiler behavior, utility functions, and core classes
  55. */
  56. const char *ZTT_general();
  57. /**
  58. * Test crypto using test vectors and simple scenarios
  59. *
  60. * This is not an absolutely exhaustive test, just a sanity check to make sure
  61. * crypto routines are basically working.
  62. */
  63. const char *ZTT_crypto();
  64. /**
  65. * Run benchmarks of cryptographic routines and common constructions
  66. */
  67. const char *ZTT_benchmarkCrypto();
  68. #ifdef __cplusplus
  69. }
  70. #endif
  71. #endif // ZT_ENABLE_TESTS
  72. #endif