| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/safe_browsing/sandboxed_dmg_analyzer_mac.h" | 5 #include "chrome/browser/safe_browsing/sandboxed_dmg_analyzer_mac.h" |
| 6 | 6 |
| 7 #include <mach-o/loader.h> | 7 #include <mach-o/loader.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "chrome/common/chrome_paths.h" | 16 #include "chrome/common/chrome_paths.h" |
| 17 #include "chrome/common/safe_browsing/zip_analyzer_results.h" | 17 #include "chrome/common/safe_browsing/archive_analyzer_results.h" |
| 18 #include "content/public/test/test_browser_thread_bundle.h" | 18 #include "content/public/test/test_browser_thread_bundle.h" |
| 19 #include "content/public/test/test_utils.h" | 19 #include "content/public/test/test_utils.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 21 |
| 22 namespace safe_browsing { | 22 namespace safe_browsing { |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 class SandboxedDMGAnalyzerTest : public testing::Test { | 25 class SandboxedDMGAnalyzerTest : public testing::Test { |
| 26 public: | 26 public: |
| 27 SandboxedDMGAnalyzerTest() | 27 SandboxedDMGAnalyzerTest() |
| 28 : browser_thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | 28 : browser_thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
| 29 } | 29 } |
| 30 | 30 |
| 31 void AnalyzeFile(const base::FilePath& path, | 31 void AnalyzeFile(const base::FilePath& path, |
| 32 zip_analyzer::Results* results) { | 32 ArchiveAnalyzerResults* results) { |
| 33 base::RunLoop run_loop; | 33 base::RunLoop run_loop; |
| 34 ResultsGetter results_getter(run_loop.QuitClosure(), results); | 34 ResultsGetter results_getter(run_loop.QuitClosure(), results); |
| 35 scoped_refptr<SandboxedDMGAnalyzer> analyzer( | 35 scoped_refptr<SandboxedDMGAnalyzer> analyzer( |
| 36 new SandboxedDMGAnalyzer(path, results_getter.GetCallback())); | 36 new SandboxedDMGAnalyzer(path, results_getter.GetCallback())); |
| 37 analyzer->Start(); | 37 analyzer->Start(); |
| 38 run_loop.Run(); | 38 run_loop.Run(); |
| 39 } | 39 } |
| 40 | 40 |
| 41 base::FilePath GetFilePath(const char* file_name) { | 41 base::FilePath GetFilePath(const char* file_name) { |
| 42 base::FilePath test_data; | 42 base::FilePath test_data; |
| 43 EXPECT_TRUE(PathService::Get(chrome::DIR_GEN_TEST_DATA, &test_data)); | 43 EXPECT_TRUE(PathService::Get(chrome::DIR_GEN_TEST_DATA, &test_data)); |
| 44 return test_data.AppendASCII("chrome") | 44 return test_data.AppendASCII("chrome") |
| 45 .AppendASCII("safe_browsing_dmg") | 45 .AppendASCII("safe_browsing_dmg") |
| 46 .AppendASCII(file_name); | 46 .AppendASCII(file_name); |
| 47 } | 47 } |
| 48 | 48 |
| 49 private: | 49 private: |
| 50 // A helper that provides a SandboxedDMGAnalyzer::ResultCallback that will | 50 // A helper that provides a SandboxedDMGAnalyzer::ResultCallback that will |
| 51 // store a copy of an analyzer's results and then run a closure. | 51 // store a copy of an analyzer's results and then run a closure. |
| 52 class ResultsGetter { | 52 class ResultsGetter { |
| 53 public: | 53 public: |
| 54 ResultsGetter(const base::Closure& next_closure, | 54 ResultsGetter(const base::Closure& next_closure, |
| 55 zip_analyzer::Results* results) | 55 ArchiveAnalyzerResults* results) |
| 56 : next_closure_(next_closure), results_(results) {} | 56 : next_closure_(next_closure), results_(results) {} |
| 57 | 57 |
| 58 SandboxedDMGAnalyzer::ResultCallback GetCallback() { | 58 SandboxedDMGAnalyzer::ResultCallback GetCallback() { |
| 59 return base::Bind(&ResultsGetter::ResultsCallback, | 59 return base::Bind(&ResultsGetter::ResultsCallback, |
| 60 base::Unretained(this)); | 60 base::Unretained(this)); |
| 61 } | 61 } |
| 62 | 62 |
| 63 private: | 63 private: |
| 64 void ResultsCallback(const zip_analyzer::Results& results) { | 64 void ResultsCallback(const ArchiveAnalyzerResults& results) { |
| 65 *results_ = results; | 65 *results_ = results; |
| 66 next_closure_.Run(); | 66 next_closure_.Run(); |
| 67 } | 67 } |
| 68 | 68 |
| 69 base::Closure next_closure_; | 69 base::Closure next_closure_; |
| 70 zip_analyzer::Results* results_; | 70 ArchiveAnalyzerResults* results_; |
| 71 | 71 |
| 72 DISALLOW_COPY_AND_ASSIGN(ResultsGetter); | 72 DISALLOW_COPY_AND_ASSIGN(ResultsGetter); |
| 73 }; | 73 }; |
| 74 | 74 |
| 75 content::TestBrowserThreadBundle browser_thread_bundle_; | 75 content::TestBrowserThreadBundle browser_thread_bundle_; |
| 76 content::InProcessUtilityThreadHelper utility_thread_helper_; | 76 content::InProcessUtilityThreadHelper utility_thread_helper_; |
| 77 }; | 77 }; |
| 78 | 78 |
| 79 TEST_F(SandboxedDMGAnalyzerTest, AnalyzeDMG) { | 79 TEST_F(SandboxedDMGAnalyzerTest, AnalyzeDMG) { |
| 80 base::FilePath path; | 80 base::FilePath path; |
| 81 ASSERT_NO_FATAL_FAILURE(path = GetFilePath("mach_o_in_dmg.dmg")); | 81 ASSERT_NO_FATAL_FAILURE(path = GetFilePath("mach_o_in_dmg.dmg")); |
| 82 | 82 |
| 83 zip_analyzer::Results results; | 83 ArchiveAnalyzerResults results; |
| 84 AnalyzeFile(path, &results); | 84 AnalyzeFile(path, &results); |
| 85 | 85 |
| 86 EXPECT_TRUE(results.success); | 86 EXPECT_TRUE(results.success); |
| 87 EXPECT_TRUE(results.has_executable); | 87 EXPECT_TRUE(results.has_executable); |
| 88 EXPECT_EQ(2, results.archived_binary.size()); | 88 EXPECT_EQ(2, results.archived_binary.size()); |
| 89 | 89 |
| 90 bool got_executable = false, got_dylib = false; | 90 bool got_executable = false, got_dylib = false; |
| 91 for (const auto& binary : results.archived_binary) { | 91 for (const auto& binary : results.archived_binary) { |
| 92 const std::string& file_name = binary.file_basename(); | 92 const std::string& file_name = binary.file_basename(); |
| 93 const google::protobuf::RepeatedPtrField< | 93 const google::protobuf::RepeatedPtrField< |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 ADD_FAILURE() << "Unepxected result file " << binary.file_basename(); | 136 ADD_FAILURE() << "Unepxected result file " << binary.file_basename(); |
| 137 } | 137 } |
| 138 } | 138 } |
| 139 | 139 |
| 140 EXPECT_TRUE(got_executable); | 140 EXPECT_TRUE(got_executable); |
| 141 EXPECT_TRUE(got_dylib); | 141 EXPECT_TRUE(got_dylib); |
| 142 } | 142 } |
| 143 | 143 |
| 144 } // namespace | 144 } // namespace |
| 145 } // namespace safe_browsing | 145 } // namespace safe_browsing |
| OLD | NEW |