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/common/safe_browsing/binary_feature_extractor.h"
7 #include "base/files/file_path.h"
8 #include "base/path_service.h"
9 #include "chrome/common/chrome_paths.h"
10 #include "chrome/common/safe_browsing/csd.pb.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace safe_browsing
{
16 class BinaryFeatureExtractorMacTest
: public testing::Test
{
18 void SetUp() override
{
19 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &test_data_
));
20 feature_extractor_
= new BinaryFeatureExtractor();
23 base::FilePath
GetPath(const char* file_name
) {
24 return test_data_
.AppendASCII("safe_browsing")
25 .AppendASCII("mach_o")
26 .AppendASCII(file_name
);
29 BinaryFeatureExtractor
* feature_extractor() {
30 return feature_extractor_
.get();
34 base::FilePath test_data_
;
35 scoped_refptr
<BinaryFeatureExtractor
> feature_extractor_
;
38 TEST_F(BinaryFeatureExtractorMacTest
, UnsignedMachOThin
) {
39 ClientDownloadRequest_ImageHeaders image_headers
;
40 google::protobuf::RepeatedPtrField
<std::string
> signed_data
;
42 base::FilePath path
= GetPath("lib32.dylib");
43 ASSERT_TRUE(feature_extractor()->ExtractImageFeatures(
44 path
, 0, &image_headers
, &signed_data
));
46 EXPECT_EQ(1, image_headers
.mach_o_headers().size());
47 EXPECT_EQ(0, signed_data
.size());
50 TEST_F(BinaryFeatureExtractorMacTest
, SignedMachOFat
) {
51 ClientDownloadRequest_ImageHeaders image_headers
;
52 google::protobuf::RepeatedPtrField
<std::string
> signed_data
;
54 base::FilePath path
= GetPath("signedexecutablefat");
55 ASSERT_TRUE(feature_extractor()->ExtractImageFeatures(
56 path
, 0, &image_headers
, &signed_data
));
58 EXPECT_EQ(2, image_headers
.mach_o_headers().size());
59 EXPECT_EQ(2, signed_data
.size());
62 TEST_F(BinaryFeatureExtractorMacTest
, NotMachO
) {
63 ClientDownloadRequest_ImageHeaders image_headers
;
64 google::protobuf::RepeatedPtrField
<std::string
> signed_data
;
66 base::FilePath path
= GetPath("src.c");
67 EXPECT_FALSE(feature_extractor()->ExtractImageFeatures(
68 path
, 0, &image_headers
, &signed_data
));
70 EXPECT_EQ(0, image_headers
.mach_o_headers().size());
71 EXPECT_EQ(0, signed_data
.size());
75 } // namespace safe_browsing