Coverage for /Syzygy/instrument/instrumenters/archive_instrumenter_unittest.cc

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

Line-by-line coverage:

   1    :  // Copyright 2014 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/instrument/instrumenters/archive_instrumenter.h"
  16    :  
  17    :  #include "base/file_util.h"
  18    :  #include "gtest/gtest.h"
  19    :  #include "syzygy/ar/unittest_util.h"
  20    :  #include "syzygy/core/unittest_util.h"
  21    :  #include "syzygy/instrument/instrumenters/asan_instrumenter.h"
  22    :  #include "syzygy/pe/unittest_util.h"
  23    :  
  24    :  namespace instrument {
  25    :  namespace instrumenters {
  26    :  
  27    :  namespace {
  28    :  
  29    :  // Some global state that is updated by IdentityInstrumenter.
  30    :  // NOTE: Because of these this unittest is not thread safe! We could do this
  31    :  //       with a complicate usage of gmock, but that's overkill for this
  32    :  //       scenario.
  33    :  size_t constructor_count = 0;
  34    :  size_t parse_count = 0;
  35    :  size_t instrument_count = 0;
  36  E :  std::set<base::FilePath> input_images;
  37  E :  std::set<base::FilePath> output_images;
  38    :  
  39    :  // An identity instrumenter. Simply copies the input-image to the output-image.
  40    :  class IdentityInstrumenter : public InstrumenterInterface {
  41    :   public:
  42  E :    IdentityInstrumenter() {
  43  E :      ++constructor_count;
  44  E :    }
  45    :  
  46  E :    virtual bool ParseCommandLine(const CommandLine* command_line) OVERRIDE {
  47  E :      ++parse_count;
  48  E :      input_image_ = command_line->GetSwitchValuePath("input-image");
  49  E :      output_image_ = command_line->GetSwitchValuePath("output-image");
  50  E :      input_images.insert(input_image_);
  51  E :      output_images.insert(output_image_);
  52  E :      return true;
  53  E :    }
  54    :  
  55  E :    virtual bool Instrument() OVERRIDE {
  56  E :      ++instrument_count;
  57  E :      base::CopyFile(input_image_, output_image_);
  58  E :      return true;
  59  E :    }
  60    :  
  61    :    base::FilePath input_image_;
  62    :    base::FilePath output_image_;
  63    :  };
  64    :  
  65  E :  InstrumenterInterface* IdentityInstrumenterFactory() {
  66  E :    return new IdentityInstrumenter();
  67  E :  }
  68    :  
  69  E :  InstrumenterInterface* AsanInstrumenterFactory() {
  70  E :    return new AsanInstrumenter();
  71  E :  }
  72    :  
  73    :  class ArchiveInstrumenterTest : public testing::PELibUnitTest {
  74    :   public:
  75  E :    virtual void SetUp() OVERRIDE {
  76  E :      testing::PELibUnitTest::SetUp();
  77    :  
  78  E :      CreateTemporaryDir(&temp_dir_);
  79    :      test_dll_dll_ = testing::GetExeTestDataRelativePath(
  80  E :          testing::kTestDllName);
  81  E :      zlib_lib_ = testing::GetSrcRelativePath(testing::kArchiveFile);
  82  E :      output_image_ = temp_dir_.Append(L"output.dat");
  83    :  
  84  E :      command_line_.reset(new CommandLine(base::FilePath(L"instrumenter.exe")));
  85  E :      command_line_->AppendSwitchPath("output-image", output_image_);
  86    :  
  87    :      // Reset function counts.
  88  E :      constructor_count = 0;
  89  E :      parse_count = 0;
  90  E :      instrument_count = 0;
  91  E :      input_images.clear();
  92  E :      output_images.clear();
  93  E :    }
  94    :  
  95  E :    virtual void TearDown() OVERRIDE {
  96  E :      testing::PELibUnitTest::TearDown();
  97  E :    }
  98    :  
  99    :    base::FilePath temp_dir_;
 100    :    base::FilePath test_dll_dll_;
 101    :    base::FilePath zlib_lib_;
 102    :    base::FilePath output_image_;
 103    :  
 104    :    scoped_ptr<CommandLine> command_line_;
 105    :  };
 106    :  
 107    :  }  // namespace
 108    :  
 109  E :  TEST_F(ArchiveInstrumenterTest, PassthroughForNonArchive) {
 110  E :    ArchiveInstrumenter inst(&IdentityInstrumenterFactory);
 111  E :    command_line_->AppendSwitchPath("input-image", test_dll_dll_);
 112    :  
 113  E :    EXPECT_TRUE(inst.ParseCommandLine(command_line_.get()));
 114  E :    EXPECT_TRUE(inst.Instrument());
 115  E :    EXPECT_EQ(1u, constructor_count);
 116  E :    EXPECT_EQ(1u, parse_count);
 117  E :    EXPECT_EQ(1u, instrument_count);
 118  E :    EXPECT_EQ(1u, input_images.size());
 119  E :    EXPECT_EQ(1u, output_images.size());
 120  E :    EXPECT_EQ(test_dll_dll_, *input_images.begin());
 121  E :    EXPECT_EQ(output_image_, *output_images.begin());
 122  E :    EXPECT_TRUE(base::PathExists(output_image_));
 123  E :  }
 124    :  
 125  E :  TEST_F(ArchiveInstrumenterTest, IteratesOverArchiveFiles) {
 126  E :    ArchiveInstrumenter inst(&IdentityInstrumenterFactory);
 127  E :    command_line_->AppendSwitchPath("input-image", zlib_lib_);
 128    :  
 129  E :    EXPECT_TRUE(inst.ParseCommandLine(command_line_.get()));
 130  E :    EXPECT_TRUE(inst.Instrument());
 131  E :    EXPECT_EQ(testing::kArchiveFileCount, constructor_count);
 132  E :    EXPECT_EQ(testing::kArchiveFileCount, parse_count);
 133  E :    EXPECT_EQ(testing::kArchiveFileCount, instrument_count);
 134  E :    EXPECT_EQ(testing::kArchiveFileCount, input_images.size());
 135  E :    EXPECT_EQ(testing::kArchiveFileCount, output_images.size());
 136  E :    EXPECT_EQ(0u, input_images.count(zlib_lib_));
 137  E :    EXPECT_EQ(0u, output_images.count(output_image_));
 138  E :    EXPECT_TRUE(base::PathExists(output_image_));
 139  E :  }
 140    :  
 141  E :  TEST_F(ArchiveInstrumenterTest, AsanInstrumentArchive) {
 142  E :    ArchiveInstrumenter inst(&AsanInstrumenterFactory);
 143  E :    command_line_->AppendSwitchPath("input-image", zlib_lib_);
 144    :  
 145  E :    EXPECT_TRUE(inst.ParseCommandLine(command_line_.get()));
 146  E :    EXPECT_TRUE(inst.Instrument());
 147  E :    EXPECT_TRUE(base::PathExists(output_image_));
 148  E :  }
 149    :  
 150    :  }  // namespace instrumenters
 151    :  }  // namespace instrument

Coverage information generated Thu Mar 26 16:15:41 2015.