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: |
| 21 | // $ echo -n hi | openssl sha1 -binary | openssl base64 |
| 22 | EXPECT_EQ("witfkXg0JglCjW9RssWvTAveakI=", calc.hash()); |
| 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: |
| 31 | // $ echo -n hi | openssl sha1 -binary | openssl base64 |
| 32 | EXPECT_EQ("witfkXg0JglCjW9RssWvTAveakI=", calc.hash()); |
| 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 |
| 51 | // done | openssl sha1 -binary | openssl base64 |
| 52 | EXPECT_EQ("qdNsMeRqzoEUu5/ABi+MGRli87s=", calc.hash()); |
| 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 |