blob: 597b21f4d74638b2992d3f21e94a824ad7602250 [file] [log] [blame]
// Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <math.h>
#include <unistd.h>
#include <vector>
#include <glib.h>
#include <gtest/gtest.h>
#include "update_engine/libcurl_http_fetcher.h"
#include "update_engine/omaha_hash_calculator.h"
using std::vector;
namespace chromeos_update_engine {
class OmahaHashCalculatorTest : public ::testing::Test { };
// Generated by running this on a linux shell:
// $ echo -n hi | openssl dgst -sha256 -binary | openssl base64
static const char kExpectedHash[] =
"j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ=";
static const char kExpectedRawHash[] = {
0x8f, 0x43, 0x43, 0x46, 0x64, 0x8f, 0x6b, 0x96,
0xdf, 0x89, 0xdd, 0xa9, 0x01, 0xc5, 0x17, 0x6b,
0x10, 0xa6, 0xd8, 0x39, 0x61, 0xdd, 0x3c, 0x1a,
0xc8, 0x8b, 0x59, 0xb2, 0xdc, 0x32, 0x7a, 0xa4
};
TEST(OmahaHashCalculatorTest, SimpleTest) {
OmahaHashCalculator calc;
calc.Update("hi", 2);
calc.Finalize();
EXPECT_EQ(kExpectedHash, calc.hash());
vector<char> raw_hash(kExpectedRawHash,
kExpectedRawHash + arraysize(kExpectedRawHash));
EXPECT_TRUE(raw_hash == calc.raw_hash());
}
TEST(OmahaHashCalculatorTest, MultiUpdateTest) {
OmahaHashCalculator calc;
calc.Update("h", 1);
calc.Update("i", 1);
calc.Finalize();
EXPECT_EQ(kExpectedHash, calc.hash());
vector<char> raw_hash(kExpectedRawHash,
kExpectedRawHash + arraysize(kExpectedRawHash));
EXPECT_TRUE(raw_hash == calc.raw_hash());
}
TEST(OmahaHashCalculatorTest, ContextTest) {
OmahaHashCalculator calc;
calc.Update("h", 1);
OmahaHashCalculator calc_next;
calc_next.SetContext(calc.GetContext());
calc_next.Update("i", 1);
calc_next.Finalize();
// Generated by running this on a linux shell:
// $ echo -n hi | openssl dgst -sha256 -binary | openssl base64
EXPECT_EQ("j0NDRmSPa5bfid2pAcUXaxCm2Dlh3TwayItZstwyeqQ=", calc_next.hash());
}
TEST(OmahaHashCalculatorTest, BigTest) {
OmahaHashCalculator calc;
for (int i = 0; i < 1000000; i++) {
char buf[8];
ASSERT_EQ(0 == i ? 1 : static_cast<int>(floorf(logf(i) / logf(10))) + 1,
snprintf(buf, sizeof(buf), "%d", i)) << " i = " << i;
calc.Update(buf, strlen(buf));
}
calc.Finalize();
// Hash constant generated by running this on a linux shell:
// $ C=0
// $ while [ $C -lt 1000000 ]; do
// echo -n $C
// let C=C+1
// done | openssl dgst -sha256 -binary | openssl base64
EXPECT_EQ("NZf8k6SPBkYMvhaX8YgzuMgbkLP1XZ+neM8K5wcSsf8=", calc.hash());
}
TEST(OmahaHashCalculatorTest, AbortTest) {
// Just make sure we don't crash and valgrind doesn't detect memory leaks
{
OmahaHashCalculator calc;
}
{
OmahaHashCalculator calc;
calc.Update("h", 1);
}
}
} // namespace chromeos_update_engine