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.
5 #include "base/logging.h"
6 #include "base/stl_util.h"
7 #include "components/webcrypto/algorithm_dispatch.h"
8 #include "components/webcrypto/crypto_data.h"
9 #include "components/webcrypto/status.h"
10 #include "components/webcrypto/test/test_helpers.h"
11 #include "components/webcrypto/webcrypto_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
14 #include "third_party/WebKit/public/platform/WebCryptoKey.h"
15 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
21 TEST(WebCryptoShaTest
, DigestSampleSets
) {
22 scoped_ptr
<base::ListValue
> tests
;
23 ASSERT_TRUE(ReadJsonTestFileToList("sha.json", &tests
));
25 for (size_t test_index
= 0; test_index
< tests
->GetSize(); ++test_index
) {
26 SCOPED_TRACE(test_index
);
27 base::DictionaryValue
* test
;
28 ASSERT_TRUE(tests
->GetDictionary(test_index
, &test
));
30 blink::WebCryptoAlgorithm test_algorithm
=
31 GetDigestAlgorithm(test
, "algorithm");
32 std::vector
<uint8_t> test_input
= GetBytesFromHexString(test
, "input");
33 std::vector
<uint8_t> test_output
= GetBytesFromHexString(test
, "output");
35 std::vector
<uint8_t> output
;
36 ASSERT_EQ(Status::Success(),
37 Digest(test_algorithm
, CryptoData(test_input
), &output
));
38 EXPECT_BYTES_EQ(test_output
, output
);
42 TEST(WebCryptoShaTest
, DigestSampleSetsInChunks
) {
43 scoped_ptr
<base::ListValue
> tests
;
44 ASSERT_TRUE(ReadJsonTestFileToList("sha.json", &tests
));
46 for (size_t test_index
= 0; test_index
< tests
->GetSize(); ++test_index
) {
47 SCOPED_TRACE(test_index
);
48 base::DictionaryValue
* test
;
49 ASSERT_TRUE(tests
->GetDictionary(test_index
, &test
));
51 blink::WebCryptoAlgorithm test_algorithm
=
52 GetDigestAlgorithm(test
, "algorithm");
53 std::vector
<uint8_t> test_input
= GetBytesFromHexString(test
, "input");
54 std::vector
<uint8_t> test_output
= GetBytesFromHexString(test
, "output");
56 // Test the chunk version of the digest functions. Test with 129 byte chunks
57 // because the SHA-512 chunk size is 128 bytes.
58 unsigned char* output
;
59 unsigned int output_length
;
60 static const size_t kChunkSizeBytes
= 129;
61 size_t length
= test_input
.size();
62 scoped_ptr
<blink::WebCryptoDigestor
> digestor(
63 CreateDigestor(test_algorithm
.id()));
64 std::vector
<uint8_t>::iterator begin
= test_input
.begin();
65 size_t chunk_index
= 0;
66 while (begin
!= test_input
.end()) {
67 size_t chunk_length
= std::min(kChunkSizeBytes
, length
- chunk_index
);
68 std::vector
<uint8_t> chunk(begin
, begin
+ chunk_length
);
69 ASSERT_TRUE(chunk
.size() > 0);
70 EXPECT_TRUE(digestor
->consume(vector_as_array(&chunk
),
71 static_cast<unsigned int>(chunk
.size())));
72 chunk_index
= chunk_index
+ chunk_length
;
73 begin
= begin
+ chunk_length
;
75 EXPECT_TRUE(digestor
->finish(output
, output_length
));
76 EXPECT_BYTES_EQ(test_output
, CryptoData(output
, output_length
));
82 } // namespace webcrypto