Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: crypto/secure_hash_unittest.cc

Issue 1751603002: [Downloads] Rework how hashes are calculated for download files. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase on top of https://codereview.chromium.org/1781983002 since that's going in first. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « crypto/secure_hash_openssl.cc ('k') | crypto/third_party/nss/sha512.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "crypto/secure_hash.h" 5 #include "crypto/secure_hash.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <string> 10 #include <string>
11 11
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/pickle.h"
14 #include "crypto/sha2.h" 13 #include "crypto/sha2.h"
15 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
16 15
17 TEST(SecureHashTest, TestUpdate) { 16 TEST(SecureHashTest, TestUpdate) {
18 // Example B.3 from FIPS 180-2: long message. 17 // Example B.3 from FIPS 180-2: long message.
19 std::string input3(500000, 'a'); // 'a' repeated half a million times 18 std::string input3(500000, 'a'); // 'a' repeated half a million times
20 int expected3[] = { 0xcd, 0xc7, 0x6e, 0x5c, 19 const int kExpectedHashOfInput3[] = {
21 0x99, 0x14, 0xfb, 0x92, 20 0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92, 0x81, 0xa1, 0xc7,
22 0x81, 0xa1, 0xc7, 0xe2, 21 0xe2, 0x84, 0xd7, 0x3e, 0x67, 0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97,
23 0x84, 0xd7, 0x3e, 0x67, 22 0x20, 0x0e, 0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0};
24 0xf1, 0x80, 0x9a, 0x48,
25 0xa4, 0x97, 0x20, 0x0e,
26 0x04, 0x6d, 0x39, 0xcc,
27 0xc7, 0x11, 0x2c, 0xd0 };
28 23
29 uint8_t output3[crypto::kSHA256Length]; 24 uint8_t output3[crypto::kSHA256Length];
30 25
31 scoped_ptr<crypto::SecureHash> ctx(crypto::SecureHash::Create( 26 scoped_ptr<crypto::SecureHash> ctx(crypto::SecureHash::Create(
32 crypto::SecureHash::SHA256)); 27 crypto::SecureHash::SHA256));
33 ctx->Update(input3.data(), input3.size()); 28 ctx->Update(input3.data(), input3.size());
34 ctx->Update(input3.data(), input3.size()); 29 ctx->Update(input3.data(), input3.size());
35 30
36 ctx->Finish(output3, sizeof(output3)); 31 ctx->Finish(output3, sizeof(output3));
37 for (size_t i = 0; i < crypto::kSHA256Length; i++) 32 for (size_t i = 0; i < crypto::kSHA256Length; i++)
38 EXPECT_EQ(expected3[i], static_cast<int>(output3[i])); 33 EXPECT_EQ(kExpectedHashOfInput3[i], static_cast<int>(output3[i]));
39 } 34 }
40 35
41 // Save the crypto state mid-stream, and create another instance with the 36 TEST(SecureHashTest, TestClone) {
42 // saved state. Then feed the same data afterwards to both.
43 // When done, both should have the same hash value.
44 TEST(SecureHashTest, TestSerialization) {
45 std::string input1(10001, 'a'); // 'a' repeated 10001 times 37 std::string input1(10001, 'a'); // 'a' repeated 10001 times
46 std::string input2(10001, 'b'); // 'b' repeated 10001 times 38 std::string input2(10001, 'd'); // 'd' repeated 10001 times
47 std::string input3(10001, 'c'); // 'c' repeated 10001 times 39
48 std::string input4(10001, 'd'); // 'd' repeated 10001 times 40 const uint8_t kExpectedHashOfInput1[crypto::kSHA256Length] = {
49 std::string input5(10001, 'e'); // 'e' repeated 10001 times 41 0x0c, 0xab, 0x99, 0xa0, 0x58, 0x60, 0x0f, 0xfa, 0xad, 0x12, 0x92,
42 0xd0, 0xc5, 0x3c, 0x05, 0x48, 0xeb, 0xaf, 0x88, 0xdd, 0x1d, 0x01,
43 0x03, 0x03, 0x45, 0x70, 0x5f, 0x01, 0x8a, 0x81, 0x39, 0x09};
44 const uint8_t kExpectedHashOfInput1And2[crypto::kSHA256Length] = {
45 0x4c, 0x8e, 0x26, 0x5a, 0xc3, 0x85, 0x1f, 0x1f, 0xa5, 0x04, 0x1c,
46 0xc7, 0x88, 0x53, 0x1c, 0xc7, 0x80, 0x47, 0x15, 0xfb, 0x47, 0xff,
47 0x72, 0xb1, 0x28, 0x37, 0xb0, 0x4d, 0x6e, 0x22, 0x2e, 0x4d};
50 48
51 uint8_t output1[crypto::kSHA256Length]; 49 uint8_t output1[crypto::kSHA256Length];
52 uint8_t output2[crypto::kSHA256Length]; 50 uint8_t output2[crypto::kSHA256Length];
51 uint8_t output3[crypto::kSHA256Length];
53 52
54 scoped_ptr<crypto::SecureHash> ctx1(crypto::SecureHash::Create( 53 scoped_ptr<crypto::SecureHash> ctx1(crypto::SecureHash::Create(
55 crypto::SecureHash::SHA256)); 54 crypto::SecureHash::SHA256));
56 scoped_ptr<crypto::SecureHash> ctx2(crypto::SecureHash::Create(
57 crypto::SecureHash::SHA256));
58 base::Pickle pickle;
59 ctx1->Update(input1.data(), input1.size()); 55 ctx1->Update(input1.data(), input1.size());
56
57 scoped_ptr<crypto::SecureHash> ctx2(ctx1->Clone());
58 scoped_ptr<crypto::SecureHash> ctx3(ctx2->Clone());
59 // At this point, ctx1, ctx2, and ctx3 are all equivalent and represent the
60 // state after hashing input1.
61
62 // Updating ctx1 and ctx2 with input2 should produce equivalent results.
60 ctx1->Update(input2.data(), input2.size()); 63 ctx1->Update(input2.data(), input2.size());
61 ctx1->Update(input3.data(), input3.size());
62
63 EXPECT_TRUE(ctx1->Serialize(&pickle));
64 ctx1->Update(input4.data(), input4.size());
65 ctx1->Update(input5.data(), input5.size());
66
67 ctx1->Finish(output1, sizeof(output1)); 64 ctx1->Finish(output1, sizeof(output1));
68 65
69 base::PickleIterator data_iterator(pickle); 66 ctx2->Update(input2.data(), input2.size());
70 EXPECT_TRUE(ctx2->Deserialize(&data_iterator));
71 ctx2->Update(input4.data(), input4.size());
72 ctx2->Update(input5.data(), input5.size());
73
74 ctx2->Finish(output2, sizeof(output2)); 67 ctx2->Finish(output2, sizeof(output2));
75 68
76 EXPECT_EQ(0, memcmp(output1, output2, crypto::kSHA256Length)); 69 EXPECT_EQ(0, memcmp(output1, output2, crypto::kSHA256Length));
70 EXPECT_EQ(0,
71 memcmp(output1, kExpectedHashOfInput1And2, crypto::kSHA256Length));
72
73 // Finish() ctx3, which should produce the hash of input1.
74 ctx3->Finish(&output3, sizeof(output3));
75 EXPECT_EQ(0, memcmp(output3, kExpectedHashOfInput1, crypto::kSHA256Length));
77 } 76 }
77
78 TEST(SecureHashTest, TestLength) {
79 scoped_ptr<crypto::SecureHash> ctx(
80 crypto::SecureHash::Create(crypto::SecureHash::SHA256));
81 EXPECT_EQ(crypto::kSHA256Length, ctx->GetHashLength());
82 }
OLDNEW
« no previous file with comments | « crypto/secure_hash_openssl.cc ('k') | crypto/third_party/nss/sha512.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698