Loading...
Searching...
No Matches
component_fuzzer.cpp
Go to the documentation of this file.
// Copyright 2021 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#include <cassert>
#include <vector>
#include "ftxui/component/component.hpp"
#include "ftxui/component/terminal_input_parser.hpp"
using namespace ftxui;
namespace {
bool GeneratorBool(const char*& data, size_t& size) {
if (size == 0) {
return false;
}
auto out = bool(data[0] % 2);
data++;
size--;
return out;
}
std::string GeneratorString(const char*& data, size_t& size) {
int index = 0;
while (index < size && data[index])
++index;
auto out = std::string(data, data + index);
data += index;
size -= index;
// The input component do not support invalid UTF8 yet.
try {
to_wstring(out);
} catch (...) {
return "0";
}
return std::move(out);
}
int GeneratorInt(const char* data, size_t size) {
if (size == 0)
return 0;
auto out = int(data[0]);
data++;
size--;
return out;
}
Color GeneratorColor(const char* data, size_t size) {
return Color::RGB(GeneratorInt(data, size), GeneratorInt(data, size),
GeneratorInt(data, size));
}
AnimatedColorOption GeneratorAnimatedColorOption(const char* data,
size_t size) {
AnimatedColorOption option;
option.enabled = GeneratorBool(data, size);
option.inactive = GeneratorColor(data, size);
option.active = GeneratorColor(data, size);
option.duration = std::chrono::milliseconds(GeneratorInt(data, size));
return option;
}
AnimatedColorsOption GeneratorAnimatedColorsOptions(const char* data,
size_t size) {
AnimatedColorsOption option;
option.background = GeneratorAnimatedColorOption(data, size);
option.foreground = GeneratorAnimatedColorOption(data, size);
return option;
}
ButtonOption GeneratorButtonOption(const char* data, size_t size) {
ButtonOption option;
option.animated_colors = GeneratorAnimatedColorsOptions(data, size);
return option;
}
UnderlineOption GeneratorUnderlineOption(const char* data, size_t size) {
UnderlineOption option;
option.enabled = GeneratorBool(data, size);
option.color_active = GeneratorColor(data, size);
option.color_inactive = GeneratorColor(data, size);
option.leader_duration = std::chrono::milliseconds(GeneratorInt(data, size));
option.follower_duration =
std::chrono::milliseconds(GeneratorInt(data, size));
option.leader_delay = std::chrono::milliseconds(GeneratorInt(data, size));
option.follower_delay = std::chrono::milliseconds(GeneratorInt(data, size));
return option;
}
MenuEntryOption GeneratorMenuEntryOption(const char* data, size_t size) {
MenuEntryOption option;
option.animated_colors = GeneratorAnimatedColorsOptions(data, size);
return option;
}
MenuOption GeneratorMenuOption(const char* data, size_t size) {
MenuOption option;
option.underline = GeneratorUnderlineOption(data, size);
option.entries_option = GeneratorMenuEntryOption(data, size);
option.direction = static_cast<Direction>(GeneratorInt(data, size) % 4);
return option;
}
bool g_bool;
int g_int;
std::vector<std::string> g_list;
Components GeneratorComponents(const char*& data, size_t& size, int depth);
Component GeneratorComponent(const char*& data, size_t& size, int depth) {
depth--;
int value = GeneratorInt(data, size);
if (depth <= 0)
return Button(GeneratorString(data, size), [] {});
constexpr int value_max = 19;
value = (value % value_max + value_max) % value_max;
switch (value) {
case 0:
return Button(
GeneratorString(data, size), [] {},
GeneratorButtonOption(data, size));
case 1:
return Checkbox(GeneratorString(data, size), &g_bool);
case 2:
return Input(GeneratorString(data, size), GeneratorString(data, size));
case 3:
return Menu(&g_list, &g_int, GeneratorMenuOption(data, size));
case 4:
return Radiobox(&g_list, &g_int);
case 5:
return Toggle(&g_list, &g_int);
case 6:
return Slider(GeneratorString(data, size), &g_int,
GeneratorInt(data, size), GeneratorInt(data, size),
GeneratorInt(data, size));
case 7:
return ResizableSplitLeft(GeneratorComponent(data, size, depth - 1),
GeneratorComponent(data, size, depth - 1),
&g_int);
case 8:
return ResizableSplitRight(GeneratorComponent(data, size, depth - 1),
GeneratorComponent(data, size, depth - 1),
&g_int);
case 9:
return ResizableSplitTop(GeneratorComponent(data, size, depth - 1),
GeneratorComponent(data, size, depth - 1),
&g_int);
case 10:
return ResizableSplitBottom(GeneratorComponent(data, size, depth - 1),
GeneratorComponent(data, size, depth - 1),
&g_int);
case 11:
return Container::Vertical(GeneratorComponents(data, size, depth - 1));
case 12:
return Container::Vertical(GeneratorComponents(data, size, depth - 1),
&g_int);
case 13:
return Container::Horizontal(GeneratorComponents(data, size, depth - 1));
case 14:
return Container::Horizontal(GeneratorComponents(data, size, depth - 1),
&g_int);
case 15:
return Container::Tab(GeneratorComponents(data, size, depth - 1), &g_int);
case 16:
return Maybe(GeneratorComponent(data, size, depth - 1), &g_bool);
case 17:
return Dropdown(&g_list, &g_int);
case 18:
return Collapsible(GeneratorString(data, size),
GeneratorComponent(data, size, depth - 1),
GeneratorBool(data, size));
default:
assert(false);
}
}
Components GeneratorComponents(const char*& data, size_t& size, int depth) {
Components out;
if (depth > 0) {
while (size && GeneratorInt(data, size) % 2) {
out.push_back(GeneratorComponent(data, size, depth - 1));
}
}
return std::move(out);
}
} // namespace
extern "C" int LLVMFuzzerTestOneInput(const char* data, size_t size) {
g_bool = GeneratorBool(data, size);
g_int = GeneratorInt(data, size);
g_list = {
"test_1", "test_2", "test_3", "test_4", "test_5",
};
int depth = 10;
auto component = GeneratorComponent(data, size, depth);
int width = GeneratorInt(data, size);
int height = GeneratorInt(data, size);
width %= 500;
width += 500;
height %= 500;
height += 500;
auto screen =
Screen::Create(Dimension::Fixed(width), Dimension::Fixed(height));
auto event_receiver = MakeReceiver<Task>();
{
auto parser = TerminalInputParser(event_receiver->MakeSender());
for (size_t i = 0; i < size; ++i)
parser.Add(data[i]);
}
Task event;
while (event_receiver->Receive(&event)) {
component->OnEvent(std::get<Event>(event));
auto document = component->Render();
Render(screen, document);
}
return 0; // Non-zero return values are reserved for future use.
}
ftxui::Color
A class representing terminal colors.
Definition color.hpp:20
ftxui::Color::RGB
static Color RGB(uint8_t red, uint8_t green, uint8_t blue)
Build a Color from its RGB representation. https://en.wikipedia.org/wiki/RGB_color_model.
Definition color.cpp:153
ftxui::Screen::Create
static Screen Create(Dimensions dimension)
Create a screen with the given dimension.
Definition screen.cpp:394
ftxui::TerminalInputParser
Definition terminal_input_parser.hpp:18
component.hpp
LLVMFuzzerTestOneInput
int LLVMFuzzerTestOneInput(const char *data, size_t size)
Definition component_fuzzer.cpp:193
ftxui::Container::Horizontal
Component Horizontal(Components children)
A list of components, drawn one by one horizontally and navigated horizontally using left/right arrow...
Definition container.cpp:360
ftxui::Container::Vertical
Component Vertical(Components children)
A list of components, drawn one by one vertically and navigated vertically using up/down arrow key or...
Definition container.cpp:317
ftxui::Container::Tab
Component Tab(Components children, int *selector)
A list of components, where only one is drawn and interacted with at a time. The |selector| gives the...
Definition container.cpp:405
ftxui::Dimension::Fixed
Dimensions Fixed(int)
Definition screen.cpp:375
ftxui
Definition animation.hpp:10
ftxui::AnimatedColorsOption::foreground
AnimatedColorOption foreground
Definition component_options.hpp:72
ftxui::size
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Definition size.cpp:89
ftxui::Maybe
Component Maybe(Component, const bool *show)
Decorate a component |child|. It is shown only when |show| is true.
Definition maybe.cpp:74
ftxui::Direction
Direction
Definition direction.hpp:8
ftxui::ResizableSplitTop
Component ResizableSplitTop(Component main, Component back, int *main_size)
An vertical split in between two components, configurable using the mouse.
Definition resizable_split.cpp:283
ftxui::Menu
Component Menu(MenuOption options)
A list of text. The focused element is selected.
Definition menu.cpp:512
ftxui::Toggle
Component Toggle(ConstStringListRef entries, int *selected)
An horizontal list of elements. The user can navigate through them.
Definition menu.cpp:554
ftxui::Components
std::vector< Component > Components
Definition component_base.hpp:25
ftxui::Radiobox
Component Radiobox(RadioboxOption options)
A list of element, where only one can be selected.
Definition radiobox.cpp:204
ftxui::Button
Component Button(ButtonOption options)
Draw a button. Execute a function when clicked.
Definition button.cpp:176
ftxui::AnimatedColorsOption::background
AnimatedColorOption background
Definition component_options.hpp:71
ftxui::to_wstring
std::wstring to_wstring(const std::string &s)
Convert a std::wstring into a UTF8 std::string.
Definition string.cpp:1637
ftxui::MakeReceiver
Receiver< T > MakeReceiver()
Definition receiver.hpp:139
ftxui::Input
Component Input(InputOption options={})
An input box for editing text.
Definition input.cpp:571
ftxui::ResizableSplitRight
Component ResizableSplitRight(Component main, Component back, int *main_size)
An horizontal split in between two components, configurable using the mouse.
Definition resizable_split.cpp:249
ftxui::Dropdown
Component Dropdown(ConstStringListRef entries, int *selected)
A dropdown menu.
Definition dropdown.cpp:22
ftxui::Slider
Component Slider(SliderOption< T > options)
A slider in any direction.
Definition slider.cpp:346
ftxui::Task
std::variant< Event, Closure, AnimationTask > Task
Definition task.hpp:14
ftxui::MenuEntryOption::animated_colors
AnimatedColorsOption animated_colors
Definition component_options.hpp:80
ftxui::ResizableSplitBottom
Component ResizableSplitBottom(Component main, Component back, int *main_size)
An vertical split in between two components, configurable using the mouse.
Definition resizable_split.cpp:317
ftxui::Render
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:88
ftxui::Checkbox
Component Checkbox(CheckboxOption options)
Draw checkable element.
Definition checkbox.cpp:108
ftxui::ResizableSplitLeft
Component ResizableSplitLeft(Component main, Component back, int *main_size)
An horizontal split in between two components, configurable using the mouse.
Definition resizable_split.cpp:215
ftxui::Component
std::shared_ptr< ComponentBase > Component
Definition component_base.hpp:24
ftxui::Collapsible
Component Collapsible(ConstStringRef label, Component child, Ref< bool > show=false)
ftxui::AnimatedColorsOption
Definition component_options.hpp:70
ftxui::AnimatedColorOption
Option about a potentially animated color.
Definition component_options.hpp:56
ftxui::AnimatedColorOption::active
Color active
Definition component_options.hpp:65
ftxui::AnimatedColorOption::duration
animation::Duration duration
Definition component_options.hpp:66
ftxui::AnimatedColorOption::enabled
bool enabled
Definition component_options.hpp:63
ftxui::AnimatedColorOption::inactive
Color inactive
Definition component_options.hpp:64
ftxui::ButtonOption
Option for the AnimatedButton component.
Definition component_options.hpp:112
ftxui::ButtonOption::animated_colors
AnimatedColorsOption animated_colors
Definition component_options.hpp:130
ftxui::UnderlineOption
Definition component_options.hpp:31
ftxui::UnderlineOption::follower_duration
animation::Duration follower_duration
Definition component_options.hpp:44
ftxui::UnderlineOption::follower_delay
animation::Duration follower_delay
Definition component_options.hpp:45
ftxui::UnderlineOption::color_active
Color color_active
Definition component_options.hpp:34
ftxui::UnderlineOption::leader_duration
animation::Duration leader_duration
Definition component_options.hpp:42
ftxui::UnderlineOption::enabled
bool enabled
Definition component_options.hpp:32
ftxui::UnderlineOption::color_inactive
Color color_inactive
Definition component_options.hpp:35
ftxui::UnderlineOption::leader_delay
animation::Duration leader_delay
Definition component_options.hpp:43
terminal_input_parser.hpp