summaryrefslogtreecommitdiff
path: root/test/src/fuzz.cpp
blob: b498bc5b4844c20136385526c7fc58ebd86573c1 (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
#include <mbgl/platform/log.hpp>
#include <mbgl/style/parser.hpp>
#include <mbgl/util/mapbox.hpp>
#include <mbgl/tile/vector_tile_data.hpp>

#include <mbgl/style/rapidjson_conversion.hpp>

#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/filter.hpp>

#include <protozero/exception.hpp>

#include <iostream>
#include <istream>
#include <iterator>
#include <ostream>
#include <string>
#include <unordered_map>

typedef void (*Callback)(std::string);
static const std::unordered_map<std::string, Callback> fuzzers{
    { "source-url",
      [](std::string input) { mbgl::util::mapbox::normalizeSourceURL(input, "key"); } },
    { "style-url", [](std::string input) { mbgl::util::mapbox::normalizeStyleURL(input, "key"); } },
    { "sprite-url",
      [](std::string input) { mbgl::util::mapbox::normalizeSpriteURL(input, "key"); } },
    { "glyphs-url",
      [](std::string input) { mbgl::util::mapbox::normalizeGlyphsURL(input, "key"); } },
    { "tile-url", [](std::string input) { mbgl::util::mapbox::normalizeTileURL(input, "key"); } },
    { "canonicalize-tile-url",
      [](std::string input) {
          mbgl::util::mapbox::canonicalizeTileURL(input, mbgl::SourceType::Raster, 42);
      } },
    { "parse-style",
      [](std::string input) {
          mbgl::style::Parser parser;
          parser.parse(input);
      } },
    { "parse-filter",
      [](std::string input) {
          using namespace mbgl;
          using namespace mbgl::style;
          rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> document;
          document.Parse<0>(input.c_str());
          if (!document.HasParseError()) {
              conversion::convert<Filter>(document);
          }
      } },
    { "parse-tile",
      [](std::string input) {
          mbgl::VectorTileData tile(std::make_shared<const std::string>(std::move(input)));
          try {
              tile.getLayer("foo");
          } catch (protozero::exception& ex) {
          }
      } },
};

void printValidFuzzers() {
    std::cerr << "Valid fuzzers are:" << std::endl;
    for (auto& fuzzer : fuzzers) {
        std::cerr << "  - " << fuzzer.first << std::endl;
    }
}

int main(int argc, char* argv[]) {
    if (argc < 2) {
        std::cerr << "Usage: fuzz <fuzzers>" << std::endl;
        printValidFuzzers();
        return 1;
    }

    auto it = fuzzers.find(argv[1]);
    if (it == fuzzers.end()) {
        std::cerr << "Fuzzer '" << argv[1] << "' does not exist." << std::endl;
        printValidFuzzers();
        return 1;
    }

    // Disables logging
    mbgl::Log::setObserver(std::make_unique<mbgl::Log::NullObserver>());

    while (__AFL_LOOP(1000)) {
        // Pass stdin to the function.
        std::cin >> std::noskipws;
        it->second({ std::istream_iterator<char>(std::cin), std::istream_iterator<char>() });
    }

    return 0;
}