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 | |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 25 | #include <brillo/data_encoding.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 26 | #include <brillo/secure_blob.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 27 | #include <gtest/gtest.h> |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 28 | |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 29 | #include "update_engine/common/test_utils.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 30 | #include "update_engine/common/utils.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 31 | |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 32 | using std::string; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 33 | using std::vector; |
| 34 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 35 | namespace chromeos_update_engine { |
| 36 | |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 37 | // Generated by running this on a linux shell: |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 38 | // $ echo -n hi | openssl dgst -sha256 -binary | |
| 39 | // hexdump -v -e '" " 12/1 "0x%02x, " "\n"' |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 40 | static const uint8_t kExpectedRawHash[] = { |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 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}; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 44 | |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 45 | class HashCalculatorTest : public ::testing::Test {}; |
Han Shen | 2643cb7 | 2012-06-26 14:45:33 -0700 | [diff] [blame] | 46 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 47 | TEST_F(HashCalculatorTest, SimpleTest) { |
| 48 | HashCalculator calc; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 49 | calc.Update("hi", 2); |
| 50 | calc.Finalize(); |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 51 | brillo::Blob raw_hash(std::begin(kExpectedRawHash), |
| 52 | std::end(kExpectedRawHash)); |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 53 | EXPECT_EQ(raw_hash, calc.raw_hash()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 56 | TEST_F(HashCalculatorTest, MultiUpdateTest) { |
| 57 | HashCalculator calc; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 58 | calc.Update("h", 1); |
| 59 | calc.Update("i", 1); |
| 60 | calc.Finalize(); |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 61 | brillo::Blob raw_hash(std::begin(kExpectedRawHash), |
| 62 | std::end(kExpectedRawHash)); |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 63 | EXPECT_EQ(raw_hash, calc.raw_hash()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 66 | TEST_F(HashCalculatorTest, ContextTest) { |
| 67 | HashCalculator calc; |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 68 | calc.Update("h", 1); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 69 | string calc_context = calc.GetContext(); |
| 70 | calc.Finalize(); |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 71 | HashCalculator calc_next; |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 72 | calc_next.SetContext(calc_context); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 73 | calc_next.Update("i", 1); |
| 74 | calc_next.Finalize(); |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 75 | brillo::Blob raw_hash(std::begin(kExpectedRawHash), |
| 76 | std::end(kExpectedRawHash)); |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 77 | EXPECT_EQ(raw_hash, calc_next.raw_hash()); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 80 | TEST_F(HashCalculatorTest, BigTest) { |
| 81 | HashCalculator calc; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 82 | |
Andrew de los Reyes | 21067cc | 2011-06-28 15:27:03 -0700 | [diff] [blame] | 83 | int digit_count = 1; |
| 84 | int next_overflow = 10; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 85 | for (int i = 0; i < 1000000; i++) { |
| 86 | char buf[8]; |
Andrew de los Reyes | 21067cc | 2011-06-28 15:27:03 -0700 | [diff] [blame] | 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; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 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 |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 101 | // done | openssl dgst -sha256 -binary | openssl base64 |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 102 | EXPECT_EQ("NZf8k6SPBkYMvhaX8YgzuMgbkLP1XZ+neM8K5wcSsf8=", |
| 103 | brillo::data_encoding::Base64Encode(calc.raw_hash())); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 106 | TEST_F(HashCalculatorTest, UpdateFileSimpleTest) { |
Amin Hassani | ed03b44 | 2020-10-26 17:21:29 -0700 | [diff] [blame] | 107 | ScopedTempFile data_file("data.XXXXXX"); |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 108 | ASSERT_TRUE(test_utils::WriteFileString(data_file.path(), "hi")); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 109 | |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 110 | for (const int length : {-1, 2, 10}) { |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 111 | HashCalculator calc; |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 112 | EXPECT_EQ(2, calc.UpdateFile(data_file.path(), length)); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 113 | EXPECT_TRUE(calc.Finalize()); |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 114 | brillo::Blob raw_hash(std::begin(kExpectedRawHash), |
| 115 | std::end(kExpectedRawHash)); |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 116 | EXPECT_EQ(raw_hash, calc.raw_hash()); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 119 | HashCalculator calc; |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 120 | EXPECT_EQ(0, calc.UpdateFile(data_file.path(), 0)); |
| 121 | EXPECT_EQ(1, calc.UpdateFile(data_file.path(), 1)); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 122 | EXPECT_TRUE(calc.Finalize()); |
| 123 | // echo -n h | openssl dgst -sha256 -binary | openssl base64 |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 124 | EXPECT_EQ("qqlAJmTxpB9A67xSyZk+tmrrNmYClY/fqig7ceZNsSM=", |
| 125 | brillo::data_encoding::Base64Encode(calc.raw_hash())); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 128 | TEST_F(HashCalculatorTest, RawHashOfFileSimpleTest) { |
Amin Hassani | ed03b44 | 2020-10-26 17:21:29 -0700 | [diff] [blame] | 129 | ScopedTempFile data_file("data.XXXXXX"); |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 130 | ASSERT_TRUE(test_utils::WriteFileString(data_file.path(), "hi")); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 131 | |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 132 | for (const int length : {-1, 2, 10}) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 133 | brillo::Blob exp_raw_hash(std::begin(kExpectedRawHash), |
| 134 | std::end(kExpectedRawHash)); |
| 135 | brillo::Blob raw_hash; |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 136 | EXPECT_EQ( |
| 137 | 2, HashCalculator::RawHashOfFile(data_file.path(), length, &raw_hash)); |
| 138 | EXPECT_EQ(exp_raw_hash, raw_hash); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 142 | TEST_F(HashCalculatorTest, UpdateFileNonexistentTest) { |
| 143 | HashCalculator calc; |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 144 | EXPECT_EQ(-1, calc.UpdateFile("/some/non-existent/file", -1)); |
| 145 | } |
| 146 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 147 | TEST_F(HashCalculatorTest, AbortTest) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 148 | // Just make sure we don't crash and valgrind doesn't detect memory leaks |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 149 | { HashCalculator calc; } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 150 | { |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 151 | HashCalculator calc; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 152 | calc.Update("h", 1); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | } // namespace chromeos_update_engine |