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> |
| 7 | #include <glib.h> |
| 8 | #include <gtest/gtest.h> |
| 9 | #include "update_engine/libcurl_http_fetcher.h" |
| 10 | #include "update_engine/omaha_hash_calculator.h" |
| 11 | |
| 12 | namespace chromeos_update_engine { |
| 13 | |
| 14 | class OmahaHashCalculatorTest : public ::testing::Test { }; |
| 15 | |
| 16 | TEST(OmahaHashCalculatorTest, SimpleTest) { |
| 17 | OmahaHashCalculator calc; |
| 18 | calc.Update("hi", 2); |
| 19 | calc.Finalize(); |
| 20 | // Generated by running this on a linux shell: |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame^] | 21 | // $ echo -n hi | openssl dgst -sha256 -binary | openssl base64 |
| 22 | EXPECT_EQ("j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ=", calc.hash()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | TEST(OmahaHashCalculatorTest, MultiUpdateTest) { |
| 26 | OmahaHashCalculator calc; |
| 27 | calc.Update("h", 1); |
| 28 | calc.Update("i", 1); |
| 29 | calc.Finalize(); |
| 30 | // Generated by running this on a linux shell: |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame^] | 31 | // $ echo -n hi | openssl dgst -sha256 -binary | openssl base64 |
| 32 | EXPECT_EQ("j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ=", calc.hash()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | TEST(OmahaHashCalculatorTest, BigTest) { |
| 36 | OmahaHashCalculator calc; |
| 37 | |
| 38 | for (int i = 0; i < 1000000; i++) { |
| 39 | char buf[8]; |
| 40 | ASSERT_EQ(0 == i ? 1 : static_cast<int>(floorf(logf(i) / logf(10))) + 1, |
| 41 | snprintf(buf, sizeof(buf), "%d", i)) << " i = " << i; |
| 42 | calc.Update(buf, strlen(buf)); |
| 43 | } |
| 44 | calc.Finalize(); |
| 45 | |
| 46 | // Hash constant generated by running this on a linux shell: |
| 47 | // $ C=0 |
| 48 | // $ while [ $C -lt 1000000 ]; do |
| 49 | // echo -n $C |
| 50 | // let C=C+1 |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame^] | 51 | // done | openssl dgst -sha256 -binary | openssl base64 |
| 52 | EXPECT_EQ("NZf8k6SPBkYMvhaX8YgzuMgbkLP1XZ+neM8K5wcSsf8=", calc.hash()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | TEST(OmahaHashCalculatorTest, AbortTest) { |
| 56 | // Just make sure we don't crash and valgrind doesn't detect memory leaks |
| 57 | { |
| 58 | OmahaHashCalculator calc; |
| 59 | } |
| 60 | { |
| 61 | OmahaHashCalculator calc; |
| 62 | calc.Update("h", 1); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | } // namespace chromeos_update_engine |