blob: 3d8d07c89af6a4b1b127af25468eea7902b03aa2 [file] [log] [blame]
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001
2#ifndef FUZZER_TEST_INIT_H_
3#define FUZZER_TEST_INIT_H_
4
5#include "fuzzer_assert.h"
6#include "test_assert.h"
7
8static_assert(__has_feature(memory_sanitizer) ||
9 __has_feature(address_sanitizer),
10 "sanitizer disabled");
11
12// Utility for test run.
13struct OneTimeTestInit {
14 // Declare trap for the Flatbuffers test engine.
15 // This hook terminate program both in Debug and Release.
16 static bool TestFailListener(const char *expval, const char *val,
17 const char *exp, const char *file, int line,
18 const char *func = 0) {
19 (void)expval;
20 (void)val;
21 (void)exp;
22 (void)file;
23 (void)line;
24 (void)func;
25 // FLATBUFFERS_ASSERT redefined to be fully independent of the Flatbuffers
26 // library implementation (see test_assert.h for details).
27 fuzzer_assert_impl(false); // terminate
28 return false;
29 }
30
31 OneTimeTestInit() : has_locale_(false) {
32 // Fuzzer test should be independent of the test engine implementation.
33 // This hook will terminate test if TEST_EQ/TEST_ASSERT asserted.
34 InitTestEngine(OneTimeTestInit::TestFailListener);
35
36 // Read a locale for the test.
37 if (flatbuffers::ReadEnvironmentVariable("FLATBUFFERS_TEST_LOCALE",
38 &test_locale_)) {
39 TEST_OUTPUT_LINE("The environment variable FLATBUFFERS_TEST_LOCALE=%s",
40 test_locale_.c_str());
41 test_locale_ = flatbuffers::RemoveStringQuotes(test_locale_);
42 has_locale_ = true;
43 }
44 }
45
46 static const char *test_locale() {
47 return one_time_init_.has_locale_ ? nullptr
48 : one_time_init_.test_locale_.c_str();
49 }
50
51 bool has_locale_;
52 std::string test_locale_;
53 static OneTimeTestInit one_time_init_;
54};
55
56#endif // !FUZZER_TEST_INIT_H_