Skip to content

Commit

Permalink
Add a basic backwards compatibility unittest.
Browse files Browse the repository at this point in the history
This verifies we can still apply old patches, but does not
verify that old clients can apply new patches. That seems
important, but I'm not sure how to do it without storing
old client binaries in version control.

Also refactors a number of unit tests to allow code sharing for
reading files into memory. This is done via a new base class.

Uses test binaries submitted seperatly because of build tools problems.

BUG=None
TEST=New Unittest


Review URL: http://codereview.chromium.org/8252011

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@105982 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
dgarrett@chromium.org committed Oct 18, 2011
1 parent ec817ab commit bf0ece4
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 131 deletions.
27 changes: 27 additions & 0 deletions courgette/base_test_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2011 The Chromium 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 "courgette/base_test_unittest.h"

#include "base/path_service.h"

void BaseTest::SetUp() {
PathService::Get(base::DIR_SOURCE_ROOT, &test_dir_);
test_dir_ = test_dir_.AppendASCII("courgette");
test_dir_ = test_dir_.AppendASCII("testdata");
}

void BaseTest::TearDown() {
}

// Reads a test file into a string.
std::string BaseTest::FileContents(const char* file_name) const {
FilePath file_path = test_dir_;
file_path = file_path.AppendASCII(file_name);
std::string file_bytes;

EXPECT_TRUE(file_util::ReadFileToString(file_path, &file_bytes));

return file_bytes;
}
26 changes: 26 additions & 0 deletions courgette/base_test_unittest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2011 The Chromium 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 <string>

#ifndef COURGETTE_BASE_TEST_UNITTEST_H_
#define COURGETTE_BASE_TEST_UNITTEST_H_

#include <string>

#include "base/file_util.h"
#include "testing/gtest/include/gtest/gtest.h"

class BaseTest : public testing::Test {
public:
std::string FileContents(const char* file_name) const;

private:
virtual void SetUp();
virtual void TearDown();

FilePath test_dir_;
};

#endif // COURGETTE_BASE_TEST_UNITTEST_H_
34 changes: 3 additions & 31 deletions courgette/bsdiff_memory_unittest.cc
Original file line number Diff line number Diff line change
@@ -1,48 +1,20 @@
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Copyright (c) 2011 The Chromium 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 "courgette/third_party/bsdiff.h"

#include <string>

#include "base/file_util.h"
#include "base/path_service.h"
#include "base/string_util.h"

#include "courgette/base_test_unittest.h"
#include "courgette/courgette.h"
#include "courgette/streams.h"

#include "testing/gtest/include/gtest/gtest.h"

