# FTXUI: component_fuzzer.cpp Source File

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 <ftxui/component/event.hpp>
#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));
// Generate some events.
std::vector<Event> events;
auto parser =
TerminalInputParser([&](const Event& event) { events.push_back(event); });
for (size_t i = 0; i < size; ++i) {
parser.Add(data[i]);
}
for (const auto& event : events) {
component->OnEvent(event);
auto document = component->Render();
Render(screen, document);
}
return 0; // Non-zero return values are reserved for future use.
}
ftxui::TerminalInputParser
Definition terminal_input_parser.hpp:17
component.hpp
LLVMFuzzerTestOneInput
int LLVMFuzzerTestOneInput(const char *data, size_t size)
Definition component_fuzzer.cpp:197
event.hpp
ftxui::UnderlineOption::follower_duration
animation::Duration follower_duration
Definition component_options.hpp:46
ftxui::MenuOption::entries_option
MenuEntryOption entries_option
Definition component_options.hpp:100
ftxui::UnderlineOption::follower_delay
animation::Duration follower_delay
Definition component_options.hpp:47
ftxui::MenuOption::direction
Direction direction
Definition component_options.hpp:101
ftxui::MenuOption::underline
UnderlineOption underline
Definition component_options.hpp:99
ftxui::UnderlineOption::color_active
Color color_active
Definition component_options.hpp:36
ftxui::AnimatedColorOption::active
Color active
Definition component_options.hpp:67
ftxui::UnderlineOption::leader_duration
animation::Duration leader_duration
Definition component_options.hpp:44
ftxui::AnimatedColorOption::duration
animation::Duration duration
Definition component_options.hpp:68
ftxui::AnimatedColorOption::enabled
bool enabled
Definition component_options.hpp:65
ftxui::AnimatedColorOption::inactive
Color inactive
Definition component_options.hpp:66
ftxui::ButtonOption::animated_colors
AnimatedColorsOption animated_colors
Definition component_options.hpp:132
ftxui::UnderlineOption::color_inactive
Color color_inactive
Definition component_options.hpp:37
ftxui::UnderlineOption::leader_delay
animation::Duration leader_delay
Definition component_options.hpp:45
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::Maybe
Component Maybe(Component, const bool *show)
Decorate a component |child|. It is shown only when |show| is true.
Definition src/ftxui/component/maybe.cpp:74
ftxui::ResizableSplitTop
Component ResizableSplitTop(Component main, Component back, int *main_size)
An vertical split in between two components, configurable using the mouse.
Definition src/ftxui/component/resizable_split.cpp:283
ftxui::Menu
Component Menu(MenuOption options)
A list of text. The focused element is selected.
Definition src/ftxui/component/menu.cpp:512
ftxui::Toggle
Component Toggle(ConstStringListRef entries, int *selected)
An horizontal list of elements. The user can navigate through them.
Definition src/ftxui/component/menu.cpp:554
ftxui::Radiobox
Component Radiobox(RadioboxOption options)
A list of element, where only one can be selected.
Definition src/ftxui/component/radiobox.cpp:204
ftxui::Button
Component Button(ButtonOption options)
Draw a button. Execute a function when clicked.
Definition src/ftxui/component/button.cpp:175
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::Input
Component Input(InputOption options={})
An input box for editing text.
Definition src/ftxui/component/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 src/ftxui/component/resizable_split.cpp:249
ftxui::Dropdown
Component Dropdown(ConstStringListRef entries, int *selected)
A dropdown menu.
Definition src/ftxui/component/dropdown.cpp:22
ftxui::ResizableSplitBottom
Component ResizableSplitBottom(Component main, Component back, int *main_size)
An vertical split in between two components, configurable using the mouse.
Definition src/ftxui/component/resizable_split.cpp:317
ftxui::Checkbox
Component Checkbox(CheckboxOption options)
Draw checkable element.
Definition src/ftxui/component/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 src/ftxui/component/resizable_split.cpp:215
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::AnimatedColorOption
Option about a potentially animated color.
Definition component_options.hpp:58
ftxui::ButtonOption
Option for the AnimatedButton component.
Definition component_options.hpp:114
ftxui::Event
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:29
ftxui::UnderlineOption
Option for the underline effect.
Definition component_options.hpp:33
ftxui::size
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Definition src/ftxui/dom/size.cpp:89
ftxui::Direction
Direction
Direction is an enumeration that represents the four cardinal directions.
Definition direction.hpp:13
ftxui::Render
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:84
ftxui::Screen::Create
static Screen Create(Dimensions dimension)
Create a screen with the given dimension.
Definition screen.cpp:395
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 screen/color.cpp:147
ftxui::Color
Color is a class that represents a color in the terminal user interface.
Definition color.hpp:22
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::Components
std::vector< Component > Components
Definition component_base.hpp:25
ftxui::to_wstring
std::wstring to_wstring(const std::string &s)
Convert a UTF8 std::string into a std::wstring.
Definition string.cpp:1638
ftxui::Collapsible
Component Collapsible(ConstStringRef label, Component child, Ref< bool > show=false)
A collapsible component. It displays a checkbox with an arrow. Once activated, the child is displayed...
Definition src/ftxui/component/collapsible.cpp:33
ftxui::Slider
Component Slider(SliderOption< T > options)
A slider in any direction.
Definition src/ftxui/component/slider.cpp:316
ftxui::Component
std::shared_ptr< ComponentBase > Component
Definition component_base.hpp:24
ftxui::AnimatedColorsOption
Definition component_options.hpp:72
ftxui::AnimatedColorsOption::foreground
AnimatedColorOption foreground
Definition component_options.hpp:74
ftxui::AnimatedColorsOption::background
AnimatedColorOption background
Definition component_options.hpp:73
terminal_input_parser.hpp