作者: 韩晨旭 10225101440 李畅 10225102463
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.

39 lines
1.0 KiB

  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #include "port/port.h"
  5. #include "util/testharness.h"
  6. namespace leveldb {
  7. namespace port {
  8. class SHA1 { };
  9. static std::string TestSHA1(const char* data, size_t len) {
  10. char hash_val[20];
  11. SHA1_Hash(data, len, hash_val);
  12. char buf[41];
  13. for (int i = 0; i < 20; i++) {
  14. snprintf(buf + i * 2, 41 - i * 2,
  15. "%02x",
  16. static_cast<unsigned int>(static_cast<unsigned char>(
  17. hash_val[i])));
  18. }
  19. return std::string(buf, 40);
  20. }
  21. TEST(SHA1, Simple) {
  22. ASSERT_EQ("da39a3ee5e6b4b0d3255bfef95601890afd80709", TestSHA1("", 0));
  23. ASSERT_EQ("aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d", TestSHA1("hello", 5));
  24. std::string x(10000, 'x');
  25. ASSERT_EQ("f8c5cde791c5056cf515881e701c8a9ecb439a75",
  26. TestSHA1(x.data(), x.size()));
  27. }
  28. }
  29. }
  30. int main(int argc, char** argv) {
  31. return leveldb::test::RunAllTests();
  32. }