1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Fuzzer launcher script tests.
6
7 #include <string>
8 #include <vector>
9
10 #include "base/command_line.h"
11 #include "base/path_service.h"
12 #include "base/process/launch.h"
13 #include "base/strings/string_split.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17
TEST(FuzzerConfigTest,DictOnly)18 TEST(FuzzerConfigTest, DictOnly) {
19 // Test of automatically generated .options file for fuzzer with dict option.
20 base::FilePath exe_path;
21 base::PathService::Get(base::FILE_EXE, &exe_path);
22 std::string launcher_path =
23 exe_path.DirName().Append("check_fuzzer_config.py").value();
24
25 std::string output;
26 base::CommandLine cmd(
27 std::vector<std::string>({launcher_path, "test_dict_only.options"}));
28 bool success = base::GetAppOutputAndError(cmd, &output);
29 EXPECT_TRUE(success);
30 std::vector<std::string> fuzzer_args = base::SplitString(
31 output, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
32
33 EXPECT_EQ(1UL, fuzzer_args.size());
34
35 EXPECT_EQ(fuzzer_args[0], "dict=test_dict_only.dict");
36 }
37
38
TEST(FuzzerConfigTest,ConfigOnly)39 TEST(FuzzerConfigTest, ConfigOnly) {
40 // Test of .options file for fuzzer with libfuzzer_options and without dict.
41 base::FilePath exe_path;
42 base::PathService::Get(base::FILE_EXE, &exe_path);
43 std::string launcher_path =
44 exe_path.DirName().Append("check_fuzzer_config.py").value();
45
46 std::string output;
47 base::CommandLine cmd(
48 std::vector<std::string>({launcher_path, "test_config_only.options"}));
49 bool success = base::GetAppOutputAndError(cmd, &output);
50 EXPECT_TRUE(success);
51 std::vector<std::string> fuzzer_args = base::SplitString(
52 output, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
53
54 EXPECT_EQ(2UL, fuzzer_args.size());
55
56 EXPECT_EQ(fuzzer_args[0], "some_test_option=test_value");
57 EXPECT_EQ(fuzzer_args[1], "max_len=1024");
58 }
59
60
TEST(FuzzerConfigTest,ConfigAndDict)61 TEST(FuzzerConfigTest, ConfigAndDict) {
62 // Test of .options file for fuzzer with options file and dictionary.
63 base::FilePath exe_path;
64 base::PathService::Get(base::FILE_EXE, &exe_path);
65 std::string launcher_path =
66 exe_path.DirName().Append("check_fuzzer_config.py").value();
67
68 std::string output;
69 base::CommandLine cmd(std::vector<std::string>(
70 {launcher_path, "test_config_and_dict.options"}));
71 bool success = base::GetAppOutputAndError(cmd, &output);
72 EXPECT_TRUE(success);
73 std::vector<std::string> fuzzer_args = base::SplitString(
74 output, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
75
76 EXPECT_EQ(4UL, fuzzer_args.size());
77
78 EXPECT_EQ(fuzzer_args[0], "dict=test_config_and_dict.dict");
79 EXPECT_EQ(fuzzer_args[1], "max_len=random(1337, 31337)");
80 EXPECT_EQ(fuzzer_args[2], "timeout=666");
81 EXPECT_EQ(fuzzer_args[3], "use_traces=1");
82 }
83
84
TEST(FuzzerConfigTest,ConfigAndSeedCorpus)85 TEST(FuzzerConfigTest, ConfigAndSeedCorpus) {
86 // Test of .options file for fuzzer with libfuzzer_options and seed corpus.
87 base::FilePath exe_path;
88 base::PathService::Get(base::FILE_EXE, &exe_path);
89 std::string launcher_path =
90 exe_path.DirName().Append("check_fuzzer_config.py").value();
91
92 std::string output;
93 base::CommandLine cmd(std::vector<std::string>(
94 {launcher_path, "test_config_and_seed_corpus.options"}));
95 bool success = base::GetAppOutputAndError(cmd, &output);
96 EXPECT_TRUE(success);
97 std::vector<std::string> fuzzer_args = base::SplitString(
98 output, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
99
100 EXPECT_EQ(2UL, fuzzer_args.size());
101
102 EXPECT_EQ(fuzzer_args[0], "some_test_option=test_value");
103 EXPECT_EQ(fuzzer_args[1], "max_len=1024");
104
105 // Test seed_corpus archive.
106 launcher_path =
107 exe_path.DirName().Append("check_seed_corpus_archive.py").value();
108
109 cmd = base::CommandLine(std::vector<std::string>(
110 {launcher_path, "test_config_and_seed_corpus_seed_corpus.zip"}));
111 success = base::GetAppOutputAndError(cmd, &output);
112 EXPECT_TRUE(success);
113 std::vector<std::string> seed_corpus_info = base::SplitString(
114 output, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
115
116 EXPECT_EQ(1UL, seed_corpus_info.size());
117 EXPECT_EQ(seed_corpus_info[0], "3");
118 }
119
120
TEST(FuzzerConfigTest,ConfigAndSeedCorpuses)121 TEST(FuzzerConfigTest, ConfigAndSeedCorpuses) {
122 // Test of .options file for fuzzer with libfuzzer_options and seed corpuses.
123 base::FilePath exe_path;
124 base::PathService::Get(base::FILE_EXE, &exe_path);
125 std::string launcher_path =
126 exe_path.DirName().Append("check_fuzzer_config.py").value();
127
128 std::string output;
129 base::CommandLine cmd(std::vector<std::string>(
130 {launcher_path, "test_config_and_seed_corpuses.options"}));
131 bool success = base::GetAppOutputAndError(cmd, &output);
132 EXPECT_TRUE(success);
133 std::vector<std::string> fuzzer_args = base::SplitString(
134 output, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
135
136 EXPECT_EQ(2UL, fuzzer_args.size());
137
138 EXPECT_EQ(fuzzer_args[0], "some_test_option=another_test_value");
139 EXPECT_EQ(fuzzer_args[1], "max_len=1337");
140
141 // Test seed_corpus archive.
142 launcher_path =
143 exe_path.DirName().Append("check_seed_corpus_archive.py").value();
144
145 cmd = base::CommandLine(std::vector<std::string>(
146 {launcher_path, "test_config_and_seed_corpuses_seed_corpus.zip"}));
147 success = base::GetAppOutputAndError(cmd, &output);
148 EXPECT_TRUE(success);
149 std::vector<std::string> seed_corpus_info = base::SplitString(
150 output, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
151
152 EXPECT_EQ(1UL, seed_corpus_info.size());
153 EXPECT_EQ(seed_corpus_info[0], "5");
154 }
155
156
TEST(FuzzerConfigTest,DictSubdir)157 TEST(FuzzerConfigTest, DictSubdir) {
158 // Test of auto-generated .options file for fuzzer with dict in sub-directory.
159 base::FilePath exe_path;
160 base::PathService::Get(base::FILE_EXE, &exe_path);
161 std::string launcher_path =
162 exe_path.DirName().Append("check_fuzzer_config.py").value();
163
164 std::string output;
165 base::CommandLine cmd(std::vector<std::string>(
166 {launcher_path, "test_dict_from_subdir.options"}));
167 bool success = base::GetAppOutputAndError(cmd, &output);
168 EXPECT_TRUE(success);
169 std::vector<std::string> fuzzer_args = base::SplitString(
170 output, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
171
172 EXPECT_EQ(1UL, fuzzer_args.size());
173
174 EXPECT_EQ(fuzzer_args[0], "dict=test_dict_from_subdir.dict");
175 }
176