1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/processor/stackwalker_unittest_utils.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,204 @@ 1.4 +// -*- mode: C++ -*- 1.5 + 1.6 +// Copyright (c) 2010, Google Inc. 1.7 +// All rights reserved. 1.8 +// 1.9 +// Redistribution and use in source and binary forms, with or without 1.10 +// modification, are permitted provided that the following conditions are 1.11 +// met: 1.12 +// 1.13 +// * Redistributions of source code must retain the above copyright 1.14 +// notice, this list of conditions and the following disclaimer. 1.15 +// * Redistributions in binary form must reproduce the above 1.16 +// copyright notice, this list of conditions and the following disclaimer 1.17 +// in the documentation and/or other materials provided with the 1.18 +// distribution. 1.19 +// * Neither the name of Google Inc. nor the names of its 1.20 +// contributors may be used to endorse or promote products derived from 1.21 +// this software without specific prior written permission. 1.22 +// 1.23 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.24 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.25 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.26 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.27 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.28 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.29 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.30 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.31 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.32 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.33 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.34 + 1.35 +// Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> 1.36 + 1.37 +// Mock classes for writing stackwalker tests, shared amongst architectures. 1.38 + 1.39 +#ifndef PROCESSOR_STACKWALKER_UNITTEST_UTILS_H_ 1.40 +#define PROCESSOR_STACKWALKER_UNITTEST_UTILS_H_ 1.41 + 1.42 +#include <stdlib.h> 1.43 +#include <string> 1.44 +#include <vector> 1.45 + 1.46 +#include "common/using_std_string.h" 1.47 +#include "google_breakpad/common/breakpad_types.h" 1.48 +#include "google_breakpad/processor/code_module.h" 1.49 +#include "google_breakpad/processor/code_modules.h" 1.50 +#include "google_breakpad/processor/memory_region.h" 1.51 +#include "google_breakpad/processor/symbol_supplier.h" 1.52 +#include "google_breakpad/processor/system_info.h" 1.53 + 1.54 +class MockMemoryRegion: public google_breakpad::MemoryRegion { 1.55 + public: 1.56 + MockMemoryRegion(): base_address_(0) { } 1.57 + 1.58 + // Set this region's address and contents. If we have placed an 1.59 + // instance of this class in a test fixture class, individual tests 1.60 + // can use this to provide the region's contents. 1.61 + void Init(uint64_t base_address, const string &contents) { 1.62 + base_address_ = base_address; 1.63 + contents_ = contents; 1.64 + } 1.65 + 1.66 + uint64_t GetBase() const { return base_address_; } 1.67 + uint32_t GetSize() const { return contents_.size(); } 1.68 + 1.69 + bool GetMemoryAtAddress(uint64_t address, uint8_t *value) const { 1.70 + return GetMemoryLittleEndian(address, value); 1.71 + } 1.72 + bool GetMemoryAtAddress(uint64_t address, uint16_t *value) const { 1.73 + return GetMemoryLittleEndian(address, value); 1.74 + } 1.75 + bool GetMemoryAtAddress(uint64_t address, uint32_t *value) const { 1.76 + return GetMemoryLittleEndian(address, value); 1.77 + } 1.78 + bool GetMemoryAtAddress(uint64_t address, uint64_t *value) const { 1.79 + return GetMemoryLittleEndian(address, value); 1.80 + } 1.81 + 1.82 + private: 1.83 + // Fetch a little-endian value from ADDRESS in contents_ whose size 1.84 + // is BYTES, and store it in *VALUE. Return true on success. 1.85 + template<typename ValueType> 1.86 + bool GetMemoryLittleEndian(uint64_t address, ValueType *value) const { 1.87 + if (address < base_address_ || 1.88 + address - base_address_ + sizeof(ValueType) > contents_.size()) 1.89 + return false; 1.90 + ValueType v = 0; 1.91 + int start = address - base_address_; 1.92 + // The loop condition is odd, but it's correct for size_t. 1.93 + for (size_t i = sizeof(ValueType) - 1; i < sizeof(ValueType); i--) 1.94 + v = (v << 8) | static_cast<unsigned char>(contents_[start + i]); 1.95 + *value = v; 1.96 + return true; 1.97 + } 1.98 + 1.99 + uint64_t base_address_; 1.100 + string contents_; 1.101 +}; 1.102 + 1.103 +class MockCodeModule: public google_breakpad::CodeModule { 1.104 + public: 1.105 + MockCodeModule(uint64_t base_address, uint64_t size, 1.106 + const string &code_file, const string &version) 1.107 + : base_address_(base_address), size_(size), code_file_(code_file) { } 1.108 + 1.109 + uint64_t base_address() const { return base_address_; } 1.110 + uint64_t size() const { return size_; } 1.111 + string code_file() const { return code_file_; } 1.112 + string code_identifier() const { return code_file_; } 1.113 + string debug_file() const { return code_file_; } 1.114 + string debug_identifier() const { return code_file_; } 1.115 + string version() const { return version_; } 1.116 + const google_breakpad::CodeModule *Copy() const { 1.117 + abort(); // Tests won't use this. 1.118 + } 1.119 + 1.120 + private: 1.121 + uint64_t base_address_; 1.122 + uint64_t size_; 1.123 + string code_file_; 1.124 + string version_; 1.125 +}; 1.126 + 1.127 +class MockCodeModules: public google_breakpad::CodeModules { 1.128 + public: 1.129 + typedef google_breakpad::CodeModule CodeModule; 1.130 + typedef google_breakpad::CodeModules CodeModules; 1.131 + 1.132 + void Add(const MockCodeModule *module) { 1.133 + modules_.push_back(module); 1.134 + } 1.135 + 1.136 + unsigned int module_count() const { return modules_.size(); } 1.137 + 1.138 + const CodeModule *GetModuleForAddress(uint64_t address) const { 1.139 + for (ModuleVector::const_iterator i = modules_.begin(); 1.140 + i != modules_.end(); i++) { 1.141 + const MockCodeModule *module = *i; 1.142 + if (module->base_address() <= address && 1.143 + address - module->base_address() < module->size()) 1.144 + return module; 1.145 + } 1.146 + return NULL; 1.147 + }; 1.148 + 1.149 + const CodeModule *GetMainModule() const { return modules_[0]; } 1.150 + 1.151 + const CodeModule *GetModuleAtSequence(unsigned int sequence) const { 1.152 + return modules_.at(sequence); 1.153 + } 1.154 + 1.155 + const CodeModule *GetModuleAtIndex(unsigned int index) const { 1.156 + return modules_.at(index); 1.157 + } 1.158 + 1.159 + const CodeModules *Copy() const { abort(); } // Tests won't use this. 1.160 + 1.161 + private: 1.162 + typedef std::vector<const MockCodeModule *> ModuleVector; 1.163 + ModuleVector modules_; 1.164 +}; 1.165 + 1.166 +class MockSymbolSupplier: public google_breakpad::SymbolSupplier { 1.167 + public: 1.168 + typedef google_breakpad::CodeModule CodeModule; 1.169 + typedef google_breakpad::SystemInfo SystemInfo; 1.170 + MOCK_METHOD3(GetSymbolFile, SymbolResult(const CodeModule *module, 1.171 + const SystemInfo *system_info, 1.172 + string *symbol_file)); 1.173 + MOCK_METHOD4(GetSymbolFile, SymbolResult(const CodeModule *module, 1.174 + const SystemInfo *system_info, 1.175 + string *symbol_file, 1.176 + string *symbol_data)); 1.177 + MOCK_METHOD4(GetCStringSymbolData, SymbolResult(const CodeModule *module, 1.178 + const SystemInfo *system_info, 1.179 + string *symbol_file, 1.180 + char **symbol_data)); 1.181 + MOCK_METHOD1(FreeSymbolData, void(const CodeModule *module)); 1.182 + 1.183 + // Copies the passed string contents into a newly allocated buffer. 1.184 + // The newly allocated buffer will be freed during destruction. 1.185 + char* CopySymbolDataAndOwnTheCopy(const std::string &info) { 1.186 + unsigned int buffer_size = info.size() + 1; 1.187 + char *symbol_data = new char [buffer_size]; 1.188 + strcpy(symbol_data, info.c_str()); 1.189 + symbol_data_to_free_.push_back(symbol_data); 1.190 + return symbol_data; 1.191 + } 1.192 + 1.193 + virtual ~MockSymbolSupplier() { 1.194 + for (SymbolDataVector::const_iterator i = symbol_data_to_free_.begin(); 1.195 + i != symbol_data_to_free_.end(); i++) { 1.196 + char* symbol_data = *i; 1.197 + delete [] symbol_data; 1.198 + } 1.199 + } 1.200 + 1.201 + private: 1.202 + // List of symbol data to be freed upon destruction 1.203 + typedef std::vector<char*> SymbolDataVector; 1.204 + SymbolDataVector symbol_data_to_free_; 1.205 +}; 1.206 + 1.207 +#endif // PROCESSOR_STACKWALKER_UNITTEST_UTILS_H_