sanitizer_flags_test.cc revision 219f20b5985371c4cfcef3e6f85fb94e8f3bc0e6
1//===-- sanitizer_flags_test.cc -------------------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file is a part of ThreadSanitizer/AddressSanitizer runtime. 11// 12//===----------------------------------------------------------------------===// 13#include "sanitizer_common/sanitizer_common.h" 14#include "sanitizer_common/sanitizer_flags.h" 15#include "gtest/gtest.h" 16 17#include "tsan_rtl.h" // FIXME: break dependency from TSan runtime. 18using __tsan::ScopedInRtl; 19 20#include <string.h> 21 22namespace __sanitizer { 23 24static const char kFlagName[] = "flag_name"; 25 26template <typename T> 27static void TestFlag(T start_value, const char *env, T final_value) { 28 T flag = start_value; 29 ParseFlag(env, &flag, kFlagName); 30 EXPECT_EQ(final_value, flag); 31} 32 33static void TestStrFlag(const char *start_value, const char *env, 34 const char *final_value) { 35 const char *flag = start_value; 36 ParseFlag(env, &flag, kFlagName); 37 EXPECT_STREQ(final_value, flag); 38} 39 40TEST(SanitizerCommon, BooleanFlags) { 41 ScopedInRtl in_rtl; 42 TestFlag(true, "--flag_name", true); 43 TestFlag(false, "flag_name", false); 44 TestFlag(false, "--flag_name=1", true); 45 TestFlag(true, "asdas flag_name=0 asdas", false); 46 TestFlag(true, " --flag_name=0 ", false); 47 TestFlag(false, "flag_name=yes", true); 48 TestFlag(false, "flag_name=true", true); 49 TestFlag(true, "flag_name=no", false); 50 TestFlag(true, "flag_name=false", false); 51} 52 53TEST(SanitizerCommon, IntFlags) { 54 ScopedInRtl in_rtl; 55 TestFlag(-11, 0, -11); 56 TestFlag(-11, "flag_name", 0); 57 TestFlag(-11, "--flag_name=", 0); 58 TestFlag(-11, "--flag_name=42", 42); 59 TestFlag(-11, "--flag_name=-42", -42); 60} 61 62TEST(SanitizerCommon, StrFlags) { 63 ScopedInRtl in_rtl; 64 TestStrFlag("zzz", 0, "zzz"); 65 TestStrFlag("zzz", "flag_name", ""); 66 TestStrFlag("zzz", "--flag_name=", ""); 67 TestStrFlag("", "--flag_name=abc", "abc"); 68 TestStrFlag("", "--flag_name='abc zxc'", "abc zxc"); 69 TestStrFlag("", "--flag_name=\"abc qwe\" asd", "abc qwe"); 70} 71 72} // namespace __sanitizer 73