Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / chrome / browser / safe_browsing / sandboxed_dmg_analyzer_mac_unittest.cc
blobe0f3c4bdc8ae1d37ae7a9493a0593e9bf7c3cebc
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.
5 #include "chrome/browser/safe_browsing/sandboxed_dmg_analyzer_mac.h"
7 #include <mach-o/loader.h>
9 #include "base/bind.h"
10 #include "base/files/file_path.h"
11 #include "base/path_service.h"
12 #include "base/run_loop.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "chrome/common/chrome_paths.h"
15 #include "chrome/common/safe_browsing/zip_analyzer_results.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "content/public/test/test_utils.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 namespace safe_browsing {
21 namespace {
23 class SandboxedDMGAnalyzerTest : public testing::Test {
24 public:
25 SandboxedDMGAnalyzerTest()
26 : browser_thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
29 void AnalyzeFile(const base::FilePath& path,
30 zip_analyzer::Results* results) {
31 base::RunLoop run_loop;
32 ResultsGetter results_getter(run_loop.QuitClosure(), results);
33 scoped_refptr<SandboxedDMGAnalyzer> analyzer(
34 new SandboxedDMGAnalyzer(path, results_getter.GetCallback()));
35 analyzer->Start();
36 run_loop.Run();
39 base::FilePath GetFilePath(const char* file_name) {
40 base::FilePath test_data;
41 EXPECT_TRUE(PathService::Get(chrome::DIR_GEN_TEST_DATA, &test_data));
42 return test_data.AppendASCII("chrome")
43 .AppendASCII("safe_browsing_dmg")
44 .AppendASCII(file_name);
47 private:
48 // A helper class to store the results from the ResultsCallback and run
49 // another closure.
50 class ResultsGetter {
51 public:
52 ResultsGetter(const base::Closure& next_closure,
53 zip_analyzer::Results* results)
54 : next_closure_(next_closure), results_(results) {}
56 SandboxedDMGAnalyzer::ResultsCallback GetCallback() {
57 return base::Bind(&ResultsGetter::ResultsCallback,
58 base::Unretained(this));
61 private:
62 void ResultsCallback(const zip_analyzer::Results& results) {
63 *results_ = results;
64 next_closure_.Run();
67 base::Closure next_closure_;
68 zip_analyzer::Results* results_;
70 DISALLOW_COPY_AND_ASSIGN(ResultsGetter);
73 content::TestBrowserThreadBundle browser_thread_bundle_;
74 content::InProcessUtilityThreadHelper utility_thread_helper_;
77 TEST_F(SandboxedDMGAnalyzerTest, AnalyzeDMG) {
78 base::FilePath path;
79 ASSERT_NO_FATAL_FAILURE(path = GetFilePath("mach_o_in_dmg.dmg"));
81 zip_analyzer::Results results;
82 AnalyzeFile(path, &results);
84 EXPECT_TRUE(results.success);
85 EXPECT_TRUE(results.has_executable);
86 EXPECT_EQ(2, results.archived_binary.size());
88 bool got_executable = false, got_dylib = false;
89 for (const auto& binary : results.archived_binary) {
90 const std::string& file_name = binary.file_basename();
91 const google::protobuf::RepeatedPtrField<
92 ClientDownloadRequest_MachOHeaders>& headers =
93 binary.image_headers().mach_o_headers();
95 EXPECT_EQ(ClientDownloadRequest_DownloadType_MAC_EXECUTABLE,
96 binary.download_type());
98 if (file_name.find("executablefat") != std::string::npos) {
99 got_executable = true;
100 ASSERT_EQ(2, headers.size());
102 const ClientDownloadRequest_MachOHeaders& arch32 = headers.Get(0);
103 EXPECT_EQ(15, arch32.load_commands().size());
104 EXPECT_EQ(MH_MAGIC,
105 *reinterpret_cast<const uint32_t*>(arch32.mach_header().c_str()));
107 const ClientDownloadRequest_MachOHeaders& arch64 = headers.Get(1);
108 EXPECT_EQ(15, arch64.load_commands().size());
109 EXPECT_EQ(MH_MAGIC_64,
110 *reinterpret_cast<const uint32_t*>(arch64.mach_header().c_str()));
112 const std::string& sha256_bytes = binary.digests().sha256();
113 std::string actual_sha256 = base::HexEncode(sha256_bytes.c_str(),
114 sha256_bytes.size());
115 EXPECT_EQ(
116 "E462FF752FF9D84E34D843E5D46E2012ADCBD48540A8473FB794B286A389B945",
117 actual_sha256);
118 } else if (file_name.find("lib64.dylib") != std::string::npos) {
119 got_dylib = true;
120 ASSERT_EQ(1, headers.size());
122 const ClientDownloadRequest_MachOHeaders& arch = headers.Get(0);
123 EXPECT_EQ(13, arch.load_commands().size());
124 EXPECT_EQ(MH_MAGIC_64,
125 *reinterpret_cast<const uint32_t*>(arch.mach_header().c_str()));
127 const std::string& sha256_bytes = binary.digests().sha256();
128 std::string actual_sha256 = base::HexEncode(sha256_bytes.c_str(),
129 sha256_bytes.size());
130 EXPECT_EQ(
131 "2012CE4987B0FA4A5D285DF7E810560E841CFAB3054BC19E1AAB345F862A6C4E",
132 actual_sha256);
133 } else {
134 ADD_FAILURE() << "Unepxected result file " << binary.file_basename();
138 EXPECT_TRUE(got_executable);
139 EXPECT_TRUE(got_dylib);
142 } // namespace
143 } // namespace safe_browsing