fuzzer_launcher_test.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 2015 The Chromium 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. // Fuzzer launcher script tests.
  5. #include <string>
  6. #include <vector>
  7. #include "base/command_line.h"
  8. #include "base/path_service.h"
  9. #include "base/process/launch.h"
  10. #include "base/strings/string_split.h"
  11. #include "testing/gmock/include/gmock/gmock.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. TEST(FuzzerConfigTest, DictOnly) {
  14. // Test of automatically generated .options file for fuzzer with dict option.
  15. base::FilePath exe_path;
  16. base::PathService::Get(base::FILE_EXE, &exe_path);
  17. std::string launcher_path =
  18. exe_path.DirName().Append("check_fuzzer_config.py").value();
  19. std::string output;
  20. base::CommandLine cmd(
  21. std::vector<std::string>({launcher_path, "test_dict_only.options"}));
  22. bool success = base::GetAppOutputAndError(cmd, &output);
  23. EXPECT_TRUE(success);
  24. std::vector<std::string> fuzzer_args = base::SplitString(
  25. output, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
  26. EXPECT_EQ(1UL, fuzzer_args.size());
  27. EXPECT_EQ(fuzzer_args[0], "dict=test_dict_only.dict");
  28. }
  29. TEST(FuzzerConfigTest, ConfigOnly) {
  30. // Test of .options file for fuzzer with libfuzzer_options and without dict.
  31. base::FilePath exe_path;
  32. base::PathService::Get(base::FILE_EXE, &exe_path);
  33. std::string launcher_path =
  34. exe_path.DirName().Append("check_fuzzer_config.py").value();
  35. std::string output;
  36. base::CommandLine cmd(
  37. std::vector<std::string>({launcher_path, "test_config_only.options"}));
  38. bool success = base::GetAppOutputAndError(cmd, &output);
  39. EXPECT_TRUE(success);
  40. std::vector<std::string> fuzzer_args = base::SplitString(
  41. output, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
  42. EXPECT_EQ(2UL, fuzzer_args.size());
  43. EXPECT_EQ(fuzzer_args[0], "some_test_option=test_value");
  44. EXPECT_EQ(fuzzer_args[1], "max_len=1024");
  45. }
  46. TEST(FuzzerConfigTest, ConfigAndDict) {
  47. // Test of .options file for fuzzer with options file and dictionary.
  48. base::FilePath exe_path;
  49. base::PathService::Get(base::FILE_EXE, &exe_path);
  50. std::string launcher_path =
  51. exe_path.DirName().Append("check_fuzzer_config.py").value();
  52. std::string output;
  53. base::CommandLine cmd(std::vector<std::string>(
  54. {launcher_path, "test_config_and_dict.options"}));
  55. bool success = base::GetAppOutputAndError(cmd, &output);
  56. EXPECT_TRUE(success);
  57. std::vector<std::string> fuzzer_args = base::SplitString(
  58. output, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
  59. EXPECT_EQ(4UL, fuzzer_args.size());
  60. EXPECT_EQ(fuzzer_args[0], "dict=test_config_and_dict.dict");
  61. EXPECT_EQ(fuzzer_args[1], "max_len=random(1337, 31337)");
  62. EXPECT_EQ(fuzzer_args[2], "timeout=666");
  63. EXPECT_EQ(fuzzer_args[3], "use_traces=1");
  64. }
  65. TEST(FuzzerConfigTest, ConfigAndSeedCorpus) {
  66. // Test of .options file for fuzzer with libfuzzer_options and seed corpus.
  67. base::FilePath exe_path;
  68. base::PathService::Get(base::FILE_EXE, &exe_path);
  69. std::string launcher_path =
  70. exe_path.DirName().Append("check_fuzzer_config.py").value();
  71. std::string output;
  72. base::CommandLine cmd(std::vector<std::string>(
  73. {launcher_path, "test_config_and_seed_corpus.options"}));
  74. bool success = base::GetAppOutputAndError(cmd, &output);
  75. EXPECT_TRUE(success);
  76. std::vector<std::string> fuzzer_args = base::SplitString(
  77. output, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
  78. EXPECT_EQ(2UL, fuzzer_args.size());
  79. EXPECT_EQ(fuzzer_args[0], "some_test_option=test_value");
  80. EXPECT_EQ(fuzzer_args[1], "max_len=1024");
  81. // Test seed_corpus archive.
  82. launcher_path =
  83. exe_path.DirName().Append("check_seed_corpus_archive.py").value();
  84. cmd = base::CommandLine(std::vector<std::string>(
  85. {launcher_path, "test_config_and_seed_corpus_seed_corpus.zip"}));
  86. success = base::GetAppOutputAndError(cmd, &output);
  87. EXPECT_TRUE(success);
  88. std::vector<std::string> seed_corpus_info = base::SplitString(
  89. output, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
  90. EXPECT_EQ(1UL, seed_corpus_info.size());
  91. EXPECT_EQ(seed_corpus_info[0], "3");
  92. }
  93. TEST(FuzzerConfigTest, ConfigAndSeedCorpuses) {
  94. // Test of .options file for fuzzer with libfuzzer_options and seed corpuses.
  95. base::FilePath exe_path;
  96. base::PathService::Get(base::FILE_EXE, &exe_path);
  97. std::string launcher_path =
  98. exe_path.DirName().Append("check_fuzzer_config.py").value();
  99. std::string output;
  100. base::CommandLine cmd(std::vector<std::string>(
  101. {launcher_path, "test_config_and_seed_corpuses.options"}));
  102. bool success = base::GetAppOutputAndError(cmd, &output);
  103. EXPECT_TRUE(success);
  104. std::vector<std::string> fuzzer_args = base::SplitString(
  105. output, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
  106. EXPECT_EQ(2UL, fuzzer_args.size());
  107. EXPECT_EQ(fuzzer_args[0], "some_test_option=another_test_value");
  108. EXPECT_EQ(fuzzer_args[1], "max_len=1337");
  109. // Test seed_corpus archive.
  110. launcher_path =
  111. exe_path.DirName().Append("check_seed_corpus_archive.py").value();
  112. cmd = base::CommandLine(std::vector<std::string>(
  113. {launcher_path, "test_config_and_seed_corpuses_seed_corpus.zip"}));
  114. success = base::GetAppOutputAndError(cmd, &output);
  115. EXPECT_TRUE(success);
  116. std::vector<std::string> seed_corpus_info = base::SplitString(
  117. output, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
  118. EXPECT_EQ(1UL, seed_corpus_info.size());
  119. EXPECT_EQ(seed_corpus_info[0], "5");
  120. }
  121. TEST(FuzzerConfigTest, DictSubdir) {
  122. // Test of auto-generated .options file for fuzzer with dict in sub-directory.
  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. std::string output;
  128. base::CommandLine cmd(std::vector<std::string>(
  129. {launcher_path, "test_dict_from_subdir.options"}));
  130. bool success = base::GetAppOutputAndError(cmd, &output);
  131. EXPECT_TRUE(success);
  132. std::vector<std::string> fuzzer_args = base::SplitString(
  133. output, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
  134. EXPECT_EQ(1UL, fuzzer_args.size());
  135. EXPECT_EQ(fuzzer_args[0], "dict=test_dict_from_subdir.dict");
  136. }