1 //===-- options.cpp ---------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "gwp_asan/tests/harness.h"
11 #include "gwp_asan/optional/options_parser.h"
12 #include "gwp_asan/options.h"
16 static char Message
[1024];
17 void MessageRecorder(const char *Format
, ...) {
19 va_start(Args
, Format
);
20 vsprintf(Message
+ strlen(Message
), Format
, Args
);
24 TEST(GwpAsanOptionsTest
, Basic
) {
26 gwp_asan::options::initOptions("Enabled=0:SampleRate=4:"
27 "InstallSignalHandlers=false",
29 gwp_asan::options::Options Opts
= gwp_asan::options::getOptions();
30 EXPECT_EQ('\0', Message
[0]);
31 EXPECT_FALSE(Opts
.Enabled
);
32 EXPECT_FALSE(Opts
.InstallSignalHandlers
);
33 EXPECT_EQ(4, Opts
.SampleRate
);
36 void RunErrorTest(const char *OptionsStr
, const char *ErrorNeedle
) {
38 gwp_asan::options::initOptions(OptionsStr
, MessageRecorder
);
39 EXPECT_NE('\0', Message
[0])
40 << "Options string \"" << OptionsStr
<< "\" didn't generate an error.";
41 EXPECT_NE(nullptr, strstr(Message
, ErrorNeedle
))
42 << "Couldn't find error needle \"" << ErrorNeedle
43 << "\" in haystack created by options string \"" << OptionsStr
44 << "\". Error was: \"" << Message
<< "\".";
47 TEST(GwpAsanOptionsTest
, FailureModes
) {
48 RunErrorTest("Enabled=2", "Invalid boolean value '2' for option 'Enabled'");
49 RunErrorTest("Enabled=1:MaxSimultaneousAllocations=0",
50 "MaxSimultaneousAllocations must be > 0");
51 RunErrorTest("Enabled=1:MaxSimultaneousAllocations=-1",
52 "MaxSimultaneousAllocations must be > 0");
53 RunErrorTest("Enabled=1:SampleRate=0", "SampleRate must be > 0");
54 RunErrorTest("Enabled=1:SampleRate=-1", "SampleRate must be > 0");
55 RunErrorTest("Enabled=", "Invalid boolean value '' for option 'Enabled'");
56 RunErrorTest("==", "Unknown option '=='");
57 RunErrorTest("Enabled==0", "Invalid boolean value '=0' for option 'Enabled'");
58 RunErrorTest("Enabled:", "Expected '=' when parsing option 'Enabled:'");
59 RunErrorTest("Enabled:=", "Expected '=' when parsing option 'Enabled:='");
60 RunErrorTest("SampleRate=NOT_A_NUMBER",
61 "Invalid integer value 'NOT_A_NUMBER' for option 'SampleRate'");
62 RunErrorTest("NOT_A_VALUE=0", "Unknown option 'NOT_A_VALUE");