| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/safe_browsing/binary_feature_extractor.h" | |
| 6 | |
| 7 #include "base/base_paths.h" | |
| 8 #include "base/files/file.h" | |
| 9 #include "base/files/scoped_temp_dir.h" | |
| 10 #include "base/path_service.h" | |
| 11 #include "chrome/common/safe_browsing/csd.pb.h" | |
| 12 #include "crypto/sha2.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 namespace safe_browsing { | |
| 16 | |
| 17 class BinaryFeatureExtractorTest : public testing::Test { | |
| 18 protected: | |
| 19 BinaryFeatureExtractorTest() : extractor_(new BinaryFeatureExtractor()) {} | |
| 20 | |
| 21 void SetUp() override { | |
| 22 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 23 path_ = temp_dir_.path().Append(FILE_PATH_LITERAL("file.dll")); | |
| 24 } | |
| 25 | |
| 26 // Writes |size| bytes from |data| to |path_|. | |
| 27 void WriteFileToHash(const char* data, int size) { | |
| 28 base::File file(path_, base::File::FLAG_CREATE | base::File::FLAG_WRITE); | |
| 29 ASSERT_TRUE(file.IsValid()); | |
| 30 ASSERT_EQ(size, file.WriteAtCurrentPos(data, size)); | |
| 31 } | |
| 32 | |
| 33 // Verifies that |path_| hashes to |digest|. | |
| 34 void ExpectFileDigestEq(const uint8_t* digest) { | |
| 35 ClientDownloadRequest_Digests digests; | |
| 36 extractor_->ExtractDigest(path_, &digests); | |
| 37 EXPECT_TRUE(digests.has_sha256()); | |
| 38 EXPECT_EQ(std::string(reinterpret_cast<const char*>(digest), | |
| 39 crypto::kSHA256Length), | |
| 40 digests.sha256()); | |
| 41 } | |
| 42 | |
| 43 static const int kBlockSize = 1 << 12; | |
| 44 scoped_refptr<BinaryFeatureExtractor> extractor_; | |
| 45 base::ScopedTempDir temp_dir_; | |
| 46 | |
| 47 // The path to a file that may be hashed. | |
| 48 base::FilePath path_; | |
| 49 }; | |
| 50 | |
| 51 TEST_F(BinaryFeatureExtractorTest, ExtractDigestNoFile) { | |
| 52 base::FilePath no_file = | |
| 53 temp_dir_.path().Append(FILE_PATH_LITERAL("does_not_exist.dll")); | |
| 54 | |
| 55 ClientDownloadRequest_Digests digests; | |
| 56 extractor_->ExtractDigest(no_file, &digests); | |
| 57 EXPECT_FALSE(digests.has_sha256()); | |
| 58 } | |
| 59 | |
| 60 // Hash a file that is less than 1 4k block. | |
| 61 TEST_F(BinaryFeatureExtractorTest, ExtractSmallDigest) { | |
| 62 static const uint8_t kDigest[] = { | |
| 63 0x70, 0x27, 0x7b, 0xad, 0xfc, 0xb9, 0x97, 0x6b, 0x24, 0xf9, 0x80, | |
| 64 0x22, 0x26, 0x2c, 0x31, 0xea, 0x8f, 0xb2, 0x1f, 0x54, 0x93, 0x6b, | |
| 65 0x69, 0x8b, 0x5d, 0x54, 0xd4, 0xd4, 0x21, 0x0b, 0x98, 0xb7}; | |
| 66 | |
| 67 static const char kFileData[] = {"The mountains are robotic."}; | |
| 68 static const int kDataLen = sizeof(kFileData) - 1; | |
| 69 WriteFileToHash(kFileData, kDataLen); | |
| 70 ExpectFileDigestEq(kDigest); | |
| 71 } | |
| 72 | |
| 73 // Hash a file that is exactly 1 4k block. | |
| 74 TEST_F(BinaryFeatureExtractorTest, ExtractOneBlockDigest) { | |
| 75 static const uint8_t kDigest[] = { | |
| 76 0x4f, 0x93, 0x6e, 0xee, 0x89, 0x55, 0xa5, 0xe7, 0x46, 0xd0, 0x61, | |
| 77 0x43, 0x54, 0x5f, 0x33, 0x7b, 0xdc, 0x30, 0x3a, 0x4b, 0x18, 0xb4, | |
| 78 0x82, 0x20, 0xe3, 0x93, 0x4c, 0x65, 0xe0, 0xc1, 0xc0, 0x19}; | |
| 79 | |
| 80 const int kDataLen = kBlockSize; | |
| 81 scoped_ptr<char[]> data(new char[kDataLen]); | |
| 82 memset(data.get(), 71, kDataLen); | |
| 83 WriteFileToHash(data.get(), kDataLen); | |
| 84 ExpectFileDigestEq(kDigest); | |
| 85 } | |
| 86 | |
| 87 // Hash a file that is larger than 1 4k block. | |
| 88 TEST_F(BinaryFeatureExtractorTest, ExtractBigBlockDigest) { | |
| 89 static const uint8_t kDigest[] = { | |
| 90 0xda, 0xae, 0xa0, 0xd5, 0x3b, 0xce, 0x0b, 0x4e, 0x5f, 0x5d, 0x0b, | |
| 91 0xc7, 0x6a, 0x69, 0x0e, 0xf1, 0x8b, 0x2d, 0x20, 0xcd, 0xf2, 0x6d, | |
| 92 0x33, 0xa7, 0x70, 0xf3, 0x6b, 0x85, 0xbf, 0xce, 0x9d, 0x5c}; | |
| 93 | |
| 94 const int kDataLen = kBlockSize + 1; | |
| 95 scoped_ptr<char[]> data(new char[kDataLen]); | |
| 96 memset(data.get(), 71, kDataLen); | |
| 97 WriteFileToHash(data.get(), kDataLen); | |
| 98 ExpectFileDigestEq(kDigest); | |
| 99 } | |
| 100 | |
| 101 } // namespace safe_browsing | |
| OLD | NEW |