reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
//===-- flags_test.cpp ------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "flags.h"
#include "flags_parser.h"

#include "gtest/gtest.h"

#include <string.h>

static const char FlagName[] = "flag_name";
static const char FlagDesc[] = "flag description";

template <typename T>
static void testFlag(scudo::FlagType Type, T StartValue, const char *Env,
                     T FinalValue) {
  scudo::FlagParser Parser;
  T Flag = StartValue;
  Parser.registerFlag(FlagName, FlagDesc, Type, &Flag);
  Parser.parseString(Env);
  EXPECT_EQ(FinalValue, Flag);
  // Reporting unrecognized flags is needed to reset them.
  scudo::reportUnrecognizedFlags();
}

TEST(ScudoFlagsTest, BooleanFlags) {
  testFlag(scudo::FlagType::FT_bool, false, "flag_name=1", true);
  testFlag(scudo::FlagType::FT_bool, false, "flag_name=yes", true);
  testFlag(scudo::FlagType::FT_bool, false, "flag_name='yes'", true);
  testFlag(scudo::FlagType::FT_bool, false, "flag_name=true", true);
  testFlag(scudo::FlagType::FT_bool, true, "flag_name=0", false);
  testFlag(scudo::FlagType::FT_bool, true, "flag_name=\"0\"", false);
  testFlag(scudo::FlagType::FT_bool, true, "flag_name=no", false);
  testFlag(scudo::FlagType::FT_bool, true, "flag_name=false", false);
  testFlag(scudo::FlagType::FT_bool, true, "flag_name='false'", false);
}

TEST(ScudoFlagsDeathTest, BooleanFlags) {
  EXPECT_DEATH(testFlag(scudo::FlagType::FT_bool, false, "flag_name", true),
               "expected '='");
  EXPECT_DEATH(testFlag(scudo::FlagType::FT_bool, false, "flag_name=", true),
               "invalid value for bool option: ''");
  EXPECT_DEATH(testFlag(scudo::FlagType::FT_bool, false, "flag_name=2", true),
               "invalid value for bool option: '2'");
  EXPECT_DEATH(testFlag(scudo::FlagType::FT_bool, false, "flag_name=-1", true),
               "invalid value for bool option: '-1'");
  EXPECT_DEATH(testFlag(scudo::FlagType::FT_bool, false, "flag_name=on", true),
               "invalid value for bool option: 'on'");
}

TEST(ScudoFlagsTest, IntFlags) {
  testFlag(scudo::FlagType::FT_int, -11, nullptr, -11);
  testFlag(scudo::FlagType::FT_int, -11, "flag_name=0", 0);
  testFlag(scudo::FlagType::FT_int, -11, "flag_name='0'", 0);
  testFlag(scudo::FlagType::FT_int, -11, "flag_name=42", 42);
  testFlag(scudo::FlagType::FT_int, -11, "flag_name=-42", -42);
  testFlag(scudo::FlagType::FT_int, -11, "flag_name=\"-42\"", -42);

  // Unrecognized flags are ignored.
  testFlag(scudo::FlagType::FT_int, -11, "--flag_name=42", -11);
  testFlag(scudo::FlagType::FT_int, -11, "zzzzzzz=42", -11);
}

TEST(ScudoFlagsDeathTest, IntFlags) {
  EXPECT_DEATH(testFlag(scudo::FlagType::FT_int, -11, "flag_name", 0),
               "expected '='");
  EXPECT_DEATH(testFlag(scudo::FlagType::FT_int, -11, "flag_name=42U", 0),
               "invalid value for int option");
}

static void testTwoFlags(const char *Env, bool ExpectedFlag1,
                         const int ExpectedFlag2, const char *Name1 = "flag1",
                         const char *Name2 = "flag2") {
  scudo::FlagParser Parser;
  bool Flag1 = !ExpectedFlag1;
  int Flag2;
  Parser.registerFlag(Name1, FlagDesc, scudo::FlagType::FT_bool, &Flag1);
  Parser.registerFlag(Name2, FlagDesc, scudo::FlagType::FT_int, &Flag2);
  Parser.parseString(Env);
  EXPECT_EQ(ExpectedFlag1, Flag1);
  EXPECT_EQ(Flag2, ExpectedFlag2);
  // Reporting unrecognized flags is needed to reset them.
  scudo::reportUnrecognizedFlags();
}

TEST(ScudoFlagsTest, MultipleFlags) {
  testTwoFlags("flag1=1 flag2=42", true, 42);
  testTwoFlags("flag2=-1 flag1=0", false, -1);
  testTwoFlags("flag1=false:flag2=1337", false, 1337);
  testTwoFlags("flag2=42:flag1=yes", true, 42);
  testTwoFlags("flag2=42\nflag1=yes", true, 42);
  testTwoFlags("flag2=42\r\nflag1=yes", true, 42);
  testTwoFlags("flag2=42\tflag1=yes", true, 42);
}

TEST(ScudoFlagsTest, CommonSuffixFlags) {
  testTwoFlags("flag=1 other_flag=42", true, 42, "flag", "other_flag");
  testTwoFlags("other_flag=42 flag=1", true, 42, "flag", "other_flag");
}

TEST(ScudoFlagsTest, AllocatorFlags) {
  scudo::FlagParser Parser;
  scudo::Flags Flags;
  scudo::registerFlags(&Parser, &Flags);
  Flags.setDefaults();
  Flags.dealloc_type_mismatch = false;
  Flags.delete_size_mismatch = false;
  Flags.quarantine_max_chunk_size = 1024;
  Parser.parseString("dealloc_type_mismatch=true:delete_size_mismatch=true:"
                     "quarantine_max_chunk_size=2048");
  EXPECT_TRUE(Flags.dealloc_type_mismatch);
  EXPECT_TRUE(Flags.delete_size_mismatch);
  EXPECT_EQ(2048, Flags.quarantine_max_chunk_size);
}