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 | |
| 8 | #include <vector> |
| 9 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 10 | #include <glib.h> |
| 11 | #include <gtest/gtest.h> |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 12 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 13 | #include "update_engine/libcurl_http_fetcher.h" |
| 14 | #include "update_engine/omaha_hash_calculator.h" |
| 15 | |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 16 | using std::vector; |
| 17 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 18 | namespace chromeos_update_engine { |
| 19 | |
| 20 | class OmahaHashCalculatorTest : public ::testing::Test { }; |
| 21 | |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 22 | // Generated by running this on a linux shell: |
| 23 | // $ echo -n hi | openssl dgst -sha256 -binary | openssl base64 |
| 24 | static const char kExpectedHash[] = |
| 25 | "j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ="; |
| 26 | static const char kExpectedRawHash[] = { |
| 27 | 0x8f, 0x43, 0x43, 0x46, 0x64, 0x8f, 0x6b, 0x96, |
| 28 | 0xdf, 0x89, 0xdd, 0xa9, 0x01, 0xc5, 0x17, 0x6b, |
| 29 | 0x10, 0xa6, 0xd8, 0x39, 0x61, 0xdd, 0x3c, 0x1a, |
| 30 | 0xc8, 0x8b, 0x59, 0xb2, 0xdc, 0x32, 0x7a, 0xa4 |
| 31 | }; |
| 32 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 33 | TEST(OmahaHashCalculatorTest, SimpleTest) { |
| 34 | OmahaHashCalculator calc; |
| 35 | calc.Update("hi", 2); |
| 36 | calc.Finalize(); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 37 | EXPECT_EQ(kExpectedHash, calc.hash()); |
| 38 | vector<char> raw_hash(kExpectedRawHash, |
| 39 | kExpectedRawHash + arraysize(kExpectedRawHash)); |
| 40 | EXPECT_TRUE(raw_hash == calc.raw_hash()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | TEST(OmahaHashCalculatorTest, MultiUpdateTest) { |
| 44 | OmahaHashCalculator calc; |
| 45 | calc.Update("h", 1); |
| 46 | calc.Update("i", 1); |
| 47 | calc.Finalize(); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 48 | EXPECT_EQ(kExpectedHash, calc.hash()); |
| 49 | vector<char> raw_hash(kExpectedRawHash, |
| 50 | kExpectedRawHash + arraysize(kExpectedRawHash)); |
| 51 | EXPECT_TRUE(raw_hash == calc.raw_hash()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame^] | 54 | TEST(OmahaHashCalculatorTest, ContextTest) { |
| 55 | OmahaHashCalculator calc; |
| 56 | calc.Update("h", 1); |
| 57 | OmahaHashCalculator calc_next; |
| 58 | calc_next.SetContext(calc.GetContext()); |
| 59 | calc_next.Update("i", 1); |
| 60 | calc_next.Finalize(); |
| 61 | // Generated by running this on a linux shell: |
| 62 | // $ echo -n hi | openssl dgst -sha256 -binary | openssl base64 |
| 63 | EXPECT_EQ("j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ=", calc_next.hash()); |
| 64 | } |
| 65 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 66 | TEST(OmahaHashCalculatorTest, BigTest) { |
| 67 | OmahaHashCalculator calc; |
| 68 | |
| 69 | for (int i = 0; i < 1000000; i++) { |
| 70 | char buf[8]; |
| 71 | ASSERT_EQ(0 == i ? 1 : static_cast<int>(floorf(logf(i) / logf(10))) + 1, |
| 72 | snprintf(buf, sizeof(buf), "%d", i)) << " i = " << i; |
| 73 | calc.Update(buf, strlen(buf)); |
| 74 | } |
| 75 | calc.Finalize(); |
| 76 | |
| 77 | // Hash constant generated by running this on a linux shell: |
| 78 | // $ C=0 |
| 79 | // $ while [ $C -lt 1000000 ]; do |
| 80 | // echo -n $C |
| 81 | // let C=C+1 |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 82 | // done | openssl dgst -sha256 -binary | openssl base64 |
| 83 | EXPECT_EQ("NZf8k6SPBkYMvhaX8YgzuMgbkLP1XZ+neM8K5wcSsf8=", calc.hash()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | TEST(OmahaHashCalculatorTest, AbortTest) { |
| 87 | // Just make sure we don't crash and valgrind doesn't detect memory leaks |
| 88 | { |
| 89 | OmahaHashCalculator calc; |
| 90 | } |
| 91 | { |
| 92 | OmahaHashCalculator calc; |
| 93 | calc.Update("h", 1); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | } // namespace chromeos_update_engine |