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/test_utils.h"
30 #include "update_engine/common/utils.h"
31
32 using std::string;
33 using std::vector;
34
35 namespace chromeos_update_engine {
36
37 // Generated by running this on a linux shell:
38 // $ echo -n hi | openssl dgst -sha256 -binary |
39 // hexdump -v -e '" " 12/1 "0x%02x, " "\n"'
40 static const uint8_t kExpectedRawHash[] = {
41 0x8f, 0x43, 0x43, 0x46, 0x64, 0x8f, 0x6b, 0x96, 0xdf, 0x89, 0xdd,
42 0xa9, 0x01, 0xc5, 0x17, 0x6b, 0x10, 0xa6, 0xd8, 0x39, 0x61, 0xdd,
43 0x3c, 0x1a, 0xc8, 0x8b, 0x59, 0xb2, 0xdc, 0x32, 0x7a, 0xa4};
44
45 class HashCalculatorTest : public ::testing::Test {};
46
TEST_F(HashCalculatorTest,SimpleTest)47 TEST_F(HashCalculatorTest, SimpleTest) {
48 HashCalculator calc;
49 calc.Update("hi", 2);
50 calc.Finalize();
51 brillo::Blob raw_hash(std::begin(kExpectedRawHash),
52 std::end(kExpectedRawHash));
53 EXPECT_EQ(raw_hash, calc.raw_hash());
54 }
55
TEST_F(HashCalculatorTest,MultiUpdateTest)56 TEST_F(HashCalculatorTest, MultiUpdateTest) {
57 HashCalculator calc;
58 calc.Update("h", 1);
59 calc.Update("i", 1);
60 calc.Finalize();
61 brillo::Blob raw_hash(std::begin(kExpectedRawHash),
62 std::end(kExpectedRawHash));
63 EXPECT_EQ(raw_hash, calc.raw_hash());
64 }
65
TEST_F(HashCalculatorTest,ContextTest)66 TEST_F(HashCalculatorTest, ContextTest) {
67 HashCalculator calc;
68 calc.Update("h", 1);
69 string calc_context = calc.GetContext();
70 calc.Finalize();
71 HashCalculator calc_next;
72 calc_next.SetContext(calc_context);
73 calc_next.Update("i", 1);
74 calc_next.Finalize();
75 brillo::Blob raw_hash(std::begin(kExpectedRawHash),
76 std::end(kExpectedRawHash));
77 EXPECT_EQ(raw_hash, calc_next.raw_hash());
78 }
79
TEST_F(HashCalculatorTest,BigTest)80 TEST_F(HashCalculatorTest, BigTest) {
81 HashCalculator calc;
82
83 int digit_count = 1;
84 int next_overflow = 10;
85 for (int i = 0; i < 1000000; i++) {
86 char buf[8];
87 if (i == next_overflow) {
88 next_overflow *= 10;
89 digit_count++;
90 }
91 ASSERT_EQ(digit_count, snprintf(buf, sizeof(buf), "%d", i)) << " i = " << i;
92 calc.Update(buf, strlen(buf));
93 }
94 calc.Finalize();
95
96 // Hash constant generated by running this on a linux shell:
97 // $ C=0
98 // $ while [ $C -lt 1000000 ]; do
99 // echo -n $C
100 // let C=C+1
101 // done | openssl dgst -sha256 -binary | openssl base64
102 EXPECT_EQ("NZf8k6SPBkYMvhaX8YgzuMgbkLP1XZ+neM8K5wcSsf8=",
103 brillo::data_encoding::Base64Encode(calc.raw_hash()));
104 }
105
TEST_F(HashCalculatorTest,UpdateFileSimpleTest)106 TEST_F(HashCalculatorTest, UpdateFileSimpleTest) {
107 test_utils::ScopedTempFile data_file("data.XXXXXX");
108 ASSERT_TRUE(test_utils::WriteFileString(data_file.path(), "hi"));
109
110 for (const int length : {-1, 2, 10}) {
111 HashCalculator calc;
112 EXPECT_EQ(2, calc.UpdateFile(data_file.path(), length));
113 EXPECT_TRUE(calc.Finalize());
114 brillo::Blob raw_hash(std::begin(kExpectedRawHash),
115 std::end(kExpectedRawHash));
116 EXPECT_EQ(raw_hash, calc.raw_hash());
117 }
118
119 HashCalculator calc;
120 EXPECT_EQ(0, calc.UpdateFile(data_file.path(), 0));
121 EXPECT_EQ(1, calc.UpdateFile(data_file.path(), 1));
122 EXPECT_TRUE(calc.Finalize());
123 // echo -n h | openssl dgst -sha256 -binary | openssl base64
124 EXPECT_EQ("qqlAJmTxpB9A67xSyZk+tmrrNmYClY/fqig7ceZNsSM=",
125 brillo::data_encoding::Base64Encode(calc.raw_hash()));
126 }
127
TEST_F(HashCalculatorTest,RawHashOfFileSimpleTest)128 TEST_F(HashCalculatorTest, RawHashOfFileSimpleTest) {
129 test_utils::ScopedTempFile data_file("data.XXXXXX");
130 ASSERT_TRUE(test_utils::WriteFileString(data_file.path(), "hi"));
131
132 for (const int length : {-1, 2, 10}) {
133 brillo::Blob exp_raw_hash(std::begin(kExpectedRawHash),
134 std::end(kExpectedRawHash));
135 brillo::Blob raw_hash;
136 EXPECT_EQ(
137 2, HashCalculator::RawHashOfFile(data_file.path(), length, &raw_hash));
138 EXPECT_EQ(exp_raw_hash, raw_hash);
139 }
140 }
141
TEST_F(HashCalculatorTest,UpdateFileNonexistentTest)142 TEST_F(HashCalculatorTest, UpdateFileNonexistentTest) {
143 HashCalculator calc;
144 EXPECT_EQ(-1, calc.UpdateFile("/some/non-existent/file", -1));
145 }
146
TEST_F(HashCalculatorTest,AbortTest)147 TEST_F(HashCalculatorTest, AbortTest) {
148 // Just make sure we don't crash and valgrind doesn't detect memory leaks
149 { HashCalculator calc; }
150 {
151 HashCalculator calc;
152 calc.Update("h", 1);
153 }
154 }
155
156 } // namespace chromeos_update_engine
157