1// Copyright 2018 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <limits.h>
6#include <stdint.h>
7#include <stdio.h>
8
9#include <fbl/string_printf.h>
10#include <unittest/unittest.h>
11
12#include "fuzzer-fixture.h"
13
14namespace fuzzing {
15namespace testing {
16
17// Public methods
18
19bool FuzzerFixture::CreateZircon() {
20    BEGIN_HELPER;
21    ASSERT_TRUE(Fixture::Create());
22
23    // Zircon binaries
24    ASSERT_TRUE(CreateFile("boot/test/fuzz/target1"));
25    ASSERT_TRUE(CreateFile("boot/test/fuzz/target2"));
26
27    // Data from "previous" runs
28    ASSERT_TRUE(CreateData("zircon_fuzzers", "target2"));
29
30    END_HELPER;
31}
32
33bool FuzzerFixture::CreateFuchsia() {
34    BEGIN_HELPER;
35    ASSERT_TRUE(Fixture::Create());
36
37    // Zircon binaries
38    ASSERT_TRUE(CreateFile("system/test/fuzz/target1"));
39    ASSERT_TRUE(CreateFile("system/test/fuzz/target2"));
40
41    // Fuchsia packages
42    ASSERT_TRUE(CreatePackage("zircon_fuzzers", 0, "target2"));
43    ASSERT_TRUE(CreatePackage("fuchsia1_fuzzers", 1, "target1"));
44    ASSERT_TRUE(CreatePackage("fuchsia1_fuzzers", 2, "target1"));
45    ASSERT_TRUE(CreatePackage("fuchsia1_fuzzers", 5, "target1"));
46    ASSERT_TRUE(CreatePackage("fuchsia1_fuzzers", 5, "target2"));
47    ASSERT_TRUE(CreatePackage("fuchsia1_fuzzers", 5, "target3"));
48    ASSERT_TRUE(CreatePackage("fuchsia2_fuzzers", 2, "target4"));
49    ASSERT_TRUE(CreatePackage("fuchsia2_fuzzers", 5, "target4"));
50    ASSERT_TRUE(CreatePackage("fuchsia2_fuzzers", 10, "target4"));
51
52    // Data from "previous" runs
53    ASSERT_TRUE(CreateData("zircon_fuzzers", "target2"));
54    ASSERT_TRUE(CreateData("fuchsia2_fuzzers", "target4"));
55
56    END_HELPER;
57}
58
59// Protected methods
60
61void FuzzerFixture::Reset() {
62    max_versions_.clear();
63    Fixture::Reset();
64}
65
66// Private methods
67
68bool FuzzerFixture::CreatePackage(const char* package, long int version, const char* target) {
69    BEGIN_HELPER;
70
71    const char* max = max_version(package);
72    if (!max || strtol(max, nullptr, 0) < version) {
73        max_versions_.set(package, fbl::StringPrintf("%ld", version).c_str());
74    }
75
76    if (strcmp(package, "zircon_fuzzers") != 0) {
77        ASSERT_TRUE(
78            CreateFile(path("pkgfs/packages/%s/%ld/bin/%s", package, version, target).c_str()));
79    }
80
81    ASSERT_TRUE(
82        CreateFile(path("pkgfs/packages/%s/%ld/meta/%s.cmx", package, version, target).c_str()));
83
84    ASSERT_TRUE(
85        CreateFile(path("pkgfs/packages/%s/%ld/data/%s/corpora", package, version, target).c_str(),
86                   "//path/to/seed/corpus\n "
87                   "//path/to/cipd/ensure/file\n"
88                   "https://gcs/url\n"));
89    ASSERT_TRUE(CreateFile(
90        path("pkgfs/packages/%s/%ld/data/%s/dictionary", package, version, target).c_str(),
91        "foo\n"
92        "bar\n"
93        "baz\n"));
94    ASSERT_TRUE(
95        CreateFile(path("pkgfs/packages/%s/%ld/data/%s/options", package, version, target).c_str(),
96                   "foo = bar\n"
97                   "baz = qux\n"));
98
99    END_HELPER;
100}
101
102bool FuzzerFixture::CreateData(const char* package, const char* target) {
103    BEGIN_HELPER;
104
105    ASSERT_TRUE(CreateDirectory(path("data/fuzzing/%s/%s/corpus", package, target).c_str()));
106    ASSERT_TRUE(CreateFile(path("data/fuzzing/%s/%s/crash-deadbeef", package, target).c_str()));
107    ASSERT_TRUE(CreateFile(path("data/fuzzing/%s/%s/leak-deadfa11", package, target).c_str()));
108    ASSERT_TRUE(CreateFile(path("data/fuzzing/%s/%s/oom-feedface", package, target).c_str()));
109
110    END_HELPER;
111}
112
113} // namespace testing
114} // namespace fuzzing
115