| 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 | 7 |
| 8 #include <string> |
| 8 #include <vector> | 9 #include <vector> |
| 9 | 10 |
| 10 #include <glib.h> | 11 #include <glib.h> |
| 11 #include <gtest/gtest.h> | 12 #include <gtest/gtest.h> |
| 12 | 13 |
| 13 #include "update_engine/libcurl_http_fetcher.h" | 14 #include "update_engine/libcurl_http_fetcher.h" |
| 14 #include "update_engine/omaha_hash_calculator.h" | 15 #include "update_engine/omaha_hash_calculator.h" |
| 16 #include "update_engine/utils.h" |
| 15 | 17 |
| 18 using std::string; |
| 16 using std::vector; | 19 using std::vector; |
| 17 | 20 |
| 18 namespace chromeos_update_engine { | 21 namespace chromeos_update_engine { |
| 19 | 22 |
| 20 class OmahaHashCalculatorTest : public ::testing::Test { }; | 23 class OmahaHashCalculatorTest : public ::testing::Test { }; |
| 21 | 24 |
| 22 // Generated by running this on a linux shell: | 25 // Generated by running this on a linux shell: |
| 23 // $ echo -n hi | openssl dgst -sha256 -binary | openssl base64 | 26 // $ echo -n hi | openssl dgst -sha256 -binary | openssl base64 |
| 24 static const char kExpectedHash[] = | 27 static const char kExpectedHash[] = |
| 25 "j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ="; | 28 "j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ="; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 47 calc.Finalize(); | 50 calc.Finalize(); |
| 48 EXPECT_EQ(kExpectedHash, calc.hash()); | 51 EXPECT_EQ(kExpectedHash, calc.hash()); |
| 49 vector<char> raw_hash(kExpectedRawHash, | 52 vector<char> raw_hash(kExpectedRawHash, |
| 50 kExpectedRawHash + arraysize(kExpectedRawHash)); | 53 kExpectedRawHash + arraysize(kExpectedRawHash)); |
| 51 EXPECT_TRUE(raw_hash == calc.raw_hash()); | 54 EXPECT_TRUE(raw_hash == calc.raw_hash()); |
| 52 } | 55 } |
| 53 | 56 |
| 54 TEST(OmahaHashCalculatorTest, ContextTest) { | 57 TEST(OmahaHashCalculatorTest, ContextTest) { |
| 55 OmahaHashCalculator calc; | 58 OmahaHashCalculator calc; |
| 56 calc.Update("h", 1); | 59 calc.Update("h", 1); |
| 60 string calc_context = calc.GetContext(); |
| 61 calc.Finalize(); |
| 57 OmahaHashCalculator calc_next; | 62 OmahaHashCalculator calc_next; |
| 58 calc_next.SetContext(calc.GetContext()); | 63 calc_next.SetContext(calc_context); |
| 59 calc_next.Update("i", 1); | 64 calc_next.Update("i", 1); |
| 60 calc_next.Finalize(); | 65 calc_next.Finalize(); |
| 61 // Generated by running this on a linux shell: | 66 EXPECT_EQ(kExpectedHash, calc_next.hash()); |
| 62 // $ echo -n hi | openssl dgst -sha256 -binary | openssl base64 | 67 vector<char> raw_hash(kExpectedRawHash, |
| 63 EXPECT_EQ("j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ=", calc_next.hash()); | 68 kExpectedRawHash + arraysize(kExpectedRawHash)); |
| 69 EXPECT_TRUE(raw_hash == calc_next.raw_hash()); |
| 64 } | 70 } |
| 65 | 71 |
| 66 TEST(OmahaHashCalculatorTest, BigTest) { | 72 TEST(OmahaHashCalculatorTest, BigTest) { |
| 67 OmahaHashCalculator calc; | 73 OmahaHashCalculator calc; |
| 68 | 74 |
| 69 for (int i = 0; i < 1000000; i++) { | 75 for (int i = 0; i < 1000000; i++) { |
| 70 char buf[8]; | 76 char buf[8]; |
| 71 ASSERT_EQ(0 == i ? 1 : static_cast<int>(floorf(logf(i) / logf(10))) + 1, | 77 ASSERT_EQ(0 == i ? 1 : static_cast<int>(floorf(logf(i) / logf(10))) + 1, |
| 72 snprintf(buf, sizeof(buf), "%d", i)) << " i = " << i; | 78 snprintf(buf, sizeof(buf), "%d", i)) << " i = " << i; |
| 73 calc.Update(buf, strlen(buf)); | 79 calc.Update(buf, strlen(buf)); |
| 74 } | 80 } |
| 75 calc.Finalize(); | 81 calc.Finalize(); |
| 76 | 82 |
| 77 // Hash constant generated by running this on a linux shell: | 83 // Hash constant generated by running this on a linux shell: |
| 78 // $ C=0 | 84 // $ C=0 |
| 79 // $ while [ $C -lt 1000000 ]; do | 85 // $ while [ $C -lt 1000000 ]; do |
| 80 // echo -n $C | 86 // echo -n $C |
| 81 // let C=C+1 | 87 // let C=C+1 |
| 82 // done | openssl dgst -sha256 -binary | openssl base64 | 88 // done | openssl dgst -sha256 -binary | openssl base64 |
| 83 EXPECT_EQ("NZf8k6SPBkYMvhaX8YgzuMgbkLP1XZ+neM8K5wcSsf8=", calc.hash()); | 89 EXPECT_EQ("NZf8k6SPBkYMvhaX8YgzuMgbkLP1XZ+neM8K5wcSsf8=", calc.hash()); |
| 84 } | 90 } |
| 85 | 91 |
| 92 TEST(OmahaHashCalculatorTest, UpdateFileSimpleTest) { |
| 93 string data_path; |
| 94 ASSERT_TRUE( |
| 95 utils::MakeTempFile("/tmp/data.XXXXXX", &data_path, NULL)); |
| 96 ScopedPathUnlinker data_path_unlinker(data_path); |
| 97 ASSERT_TRUE(utils::WriteFile(data_path.c_str(), "hi", 2)); |
| 98 |
| 99 static const int kLengths[] = { -1, 2, 10 }; |
| 100 for (size_t i = 0; i < arraysize(kLengths); i++) { |
| 101 OmahaHashCalculator calc; |
| 102 EXPECT_EQ(2, calc.UpdateFile(data_path, kLengths[i])); |
| 103 EXPECT_TRUE(calc.Finalize()); |
| 104 EXPECT_EQ(kExpectedHash, calc.hash()); |
| 105 vector<char> raw_hash(kExpectedRawHash, |
| 106 kExpectedRawHash + arraysize(kExpectedRawHash)); |
| 107 EXPECT_TRUE(raw_hash == calc.raw_hash()); |
| 108 } |
| 109 |
| 110 OmahaHashCalculator calc; |
| 111 EXPECT_EQ(0, calc.UpdateFile(data_path, 0)); |
| 112 EXPECT_EQ(1, calc.UpdateFile(data_path, 1)); |
| 113 EXPECT_TRUE(calc.Finalize()); |
| 114 // echo -n h | openssl dgst -sha256 -binary | openssl base64 |
| 115 EXPECT_EQ("qqlAJmTxpB9A67xSyZk+tmrrNmYClY/fqig7ceZNsSM=", calc.hash()); |
| 116 } |
| 117 |
| 118 TEST(OmahaHashCalculatorTest, UpdateFileNonexistentTest) { |
| 119 OmahaHashCalculator calc; |
| 120 EXPECT_EQ(-1, calc.UpdateFile("/some/non-existent/file", -1)); |
| 121 } |
| 122 |
| 86 TEST(OmahaHashCalculatorTest, AbortTest) { | 123 TEST(OmahaHashCalculatorTest, AbortTest) { |
| 87 // Just make sure we don't crash and valgrind doesn't detect memory leaks | 124 // Just make sure we don't crash and valgrind doesn't detect memory leaks |
| 88 { | 125 { |
| 89 OmahaHashCalculator calc; | 126 OmahaHashCalculator calc; |
| 90 } | 127 } |
| 91 { | 128 { |
| 92 OmahaHashCalculator calc; | 129 OmahaHashCalculator calc; |
| 93 calc.Update("h", 1); | 130 calc.Update("h", 1); |
| 94 } | 131 } |
| 95 } | 132 } |
| 96 | 133 |
| 97 } // namespace chromeos_update_engine | 134 } // namespace chromeos_update_engine |
| OLD | NEW |