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
16static char Message[1024];
17void 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
24TEST(GwpAsanOptionsTest, Basic) {
25 Message[0] = '\0';
26 gwp_asan::options::initOptions(OptionsStr: "Enabled=0:SampleRate=4:"
27 "InstallSignalHandlers=false",
28 PrintfForWarnings: 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
36void RunErrorTest(const char *OptionsStr, const char *ErrorNeedle) {
37 Message[0] = '\0';
38 gwp_asan::options::initOptions(OptionsStr, PrintfForWarnings: 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
47TEST(GwpAsanOptionsTest, FailureModes) {
48 RunErrorTest(OptionsStr: "Enabled=2", ErrorNeedle: "Invalid boolean value '2' for option 'Enabled'");
49 RunErrorTest(OptionsStr: "Enabled=1:MaxSimultaneousAllocations=0",
50 ErrorNeedle: "MaxSimultaneousAllocations must be > 0");
51 RunErrorTest(OptionsStr: "Enabled=1:MaxSimultaneousAllocations=-1",
52 ErrorNeedle: "MaxSimultaneousAllocations must be > 0");
53 RunErrorTest(OptionsStr: "Enabled=1:SampleRate=0", ErrorNeedle: "SampleRate must be > 0");
54 RunErrorTest(OptionsStr: "Enabled=1:SampleRate=-1", ErrorNeedle: "SampleRate must be > 0");
55 RunErrorTest(OptionsStr: "Enabled=", ErrorNeedle: "Invalid boolean value '' for option 'Enabled'");
56 RunErrorTest(OptionsStr: "==", ErrorNeedle: "Unknown option '=='");
57 RunErrorTest(OptionsStr: "Enabled==0", ErrorNeedle: "Invalid boolean value '=0' for option 'Enabled'");
58 RunErrorTest(OptionsStr: "Enabled:", ErrorNeedle: "Expected '=' when parsing option 'Enabled:'");
59 RunErrorTest(OptionsStr: "Enabled:=", ErrorNeedle: "Expected '=' when parsing option 'Enabled:='");
60 RunErrorTest(OptionsStr: "SampleRate=NOT_A_NUMBER",
61 ErrorNeedle: "Invalid integer value 'NOT_A_NUMBER' for option 'SampleRate'");
62 RunErrorTest(OptionsStr: "NOT_A_VALUE=0", ErrorNeedle: "Unknown option 'NOT_A_VALUE");
63}
64

source code of compiler-rt/lib/gwp_asan/tests/options.cpp