<commit_before>// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "syzygy/refinery/analyzers/type_propagator_analyzer.h"

#include <string>

#include "base/memory/ref_counted.h"
#include "base/strings/string_util.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "syzygy/minidump/minidump.h"
#include "syzygy/refinery/process_state/process_state.h"
#include "syzygy/refinery/process_state/process_state_util.h"
#include "syzygy/refinery/process_state/refinery.pb.h"
#include "syzygy/refinery/types/type.h"
#include "syzygy/refinery/types/type_namer.h"
#include "syzygy/refinery/types/type_repository.h"

namespace refinery {

using testing::_;
using testing::DoAll;
using testing::Return;
using testing::SetArgPointee;

namespace {

const Address kAddress = 0x0000CAFE;  // Fits 32-bit.
const Size kSize = 42U;
const uint32 kChecksum = 11U;
const uint32 kTimestamp = 22U;
const wchar_t kPath[] = L"c:\\path\\ModuleName";

class MockSymbolProvider : public SymbolProvider {
 public:
  MOCK_METHOD2(FindOrCreateTypeRepository,
               bool(const pe::PEFile::Signature& signature,
                    scoped_refptr<TypeRepository>* type_repo));
};

}  // namespace

class TypePropagatorAnalyzerTest : public testing::Test {
 public:
  TypePropagatorAnalyzerTest()
      : variable_(42),
        variable_ptr_(&variable_),
        expected_sig_(kPath,
                      core::AbsoluteAddress(0U),
                      kSize,
                      kChecksum,
                      kTimestamp),
        repo_(new TypeRepository(expected_sig_)),
        type_namer_(true) {
    // Create a basic type and a pointer type to it.
    basic_type_ = new BasicType(L"int32_t", sizeof(int32_t));
    repo_->AddType(basic_type_);

    ptr_type_ = new PointerType(basic_type_->size(), PointerType::PTR_MODE_PTR);
    repo_->AddType(ptr_type_);
    ptr_type_->Finalize(kNoTypeFlags, basic_type_->type_id());
    CHECK(type_namer_.EnsureTypeName(ptr_type_));
  }

 protected:
  // Data for the test.
  int32_t variable_;
  int32_t* variable_ptr_;

  TypePtr basic_type_;
  PointerTypePtr ptr_type_;

  pe::PEFile::Signature expected_sig_;
  scoped_refptr<TypeRepository> repo_;
  TypeNamer type_namer_;
};

TEST_F(TypePropagatorAnalyzerTest, AnalyzeMinidump) {
  ProcessState process_state;

  // Populate the bytes layer with the contents of variable_ptr_.
  BytesLayerPtr bytes_layer;<commit_msg>[Refinery] Fix one TypePropagatorAnalyzerTest issue.<commit_after>// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "syzygy/refinery/analyzers/type_propagator_analyzer.h"

#include <string>

#include "base/memory/ref_counted.h"
#include "base/strings/string_util.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "syzygy/minidump/minidump.h"
#include "syzygy/refinery/process_state/process_state.h"
#include "syzygy/refinery/process_state/process_state_util.h"
#include "syzygy/refinery/process_state/refinery.pb.h"
#include "syzygy/refinery/types/type.h"
#include "syzygy/refinery/types/type_namer.h"
#include "syzygy/refinery/types/type_repository.h"

namespace refinery {

using testing::_;
using testing::DoAll;
using testing::Return;
using testing::SetArgPointee;

namespace {

const Address kAddress = 0x0000CAFE;  // Fits 32-bit.
const Size kSize = 42U;
const uint32 kChecksum = 11U;
const uint32 kTimestamp = 22U;
const wchar_t kPath[] = L"c:\\path\\ModuleName";

class MockSymbolProvider : public SymbolProvider {
 public:
  MOCK_METHOD2(FindOrCreateTypeRepository,
               bool(const pe::PEFile::Signature& signature,
                    scoped_refptr<TypeRepository>* type_repo));
};

}  // namespace

class TypePropagatorAnalyzerTest : public testing::Test {
 public:
  TypePropagatorAnalyzerTest()
      : variable_(42),
        variable_ptr_(&variable_),
        expected_sig_(kPath,
                      core::AbsoluteAddress(0U),
                      kSize,
                      kChecksum,
                      kTimestamp),
        repo_(new TypeRepository(expected_sig_)),
        type_namer_(true) {
    // Create a basic type and a pointer type to it.
    basic_type_ = new BasicType(L"int32_t", sizeof(int32_t));
    repo_->AddType(basic_type_);

    ptr_type_ = new PointerType(sizeof(int32_t*), PointerType::PTR_MODE_PTR);
    repo_->AddType(ptr_type_);
    ptr_type_->Finalize(kNoTypeFlags, basic_type_->type_id());
    CHECK(type_namer_.EnsureTypeName(ptr_type_));
  }

 protected:
  // Data for the test.
  int32_t variable_;
  int32_t* variable_ptr_;

  TypePtr basic_type_;
  PointerTypePtr ptr_type_;

  pe::PEFile::Signature expected_sig_;
  scoped_refptr<TypeRepository> repo_;
  TypeNamer type_namer_;
};

TEST_F(TypePropagatorAnalyzerTest, AnalyzeMinidump) {
  ProcessState process_state;

  // Populate the bytes layer with the contents of variable_ptr_.
  BytesLayerPtr bytes_layer;
{'fraction_non_alphanumeric': 0.10367892976588629, 'fraction_numerical': 0.012709030100334449, 'mean_word_length': 3.842914979757085, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6959913', 'n_tokens_mistral': 1940, 'n_tokens_neox': 1868, 'n_words': 463}