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 "sanitizer_common/sanitizer_libc.h" 16 #include "gtest/gtest.h" 17 18 #include <string.h> 19 20 namespace __sanitizer { 21 22 static const char kFlagName[] = "flag_name"; 23 24 template <typename T> 25 static void TestFlag(T start_value, const char *env, T final_value) { 26 T flag = start_value; 27 ParseFlag(env, &flag, kFlagName, "flag description"); 28 EXPECT_EQ(final_value, flag); 29 } 30 31 static void TestStrFlag(const char *start_value, const char *env, 32 const char *final_value) { 33 const char *flag = start_value; 34 ParseFlag(env, &flag, kFlagName, "flag description"); 35 EXPECT_EQ(0, internal_strcmp(final_value, flag)); 36 } 37 38 TEST(SanitizerCommon, BooleanFlags) { 39 TestFlag(true, "--flag_name", true); 40 TestFlag(false, "flag_name", false); 41 TestFlag(false, "--flag_name=1", true); 42 TestFlag(true, "asdas flag_name=0 asdas", false); 43 TestFlag(true, " --flag_name=0 ", false); 44 TestFlag(false, "flag_name=yes", true); 45 TestFlag(false, "flag_name=true", true); 46 TestFlag(true, "flag_name=no", false); 47 TestFlag(true, "flag_name=false", false); 48 } 49 50 TEST(SanitizerCommon, IntFlags) { 51 TestFlag(-11, 0, -11); 52 TestFlag(-11, "flag_name", -11); 53 TestFlag(-11, "--flag_name=", 0); 54 TestFlag(-11, "--flag_name=42", 42); 55 TestFlag(-11, "--flag_name=-42", -42); 56 } 57 58 TEST(SanitizerCommon, StrFlags) { 59 TestStrFlag("zzz", 0, "zzz"); 60 TestStrFlag("zzz", "flag_name", "zzz"); 61 TestStrFlag("zzz", "--flag_name=", ""); 62 TestStrFlag("", "--flag_name=abc", "abc"); 63 TestStrFlag("", "--flag_name='abc zxc'", "abc zxc"); 64 TestStrFlag("", "--flag_name='abc zxcc'", "abc zxcc"); 65 TestStrFlag("", "--flag_name=\"abc qwe\" asd", "abc qwe"); 66 TestStrFlag("", "other_flag_name=zzz", ""); 67 } 68 69 static void TestTwoFlags(const char *env, bool expected_flag1, 70 const char *expected_flag2) { 71 bool flag1 = !expected_flag1; 72 const char *flag2 = ""; 73 ParseFlag(env, &flag1, "flag1", "flag1 description"); 74 ParseFlag(env, &flag2, "flag2", "flag2 description"); 75 EXPECT_EQ(expected_flag1, flag1); 76 EXPECT_EQ(0, internal_strcmp(flag2, expected_flag2)); 77 } 78 79 TEST(SanitizerCommon, MultipleFlags) { 80 TestTwoFlags("flag1=1 flag2='zzz'", true, "zzz"); 81 TestTwoFlags("flag2='qxx' flag1=0", false, "qxx"); 82 TestTwoFlags("flag1=false:flag2='zzz'", false, "zzz"); 83 TestTwoFlags("flag2=qxx:flag1=yes", true, "qxx"); 84 } 85 86 } // namespace __sanitizer 87