blob: 449cb9021e487a728c1a07a5b7e5f72992cb6743 [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
54TEST(OmahaHashCalculatorTest, BigTest) {
55 OmahaHashCalculator calc;
56
57 for (int i = 0; i < 1000000; i++) {
58 char buf[8];
59 ASSERT_EQ(0 == i ? 1 : static_cast<int>(floorf(logf(i) / logf(10))) + 1,
60 snprintf(buf, sizeof(buf), "%d", i)) << " i = " << i;
61 calc.Update(buf, strlen(buf));
62 }
63 calc.Finalize();
64
65 // Hash constant generated by running this on a linux shell:
66 // $ C=0
67 // $ while [ $C -lt 1000000 ]; do
68 // echo -n $C
69 // let C=C+1
Darin Petkovd22cb292010-09-29 10:02:29 -070070 // done | openssl dgst -sha256 -binary | openssl base64
71 EXPECT_EQ("NZf8k6SPBkYMvhaX8YgzuMgbkLP1XZ+neM8K5wcSsf8=", calc.hash());
rspangler@google.com49fdf182009-10-10 00:57:34 +000072}
73
74TEST(OmahaHashCalculatorTest, AbortTest) {
75 // Just make sure we don't crash and valgrind doesn't detect memory leaks
76 {
77 OmahaHashCalculator calc;
78 }
79 {
80 OmahaHashCalculator calc;
81 calc.Update("h", 1);
82 }
83}
84
85} // namespace chromeos_update_engine