Coverage for /Syzygy/pe/coff_relinker_unittest.cc

CoverageLines executed / instrumented / missingexe / inst / missLanguageGroup
100.0%92920.C++test

Line-by-line coverage:

   1    :  // Copyright 2013 Google Inc. All Rights Reserved.
   2    :  //
   3    :  // Licensed under the Apache License, Version 2.0 (the "License");
   4    :  // you may not use this file except in compliance with the License.
   5    :  // You may obtain a copy of the License at
   6    :  //
   7    :  //     http://www.apache.org/licenses/LICENSE-2.0
   8    :  //
   9    :  // Unless required by applicable law or agreed to in writing, software
  10    :  // distributed under the License is distributed on an "AS IS" BASIS,
  11    :  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12    :  // See the License for the specific language governing permissions and
  13    :  // limitations under the License.
  14    :  
  15    :  #include "syzygy/pe/coff_relinker.h"
  16    :  
  17    :  #include "base/files/file_util.h"
  18    :  #include "gmock/gmock.h"
  19    :  #include "gtest/gtest.h"
  20    :  #include "syzygy/common/defs.h"
  21    :  #include "syzygy/core/unittest_util.h"
  22    :  #include "syzygy/pe/unittest_util.h"
  23    :  
  24    :  namespace pe {
  25    :  namespace {
  26    :  
  27    :  using block_graph::BlockGraph;
  28    :  using block_graph::BlockGraphOrdererInterface;
  29    :  using block_graph::BlockGraphTransformInterface;
  30    :  using block_graph::OrderedBlockGraph;
  31    :  using block_graph::TransformPolicyInterface;
  32    :  using testing::_;
  33    :  using testing::Return;
  34    :  using testing::StrictMock;
  35    :  
  36    :  class TestCoffRelinker : public CoffRelinker {
  37    :   public:
  38  E :    explicit TestCoffRelinker(const CoffTransformPolicy* transform_policy)
  39    :        : CoffRelinker(transform_policy) {
  40  E :    }
  41    :  
  42    :    using CoffRelinker::transforms_;
  43    :    using CoffRelinker::orderers_;
  44    :  };
  45    :  
  46    :  class CoffRelinkerTest : public testing::PELibUnitTest {
  47    :   public:
  48  E :    virtual void SetUp() override {
  49  E :      testing::PELibUnitTest::SetUp();
  50    :  
  51    :      test_dll_obj_path_ =
  52  E :          testing::GetExeTestDataRelativePath(testing::kTestDllCoffObjName);
  53  E :      ASSERT_NO_FATAL_FAILURE(CreateTemporaryDir(&temp_dir_path_));
  54  E :      new_test_dll_obj_path_ = temp_dir_path_.Append(L"test_dll.obj");
  55  E :      new_test_dll_path_ = temp_dir_path_.Append(testing::kTestDllName);
  56  E :    }
  57    :  
  58    :    CoffTransformPolicy policy_;
  59    :    base::FilePath test_dll_obj_path_;
  60    :    base::FilePath new_test_dll_obj_path_;
  61    :    base::FilePath new_test_dll_path_;
  62    :    base::FilePath temp_dir_path_;
  63    :  };
  64    :  
  65    :  class MockTransform : public BlockGraphTransformInterface {
  66    :   public:
  67  E :    const char* name() const { return "MockTransform"; }
  68    :    MOCK_METHOD3(TransformBlockGraph,
  69    :                 bool(const TransformPolicyInterface*,
  70    :                      BlockGraph*,
  71  E :                      BlockGraph::Block*));
  72    :  };
  73    :  
  74    :  class MockOrderer : public BlockGraphOrdererInterface {
  75    :   public:
  76  E :    const char* name() const { return "MockOrderer"; }
  77    :    MOCK_METHOD2(OrderBlockGraph,
  78    :                 bool(OrderedBlockGraph*,
  79  E :                      BlockGraph::Block*));
  80    :  };
  81    :  
  82    :  }  // namespace
  83    :  
  84  E :  TEST_F(CoffRelinkerTest, InitFailsOnUnspecifiedInput) {
  85  E :    TestCoffRelinker relinker(&policy_);
  86    :  
  87  E :    relinker.set_output_path(new_test_dll_obj_path_);
  88  E :    EXPECT_FALSE(relinker.Init());
  89  E :  }
  90    :  
  91  E :  TEST_F(CoffRelinkerTest, InitFailsOnUnspecifiedOutput) {
  92  E :    TestCoffRelinker relinker(&policy_);
  93    :  
  94  E :    relinker.set_input_path(test_dll_obj_path_);
  95  E :    EXPECT_FALSE(relinker.Init());
  96  E :  }
  97    :  
  98  E :  TEST_F(CoffRelinkerTest, InitFailsOnNonexistentInput) {
  99  E :    TestCoffRelinker relinker(&policy_);
 100    :  
 101  E :    relinker.set_input_path(temp_dir_path_.Append(L"nonexistent.dll"));
 102  E :    relinker.set_output_path(new_test_dll_obj_path_);
 103  E :    EXPECT_FALSE(relinker.Init());
 104  E :  }
 105    :  
 106  E :  TEST_F(CoffRelinkerTest, InitFailsOnDisallowedOverwrite) {
 107  E :    TestCoffRelinker relinker(&policy_);
 108    :  
 109    :    // Copy the image in case the test actually does overwrite the input; this
 110    :    // way we don't accidentally turf our test data.
 111  E :    base::CopyFile(test_dll_obj_path_, new_test_dll_obj_path_);
 112    :  
 113  E :    relinker.set_input_path(new_test_dll_obj_path_);
 114  E :    relinker.set_output_path(new_test_dll_obj_path_);
 115    :  
 116  E :    relinker.set_allow_overwrite(false);
 117  E :    EXPECT_FALSE(relinker.Init());
 118  E :  }
 119    :  
 120  E :  TEST_F(CoffRelinkerTest, InitSucceeds) {
 121  E :    TestCoffRelinker relinker(&policy_);
 122    :  
 123  E :    relinker.set_input_path(test_dll_obj_path_);
 124  E :    relinker.set_output_path(new_test_dll_obj_path_);
 125    :  
 126  E :    EXPECT_TRUE(relinker.Init());
 127  E :  }
 128    :  
 129  E :  TEST_F(CoffRelinkerTest, IntermediateAccessors) {
 130  E :    TestCoffRelinker relinker(&policy_);
 131    :  
 132  E :    relinker.set_input_path(test_dll_obj_path_);
 133  E :    relinker.set_output_path(new_test_dll_obj_path_);
 134    :  
 135  E :    EXPECT_TRUE(relinker.Init());
 136    :  
 137  E :    EXPECT_EQ(test_dll_obj_path_, relinker.input_image_file().path());
 138  E :    EXPECT_TRUE(relinker.headers_block() != NULL);
 139  E :  }
 140    :  
 141  E :  TEST_F(CoffRelinkerTest, FailsWhenTransformFails) {
 142  E :    TestCoffRelinker relinker(&policy_);
 143  E :    StrictMock<MockTransform> transform;
 144    :  
 145  E :    EXPECT_CALL(transform, TransformBlockGraph(_, _, _)).WillOnce(Return(false));
 146    :  
 147  E :    relinker.AppendTransform(&transform);
 148  E :    relinker.set_input_path(test_dll_obj_path_);
 149  E :    relinker.set_output_path(new_test_dll_obj_path_);
 150  E :    EXPECT_TRUE(relinker.Init());
 151  E :    EXPECT_FALSE(relinker.Relink());
 152  E :  }
 153    :  
 154  E :  TEST_F(CoffRelinkerTest, FailsWhenOrdererFails) {
 155  E :    TestCoffRelinker relinker(&policy_);
 156  E :    StrictMock<MockOrderer> orderer;
 157    :  
 158  E :    EXPECT_CALL(orderer, OrderBlockGraph(_, _)).WillOnce(Return(false));
 159    :  
 160  E :    relinker.AppendOrderer(&orderer);
 161  E :    relinker.set_input_path(test_dll_obj_path_);
 162  E :    relinker.set_output_path(new_test_dll_obj_path_);
 163  E :    EXPECT_TRUE(relinker.Init());
 164  E :    EXPECT_FALSE(relinker.Relink());
 165  E :  }
 166    :  
 167  E :  TEST_F(CoffRelinkerTest, Success) {
 168  E :    TestCoffRelinker relinker(&policy_);
 169  E :    StrictMock<MockTransform> transform;
 170  E :    StrictMock<MockOrderer> orderer;
 171    :  
 172  E :    EXPECT_CALL(transform, TransformBlockGraph(_, _, _)).WillOnce(Return(true));
 173  E :    EXPECT_CALL(orderer, OrderBlockGraph(_, _)).WillOnce(Return(true));
 174    :  
 175  E :    relinker.AppendTransform(&transform);
 176  E :    relinker.AppendOrderer(&orderer);
 177    :  
 178  E :    relinker.set_input_path(test_dll_obj_path_);
 179  E :    relinker.set_output_path(new_test_dll_obj_path_);
 180    :  
 181  E :    EXPECT_TRUE(relinker.Init());
 182  E :    EXPECT_TRUE(relinker.Relink());
 183  E :  }
 184    :  
 185  E :  TEST_F(CoffRelinkerTest, IdentityRelink) {
 186  E :    TestCoffRelinker relinker(&policy_);
 187    :  
 188  E :    relinker.set_input_path(test_dll_obj_path_);
 189  E :    relinker.set_output_path(new_test_dll_obj_path_);
 190    :  
 191  E :    EXPECT_TRUE(relinker.Init());
 192  E :    EXPECT_TRUE(relinker.Relink());
 193    :  
 194  E :    EXPECT_TRUE(base::PathExists(relinker.output_path()));
 195    :  
 196    :    // We assume the contents of the file are what they should be;
 197    :    // CoffImageLayoutBuilder has more unit tests that check the specifics of
 198    :    // writing COFF files.
 199  E :  }
 200    :  
 201    :  }  // namespace pe

Coverage information generated Thu Jan 14 17:40:38 2016.