blob: d228f12641ddab3d3f048fa94c70fa2f5ad9453c [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>
7#include <glib.h>
8#include <gtest/gtest.h>
9#include "update_engine/libcurl_http_fetcher.h"
10#include "update_engine/omaha_hash_calculator.h"
11
12namespace chromeos_update_engine {
13
14class OmahaHashCalculatorTest : public ::testing::Test { };
15
16TEST(OmahaHashCalculatorTest, SimpleTest) {
17 OmahaHashCalculator calc;
18 calc.Update("hi", 2);
19 calc.Finalize();
20 // Generated by running this on a linux shell:
Darin Petkovd22cb292010-09-29 10:02:29 -070021 // $ echo -n hi | openssl dgst -sha256 -binary | openssl base64
22 EXPECT_EQ("j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ=", calc.hash());
rspangler@google.com49fdf182009-10-10 00:57:34 +000023}
24
25TEST(OmahaHashCalculatorTest, MultiUpdateTest) {
26 OmahaHashCalculator calc;
27 calc.Update("h", 1);
28 calc.Update("i", 1);
29 calc.Finalize();
30 // Generated by running this on a linux shell:
Darin Petkovd22cb292010-09-29 10:02:29 -070031 // $ echo -n hi | openssl dgst -sha256 -binary | openssl base64
32 EXPECT_EQ("j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ=", calc.hash());
rspangler@google.com49fdf182009-10-10 00:57:34 +000033}
34
35TEST(OmahaHashCalculatorTest, BigTest) {
36 OmahaHashCalculator calc;
37
38 for (int i = 0; i < 1000000; i++) {
39 char buf[8];
40 ASSERT_EQ(0 == i ? 1 : static_cast<int>(floorf(logf(i) / logf(10))) + 1,
41 snprintf(buf, sizeof(buf), "%d", i)) << " i = " << i;
42 calc.Update(buf, strlen(buf));
43 }
44 calc.Finalize();
45
46 // Hash constant generated by running this on a linux shell:
47 // $ C=0
48 // $ while [ $C -lt 1000000 ]; do
49 // echo -n $C
50 // let C=C+1
Darin Petkovd22cb292010-09-29 10:02:29 -070051 // done | openssl dgst -sha256 -binary | openssl base64
52 EXPECT_EQ("NZf8k6SPBkYMvhaX8YgzuMgbkLP1XZ+neM8K5wcSsf8=", calc.hash());
rspangler@google.com49fdf182009-10-10 00:57:34 +000053}
54
55TEST(OmahaHashCalculatorTest, AbortTest) {
56 // Just make sure we don't crash and valgrind doesn't detect memory leaks
57 {
58 OmahaHashCalculator calc;
59 }
60 {
61 OmahaHashCalculator calc;
62 calc.Update("h", 1);
63 }
64}
65
66} // namespace chromeos_update_engine