class BSDiffMemoryTest : public testing::Test {
class BSDiffMemoryTest : public BaseTest {
public:
std::string FileContents(const char* file_name) const;
void GenerateAndTestPatch(const std::string& a, const std::string& b) const;

std::string GenerateSyntheticInput(size_t length, int seed) const;

private:
void SetUp() {
PathService::Get(base::DIR_SOURCE_ROOT, &test_dir_);
test_dir_ = test_dir_.AppendASCII("courgette");
test_dir_ = test_dir_.AppendASCII("testdata");
}

FilePath test_dir_;
};

// Reads a test file into a string.
std::string BSDiffMemoryTest::FileContents(const char* file_name) const {
FilePath file_path = test_dir_;
file_path = file_path.AppendASCII(file_name);
std::string file_bytes;
if (!file_util::ReadFileToString(file_path, &file_bytes)) {
EXPECT_TRUE(!"Could not read test data");
}
return file_bytes;
}

void BSDiffMemoryTest::GenerateAndTestPatch(const std::string& old_text,
const std::string& new_text) const {
courgette::SourceStream old1;
Expand Down
7 changes: 6 additions & 1 deletion courgette/courgette.gyp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2009 The Chromium Authors. All rights reserved.
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

Expand Down Expand Up @@ -86,12 +86,15 @@
'sources': [
'adjustment_method_unittest.cc',
'bsdiff_memory_unittest.cc',
'base_test_unittest.cc',
'base_test_unittest.h',
'difference_estimator_unittest.cc',
'encoded_program_unittest.cc',
'encode_decode_unittest.cc',
'image_info_unittest.cc',
'run_all_unittests.cc',
'streams_unittest.cc',
'versioning_unittest.cc',
'third_party/paged_array_unittest.cc'
],
'dependencies': [
Expand All @@ -117,6 +120,8 @@
'target_name': 'courgette_fuzz',
'type': 'executable',
'sources': [
'base_test_unittest.cc',
'base_test_unittest.h',
'encoded_program_fuzz_unittest.cc',
],
'dependencies': [
Expand Down
36 changes: 2 additions & 34 deletions courgette/encode_decode_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "base/path_service.h"
#include "base/file_util.h"
#include "base/string_util.h"

#include "courgette/base_test_unittest.h"
#include "courgette/courgette.h"
#include "courgette/streams.h"

#include "testing/gtest/include/gtest/gtest.h"

class EncodeDecodeTest : public testing::Test {
class EncodeDecodeTest : public BaseTest {
public:
void TestExe(const char *) const;

private:
void SetUp() {
PathService::Get(base::DIR_SOURCE_ROOT, &testdata_dir_);
testdata_dir_ = testdata_dir_.AppendASCII("courgette");
testdata_dir_ = testdata_dir_.AppendASCII("testdata");
}

void TearDown() { }

// Returns contents of |file_name| as uninterprested bytes stored in a string.
std::string FileContents(const char* file_name) const;

FilePath testdata_dir_; // Full path name of testdata directory
};

// Reads a test file into a string.
std::string EncodeDecodeTest::FileContents(const char* file_name) const {
FilePath file_path = testdata_dir_;
file_path = file_path.AppendASCII(file_name);
std::string file_contents;
if (!file_util::ReadFileToString(file_path, &file_contents)) {
EXPECT_TRUE(!"Could not read test data");
}
return file_contents;
}

void EncodeDecodeTest::TestExe(const char* file_name) const {
// Test top-level Courgette API for converting an a file to a binary
// assembly representation and back.
Expand Down
36 changes: 3 additions & 33 deletions courgette/encoded_program_fuzz_unittest.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

Expand All @@ -11,56 +11,26 @@
// We try a lot of arbitrary modifications to the serialized form and make sure
// that the outcome is not a crash.

#include <string>

#include "base/path_service.h"
#include "base/file_util.h"
#include "base/string_util.h"
#include "base/test/test_suite.h"

#include "courgette/base_test_unittest.h"
#include "courgette/courgette.h"
#include "courgette/streams.h"

#include "testing/gtest/include/gtest/gtest.h"

class DecodeFuzzTest : public testing::Test {
class DecodeFuzzTest : public BaseTest {
public:
void FuzzExe(const char *) const;

private:
virtual void SetUp() {
PathService::Get(base::DIR_SOURCE_ROOT, &testdata_dir_);
testdata_dir_ = testdata_dir_.AppendASCII("courgette");
testdata_dir_ = testdata_dir_.AppendASCII("testdata");
}

virtual void TearDown() { }

void FuzzByte(const std::string& buffer, const std::string& output,
size_t index) const;
void FuzzBits(const std::string& buffer, const std::string& output,
size_t index, int bits_to_flip) const;

// Returns true if could assemble, false if rejected.
bool TryAssemble(const std::string& buffer, std::string* output) const;

// Returns contents of |file_name| as uninterprested bytes stored in a string.
std::string FileContents(const char* file_name) const;

// Full path name of testdata directory
FilePath testdata_dir_;
};

// Reads a test file into a string.
std::string DecodeFuzzTest::FileContents(const char* file_name) const {
FilePath file_path = testdata_dir_.AppendASCII(file_name);
std::string file_contents;
if (!file_util::ReadFileToString(file_path, &file_contents)) {
EXPECT_TRUE(!"Could not read test data");
}
return file_contents;
}

// Loads an executable and does fuzz testing in the serialized format.
void DecodeFuzzTest::FuzzExe(const char* file_name) const {
std::string file1 = FileContents(file_name);
Expand Down
34 changes: 2 additions & 32 deletions courgette/image_info_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,20 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "courgette/base_test_unittest.h"
#include "courgette/image_info.h"

#include <string>

#include "base/path_service.h"
#include "base/file_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/string_util.h"
#include "testing/gtest/include/gtest/gtest.h"

class ImageInfoTest : public testing::Test {
class ImageInfoTest : public BaseTest {
public:

void TestExe() const;
void TestResourceDll() const;

private:
void SetUp() {
PathService::Get(base::DIR_SOURCE_ROOT, &test_dir_);
test_dir_ = test_dir_.AppendASCII("courgette");
test_dir_ = test_dir_.AppendASCII("testdata");
}

void TearDown() {
}

void ExpectExecutable(courgette::PEInfo* info) const;

std::string FileContents(const char* file_name) const;

FilePath test_dir_;
};

// Reads a test file into a string.
std::string ImageInfoTest::FileContents(const char* file_name) const {
FilePath file_path = test_dir_;
file_path = file_path.AppendASCII(file_name);
std::string file_bytes;
if (!file_util::ReadFileToString(file_path, &file_bytes)) {
EXPECT_TRUE(!"Could not read test data");
}
return file_bytes;
}

void ImageInfoTest::ExpectExecutable(courgette::PEInfo* info) const {
EXPECT_TRUE(info->ok());
EXPECT_TRUE(info->has_text_section());
Expand Down
57 changes: 57 additions & 0 deletions courgette/versioning_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) 2011 The Chromium 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 "courgette/base_test_unittest.h"

#include <string>

#include "base/basictypes.h"
#include "courgette/courgette.h"
#include "courgette/streams.h"

class VersioningTest : public BaseTest {
public:
void TestApplyingOldPatch(const char* src_file,
const char* patch_file,
const char* expected_file) const;
};

void VersioningTest::TestApplyingOldPatch(const char* src_file,
const char* patch_file,
const char* expected_file) const {

std::string old_buffer = FileContents(src_file);
std::string new_buffer = FileContents(patch_file);
std::string expected_buffer = FileContents(expected_file);

courgette::SourceStream old_stream;
courgette::SourceStream patch_stream;
old_stream.Init(old_buffer);
patch_stream.Init(new_buffer);

courgette::SinkStream generated_stream;

courgette::Status status =
courgette::ApplyEnsemblePatch(&old_stream,
&patch_stream,
&generated_stream);

EXPECT_EQ(status, courgette::C_OK);

size_t expected_length = expected_buffer.size();
size_t generated_length = generated_stream.Length();

EXPECT_EQ(generated_length, expected_length);
EXPECT_EQ(0, memcmp(generated_stream.Buffer(),
expected_buffer.c_str(),
expected_length));
}


TEST_F(VersioningTest, All) {
TestApplyingOldPatch("setup1.exe", "setup1-setup2.v1.patch", "setup2.exe");

// We also need a way to test that newly generated patches are appropriately
// applicable by older clients... not sure of the best way to do that.
}

0 comments on commit bf0ece4

Please sign in to comment.