c7d36a6a10827e1ca0f2634fef72b6eb3009b7ea
[platform/framework/web/crosswalk.git] / src / extensions / browser / computed_hashes_unittest.cc
1 // Copyright 2014 The Chromium 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.
4
5 #include "base/base64.h"
6 #include "base/files/file_path.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "crypto/sha2.h"
9 #include "extensions/browser/computed_hashes.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace {
13
14 // Helper to return base64 encode result by value.
15 std::string Base64Encode(const std::string& data) {
16   std::string result;
17   base::Base64Encode(data, &result);
18   return result;
19 }
20
21 }  // namespace
22
23 namespace extensions {
24
25 TEST(ComputedHashes, ComputedHashes) {
26   base::ScopedTempDir scoped_dir;
27   ASSERT_TRUE(scoped_dir.CreateUniqueTempDir());
28   base::FilePath computed_hashes =
29       scoped_dir.path().AppendASCII("computed_hashes.json");
30
31   // We'll add hashes for 2 files, one of which uses a subdirectory
32   // path. The first file will have a list of 1 block hash, and the
33   // second file will have 2 block hashes.
34   base::FilePath path1(FILE_PATH_LITERAL("foo.txt"));
35   base::FilePath path2 =
36       base::FilePath(FILE_PATH_LITERAL("foo")).AppendASCII("bar.txt");
37   std::vector<std::string> hashes1;
38   std::vector<std::string> hashes2;
39   hashes1.push_back(crypto::SHA256HashString("first"));
40   hashes2.push_back(crypto::SHA256HashString("second"));
41   hashes2.push_back(crypto::SHA256HashString("third"));
42
43   // Write them into the file.
44   ComputedHashes::Writer writer;
45   writer.AddHashes(path1, 4096, hashes1);
46   writer.AddHashes(path2, 2048, hashes2);
47   EXPECT_TRUE(writer.WriteToFile(computed_hashes));
48
49   // Now read them back again and assert that we got what we wrote.
50   ComputedHashes::Reader reader;
51   std::vector<std::string> read_hashes1;
52   std::vector<std::string> read_hashes2;
53   EXPECT_TRUE(reader.InitFromFile(computed_hashes));
54
55   int block_size = 0;
56   EXPECT_TRUE(reader.GetHashes(path1, &block_size, &read_hashes1));
57   EXPECT_EQ(block_size, 4096);
58   block_size = 0;
59   EXPECT_TRUE(reader.GetHashes(path2, &block_size, &read_hashes2));
60   EXPECT_EQ(block_size, 2048);
61
62   EXPECT_EQ(hashes1, read_hashes1);
63   EXPECT_EQ(hashes2, read_hashes2);
64
65   // Finally make sure that we can retrieve the hashes for the subdir
66   // path even when that path contains forward slashes (on windows).
67   base::FilePath path2_fwd_slashes =
68       base::FilePath::FromUTF8Unsafe("foo/bar.txt");
69   block_size = 0;
70   EXPECT_TRUE(reader.GetHashes(path2_fwd_slashes, &block_size, &read_hashes2));
71   EXPECT_EQ(hashes2, read_hashes2);
72 }
73
74 // Note: the expected hashes used in this test were generated using linux
75 // command line tools. E.g., from a bash prompt:
76 //  $ printf "hello world" | openssl dgst -sha256 -binary | base64
77 //
78 // The file with multiple-blocks expectations were generated by doing:
79 // $ for i in `seq 500 ; do printf "hello world" ; done > hello.txt
80 // $ dd if=hello.txt bs=4096 count=1 | openssl dgst -sha256 -binary | base64
81 // $ dd if=hello.txt skip=1 bs=4096 count=1 |
82 //   openssl dgst -sha256 -binary | base64
83 TEST(ComputedHashes, ComputeHashesForContent) {
84   const int block_size = 4096;
85
86   // Simple short input.
87   std::string content1 = "hello world";
88   std::string content1_expected_hash =
89       "uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=";
90   std::vector<std::string> hashes1;
91   ComputedHashes::ComputeHashesForContent(content1, block_size, &hashes1);
92   ASSERT_EQ(1u, hashes1.size());
93   EXPECT_EQ(content1_expected_hash, Base64Encode(hashes1[0]));
94
95   // Multiple blocks input.
96   std::string content2;
97   for (int i = 0; i < 500; i++)
98     content2 += "hello world";
99   const char* content2_expected_hashes[] = {
100       "bvtt5hXo8xvHrlzGAhhoqPL/r+4zJXHx+6wAvkv15V8=",
101       "lTD45F7P6I/HOdi8u7FLRA4qzAYL+7xSNVeusG6MJI0="};
102   std::vector<std::string> hashes2;
103   ComputedHashes::ComputeHashesForContent(content2, block_size, &hashes2);
104   ASSERT_EQ(2u, hashes2.size());
105   EXPECT_EQ(content2_expected_hashes[0], Base64Encode(hashes2[0]));
106   EXPECT_EQ(content2_expected_hashes[1], Base64Encode(hashes2[1]));
107
108   // Now an empty input.
109   std::string content3;
110   std::vector<std::string> hashes3;
111   ComputedHashes::ComputeHashesForContent(content3, block_size, &hashes3);
112   ASSERT_EQ(1u, hashes3.size());
113   ASSERT_EQ(std::string("47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="),
114             Base64Encode(hashes3[0]));
115 }
116
117 }  // namespace extensions