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 |
| 8 #include <vector> |
| 9 |
7 #include <glib.h> | 10 #include <glib.h> |
8 #include <gtest/gtest.h> | 11 #include <gtest/gtest.h> |
| 12 |
9 #include "update_engine/libcurl_http_fetcher.h" | 13 #include "update_engine/libcurl_http_fetcher.h" |
10 #include "update_engine/omaha_hash_calculator.h" | 14 #include "update_engine/omaha_hash_calculator.h" |
11 | 15 |
| 16 using std::vector; |
| 17 |
12 namespace chromeos_update_engine { | 18 namespace chromeos_update_engine { |
13 | 19 |
14 class OmahaHashCalculatorTest : public ::testing::Test { }; | 20 class OmahaHashCalculatorTest : public ::testing::Test { }; |
15 | 21 |
| 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 |
16 TEST(OmahaHashCalculatorTest, SimpleTest) { | 33 TEST(OmahaHashCalculatorTest, SimpleTest) { |
17 OmahaHashCalculator calc; | 34 OmahaHashCalculator calc; |
18 calc.Update("hi", 2); | 35 calc.Update("hi", 2); |
19 calc.Finalize(); | 36 calc.Finalize(); |
20 // Generated by running this on a linux shell: | 37 EXPECT_EQ(kExpectedHash, calc.hash()); |
21 // $ echo -n hi | openssl dgst -sha256 -binary | openssl base64 | 38 vector<char> raw_hash(kExpectedRawHash, |
22 EXPECT_EQ("j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ=", calc.hash()); | 39 kExpectedRawHash + arraysize(kExpectedRawHash)); |
| 40 EXPECT_TRUE(raw_hash == calc.raw_hash()); |
23 } | 41 } |
24 | 42 |
25 TEST(OmahaHashCalculatorTest, MultiUpdateTest) { | 43 TEST(OmahaHashCalculatorTest, MultiUpdateTest) { |
26 OmahaHashCalculator calc; | 44 OmahaHashCalculator calc; |
27 calc.Update("h", 1); | 45 calc.Update("h", 1); |
28 calc.Update("i", 1); | 46 calc.Update("i", 1); |
29 calc.Finalize(); | 47 calc.Finalize(); |
30 // Generated by running this on a linux shell: | 48 EXPECT_EQ(kExpectedHash, calc.hash()); |
31 // $ echo -n hi | openssl dgst -sha256 -binary | openssl base64 | 49 vector<char> raw_hash(kExpectedRawHash, |
32 EXPECT_EQ("j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ=", calc.hash()); | 50 kExpectedRawHash + arraysize(kExpectedRawHash)); |
| 51 EXPECT_TRUE(raw_hash == calc.raw_hash()); |
33 } | 52 } |
34 | 53 |
35 TEST(OmahaHashCalculatorTest, BigTest) { | 54 TEST(OmahaHashCalculatorTest, BigTest) { |
36 OmahaHashCalculator calc; | 55 OmahaHashCalculator calc; |
37 | 56 |
38 for (int i = 0; i < 1000000; i++) { | 57 for (int i = 0; i < 1000000; i++) { |
39 char buf[8]; | 58 char buf[8]; |
40 ASSERT_EQ(0 == i ? 1 : static_cast<int>(floorf(logf(i) / logf(10))) + 1, | 59 ASSERT_EQ(0 == i ? 1 : static_cast<int>(floorf(logf(i) / logf(10))) + 1, |
41 snprintf(buf, sizeof(buf), "%d", i)) << " i = " << i; | 60 snprintf(buf, sizeof(buf), "%d", i)) << " i = " << i; |
42 calc.Update(buf, strlen(buf)); | 61 calc.Update(buf, strlen(buf)); |
(...skipping 14 matching lines...) Expand all Loading... |
57 { | 76 { |
58 OmahaHashCalculator calc; | 77 OmahaHashCalculator calc; |
59 } | 78 } |
60 { | 79 { |
61 OmahaHashCalculator calc; | 80 OmahaHashCalculator calc; |
62 calc.Update("h", 1); | 81 calc.Update("h", 1); |
63 } | 82 } |
64 } | 83 } |
65 | 84 |
66 } // namespace chromeos_update_engine | 85 } // namespace chromeos_update_engine |
OLD | NEW |