| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <math.h> | 5 #include <math.h> |
| 6 #include <unistd.h> | 6 #include <unistd.h> |
| 7 #include <glib.h> | 7 #include <glib.h> |
| 8 #include <gtest/gtest.h> | 8 #include <gtest/gtest.h> |
| 9 #include "update_engine/libcurl_http_fetcher.h" | 9 #include "update_engine/libcurl_http_fetcher.h" |
| 10 #include "update_engine/omaha_hash_calculator.h" | 10 #include "update_engine/omaha_hash_calculator.h" |
| 11 | 11 |
| 12 namespace chromeos_update_engine { | 12 namespace chromeos_update_engine { |
| 13 | 13 |
| 14 class OmahaHashCalculatorTest : public ::testing::Test { }; | 14 class OmahaHashCalculatorTest : public ::testing::Test { }; |
| 15 | 15 |
| 16 TEST(OmahaHashCalculatorTest, SimpleTest) { | 16 TEST(OmahaHashCalculatorTest, SimpleTest) { |
| 17 OmahaHashCalculator calc; | 17 OmahaHashCalculator calc; |
| 18 calc.Update("hi", 2); | 18 calc.Update("hi", 2); |
| 19 calc.Finalize(); | 19 calc.Finalize(); |
| 20 // Generated by running this on a linux shell: | 20 // Generated by running this on a linux shell: |
| 21 // $ echo -n hi | openssl sha1 -binary | openssl base64 | 21 // $ echo -n hi | openssl dgst -sha256 -binary | openssl base64 |
| 22 EXPECT_EQ("witfkXg0JglCjW9RssWvTAveakI=", calc.hash()); | 22 EXPECT_EQ("j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ=", calc.hash()); |
| 23 } | 23 } |
| 24 | 24 |
| 25 TEST(OmahaHashCalculatorTest, MultiUpdateTest) { | 25 TEST(OmahaHashCalculatorTest, MultiUpdateTest) { |
| 26 OmahaHashCalculator calc; | 26 OmahaHashCalculator calc; |
| 27 calc.Update("h", 1); | 27 calc.Update("h", 1); |
| 28 calc.Update("i", 1); | 28 calc.Update("i", 1); |
| 29 calc.Finalize(); | 29 calc.Finalize(); |
| 30 // Generated by running this on a linux shell: | 30 // Generated by running this on a linux shell: |
| 31 // $ echo -n hi | openssl sha1 -binary | openssl base64 | 31 // $ echo -n hi | openssl dgst -sha256 -binary | openssl base64 |
| 32 EXPECT_EQ("witfkXg0JglCjW9RssWvTAveakI=", calc.hash()); | 32 EXPECT_EQ("j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ=", calc.hash()); |
| 33 } | 33 } |
| 34 | 34 |
| 35 TEST(OmahaHashCalculatorTest, BigTest) { | 35 TEST(OmahaHashCalculatorTest, BigTest) { |
| 36 OmahaHashCalculator calc; | 36 OmahaHashCalculator calc; |
| 37 | 37 |
| 38 for (int i = 0; i < 1000000; i++) { | 38 for (int i = 0; i < 1000000; i++) { |
| 39 char buf[8]; | 39 char buf[8]; |
| 40 ASSERT_EQ(0 == i ? 1 : static_cast<int>(floorf(logf(i) / logf(10))) + 1, | 40 ASSERT_EQ(0 == i ? 1 : static_cast<int>(floorf(logf(i) / logf(10))) + 1, |
| 41 snprintf(buf, sizeof(buf), "%d", i)) << " i = " << i; | 41 snprintf(buf, sizeof(buf), "%d", i)) << " i = " << i; |
| 42 calc.Update(buf, strlen(buf)); | 42 calc.Update(buf, strlen(buf)); |
| 43 } | 43 } |
| 44 calc.Finalize(); | 44 calc.Finalize(); |
| 45 | 45 |
| 46 // Hash constant generated by running this on a linux shell: | 46 // Hash constant generated by running this on a linux shell: |
| 47 // $ C=0 | 47 // $ C=0 |
| 48 // $ while [ $C -lt 1000000 ]; do | 48 // $ while [ $C -lt 1000000 ]; do |
| 49 // echo -n $C | 49 // echo -n $C |
| 50 // let C=C+1 | 50 // let C=C+1 |
| 51 // done | openssl sha1 -binary | openssl base64 | 51 // done | openssl dgst -sha256 -binary | openssl base64 |
| 52 EXPECT_EQ("qdNsMeRqzoEUu5/ABi+MGRli87s=", calc.hash()); | 52 EXPECT_EQ("NZf8k6SPBkYMvhaX8YgzuMgbkLP1XZ+neM8K5wcSsf8=", calc.hash()); |
| 53 } | 53 } |
| 54 | 54 |
| 55 TEST(OmahaHashCalculatorTest, AbortTest) { | 55 TEST(OmahaHashCalculatorTest, AbortTest) { |
| 56 // Just make sure we don't crash and valgrind doesn't detect memory leaks | 56 // Just make sure we don't crash and valgrind doesn't detect memory leaks |
| 57 { | 57 { |
| 58 OmahaHashCalculator calc; | 58 OmahaHashCalculator calc; |
| 59 } | 59 } |
| 60 { | 60 { |
| 61 OmahaHashCalculator calc; | 61 OmahaHashCalculator calc; |
| 62 calc.Update("h", 1); | 62 calc.Update("h", 1); |
| 63 } | 63 } |
| 64 } | 64 } |
| 65 | 65 |
| 66 } // namespace chromeos_update_engine | 66 } // namespace chromeos_update_engine |
| OLD | NEW |