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