Home | History | Annotate | Download | only in base
      1 /*
      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 
     11 #include "webrtc/base/sha1digest.h"
     12 #include "webrtc/base/gunit.h"
     13 #include "webrtc/base/stringencode.h"
     14 
     15 namespace rtc {
     16 
     17 std::string Sha1(const std::string& input) {
     18   Sha1Digest sha1;
     19   return ComputeDigest(&sha1, input);
     20 }
     21 
     22 TEST(Sha1DigestTest, TestSize) {
     23   Sha1Digest sha1;
     24   EXPECT_EQ(20, static_cast<int>(Sha1Digest::kSize));
     25   EXPECT_EQ(20U, sha1.Size());
     26 }
     27 
     28 TEST(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 
     38 TEST(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 
     51 TEST(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 
     61 TEST(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 
     69 TEST(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
     83