1 /*
2 * libjingle
3 * Copyright 2012, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "talk/base/sha1digest.h"
29 #include "talk/base/gunit.h"
30 #include "talk/base/stringencode.h"
31
32 namespace talk_base {
33
Sha1(const std::string & input)34 std::string Sha1(const std::string& input) {
35 Sha1Digest sha1;
36 return ComputeDigest(&sha1, input);
37 }
38
TEST(Sha1DigestTest,TestSize)39 TEST(Sha1DigestTest, TestSize) {
40 Sha1Digest sha1;
41 EXPECT_EQ(20U, Sha1Digest::kSize);
42 EXPECT_EQ(20U, sha1.Size());
43 }
44
TEST(Sha1DigestTest,TestBasic)45 TEST(Sha1DigestTest, TestBasic) {
46 // Test vectors from sha1.c.
47 EXPECT_EQ("da39a3ee5e6b4b0d3255bfef95601890afd80709", Sha1(""));
48 EXPECT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d", Sha1("abc"));
49 EXPECT_EQ("84983e441c3bd26ebaae4aa1f95129e5e54670f1",
50 Sha1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"));
51 std::string a_million_as(1000000, 'a');
52 EXPECT_EQ("34aa973cd4c4daa4f61eeb2bdbad27316534016f", Sha1(a_million_as));
53 }
54
TEST(Sha1DigestTest,TestMultipleUpdates)55 TEST(Sha1DigestTest, TestMultipleUpdates) {
56 Sha1Digest sha1;
57 std::string input =
58 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
59 char output[Sha1Digest::kSize];
60 for (size_t i = 0; i < input.size(); ++i) {
61 sha1.Update(&input[i], 1);
62 }
63 EXPECT_EQ(sha1.Size(), sha1.Finish(output, sizeof(output)));
64 EXPECT_EQ("84983e441c3bd26ebaae4aa1f95129e5e54670f1",
65 hex_encode(output, sizeof(output)));
66 }
67
TEST(Sha1DigestTest,TestReuse)68 TEST(Sha1DigestTest, TestReuse) {
69 Sha1Digest sha1;
70 std::string input = "abc";
71 EXPECT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d",
72 ComputeDigest(&sha1, input));
73 input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
74 EXPECT_EQ("84983e441c3bd26ebaae4aa1f95129e5e54670f1",
75 ComputeDigest(&sha1, input));
76 }
77
TEST(Sha1DigestTest,TestBufferTooSmall)78 TEST(Sha1DigestTest, TestBufferTooSmall) {
79 Sha1Digest sha1;
80 std::string input = "abc";
81 char output[Sha1Digest::kSize - 1];
82 sha1.Update(input.c_str(), input.size());
83 EXPECT_EQ(0U, sha1.Finish(output, sizeof(output)));
84 }
85
86 } // namespace talk_base
87