Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 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 | // |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame^] | 17 | #include "update_engine/common/hash_calculator.h" |
Alex Deymo | aab50e3 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 18 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 19 | #include <math.h> |
| 20 | #include <unistd.h> |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 21 | |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 22 | #include <string> |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 25 | #include <brillo/secure_blob.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 26 | #include <gtest/gtest.h> |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 27 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame^] | 28 | #include "update_engine/common/libcurl_http_fetcher.h" |
| 29 | #include "update_engine/common/utils.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 30 | |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 31 | using std::string; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 32 | using std::vector; |
| 33 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 34 | namespace chromeos_update_engine { |
| 35 | |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 36 | // Generated by running this on a linux shell: |
| 37 | // $ echo -n hi | openssl dgst -sha256 -binary | openssl base64 |
| 38 | static const char kExpectedHash[] = |
| 39 | "j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ="; |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 40 | static const uint8_t kExpectedRawHash[] = { |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 41 | 0x8f, 0x43, 0x43, 0x46, 0x64, 0x8f, 0x6b, 0x96, |
| 42 | 0xdf, 0x89, 0xdd, 0xa9, 0x01, 0xc5, 0x17, 0x6b, |
| 43 | 0x10, 0xa6, 0xd8, 0x39, 0x61, 0xdd, 0x3c, 0x1a, |
| 44 | 0xc8, 0x8b, 0x59, 0xb2, 0xdc, 0x32, 0x7a, 0xa4 |
| 45 | }; |
| 46 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame^] | 47 | class HashCalculatorTest : public ::testing::Test { |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 48 | public: |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame^] | 49 | HashCalculatorTest() {} |
Han Shen | 2643cb7 | 2012-06-26 14:45:33 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame^] | 52 | TEST_F(HashCalculatorTest, SimpleTest) { |
| 53 | HashCalculator calc; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 54 | calc.Update("hi", 2); |
| 55 | calc.Finalize(); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 56 | EXPECT_EQ(kExpectedHash, calc.hash()); |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 57 | brillo::Blob raw_hash(std::begin(kExpectedRawHash), |
| 58 | std::end(kExpectedRawHash)); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 59 | EXPECT_TRUE(raw_hash == calc.raw_hash()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame^] | 62 | TEST_F(HashCalculatorTest, MultiUpdateTest) { |
| 63 | HashCalculator calc; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 64 | calc.Update("h", 1); |
| 65 | calc.Update("i", 1); |
| 66 | calc.Finalize(); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 67 | EXPECT_EQ(kExpectedHash, calc.hash()); |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 68 | brillo::Blob raw_hash(std::begin(kExpectedRawHash), |
| 69 | std::end(kExpectedRawHash)); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 70 | EXPECT_TRUE(raw_hash == calc.raw_hash()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame^] | 73 | TEST_F(HashCalculatorTest, ContextTest) { |
| 74 | HashCalculator calc; |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 75 | calc.Update("h", 1); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 76 | string calc_context = calc.GetContext(); |
| 77 | calc.Finalize(); |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame^] | 78 | HashCalculator calc_next; |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 79 | calc_next.SetContext(calc_context); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 80 | calc_next.Update("i", 1); |
| 81 | calc_next.Finalize(); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 82 | EXPECT_EQ(kExpectedHash, calc_next.hash()); |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 83 | brillo::Blob raw_hash(std::begin(kExpectedRawHash), |
| 84 | std::end(kExpectedRawHash)); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 85 | EXPECT_TRUE(raw_hash == calc_next.raw_hash()); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame^] | 88 | TEST_F(HashCalculatorTest, BigTest) { |
| 89 | HashCalculator calc; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 90 | |
Andrew de los Reyes | 21067cc | 2011-06-28 15:27:03 -0700 | [diff] [blame] | 91 | int digit_count = 1; |
| 92 | int next_overflow = 10; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 93 | for (int i = 0; i < 1000000; i++) { |
| 94 | char buf[8]; |
Andrew de los Reyes | 21067cc | 2011-06-28 15:27:03 -0700 | [diff] [blame] | 95 | if (i == next_overflow) { |
| 96 | next_overflow *= 10; |
| 97 | digit_count++; |
| 98 | } |
| 99 | ASSERT_EQ(digit_count, snprintf(buf, sizeof(buf), "%d", i)) << " i = " << i; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 100 | calc.Update(buf, strlen(buf)); |
| 101 | } |
| 102 | calc.Finalize(); |
| 103 | |
| 104 | // Hash constant generated by running this on a linux shell: |
| 105 | // $ C=0 |
| 106 | // $ while [ $C -lt 1000000 ]; do |
| 107 | // echo -n $C |
| 108 | // let C=C+1 |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 109 | // done | openssl dgst -sha256 -binary | openssl base64 |
| 110 | EXPECT_EQ("NZf8k6SPBkYMvhaX8YgzuMgbkLP1XZ+neM8K5wcSsf8=", calc.hash()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame^] | 113 | TEST_F(HashCalculatorTest, UpdateFileSimpleTest) { |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 114 | string data_path; |
| 115 | ASSERT_TRUE( |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 116 | utils::MakeTempFile("data.XXXXXX", &data_path, nullptr)); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 117 | ScopedPathUnlinker data_path_unlinker(data_path); |
| 118 | ASSERT_TRUE(utils::WriteFile(data_path.c_str(), "hi", 2)); |
| 119 | |
| 120 | static const int kLengths[] = { -1, 2, 10 }; |
| 121 | for (size_t i = 0; i < arraysize(kLengths); i++) { |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame^] | 122 | HashCalculator calc; |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 123 | EXPECT_EQ(2, calc.UpdateFile(data_path, kLengths[i])); |
| 124 | EXPECT_TRUE(calc.Finalize()); |
| 125 | EXPECT_EQ(kExpectedHash, calc.hash()); |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 126 | brillo::Blob raw_hash(std::begin(kExpectedRawHash), |
| 127 | std::end(kExpectedRawHash)); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 128 | EXPECT_TRUE(raw_hash == calc.raw_hash()); |
| 129 | } |
| 130 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame^] | 131 | HashCalculator calc; |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 132 | EXPECT_EQ(0, calc.UpdateFile(data_path, 0)); |
| 133 | EXPECT_EQ(1, calc.UpdateFile(data_path, 1)); |
| 134 | EXPECT_TRUE(calc.Finalize()); |
| 135 | // echo -n h | openssl dgst -sha256 -binary | openssl base64 |
| 136 | EXPECT_EQ("qqlAJmTxpB9A67xSyZk+tmrrNmYClY/fqig7ceZNsSM=", calc.hash()); |
| 137 | } |
| 138 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame^] | 139 | TEST_F(HashCalculatorTest, RawHashOfFileSimpleTest) { |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 140 | string data_path; |
| 141 | ASSERT_TRUE( |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 142 | utils::MakeTempFile("data.XXXXXX", &data_path, nullptr)); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 143 | ScopedPathUnlinker data_path_unlinker(data_path); |
| 144 | ASSERT_TRUE(utils::WriteFile(data_path.c_str(), "hi", 2)); |
| 145 | |
| 146 | static const int kLengths[] = { -1, 2, 10 }; |
| 147 | for (size_t i = 0; i < arraysize(kLengths); i++) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 148 | brillo::Blob exp_raw_hash(std::begin(kExpectedRawHash), |
| 149 | std::end(kExpectedRawHash)); |
| 150 | brillo::Blob raw_hash; |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame^] | 151 | EXPECT_EQ(2, HashCalculator::RawHashOfFile(data_path, |
| 152 | kLengths[i], |
| 153 | &raw_hash)); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 154 | EXPECT_TRUE(exp_raw_hash == raw_hash); |
| 155 | } |
| 156 | } |
| 157 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame^] | 158 | TEST_F(HashCalculatorTest, UpdateFileNonexistentTest) { |
| 159 | HashCalculator calc; |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 160 | EXPECT_EQ(-1, calc.UpdateFile("/some/non-existent/file", -1)); |
| 161 | } |
| 162 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame^] | 163 | TEST_F(HashCalculatorTest, AbortTest) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 164 | // Just make sure we don't crash and valgrind doesn't detect memory leaks |
| 165 | { |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame^] | 166 | HashCalculator calc; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 167 | } |
| 168 | { |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame^] | 169 | HashCalculator calc; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 170 | calc.Update("h", 1); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | } // namespace chromeos_update_engine |