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>
TestFlag(T start_value,const char * env,T final_value)25 static void TestFlag(T start_value, const char *env, T final_value) {
26 T flag = start_value;
27 ParseFlag(env, &flag, kFlagName);
28 EXPECT_EQ(final_value, flag);
29 }
30
TestStrFlag(const char * start_value,const char * env,const char * final_value)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);
35 EXPECT_EQ(0, internal_strcmp(final_value, flag));
36 }
37
TEST(SanitizerCommon,BooleanFlags)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
TEST(SanitizerCommon,IntFlags)50 TEST(SanitizerCommon, IntFlags) {
51 TestFlag(-11, 0, -11);
52 TestFlag(-11, "flag_name", 0);
53 TestFlag(-11, "--flag_name=", 0);
54 TestFlag(-11, "--flag_name=42", 42);
55 TestFlag(-11, "--flag_name=-42", -42);
56 }
57
TEST(SanitizerCommon,StrFlags)58 TEST(SanitizerCommon, StrFlags) {
59 TestStrFlag("zzz", 0, "zzz");
60 TestStrFlag("zzz", "flag_name", "");
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 }
67
TestTwoFlags(const char * env,bool expected_flag1,const char * expected_flag2)68 static void TestTwoFlags(const char *env, bool expected_flag1,
69 const char *expected_flag2) {
70 bool flag1 = !expected_flag1;
71 const char *flag2 = "";
72 ParseFlag(env, &flag1, "flag1");
73 ParseFlag(env, &flag2, "flag2");
74 EXPECT_EQ(expected_flag1, flag1);
75 EXPECT_EQ(0, internal_strcmp(flag2, expected_flag2));
76 }
77
TEST(SanitizerCommon,MultipleFlags)78 TEST(SanitizerCommon, MultipleFlags) {
79 TestTwoFlags("flag1=1 flag2='zzz'", true, "zzz");
80 TestTwoFlags("flag2='qxx' flag1=0", false, "qxx");
81 TestTwoFlags("flag1=false:flag2='zzz'", false, "zzz");
82 TestTwoFlags("flag2=qxx:flag1=yes", true, "qxx");
83 }
84
85 } // namespace __sanitizer
86