OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/common/safe_browsing/binary_feature_extractor.h" | 5 #include "chrome/common/safe_browsing/binary_feature_extractor.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 | 11 |
12 #include "base/base_paths.h" | 12 #include "base/base_paths.h" |
13 #include "base/files/file.h" | 13 #include "base/files/file.h" |
14 #include "base/files/scoped_temp_dir.h" | 14 #include "base/files/scoped_temp_dir.h" |
15 #include "base/path_service.h" | 15 #include "base/path_service.h" |
16 #include "chrome/common/safe_browsing/csd.pb.h" | 16 #include "chrome/common/safe_browsing/csd.pb.h" |
17 #include "crypto/sha2.h" | 17 #include "crypto/sha2.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 | 19 |
20 namespace safe_browsing { | 20 namespace safe_browsing { |
21 | 21 |
22 class BinaryFeatureExtractorTest : public testing::Test { | 22 class BinaryFeatureExtractorTest : public testing::Test { |
23 protected: | 23 protected: |
24 BinaryFeatureExtractorTest() : extractor_(new BinaryFeatureExtractor()) {} | 24 BinaryFeatureExtractorTest() : extractor_(new BinaryFeatureExtractor()) {} |
25 | 25 |
26 void SetUp() override { | 26 void SetUp() override { |
27 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 27 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
28 path_ = temp_dir_.path().Append(FILE_PATH_LITERAL("file.dll")); | 28 path_ = temp_dir_.GetPath().Append(FILE_PATH_LITERAL("file.dll")); |
29 } | 29 } |
30 | 30 |
31 // Writes |size| bytes from |data| to |path_|. | 31 // Writes |size| bytes from |data| to |path_|. |
32 void WriteFileToHash(const char* data, int size) { | 32 void WriteFileToHash(const char* data, int size) { |
33 base::File file(path_, base::File::FLAG_CREATE | base::File::FLAG_WRITE); | 33 base::File file(path_, base::File::FLAG_CREATE | base::File::FLAG_WRITE); |
34 ASSERT_TRUE(file.IsValid()); | 34 ASSERT_TRUE(file.IsValid()); |
35 ASSERT_EQ(size, file.WriteAtCurrentPos(data, size)); | 35 ASSERT_EQ(size, file.WriteAtCurrentPos(data, size)); |
36 } | 36 } |
37 | 37 |
38 // Verifies that |path_| hashes to |digest|. | 38 // Verifies that |path_| hashes to |digest|. |
39 void ExpectFileDigestEq(const uint8_t* digest) { | 39 void ExpectFileDigestEq(const uint8_t* digest) { |
40 ClientDownloadRequest_Digests digests; | 40 ClientDownloadRequest_Digests digests; |
41 extractor_->ExtractDigest(path_, &digests); | 41 extractor_->ExtractDigest(path_, &digests); |
42 EXPECT_TRUE(digests.has_sha256()); | 42 EXPECT_TRUE(digests.has_sha256()); |
43 EXPECT_EQ(std::string(reinterpret_cast<const char*>(digest), | 43 EXPECT_EQ(std::string(reinterpret_cast<const char*>(digest), |
44 crypto::kSHA256Length), | 44 crypto::kSHA256Length), |
45 digests.sha256()); | 45 digests.sha256()); |
46 } | 46 } |
47 | 47 |
48 static const int kBlockSize = 1 << 12; | 48 static const int kBlockSize = 1 << 12; |
49 scoped_refptr<BinaryFeatureExtractor> extractor_; | 49 scoped_refptr<BinaryFeatureExtractor> extractor_; |
50 base::ScopedTempDir temp_dir_; | 50 base::ScopedTempDir temp_dir_; |
51 | 51 |
52 // The path to a file that may be hashed. | 52 // The path to a file that may be hashed. |
53 base::FilePath path_; | 53 base::FilePath path_; |
54 }; | 54 }; |
55 | 55 |
56 TEST_F(BinaryFeatureExtractorTest, ExtractDigestNoFile) { | 56 TEST_F(BinaryFeatureExtractorTest, ExtractDigestNoFile) { |
57 base::FilePath no_file = | 57 base::FilePath no_file = |
58 temp_dir_.path().Append(FILE_PATH_LITERAL("does_not_exist.dll")); | 58 temp_dir_.GetPath().Append(FILE_PATH_LITERAL("does_not_exist.dll")); |
59 | 59 |
60 ClientDownloadRequest_Digests digests; | 60 ClientDownloadRequest_Digests digests; |
61 extractor_->ExtractDigest(no_file, &digests); | 61 extractor_->ExtractDigest(no_file, &digests); |
62 EXPECT_FALSE(digests.has_sha256()); | 62 EXPECT_FALSE(digests.has_sha256()); |
63 } | 63 } |
64 | 64 |
65 // Hash a file that is less than 1 4k block. | 65 // Hash a file that is less than 1 4k block. |
66 TEST_F(BinaryFeatureExtractorTest, ExtractSmallDigest) { | 66 TEST_F(BinaryFeatureExtractorTest, ExtractSmallDigest) { |
67 static const uint8_t kDigest[] = { | 67 static const uint8_t kDigest[] = { |
68 0x70, 0x27, 0x7b, 0xad, 0xfc, 0xb9, 0x97, 0x6b, 0x24, 0xf9, 0x80, | 68 0x70, 0x27, 0x7b, 0xad, 0xfc, 0xb9, 0x97, 0x6b, 0x24, 0xf9, 0x80, |
(...skipping 28 matching lines...) Expand all Loading... |
97 0x33, 0xa7, 0x70, 0xf3, 0x6b, 0x85, 0xbf, 0xce, 0x9d, 0x5c}; | 97 0x33, 0xa7, 0x70, 0xf3, 0x6b, 0x85, 0xbf, 0xce, 0x9d, 0x5c}; |
98 | 98 |
99 const int kDataLen = kBlockSize + 1; | 99 const int kDataLen = kBlockSize + 1; |
100 std::unique_ptr<char[]> data(new char[kDataLen]); | 100 std::unique_ptr<char[]> data(new char[kDataLen]); |
101 memset(data.get(), 71, kDataLen); | 101 memset(data.get(), 71, kDataLen); |
102 WriteFileToHash(data.get(), kDataLen); | 102 WriteFileToHash(data.get(), kDataLen); |
103 ExpectFileDigestEq(kDigest); | 103 ExpectFileDigestEq(kDigest); |
104 } | 104 } |
105 | 105 |
106 } // namespace safe_browsing | 106 } // namespace safe_browsing |
OLD | NEW |