1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157

base / debug / crash_logging_unittest.cc [blame]

// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/debug/crash_logging.h"

#include <map>
#include <memory>
#include <sstream>
#include <string_view>

#include "base/memory/raw_ref.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::ElementsAre;
using ::testing::IsEmpty;
using ::testing::Pair;

namespace base {
namespace debug {

namespace {

class TestCrashKeyImplementation : public CrashKeyImplementation {
 public:
  explicit TestCrashKeyImplementation(std::map<std::string, std::string>& data)
      : data_(data) {}

  TestCrashKeyImplementation(const TestCrashKeyImplementation&) = delete;
  TestCrashKeyImplementation& operator=(const TestCrashKeyImplementation&) =
      delete;

  CrashKeyString* Allocate(const char* name, CrashKeySize size) override {
    return new CrashKeyString(name, size);
  }

  void Set(CrashKeyString* crash_key, std::string_view value) override {
    ASSERT_TRUE(data_->emplace(crash_key->name, value).second);
  }

  void Clear(CrashKeyString* crash_key) override {
    ASSERT_EQ(1u, data_->erase(crash_key->name));
  }

  void OutputCrashKeysToStream(std::ostream& out) override {
    out << "Got " << data_->size() << " crash keys.";
  }

 private:
  const raw_ref<std::map<std::string, std::string>> data_;
};

}  // namespace

class CrashLoggingTest : public ::testing::Test {
 public:
  CrashLoggingTest() {
    SetCrashKeyImplementation(
        std::make_unique<TestCrashKeyImplementation>(data_));
  }

  ~CrashLoggingTest() override { SetCrashKeyImplementation(nullptr); }

  const std::map<std::string, std::string>& data() const { return data_; }

 private:
  std::map<std::string, std::string> data_;
};

// Should not crash.
TEST(UninitializedCrashLoggingTest, Basic) {
  static auto* crash_key = AllocateCrashKeyString("test", CrashKeySize::Size32);
  EXPECT_FALSE(crash_key);

  SetCrashKeyString(crash_key, "value");

  ClearCrashKeyString(crash_key);
}

TEST_F(CrashLoggingTest, Basic) {
  static auto* crash_key = AllocateCrashKeyString("test", CrashKeySize::Size32);
  EXPECT_TRUE(crash_key);
  EXPECT_THAT(data(), IsEmpty());

  SetCrashKeyString(crash_key, "value");
  EXPECT_THAT(data(), ElementsAre(Pair("test", "value")));
  std::ostringstream stream;
  OutputCrashKeysToStream(stream);
  EXPECT_EQ("Got 1 crash keys.", stream.str());

  ClearCrashKeyString(crash_key);
  EXPECT_THAT(data(), IsEmpty());
  std::ostringstream stream2;
  OutputCrashKeysToStream(stream2);
  EXPECT_EQ("Got 0 crash keys.", stream2.str());
}

// Verify that the macros are properly setting crash keys.
TEST_F(CrashLoggingTest, Macros) {
  {
    SCOPED_CRASH_KEY_BOOL("category", "bool-value", false);
    EXPECT_THAT(data(), ElementsAre(Pair("category-bool-value", "false")));
  }

  {
    SCOPED_CRASH_KEY_BOOL("category", "bool-value", true);
    EXPECT_THAT(data(), ElementsAre(Pair("category-bool-value", "true")));
  }

  {
    SCOPED_CRASH_KEY_NUMBER("category", "float-value", 0.5);
    EXPECT_THAT(data(), ElementsAre(Pair("category-float-value", "0.5")));
  }

  {
    SCOPED_CRASH_KEY_NUMBER("category", "int-value", 1);
    EXPECT_THAT(data(), ElementsAre(Pair("category-int-value", "1")));
  }

  {
    SCOPED_CRASH_KEY_STRING32("category", "string32-value", "餅");
    EXPECT_THAT(data(), ElementsAre(Pair("category-string32-value", "餅")));
  }

  {
    SCOPED_CRASH_KEY_STRING64("category", "string64-value", "餅");
    EXPECT_THAT(data(), ElementsAre(Pair("category-string64-value", "餅")));
  }

  {
    SCOPED_CRASH_KEY_STRING256("category", "string256-value", "餅");
    EXPECT_THAT(data(), ElementsAre(Pair("category-string256-value", "餅")));
  }

  {
    SCOPED_CRASH_KEY_STRING1024("category", "string1024-value", "餅");
    EXPECT_THAT(data(), ElementsAre(Pair("category-string1024-value", "餅")));
  }
}

// Test that the helper macros properly uniqify the internal variable used for
// the scoper.
TEST_F(CrashLoggingTest, MultipleCrashKeysInSameScope) {
  SCOPED_CRASH_KEY_BOOL("category", "bool-value", false);
  SCOPED_CRASH_KEY_NUMBER("category", "int-value", 1);

  EXPECT_THAT(data(), ElementsAre(Pair("category-bool-value", "false"),
                                  Pair("category-int-value", "1")));

  std::ostringstream stream;
  OutputCrashKeysToStream(stream);
  EXPECT_EQ("Got 2 crash keys.", stream.str());
}

}  // namespace debug
}  // namespace base