1 //===-- options.cpp ---------------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "gwp_asan/tests/harness.h"
10 
11 #include "gwp_asan/optional/options_parser.h"
12 #include "gwp_asan/options.h"
13 
14 #include <stdarg.h>
15 
16 static char Message[1024];
MessageRecorder(const char * Format,...)17 void MessageRecorder(const char *Format, ...) {
18   va_list Args;
19   va_start(Args, Format);
20   vsprintf(Message + strlen(Message), Format, Args);
21   va_end(Args);
22 }
23 
TEST(GwpAsanOptionsTest,Basic)24 TEST(GwpAsanOptionsTest, Basic) {
25   Message[0] = '\0';
26   gwp_asan::options::initOptions("Enabled=0:SampleRate=4:"
27                                  "InstallSignalHandlers=false",
28                                  MessageRecorder);
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);
34 }
35 
RunErrorTest(const char * OptionsStr,const char * ErrorNeedle)36 void RunErrorTest(const char *OptionsStr, const char *ErrorNeedle) {
37   Message[0] = '\0';
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 << "\".";
45 }
46 
TEST(GwpAsanOptionsTest,FailureModes)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");
63 }
64