PageRenderTime 31ms CodeModel.GetById 12ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/processor/stackwalker_unittest_utils.h

http://github.com/tomahawk-player/tomahawk
C++ Header | 180 lines | 117 code | 26 blank | 37 comment | 5 complexity | 4c44630c7c1b4702fc6257755c262be7 MD5 | raw file
  1// -*- mode: C++ -*-
  2
  3// Copyright (c) 2010, Google Inc.
  4// All rights reserved.
  5//
  6// Redistribution and use in source and binary forms, with or without
  7// modification, are permitted provided that the following conditions are
  8// met:
  9//
 10//     * Redistributions of source code must retain the above copyright
 11// notice, this list of conditions and the following disclaimer.
 12//     * Redistributions in binary form must reproduce the above
 13// copyright notice, this list of conditions and the following disclaimer
 14// in the documentation and/or other materials provided with the
 15// distribution.
 16//     * Neither the name of Google Inc. nor the names of its
 17// contributors may be used to endorse or promote products derived from
 18// this software without specific prior written permission.
 19//
 20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 31
 32// Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
 33
 34// Mock classes for writing stackwalker tests, shared amongst architectures.
 35
 36#ifndef PROCESSOR_STACKWALKER_UNITTEST_UTILS_H_
 37#define PROCESSOR_STACKWALKER_UNITTEST_UTILS_H_
 38
 39#include <stdlib.h>
 40#include <string>
 41#include <vector>
 42
 43#include "google_breakpad/common/breakpad_types.h"
 44#include "google_breakpad/processor/code_module.h"
 45#include "google_breakpad/processor/code_modules.h"
 46#include "google_breakpad/processor/memory_region.h"
 47#include "google_breakpad/processor/symbol_supplier.h"
 48#include "google_breakpad/processor/system_info.h"
 49
 50class MockMemoryRegion: public google_breakpad::MemoryRegion {
 51 public:
 52  MockMemoryRegion(): base_address_(0) { }
 53
 54  // Set this region's address and contents. If we have placed an
 55  // instance of this class in a test fixture class, individual tests
 56  // can use this to provide the region's contents.
 57  void Init(u_int64_t base_address, const std::string &contents) {
 58    base_address_ = base_address;
 59    contents_ = contents;
 60  }
 61
 62  u_int64_t GetBase() const { return base_address_; }
 63  u_int32_t GetSize() const { return contents_.size(); }
 64
 65  bool GetMemoryAtAddress(u_int64_t address, u_int8_t  *value) const {
 66    return GetMemoryLittleEndian(address, value);
 67  }
 68  bool GetMemoryAtAddress(u_int64_t address, u_int16_t *value) const {
 69    return GetMemoryLittleEndian(address, value);
 70  }
 71  bool GetMemoryAtAddress(u_int64_t address, u_int32_t *value) const {
 72    return GetMemoryLittleEndian(address, value);
 73  }
 74  bool GetMemoryAtAddress(u_int64_t address, u_int64_t *value) const {
 75    return GetMemoryLittleEndian(address, value);
 76  }
 77
 78 private:
 79  // Fetch a little-endian value from ADDRESS in contents_ whose size
 80  // is BYTES, and store it in *VALUE. Return true on success.
 81  template<typename ValueType>
 82  bool GetMemoryLittleEndian(u_int64_t address, ValueType *value) const {
 83    if (address < base_address_ ||
 84        address - base_address_ + sizeof(ValueType) > contents_.size())
 85      return false;
 86    ValueType v = 0;
 87    int start = address - base_address_;
 88    // The loop condition is odd, but it's correct for size_t.
 89    for (size_t i = sizeof(ValueType) - 1; i < sizeof(ValueType); i--)
 90      v = (v << 8) | static_cast<unsigned char>(contents_[start + i]);
 91    *value = v;
 92    return true;
 93  }
 94
 95  u_int64_t base_address_;
 96  std::string contents_;
 97};
 98
 99class MockCodeModule: public google_breakpad::CodeModule {
100 public:
101  MockCodeModule(u_int64_t base_address, u_int64_t size,
102                 const std::string &code_file, const std::string &version)
103      : base_address_(base_address), size_(size), code_file_(code_file) { }
104
105  u_int64_t base_address()       const { return base_address_; }
106  u_int64_t size()               const { return size_; }
107  std::string code_file()        const { return code_file_; }
108  std::string code_identifier()  const { return code_file_; }
109  std::string debug_file()       const { return code_file_; }
110  std::string debug_identifier() const { return code_file_; }
111  std::string version()          const { return version_; }
112  const google_breakpad::CodeModule *Copy() const {
113    abort(); // Tests won't use this.
114  }
115
116 private:
117  u_int64_t base_address_;
118  u_int64_t size_;
119  std::string code_file_;
120  std::string version_;
121};
122
123class MockCodeModules: public google_breakpad::CodeModules {
124 public:  
125  typedef google_breakpad::CodeModule CodeModule;
126  typedef google_breakpad::CodeModules CodeModules;
127
128  void Add(const MockCodeModule *module) { 
129    modules_.push_back(module);
130  }
131
132  unsigned int module_count() const { return modules_.size(); }
133
134  const CodeModule *GetModuleForAddress(u_int64_t address) const {
135    for (ModuleVector::const_iterator i = modules_.begin();
136         i != modules_.end(); i++) {
137      const MockCodeModule *module = *i;
138      if (module->base_address() <= address &&
139          address - module->base_address() < module->size())
140        return module;
141    }
142    return NULL;
143  };
144
145  const CodeModule *GetMainModule() const { return modules_[0]; }
146
147  const CodeModule *GetModuleAtSequence(unsigned int sequence) const {
148    return modules_.at(sequence);
149  }
150
151  const CodeModule *GetModuleAtIndex(unsigned int index) const {
152    return modules_.at(index);
153  }
154
155  const CodeModules *Copy() const { abort(); } // Tests won't use this.
156
157 private:  
158  typedef std::vector<const MockCodeModule *> ModuleVector;
159  ModuleVector modules_;
160};
161
162class MockSymbolSupplier: public google_breakpad::SymbolSupplier {
163 public:
164  typedef google_breakpad::CodeModule CodeModule;
165  typedef google_breakpad::SystemInfo SystemInfo;
166  MOCK_METHOD3(GetSymbolFile, SymbolResult(const CodeModule *module,
167                                           const SystemInfo *system_info,
168                                           std::string *symbol_file));
169  MOCK_METHOD4(GetSymbolFile, SymbolResult(const CodeModule *module,
170                                           const SystemInfo *system_info,
171                                           std::string *symbol_file,
172                                           std::string *symbol_data));
173  MOCK_METHOD4(GetCStringSymbolData, SymbolResult(const CodeModule *module,
174                                                  const SystemInfo *system_info,
175                                                  std::string *symbol_file,
176                                                  char **symbol_data));
177  MOCK_METHOD1(FreeSymbolData, void(const CodeModule *module));
178};
179
180#endif // PROCESSOR_STACKWALKER_UNITTEST_UTILS_H_