OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 Google Inc. All Rights Reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 // |
| 15 |
| 16 #include "syzygy/instrument/instrumenters/afl_instrumenter.h" |
| 17 |
| 18 #include "base/command_line.h" |
| 19 #include "gtest/gtest.h" |
| 20 #include "syzygy/core/unittest_util.h" |
| 21 #include "syzygy/pe/unittest_util.h" |
| 22 |
| 23 namespace instrument { |
| 24 namespace instrumenters { |
| 25 |
| 26 namespace { |
| 27 |
| 28 static const wchar_t kAFLWhitelistPath[] = |
| 29 LR"(syzygy\instrument\test_data\afl-good-whitelist.json)"; |
| 30 |
| 31 static const wchar_t kAFLBlacklistPath[] = |
| 32 LR"(syzygy\instrument\test_data\afl-good-blacklist.json)"; |
| 33 |
| 34 static const wchar_t kAFLBadConfigPath[] = |
| 35 LR"(syzygy\instrument\test_data\afl-bad-config.json)"; |
| 36 |
| 37 static const wchar_t kAFLBadConfigEmptyWhitelistPath[] = |
| 38 LR"(syzygy\instrument\test_data\afl-bad-empty-whitelist.json)"; |
| 39 |
| 40 static const wchar_t kAFLBadConfigEmptyBlacklistPath[] = |
| 41 LR"(syzygy\instrument\test_data\afl-bad-empty-blacklist.json)"; |
| 42 |
| 43 class TestAFLInstrumenter : public AFLInstrumenter { |
| 44 public: |
| 45 using AFLInstrumenter::input_image_path_; |
| 46 using AFLInstrumenter::input_pdb_path_; |
| 47 using AFLInstrumenter::output_image_path_; |
| 48 using AFLInstrumenter::output_pdb_path_; |
| 49 using AFLInstrumenter::force_decomposition_; |
| 50 using AFLInstrumenter::multithread_mode_; |
| 51 using AFLInstrumenter::cookie_check_hook_; |
| 52 using AFLInstrumenter::target_set_; |
| 53 using AFLInstrumenter::whitelist_mode_; |
| 54 using InstrumenterWithRelinker::CreateRelinker; |
| 55 }; |
| 56 |
| 57 class AFLInstrumenterTest : public testing::PELibUnitTest { |
| 58 public: |
| 59 AFLInstrumenterTest() : cmd_line_(base::FilePath(L"instrument.exe")) {} |
| 60 |
| 61 void SetUp() override { |
| 62 testing::Test::SetUp(); |
| 63 |
| 64 // Setup the IO streams. |
| 65 CreateTemporaryDir(&temp_dir_); |
| 66 stdin_path_ = temp_dir_.Append(L"NUL"); |
| 67 stdout_path_ = temp_dir_.Append(L"stdout.txt"); |
| 68 stderr_path_ = temp_dir_.Append(L"stderr.txt"); |
| 69 InitStreams(stdin_path_, stdout_path_, stderr_path_); |
| 70 |
| 71 // Initialize the (potential) input and output path values. |
| 72 abs_input_image_path_ = testing::GetExeRelativePath(testing::kTestDllName); |
| 73 input_image_path_ = testing::GetRelativePath(abs_input_image_path_); |
| 74 abs_input_pdb_path_ = testing::GetExeRelativePath(testing::kTestDllPdbName); |
| 75 input_pdb_path_ = testing::GetRelativePath(abs_input_pdb_path_); |
| 76 output_pdb_path_ = temp_dir_.Append(input_pdb_path_.BaseName()); |
| 77 output_image_path_ = temp_dir_.Append(input_image_path_.BaseName()); |
| 78 } |
| 79 |
| 80 void SetUpValidCommandLine() { |
| 81 cmd_line_.AppendSwitchPath("input-image", input_image_path_); |
| 82 cmd_line_.AppendSwitchPath("output-image", output_image_path_); |
| 83 } |
| 84 |
| 85 protected: |
| 86 base::FilePath temp_dir_; |
| 87 |
| 88 // @name The redirected streams paths. |
| 89 // @{ |
| 90 base::FilePath stdin_path_; |
| 91 base::FilePath stdout_path_; |
| 92 base::FilePath stderr_path_; |
| 93 // @} |
| 94 |
| 95 // @name Command-line and parameters. |
| 96 // @{ |
| 97 base::CommandLine cmd_line_; |
| 98 base::FilePath input_image_path_; |
| 99 base::FilePath input_pdb_path_; |
| 100 base::FilePath output_image_path_; |
| 101 base::FilePath output_pdb_path_; |
| 102 // @} |
| 103 |
| 104 // @name Expected final values of input parameters. |
| 105 // @{ |
| 106 base::FilePath abs_input_image_path_; |
| 107 base::FilePath abs_input_pdb_path_; |
| 108 // @} |
| 109 |
| 110 // The fake instrumenter we delegate to. |
| 111 TestAFLInstrumenter instrumenter_; |
| 112 }; |
| 113 |
| 114 } // namespace |
| 115 |
| 116 TEST_F(AFLInstrumenterTest, ParseMinimalCli) { |
| 117 SetUpValidCommandLine(); |
| 118 |
| 119 EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_)); |
| 120 |
| 121 EXPECT_EQ(abs_input_image_path_, instrumenter_.input_image_path_); |
| 122 EXPECT_EQ(output_image_path_, instrumenter_.output_image_path_); |
| 123 EXPECT_FALSE(instrumenter_.force_decomposition_); |
| 124 EXPECT_FALSE(instrumenter_.multithread_mode_); |
| 125 EXPECT_FALSE(instrumenter_.cookie_check_hook_); |
| 126 EXPECT_EQ(instrumenter_.target_set_.size(), 0); |
| 127 } |
| 128 |
| 129 TEST_F(AFLInstrumenterTest, ParseFullCli) { |
| 130 SetUpValidCommandLine(); |
| 131 cmd_line_.AppendSwitchPath("input-pdb", input_pdb_path_); |
| 132 cmd_line_.AppendSwitch("multithread"); |
| 133 cmd_line_.AppendSwitch("force-decompose"); |
| 134 cmd_line_.AppendSwitch("cookie-check-hook"); |
| 135 cmd_line_.AppendSwitchPath("output-pdb", output_pdb_path_); |
| 136 |
| 137 EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_)); |
| 138 |
| 139 EXPECT_EQ(abs_input_image_path_, instrumenter_.input_image_path_); |
| 140 EXPECT_EQ(output_image_path_, instrumenter_.output_image_path_); |
| 141 EXPECT_EQ(abs_input_pdb_path_, instrumenter_.input_pdb_path_); |
| 142 EXPECT_EQ(output_pdb_path_, instrumenter_.output_pdb_path_); |
| 143 EXPECT_EQ(instrumenter_.target_set_.size(), 0); |
| 144 EXPECT_TRUE(instrumenter_.multithread_mode_); |
| 145 EXPECT_TRUE(instrumenter_.force_decomposition_); |
| 146 EXPECT_TRUE(instrumenter_.cookie_check_hook_); |
| 147 } |
| 148 |
| 149 TEST_F(AFLInstrumenterTest, ParseWhitelist) { |
| 150 SetUpValidCommandLine(); |
| 151 cmd_line_.AppendSwitchPath("config", |
| 152 testing::GetSrcRelativePath(kAFLWhitelistPath)); |
| 153 |
| 154 EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_)); |
| 155 |
| 156 EXPECT_TRUE(instrumenter_.whitelist_mode_); |
| 157 EXPECT_EQ(instrumenter_.target_set_.size(), 4); |
| 158 EXPECT_NE(instrumenter_.target_set_.find("fuzzme"), |
| 159 instrumenter_.target_set_.end()); |
| 160 EXPECT_NE(instrumenter_.target_set_.find("pattern1"), |
| 161 instrumenter_.target_set_.end()); |
| 162 EXPECT_NE(instrumenter_.target_set_.find("_pattern2"), |
| 163 instrumenter_.target_set_.end()); |
| 164 EXPECT_NE(instrumenter_.target_set_.find("Unused::M"), |
| 165 instrumenter_.target_set_.end()); |
| 166 } |
| 167 |
| 168 TEST_F(AFLInstrumenterTest, ParseBlacklist) { |
| 169 SetUpValidCommandLine(); |
| 170 cmd_line_.AppendSwitchPath("config", |
| 171 testing::GetSrcRelativePath(kAFLBlacklistPath)); |
| 172 |
| 173 EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_)); |
| 174 |
| 175 EXPECT_FALSE(instrumenter_.whitelist_mode_); |
| 176 EXPECT_EQ(instrumenter_.target_set_.size(), 4); |
| 177 EXPECT_NE(instrumenter_.target_set_.find("fuzzme"), |
| 178 instrumenter_.target_set_.end()); |
| 179 EXPECT_NE(instrumenter_.target_set_.find("pattern1"), |
| 180 instrumenter_.target_set_.end()); |
| 181 EXPECT_NE(instrumenter_.target_set_.find("_pattern2"), |
| 182 instrumenter_.target_set_.end()); |
| 183 EXPECT_NE(instrumenter_.target_set_.find("Unused::M"), |
| 184 instrumenter_.target_set_.end()); |
| 185 } |
| 186 |
| 187 TEST_F(AFLInstrumenterTest, ParseWrongConfig) { |
| 188 SetUpValidCommandLine(); |
| 189 cmd_line_.AppendSwitchPath("config", |
| 190 testing::GetSrcRelativePath(kAFLBadConfigPath)); |
| 191 |
| 192 EXPECT_FALSE(instrumenter_.ParseCommandLine(&cmd_line_)); |
| 193 } |
| 194 |
| 195 TEST_F(AFLInstrumenterTest, ParseEmptyWhitelist) { |
| 196 SetUpValidCommandLine(); |
| 197 cmd_line_.AppendSwitchPath( |
| 198 "config", testing::GetSrcRelativePath(kAFLBadConfigEmptyWhitelistPath)); |
| 199 |
| 200 EXPECT_FALSE(instrumenter_.ParseCommandLine(&cmd_line_)); |
| 201 EXPECT_EQ(instrumenter_.target_set_.size(), 0); |
| 202 } |
| 203 |
| 204 TEST_F(AFLInstrumenterTest, ParseEmptyBlacklist) { |
| 205 SetUpValidCommandLine(); |
| 206 cmd_line_.AppendSwitchPath( |
| 207 "config", testing::GetSrcRelativePath(kAFLBadConfigEmptyBlacklistPath)); |
| 208 |
| 209 EXPECT_FALSE(instrumenter_.ParseCommandLine(&cmd_line_)); |
| 210 EXPECT_EQ(instrumenter_.target_set_.size(), 0); |
| 211 } |
| 212 |
| 213 TEST_F(AFLInstrumenterTest, InstrumentImpl) { |
| 214 SetUpValidCommandLine(); |
| 215 |
| 216 EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_)); |
| 217 EXPECT_TRUE(instrumenter_.InstrumentPrepare()); |
| 218 EXPECT_TRUE(instrumenter_.CreateRelinker()); |
| 219 EXPECT_TRUE(instrumenter_.InstrumentImpl()); |
| 220 } |
| 221 |
| 222 } // namespace instrumenters |
| 223 } // namespace instrument |
OLD | NEW |