1 //
2 // Copyright (C) 2009 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "update_engine/common/hash_calculator.h"
18
19 #include <math.h>
20 #include <unistd.h>
21
22 #include <string>
23 #include <vector>
24
25 #include <brillo/data_encoding.h>
26 #include <brillo/secure_blob.h>
27 #include <gtest/gtest.h>
28
29 #include "update_engine/common/utils.h"
30
31 using std::string;
32 using std::vector;
33
34 namespace chromeos_update_engine {
35
36 // Generated by running this on a linux shell:
37 // $ echo -n hi | openssl dgst -sha256 -binary |
38 // hexdump -v -e '" " 12/1 "0x%02x, " "\n"'
39 static const uint8_t kExpectedRawHash[] = {
40 0x8f, 0x43, 0x43, 0x46, 0x64, 0x8f, 0x6b, 0x96,
41 0xdf, 0x89, 0xdd, 0xa9, 0x01, 0xc5, 0x17, 0x6b,
42 0x10, 0xa6, 0xd8, 0x39, 0x61, 0xdd, 0x3c, 0x1a,
43 0xc8, 0x8b, 0x59, 0xb2, 0xdc, 0x32, 0x7a, 0xa4
44 };
45
46 class HashCalculatorTest : public ::testing::Test {
47 public:
HashCalculatorTest()48 HashCalculatorTest() {}
49 };
50
TEST_F(HashCalculatorTest,SimpleTest)51 TEST_F(HashCalculatorTest, SimpleTest) {
52 HashCalculator calc;
53 calc.Update("hi", 2);
54 calc.Finalize();
55 brillo::Blob raw_hash(std::begin(kExpectedRawHash),
56 std::end(kExpectedRawHash));
57 EXPECT_TRUE(raw_hash == calc.raw_hash());
58 }
59
TEST_F(HashCalculatorTest,MultiUpdateTest)60 TEST_F(HashCalculatorTest, MultiUpdateTest) {
61 HashCalculator calc;
62 calc.Update("h", 1);
63 calc.Update("i", 1);
64 calc.Finalize();
65 brillo::Blob raw_hash(std::begin(kExpectedRawHash),
66 std::end(kExpectedRawHash));
67 EXPECT_TRUE(raw_hash == calc.raw_hash());
68 }
69
TEST_F(HashCalculatorTest,ContextTest)70 TEST_F(HashCalculatorTest, ContextTest) {
71 HashCalculator calc;
72 calc.Update("h", 1);
73 string calc_context = calc.GetContext();
74 calc.Finalize();
75 HashCalculator calc_next;
76 calc_next.SetContext(calc_context);
77 calc_next.Update("i", 1);
78 calc_next.Finalize();
79 brillo::Blob raw_hash(std::begin(kExpectedRawHash),
80 std::end(kExpectedRawHash));
81 EXPECT_TRUE(raw_hash == calc_next.raw_hash());
82 }
83
TEST_F(HashCalculatorTest,BigTest)84 TEST_F(HashCalculatorTest, BigTest) {
85 HashCalculator calc;
86
87 int digit_count = 1;
88 int next_overflow = 10;
89 for (int i = 0; i < 1000000; i++) {
90 char buf[8];
91 if (i == next_overflow) {
92 next_overflow *= 10;
93 digit_count++;
94 }
95 ASSERT_EQ(digit_count, snprintf(buf, sizeof(buf), "%d", i)) << " i = " << i;
96 calc.Update(buf, strlen(buf));
97 }
98 calc.Finalize();
99
100 // Hash constant generated by running this on a linux shell:
101 // $ C=0
102 // $ while [ $C -lt 1000000 ]; do
103 // echo -n $C
104 // let C=C+1
105 // done | openssl dgst -sha256 -binary | openssl base64
106 EXPECT_EQ("NZf8k6SPBkYMvhaX8YgzuMgbkLP1XZ+neM8K5wcSsf8=",
107 brillo::data_encoding::Base64Encode(calc.raw_hash()));
108 }
109
TEST_F(HashCalculatorTest,UpdateFileSimpleTest)110 TEST_F(HashCalculatorTest, UpdateFileSimpleTest) {
111 string data_path;
112 ASSERT_TRUE(
113 utils::MakeTempFile("data.XXXXXX", &data_path, nullptr));
114 ScopedPathUnlinker data_path_unlinker(data_path);
115 ASSERT_TRUE(utils::WriteFile(data_path.c_str(), "hi", 2));
116
117 static const int kLengths[] = { -1, 2, 10 };
118 for (size_t i = 0; i < arraysize(kLengths); i++) {
119 HashCalculator calc;
120 EXPECT_EQ(2, calc.UpdateFile(data_path, kLengths[i]));
121 EXPECT_TRUE(calc.Finalize());
122 brillo::Blob raw_hash(std::begin(kExpectedRawHash),
123 std::end(kExpectedRawHash));
124 EXPECT_TRUE(raw_hash == calc.raw_hash());
125 }
126
127 HashCalculator calc;
128 EXPECT_EQ(0, calc.UpdateFile(data_path, 0));
129 EXPECT_EQ(1, calc.UpdateFile(data_path, 1));
130 EXPECT_TRUE(calc.Finalize());
131 // echo -n h | openssl dgst -sha256 -binary | openssl base64
132 EXPECT_EQ("qqlAJmTxpB9A67xSyZk+tmrrNmYClY/fqig7ceZNsSM=",
133 brillo::data_encoding::Base64Encode(calc.raw_hash()));
134 }
135
TEST_F(HashCalculatorTest,RawHashOfFileSimpleTest)136 TEST_F(HashCalculatorTest, RawHashOfFileSimpleTest) {
137 string data_path;
138 ASSERT_TRUE(
139 utils::MakeTempFile("data.XXXXXX", &data_path, nullptr));
140 ScopedPathUnlinker data_path_unlinker(data_path);
141 ASSERT_TRUE(utils::WriteFile(data_path.c_str(), "hi", 2));
142
143 static const int kLengths[] = { -1, 2, 10 };
144 for (size_t i = 0; i < arraysize(kLengths); i++) {
145 brillo::Blob exp_raw_hash(std::begin(kExpectedRawHash),
146 std::end(kExpectedRawHash));
147 brillo::Blob raw_hash;
148 EXPECT_EQ(2, HashCalculator::RawHashOfFile(data_path,
149 kLengths[i],
150 &raw_hash));
151 EXPECT_TRUE(exp_raw_hash == raw_hash);
152 }
153 }
154
TEST_F(HashCalculatorTest,UpdateFileNonexistentTest)155 TEST_F(HashCalculatorTest, UpdateFileNonexistentTest) {
156 HashCalculator calc;
157 EXPECT_EQ(-1, calc.UpdateFile("/some/non-existent/file", -1));
158 }
159
TEST_F(HashCalculatorTest,AbortTest)160 TEST_F(HashCalculatorTest, AbortTest) {
161 // Just make sure we don't crash and valgrind doesn't detect memory leaks
162 {
163 HashCalculator calc;
164 }
165 {
166 HashCalculator calc;
167 calc.Update("h", 1);
168 }
169 }
170
171 } // namespace chromeos_update_engine
172