aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/src/vcpkg-fuzz/main.cpp
blob: 881577654d32e82054472904836518f05bcca1ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <vcpkg/base/checks.h>
#include <vcpkg/base/json.h>
#include <vcpkg/base/stringview.h>
#include <vcpkg/base/system.print.h>

#include <iostream>
#include <sstream>
#include <string.h>
#include <utility>

using namespace vcpkg;

namespace
{
    enum class FuzzKind
    {
        None,
        Utf8Decoder,
        JsonParser,
    };

    struct FuzzArgs
    {
        FuzzArgs(int argc, char** argv)
        {
            if (argc <= 1)
            {
                print_help_and_exit();
            }

            char** it = argv + 1; // skip the name of the program
            char** last = argv + argc;

            for (; it != last; ++it)
            {
                auto arg = StringView(*it, strlen(*it));
                if (arg == "/?")
                {
                    print_help_and_exit();
                }

                auto pr = split_arg(arg);
                auto key = pr.first;
                auto value = pr.second;
                if (key == "h" || key == "help")
                {
                    print_help_and_exit();
                }

                if (key == "kind")
                {
                    if (value == "json")
                    {
                        kind = FuzzKind::JsonParser;
                    }
                    else if (value == "utf-8")
                    {
                        kind = FuzzKind::Utf8Decoder;
                    }
                    else
                    {
                        System::print2(System::Color::error, "Invalid kind: ", value, "\n");
                        System::print2(System::Color::error, "  Expected one of: utf-8, json\n\n");
                        print_help_and_exit(true);
                    }
                }
                else
                {
                    System::print2("Unknown option: ", key, "\n\n");
                    print_help_and_exit(true);
                }
            }
        }

        // returns {arg, ""} when there isn't an `=`
        // skips preceding `-`s
        std::pair<StringView, StringView> split_arg(StringView arg)
        {
            auto first = std::find_if(arg.begin(), arg.end(), [](char c) { return c != '-'; });
            auto division = std::find(first, arg.end(), '=');
            if (division == arg.end()) {
                return {StringView(first, arg.end()), StringView(arg.end(), arg.end())};
            } else {
                return {StringView(first, division), StringView(division + 1, arg.end())};
            }
        }

        [[noreturn]] void print_help_and_exit(bool invalid = false)
        {
            constexpr auto help =
                R"(
Usage: vcpkg-fuzz <options>

Accepts input on stdin.

Options:
  --kind=...                One of {utf-8, json}
)";

            auto color = invalid ? System::Color::error : System::Color::success;

            System::print2(color, help);
            if (invalid)
            {
                Checks::exit_fail(VCPKG_LINE_INFO);
            }
            else
            {
                Checks::exit_success(VCPKG_LINE_INFO);
            }
        }

        FuzzKind kind;
    };

    std::string read_all_of_stdin()
    {
        std::stringstream ss;
        ss << std::cin.rdbuf();
        return std::move(ss).str();
    }

}

int main(int argc, char** argv)
{
    auto args = FuzzArgs(argc, argv);

    if (args.kind == FuzzKind::None)
    {
        args.print_help_and_exit(true);
    }

    auto text = read_all_of_stdin();
    auto res = Json::parse(text);
    if (!res)
    {
        System::print2(System::Color::error, res.error()->format());
    }
    else
    {
        System::print2(System::Color::success, "success!");
    }
}