blob: b3dd8323da84ea1bad88ff6038735ca236a6cac0 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2012 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/sha1digest.h"
12#include "rtc_base/gunit.h"
13#include "rtc_base/stringencode.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000014
15namespace rtc {
16
17std::string Sha1(const std::string& input) {
18 Sha1Digest sha1;
19 return ComputeDigest(&sha1, input);
20}
21
22TEST(Sha1DigestTest, TestSize) {
23 Sha1Digest sha1;
24 EXPECT_EQ(20, static_cast<int>(Sha1Digest::kSize));
25 EXPECT_EQ(20U, sha1.Size());
26}
27
28TEST(Sha1DigestTest, TestBasic) {
29 // Test vectors from sha1.c.
30 EXPECT_EQ("da39a3ee5e6b4b0d3255bfef95601890afd80709", Sha1(""));
31 EXPECT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d", Sha1("abc"));
32 EXPECT_EQ("84983e441c3bd26ebaae4aa1f95129e5e54670f1",
33 Sha1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"));
34 std::string a_million_as(1000000, 'a');
35 EXPECT_EQ("34aa973cd4c4daa4f61eeb2bdbad27316534016f", Sha1(a_million_as));
36}
37
38TEST(Sha1DigestTest, TestMultipleUpdates) {
39 Sha1Digest sha1;
40 std::string input =
41 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
42 char output[Sha1Digest::kSize];
43 for (size_t i = 0; i < input.size(); ++i) {
44 sha1.Update(&input[i], 1);
45 }
46 EXPECT_EQ(sha1.Size(), sha1.Finish(output, sizeof(output)));
47 EXPECT_EQ("84983e441c3bd26ebaae4aa1f95129e5e54670f1",
48 hex_encode(output, sizeof(output)));
49}
50
51TEST(Sha1DigestTest, TestReuse) {
52 Sha1Digest sha1;
53 std::string input = "abc";
54 EXPECT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d",
55 ComputeDigest(&sha1, input));
56 input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
57 EXPECT_EQ("84983e441c3bd26ebaae4aa1f95129e5e54670f1",
58 ComputeDigest(&sha1, input));
59}
60
61TEST(Sha1DigestTest, TestBufferTooSmall) {
62 Sha1Digest sha1;
63 std::string input = "abcdefghijklmnopqrstuvwxyz";
64 char output[Sha1Digest::kSize - 1];
65 sha1.Update(input.c_str(), input.size());
66 EXPECT_EQ(0U, sha1.Finish(output, sizeof(output)));
67}
68
69TEST(Sha1DigestTest, TestBufferConst) {
70 Sha1Digest sha1;
71 const int kLongSize = 1000000;
72 std::string input(kLongSize, '\0');
73 for (int i = 0; i < kLongSize; ++i) {
74 input[i] = static_cast<char>(i);
75 }
76 sha1.Update(input.c_str(), input.size());
77 for (int i = 0; i < kLongSize; ++i) {
78 EXPECT_EQ(static_cast<char>(i), input[i]);
79 }
80}
81
82} // namespace rtc