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/secure_blob.h> 26#include <gtest/gtest.h> 27 28#include "update_engine/common/utils.h" 29 30using std::string; 31using std::vector; 32 33namespace chromeos_update_engine { 34 35// Generated by running this on a linux shell: 36// $ echo -n hi | openssl dgst -sha256 -binary | openssl base64 37static const char kExpectedHash[] = 38 "j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ="; 39static 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 46class HashCalculatorTest : public ::testing::Test { 47 public: 48 HashCalculatorTest() {} 49}; 50 51TEST_F(HashCalculatorTest, SimpleTest) { 52 HashCalculator calc; 53 calc.Update("hi", 2); 54 calc.Finalize(); 55 EXPECT_EQ(kExpectedHash, calc.hash()); 56 brillo::Blob raw_hash(std::begin(kExpectedRawHash), 57 std::end(kExpectedRawHash)); 58 EXPECT_TRUE(raw_hash == calc.raw_hash()); 59} 60 61TEST_F(HashCalculatorTest, MultiUpdateTest) { 62 HashCalculator calc; 63 calc.Update("h", 1); 64 calc.Update("i", 1); 65 calc.Finalize(); 66 EXPECT_EQ(kExpectedHash, calc.hash()); 67 brillo::Blob raw_hash(std::begin(kExpectedRawHash), 68 std::end(kExpectedRawHash)); 69 EXPECT_TRUE(raw_hash == calc.raw_hash()); 70} 71 72TEST_F(HashCalculatorTest, ContextTest) { 73 HashCalculator calc; 74 calc.Update("h", 1); 75 string calc_context = calc.GetContext(); 76 calc.Finalize(); 77 HashCalculator calc_next; 78 calc_next.SetContext(calc_context); 79 calc_next.Update("i", 1); 80 calc_next.Finalize(); 81 EXPECT_EQ(kExpectedHash, calc_next.hash()); 82 brillo::Blob raw_hash(std::begin(kExpectedRawHash), 83 std::end(kExpectedRawHash)); 84 EXPECT_TRUE(raw_hash == calc_next.raw_hash()); 85} 86 87TEST_F(HashCalculatorTest, BigTest) { 88 HashCalculator calc; 89 90 int digit_count = 1; 91 int next_overflow = 10; 92 for (int i = 0; i < 1000000; i++) { 93 char buf[8]; 94 if (i == next_overflow) { 95 next_overflow *= 10; 96 digit_count++; 97 } 98 ASSERT_EQ(digit_count, snprintf(buf, sizeof(buf), "%d", i)) << " i = " << i; 99 calc.Update(buf, strlen(buf)); 100 } 101 calc.Finalize(); 102 103 // Hash constant generated by running this on a linux shell: 104 // $ C=0 105 // $ while [ $C -lt 1000000 ]; do 106 // echo -n $C 107 // let C=C+1 108 // done | openssl dgst -sha256 -binary | openssl base64 109 EXPECT_EQ("NZf8k6SPBkYMvhaX8YgzuMgbkLP1XZ+neM8K5wcSsf8=", calc.hash()); 110} 111 112TEST_F(HashCalculatorTest, UpdateFileSimpleTest) { 113 string data_path; 114 ASSERT_TRUE( 115 utils::MakeTempFile("data.XXXXXX", &data_path, nullptr)); 116 ScopedPathUnlinker data_path_unlinker(data_path); 117 ASSERT_TRUE(utils::WriteFile(data_path.c_str(), "hi", 2)); 118 119 static const int kLengths[] = { -1, 2, 10 }; 120 for (size_t i = 0; i < arraysize(kLengths); i++) { 121 HashCalculator calc; 122 EXPECT_EQ(2, calc.UpdateFile(data_path, kLengths[i])); 123 EXPECT_TRUE(calc.Finalize()); 124 EXPECT_EQ(kExpectedHash, calc.hash()); 125 brillo::Blob raw_hash(std::begin(kExpectedRawHash), 126 std::end(kExpectedRawHash)); 127 EXPECT_TRUE(raw_hash == calc.raw_hash()); 128 } 129 130 HashCalculator calc; 131 EXPECT_EQ(0, calc.UpdateFile(data_path, 0)); 132 EXPECT_EQ(1, calc.UpdateFile(data_path, 1)); 133 EXPECT_TRUE(calc.Finalize()); 134 // echo -n h | openssl dgst -sha256 -binary | openssl base64 135 EXPECT_EQ("qqlAJmTxpB9A67xSyZk+tmrrNmYClY/fqig7ceZNsSM=", calc.hash()); 136} 137 138TEST_F(HashCalculatorTest, RawHashOfFileSimpleTest) { 139 string data_path; 140 ASSERT_TRUE( 141 utils::MakeTempFile("data.XXXXXX", &data_path, nullptr)); 142 ScopedPathUnlinker data_path_unlinker(data_path); 143 ASSERT_TRUE(utils::WriteFile(data_path.c_str(), "hi", 2)); 144 145 static const int kLengths[] = { -1, 2, 10 }; 146 for (size_t i = 0; i < arraysize(kLengths); i++) { 147 brillo::Blob exp_raw_hash(std::begin(kExpectedRawHash), 148 std::end(kExpectedRawHash)); 149 brillo::Blob raw_hash; 150 EXPECT_EQ(2, HashCalculator::RawHashOfFile(data_path, 151 kLengths[i], 152 &raw_hash)); 153 EXPECT_TRUE(exp_raw_hash == raw_hash); 154 } 155} 156 157TEST_F(HashCalculatorTest, UpdateFileNonexistentTest) { 158 HashCalculator calc; 159 EXPECT_EQ(-1, calc.UpdateFile("/some/non-existent/file", -1)); 160} 161 162TEST_F(HashCalculatorTest, AbortTest) { 163 // Just make sure we don't crash and valgrind doesn't detect memory leaks 164 { 165 HashCalculator calc; 166 } 167 { 168 HashCalculator calc; 169 calc.Update("h", 1); 170 } 171} 172 173} // namespace chromeos_update_engine 174