OLD | NEW |
---|---|
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "base/sha2.h" | 5 #include "base/sha2.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/scoped_ptr.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
9 | 10 |
10 TEST(Sha256Test, Test1) { | 11 TEST(Sha256Test, Test1) { |
11 // Example B.1 from FIPS 180-2: one-block message. | 12 // Example B.1 from FIPS 180-2: one-block message. |
12 std::string input1 = "abc"; | 13 std::string input1 = "abc"; |
13 int expected1[] = { 0xba, 0x78, 0x16, 0xbf, | 14 int expected1[] = { 0xba, 0x78, 0x16, 0xbf, |
14 0x8f, 0x01, 0xcf, 0xea, | 15 0x8f, 0x01, 0xcf, 0xea, |
15 0x41, 0x41, 0x40, 0xde, | 16 0x41, 0x41, 0x40, 0xde, |
16 0x5d, 0xae, 0x22, 0x23, | 17 0x5d, 0xae, 0x22, 0x23, |
17 0xb0, 0x03, 0x61, 0xa3, | 18 0xb0, 0x03, 0x61, 0xa3, |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 uint8 output3[base::SHA256_LENGTH]; | 89 uint8 output3[base::SHA256_LENGTH]; |
89 base::SHA256HashString(input3, output3, sizeof(output3)); | 90 base::SHA256HashString(input3, output3, sizeof(output3)); |
90 for (size_t i = 0; i < base::SHA256_LENGTH; i++) | 91 for (size_t i = 0; i < base::SHA256_LENGTH; i++) |
91 EXPECT_EQ(expected3[i], static_cast<int>(output3[i])); | 92 EXPECT_EQ(expected3[i], static_cast<int>(output3[i])); |
92 | 93 |
93 uint8 output_truncated3[12]; | 94 uint8 output_truncated3[12]; |
94 base::SHA256HashString(input3, output_truncated3, sizeof(output_truncated3)); | 95 base::SHA256HashString(input3, output_truncated3, sizeof(output_truncated3)); |
95 for (size_t i = 0; i < sizeof(output_truncated3); i++) | 96 for (size_t i = 0; i < sizeof(output_truncated3); i++) |
96 EXPECT_EQ(expected3[i], static_cast<int>(output_truncated3[i])); | 97 EXPECT_EQ(expected3[i], static_cast<int>(output_truncated3[i])); |
97 } | 98 } |
99 | |
100 TEST(Sha256Test, TestContext) { | |
wtc
2011/01/21 22:48:51
You'll need to rename this test "TestUpdate" or "T
bulach
2011/01/24 20:54:33
renamed to TestUpdate (in the new file).
| |
101 // Example B.3 from FIPS 180-2: long message. | |
102 std::string input3(500000, 'a'); // 'a' repeated half a million times | |
103 int expected3[] = { 0xcd, 0xc7, 0x6e, 0x5c, | |
104 0x99, 0x14, 0xfb, 0x92, | |
105 0x81, 0xa1, 0xc7, 0xe2, | |
106 0x84, 0xd7, 0x3e, 0x67, | |
107 0xf1, 0x80, 0x9a, 0x48, | |
108 0xa4, 0x97, 0x20, 0x0e, | |
109 0x04, 0x6d, 0x39, 0xcc, | |
110 0xc7, 0x11, 0x2c, 0xd0 }; | |
111 | |
112 uint8 output3[base::SHA256_LENGTH]; | |
113 | |
114 scoped_ptr<base::SHA256Context> ctx(base::SHA256Context::Create()); | |
115 ctx->Update(input3); | |
116 ctx->Update(input3); | |
117 | |
118 ctx->Finish(output3, sizeof(output3)); | |
119 for (size_t i = 0; i < base::SHA256_LENGTH; i++) | |
120 EXPECT_EQ(expected3[i], static_cast<int>(output3[i])); | |
121 } | |
OLD | NEW |