Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Unified Diff: base/sha1_unittest.cc

Issue 1460513002: Added SHA1HashFile unittests. (Closed) Base URL: https://github.com/domokit/mojo.git@base_tests
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/sha1_unittest.cc
diff --git a/base/sha1_unittest.cc b/base/sha1_unittest.cc
index b29fe4662aef30636ecf0086bca33cc6026887cf..67983d12d1c6e84ce5db35168af8e6871b1f81ac 100644
--- a/base/sha1_unittest.cc
+++ b/base/sha1_unittest.cc
@@ -7,6 +7,7 @@
#include <string>
#include "base/basictypes.h"
+#include "base/files/scoped_file.h"
#include "testing/gtest/include/gtest/gtest.h"
TEST(SHA1Test, Test1) {
@@ -106,3 +107,64 @@ TEST(SHA1Test, Test3Bytes) {
for (size_t i = 0; i < base::kSHA1Length; i++)
EXPECT_EQ(expected[i], output[i]);
}
+
+// Helper function to test SHA1HashFile.
+void doHashFile(const std::string& input, unsigned char *output) {
Petr Hosek 2015/12/02 22:51:31 Nit: * belongs to type.
Sean Klein 2015/12/02 23:01:22 Done.
+ std::string file_name = "sha1_test_data.txt";
+ base::ScopedFILE file(fopen(file_name.c_str(), "w+"));
+ EXPECT_NE(nullptr, file);
+ EXPECT_EQ(input.length(),
+ fwrite(input.c_str(), 1, input.length(), file.get()));
Petr Hosek 2015/12/02 22:51:31 Nit: I'd use sizeof(char) instead of 1.
Sean Klein 2015/12/02 23:01:22 Done.
+ EXPECT_EQ(0, fflush(file.get()));
+ EXPECT_EQ(true, base::SHA1HashFile(file_name, output));
+ EXPECT_EQ(0, unlink(file_name.c_str()));
+}
+
+TEST(SHA1Test, Test1File) {
+ // Example A.1 from FIPS 180-2: one-block message.
+ std::string input = "abc";
+ unsigned char output[base::kSHA1Length];
+
+ unsigned char expected[] = { 0xa9, 0x99, 0x3e, 0x36,
Petr Hosek 2015/12/02 22:51:31 This should be const.
Sean Klein 2015/12/02 23:01:22 Done.
+ 0x47, 0x06, 0x81, 0x6a,
+ 0xba, 0x3e, 0x25, 0x71,
+ 0x78, 0x50, 0xc2, 0x6c,
+ 0x9c, 0xd0, 0xd8, 0x9d };
+
+ doHashFile(input, output);
+ for (size_t i = 0; i < base::kSHA1Length; i++)
+ EXPECT_EQ(expected[i], output[i]);
+}
+
+TEST(SHA1Test, Test2File) {
+ // Example A.2 from FIPS 180-2: multi-block message.
+ std::string input =
+ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
+ unsigned char output[base::kSHA1Length];
+
+ unsigned char expected[] = { 0x84, 0x98, 0x3e, 0x44,
Petr Hosek 2015/12/02 22:51:31 ditto
Sean Klein 2015/12/02 23:01:21 Done.
+ 0x1c, 0x3b, 0xd2, 0x6e,
+ 0xba, 0xae, 0x4a, 0xa1,
+ 0xf9, 0x51, 0x29, 0xe5,
+ 0xe5, 0x46, 0x70, 0xf1 };
+
+ doHashFile(input, output);
+ for (size_t i = 0; i < base::kSHA1Length; i++)
+ EXPECT_EQ(expected[i], output[i]);
+}
+
+TEST(SHA1Test, Test3File) {
+ // Example A.3 from FIPS 180-2: long message.
+ std::string input(1000000, 'a');
+ unsigned char output[base::kSHA1Length];
+
+ unsigned char expected[] = { 0x34, 0xaa, 0x97, 0x3c,
Petr Hosek 2015/12/02 22:51:32 ditto
Sean Klein 2015/12/02 23:01:22 Done.
+ 0xd4, 0xc4, 0xda, 0xa4,
+ 0xf6, 0x1e, 0xeb, 0x2b,
+ 0xdb, 0xad, 0x27, 0x31,
+ 0x65, 0x34, 0x01, 0x6f };
+
+ doHashFile(input, output);
+ for (size_t i = 0; i < base::kSHA1Length; i++)
+ EXPECT_EQ(expected[i], output[i]);
+}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698