summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/webrtc/rtc_base/sha1digest_unittest.cc
blob: b3dd8323da84ea1bad88ff6038735ca236a6cac0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 *  Copyright 2012 The WebRTC Project Authors. All rights reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "rtc_base/sha1digest.h"
#include "rtc_base/gunit.h"
#include "rtc_base/stringencode.h"

namespace rtc {

std::string Sha1(const std::string& input) {
  Sha1Digest sha1;
  return ComputeDigest(&sha1, input);
}

TEST(Sha1DigestTest, TestSize) {
  Sha1Digest sha1;
  EXPECT_EQ(20, static_cast<int>(Sha1Digest::kSize));
  EXPECT_EQ(20U, sha1.Size());
}

TEST(Sha1DigestTest, TestBasic) {
  // Test vectors from sha1.c.
  EXPECT_EQ("da39a3ee5e6b4b0d3255bfef95601890afd80709", Sha1(""));
  EXPECT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d", Sha1("abc"));
  EXPECT_EQ("84983e441c3bd26ebaae4aa1f95129e5e54670f1",
            Sha1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"));
  std::string a_million_as(1000000, 'a');
  EXPECT_EQ("34aa973cd4c4daa4f61eeb2bdbad27316534016f", Sha1(a_million_as));
}

TEST(Sha1DigestTest, TestMultipleUpdates) {
  Sha1Digest sha1;
  std::string input =
      "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  char output[Sha1Digest::kSize];
  for (size_t i = 0; i < input.size(); ++i) {
    sha1.Update(&input[i], 1);
  }
  EXPECT_EQ(sha1.Size(), sha1.Finish(output, sizeof(output)));
  EXPECT_EQ("84983e441c3bd26ebaae4aa1f95129e5e54670f1",
            hex_encode(output, sizeof(output)));
}

TEST(Sha1DigestTest, TestReuse) {
  Sha1Digest sha1;
  std::string input = "abc";
  EXPECT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d",
            ComputeDigest(&sha1, input));
  input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  EXPECT_EQ("84983e441c3bd26ebaae4aa1f95129e5e54670f1",
            ComputeDigest(&sha1, input));
}

TEST(Sha1DigestTest, TestBufferTooSmall) {
  Sha1Digest sha1;
  std::string input = "abcdefghijklmnopqrstuvwxyz";
  char output[Sha1Digest::kSize - 1];
  sha1.Update(input.c_str(), input.size());
  EXPECT_EQ(0U, sha1.Finish(output, sizeof(output)));
}

TEST(Sha1DigestTest, TestBufferConst) {
  Sha1Digest sha1;
  const int kLongSize = 1000000;
  std::string input(kLongSize, '\0');
  for (int i = 0; i < kLongSize; ++i) {
    input[i] = static_cast<char>(i);
  }
  sha1.Update(input.c_str(), input.size());
  for (int i = 0; i < kLongSize; ++i) {
    EXPECT_EQ(static_cast<char>(i), input[i]);
  }
}

}  // namespace rtc