You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "talk/base/sha1digest.h"
  28. #include "talk/base/gunit.h"
  29. #include "talk/base/stringencode.h"
  30. namespace talk_base {
  31. std::string Sha1(const std::string& input) {
  32. Sha1Digest sha1;
  33. return ComputeDigest(&sha1, input);
  34. }
  35. TEST(Sha1DigestTest, TestSize) {
  36. Sha1Digest sha1;
  37. EXPECT_EQ(20U, Sha1Digest::kSize);
  38. EXPECT_EQ(20U, sha1.Size());
  39. }
  40. TEST(Sha1DigestTest, TestBasic) {
  41. // Test vectors from sha1.c.
  42. EXPECT_EQ("da39a3ee5e6b4b0d3255bfef95601890afd80709", Sha1(""));
  43. EXPECT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d", Sha1("abc"));
  44. EXPECT_EQ("84983e441c3bd26ebaae4aa1f95129e5e54670f1",
  45. Sha1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"));
  46. std::string a_million_as(1000000, 'a');
  47. EXPECT_EQ("34aa973cd4c4daa4f61eeb2bdbad27316534016f", Sha1(a_million_as));
  48. }
  49. TEST(Sha1DigestTest, TestMultipleUpdates) {
  50. Sha1Digest sha1;
  51. std::string input =
  52. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  53. char output[Sha1Digest::kSize];
  54. for (size_t i = 0; i < input.size(); ++i) {
  55. sha1.Update(&input[i], 1);
  56. }
  57. EXPECT_EQ(sha1.Size(), sha1.Finish(output, sizeof(output)));
  58. EXPECT_EQ("84983e441c3bd26ebaae4aa1f95129e5e54670f1",
  59. hex_encode(output, sizeof(output)));
  60. }
  61. TEST(Sha1DigestTest, TestReuse) {
  62. Sha1Digest sha1;
  63. std::string input = "abc";
  64. EXPECT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d",
  65. ComputeDigest(&sha1, input));
  66. input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  67. EXPECT_EQ("84983e441c3bd26ebaae4aa1f95129e5e54670f1",
  68. ComputeDigest(&sha1, input));
  69. }
  70. TEST(Sha1DigestTest, TestBufferTooSmall) {
  71. Sha1Digest sha1;
  72. std::string input = "abcdefghijklmnopqrstuvwxyz";
  73. char output[Sha1Digest::kSize - 1];
  74. sha1.Update(input.c_str(), input.size());
  75. EXPECT_EQ(0U, sha1.Finish(output, sizeof(output)));
  76. }
  77. TEST(Sha1DigestTest, TestBufferConst) {
  78. Sha1Digest sha1;
  79. const int kLongSize = 1000000;
  80. std::string input(kLongSize, '\0');
  81. for (int i = 0; i < kLongSize; ++i) {
  82. input[i] = static_cast<char>(i);
  83. }
  84. sha1.Update(input.c_str(), input.size());
  85. for (int i = 0; i < kLongSize; ++i) {
  86. EXPECT_EQ(static_cast<char>(i), input[i]);
  87. }
  88. }
  89. } // namespace talk_base