blob: 597b21f4d74638b2992d3f21e94a824ad7602250 [file] [log] [blame]
rspangler@google.com49fdf182009-10-10 00:57:34 +00001// 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>
Darin Petkovd7061ab2010-10-06 14:37:09 -07007
8#include <vector>
9
rspangler@google.com49fdf182009-10-10 00:57:34 +000010#include <glib.h>
11#include <gtest/gtest.h>
Darin Petkovd7061ab2010-10-06 14:37:09 -070012
rspangler@google.com49fdf182009-10-10 00:57:34 +000013#include "update_engine/libcurl_http_fetcher.h"
14#include "update_engine/omaha_hash_calculator.h"
15
Darin Petkovd7061ab2010-10-06 14:37:09 -070016using std::vector;
17
rspangler@google.com49fdf182009-10-10 00:57:34 +000018namespace chromeos_update_engine {
19
20class OmahaHashCalculatorTest : public ::testing::Test { };
21
Darin Petkovd7061ab2010-10-06 14:37:09 -070022// Generated by running this on a linux shell:
23// $ echo -n hi | openssl dgst -sha256 -binary | openssl base64
24static const char kExpectedHash[] =
25 "j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ=";
26static 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
rspangler@google.com49fdf182009-10-10 00:57:34 +000033TEST(OmahaHashCalculatorTest, SimpleTest) {
34 OmahaHashCalculator calc;
35 calc.Update("hi", 2);
36 calc.Finalize();
Darin Petkovd7061ab2010-10-06 14:37:09 -070037 EXPECT_EQ(kExpectedHash, calc.hash());
38 vector<char> raw_hash(kExpectedRawHash,
39 kExpectedRawHash + arraysize(kExpectedRawHash));
40 EXPECT_TRUE(raw_hash == calc.raw_hash());
rspangler@google.com49fdf182009-10-10 00:57:34 +000041}
42
43TEST(OmahaHashCalculatorTest, MultiUpdateTest) {
44 OmahaHashCalculator calc;
45 calc.Update("h", 1);
46 calc.Update("i", 1);
47 calc.Finalize();
Darin Petkovd7061ab2010-10-06 14:37:09 -070048 EXPECT_EQ(kExpectedHash, calc.hash());
49 vector<char> raw_hash(kExpectedRawHash,
50 kExpectedRawHash + arraysize(kExpectedRawHash));
51 EXPECT_TRUE(raw_hash == calc.raw_hash());
rspangler@google.com49fdf182009-10-10 00:57:34 +000052}
53
Darin Petkov73058b42010-10-06 16:32:19 -070054TEST(OmahaHashCalculatorTest, ContextTest) {
55 OmahaHashCalculator calc;
56 calc.Update("h", 1);
57 OmahaHashCalculator calc_next;
58 calc_next.SetContext(calc.GetContext());
59 calc_next.Update("i", 1);
60 calc_next.Finalize();
61 // Generated by running this on a linux shell:
62 // $ echo -n hi | openssl dgst -sha256 -binary | openssl base64
63 EXPECT_EQ("j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ=", calc_next.hash());
64}
65
rspangler@google.com49fdf182009-10-10 00:57:34 +000066TEST(OmahaHashCalculatorTest, BigTest) {
67 OmahaHashCalculator calc;
68
69 for (int i = 0; i < 1000000; i++) {
70 char buf[8];
71 ASSERT_EQ(0 == i ? 1 : static_cast<int>(floorf(logf(i) / logf(10))) + 1,
72 snprintf(buf, sizeof(buf), "%d", i)) << " i = " << i;
73 calc.Update(buf, strlen(buf));
74 }
75 calc.Finalize();
76
77 // Hash constant generated by running this on a linux shell:
78 // $ C=0
79 // $ while [ $C -lt 1000000 ]; do
80 // echo -n $C
81 // let C=C+1
Darin Petkovd22cb292010-09-29 10:02:29 -070082 // done | openssl dgst -sha256 -binary | openssl base64
83 EXPECT_EQ("NZf8k6SPBkYMvhaX8YgzuMgbkLP1XZ+neM8K5wcSsf8=", calc.hash());
rspangler@google.com49fdf182009-10-10 00:57:34 +000084}
85
86TEST(OmahaHashCalculatorTest, AbortTest) {
87 // Just make sure we don't crash and valgrind doesn't detect memory leaks
88 {
89 OmahaHashCalculator calc;
90 }
91 {
92 OmahaHashCalculator calc;
93 calc.Update("h", 1);
94 }
95}
96
97} // namespace chromeos_update_engine