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
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359
  360
  361
  362
  363
  364
  365
  366
  367
  368
  369
  370
  371
  372
  373
  374
  375
  376
  377
  378
  379
  380
  381
  382
  383
  384
  385
  386
  387
  388
  389
  390
  391
  392
  393
  394
  395
  396
  397
  398
  399
  400
  401
  402
  403
  404
  405
  406
  407
  408
  409
  410
  411
  412
  413
  414
  415
  416
  417
  418
  419
  420
  421
  422
  423
  424
  425
  426
  427
  428
  429
  430
  431
  432
  433
  434
  435
  436
  437
  438
  439
  440
  441
  442
  443
  444
  445
  446
  447
  448
  449
  450
  451
  452
  453
  454
  455
  456
  457
  458
  459
  460
  461
  462
  463
  464
  465
  466
  467
  468
  469
  470
  471
  472
  473
  474
  475
  476
  477
  478
  479
  480
  481
  482
  483
  484
  485
  486
  487
  488
  489
  490
  491
  492
  493
  494
  495
  496
  497
  498
  499
  500
  501
  502
  503
  504
  505
  506
  507
  508
  509
  510
  511
  512
  513
  514
  515
  516
  517
  518

base / trace_event / trace_arguments_unittest.cc [blame]

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

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
#pragma allow_unsafe_buffers
#endif

#include "base/trace_event/trace_arguments.h"

#include <gtest/gtest.h>
#include <limits>
#include <string>

#include "base/memory/raw_ptr.h"

namespace base {
namespace trace_event {

namespace {

// Simple convertable that holds a string to append to the trace,
// and can also write to a boolean flag on destruction.
class MyConvertable : public ConvertableToTraceFormat {
 public:
  MyConvertable(const char* text, bool* destroy_flag = nullptr)
      : text_(text), destroy_flag_(destroy_flag) {}
  ~MyConvertable() override {
    if (destroy_flag_)
      *destroy_flag_ = true;
  }
  void AppendAsTraceFormat(std::string* out) const override { *out += text_; }
  const char* text() const { return text_; }

 private:
  const char* text_;
  raw_ptr<bool> destroy_flag_;
};

}  // namespace

TEST(TraceArguments, StringStorageDefaultConstruction) {
  StringStorage storage;
  EXPECT_TRUE(storage.empty());
  EXPECT_FALSE(storage.data());
  EXPECT_EQ(0U, storage.size());
}

TEST(TraceArguments, StringStorageConstructionWithSize) {
  const size_t kSize = 128;
  StringStorage storage(kSize);
  EXPECT_FALSE(storage.empty());
  EXPECT_TRUE(storage.data());
  EXPECT_EQ(kSize, storage.size());
  EXPECT_EQ(storage.data(), storage.begin());
  EXPECT_EQ(storage.data() + kSize, storage.end());
}

TEST(TraceArguments, StringStorageReset) {
  StringStorage storage(128);
  EXPECT_FALSE(storage.empty());

  storage.Reset();
  EXPECT_TRUE(storage.empty());
  EXPECT_FALSE(storage.data());
  EXPECT_EQ(0u, storage.size());
}

TEST(TraceArguments, StringStorageResetWithSize) {
  StringStorage storage;
  EXPECT_TRUE(storage.empty());

  const size_t kSize = 128;
  storage.Reset(kSize);
  EXPECT_FALSE(storage.empty());
  EXPECT_TRUE(storage.data());
  EXPECT_EQ(kSize, storage.size());
  EXPECT_EQ(storage.data(), storage.begin());
  EXPECT_EQ(storage.data() + kSize, storage.end());
}

static void CheckJSONFor(TraceValue v, char type, const char* expected) {
  std::string out;
  v.AppendAsJSON(type, &out);
  EXPECT_STREQ(expected, out.c_str());
}

static void CheckStringFor(TraceValue v, char type, const char* expected) {
  std::string out;
  v.AppendAsString(type, &out);
  EXPECT_STREQ(expected, out.c_str());
}

TEST(TraceArguments, TraceValueAppend) {
  TraceValue v;

  v.Init(-1024);
  CheckJSONFor(v, TRACE_VALUE_TYPE_INT, "-1024");
  CheckStringFor(v, TRACE_VALUE_TYPE_INT, "-1024");
  v.Init(1024ULL);
  CheckJSONFor(v, TRACE_VALUE_TYPE_UINT, "1024");
  CheckStringFor(v, TRACE_VALUE_TYPE_UINT, "1024");
  v.Init(3.1415926535);
  CheckJSONFor(v, TRACE_VALUE_TYPE_DOUBLE, "3.1415926535");
  CheckStringFor(v, TRACE_VALUE_TYPE_DOUBLE, "3.1415926535");
  v.Init(2.0);
  CheckJSONFor(v, TRACE_VALUE_TYPE_DOUBLE, "2.0");
  CheckStringFor(v, TRACE_VALUE_TYPE_DOUBLE, "2.0");
  v.Init(0.5);
  CheckJSONFor(v, TRACE_VALUE_TYPE_DOUBLE, "0.5");
  CheckStringFor(v, TRACE_VALUE_TYPE_DOUBLE, "0.5");
  v.Init(-0.5);
  CheckJSONFor(v, TRACE_VALUE_TYPE_DOUBLE, "-0.5");
  CheckStringFor(v, TRACE_VALUE_TYPE_DOUBLE, "-0.5");
  v.Init(std::numeric_limits<double>::quiet_NaN());
  CheckJSONFor(v, TRACE_VALUE_TYPE_DOUBLE, "\"NaN\"");
  CheckStringFor(v, TRACE_VALUE_TYPE_DOUBLE, "NaN");
  v.Init(std::numeric_limits<double>::quiet_NaN());
  CheckJSONFor(v, TRACE_VALUE_TYPE_DOUBLE, "\"NaN\"");
  CheckStringFor(v, TRACE_VALUE_TYPE_DOUBLE, "NaN");
  v.Init(std::numeric_limits<double>::infinity());
  CheckJSONFor(v, TRACE_VALUE_TYPE_DOUBLE, "\"Infinity\"");
  CheckStringFor(v, TRACE_VALUE_TYPE_DOUBLE, "Infinity");
  v.Init(-std::numeric_limits<double>::infinity());
  CheckJSONFor(v, TRACE_VALUE_TYPE_DOUBLE, "\"-Infinity\"");
  CheckStringFor(v, TRACE_VALUE_TYPE_DOUBLE, "-Infinity");
  v.Init(true);
  CheckJSONFor(v, TRACE_VALUE_TYPE_BOOL, "true");
  CheckStringFor(v, TRACE_VALUE_TYPE_BOOL, "true");
  v.Init(false);
  CheckJSONFor(v, TRACE_VALUE_TYPE_BOOL, "false");
  CheckStringFor(v, TRACE_VALUE_TYPE_BOOL, "false");
  v.Init("Some \"nice\" String");
  CheckJSONFor(v, TRACE_VALUE_TYPE_STRING, "\"Some \\\"nice\\\" String\"");
  CheckStringFor(v, TRACE_VALUE_TYPE_STRING, "Some \"nice\" String");
  CheckJSONFor(v, TRACE_VALUE_TYPE_COPY_STRING, "\"Some \\\"nice\\\" String\"");
  CheckStringFor(v, TRACE_VALUE_TYPE_COPY_STRING, "Some \"nice\" String");

  int* p = nullptr;
  v.Init(static_cast<void*>(p));
  CheckJSONFor(v, TRACE_VALUE_TYPE_POINTER, "\"0x0\"");
  CheckStringFor(v, TRACE_VALUE_TYPE_POINTER, "0x0");

  const char kText[] = "Hello World";
  bool destroy_flag = false;
  TraceArguments args("arg1",
                      std::make_unique<MyConvertable>(kText, &destroy_flag));

  CheckJSONFor(std::move(args.values()[0]), args.types()[0], kText);
  CheckStringFor(std::move(args.values()[0]), args.types()[0], kText);
}

TEST(TraceArguments, DefaultConstruction) {
  TraceArguments args;
  EXPECT_EQ(0U, args.size());
}

TEST(TraceArguments, ConstructorSingleInteger) {
  TraceArguments args("foo_int", int(10));
  EXPECT_EQ(1U, args.size());
  EXPECT_EQ(TRACE_VALUE_TYPE_INT, args.types()[0]);
  EXPECT_STREQ("foo_int", args.names()[0]);
  EXPECT_EQ(10, args.values()[0].as_int);
}

TEST(TraceArguments, ConstructorSingleFloat) {
  TraceArguments args("foo_pi", float(3.1415));
  double expected = float(3.1415);
  EXPECT_EQ(1U, args.size());
  EXPECT_EQ(TRACE_VALUE_TYPE_DOUBLE, args.types()[0]);
  EXPECT_STREQ("foo_pi", args.names()[0]);
  EXPECT_EQ(expected, args.values()[0].as_double);
}

TEST(TraceArguments, ConstructorSingleNoCopyString) {
  const char kText[] = "Persistent string";
  TraceArguments args("foo_cstring", kText);
  EXPECT_EQ(1U, args.size());
  EXPECT_EQ(TRACE_VALUE_TYPE_STRING, args.types()[0]);
  EXPECT_STREQ("foo_cstring", args.names()[0]);
  EXPECT_EQ(kText, args.values()[0].as_string);
}

TEST(TraceArguments, ConstructorSingleStdString) {
  std::string text = "Non-persistent string";
  TraceArguments args("foo_stdstring", text);
  EXPECT_EQ(1U, args.size());
  EXPECT_EQ(TRACE_VALUE_TYPE_COPY_STRING, args.types()[0]);
  EXPECT_STREQ("foo_stdstring", args.names()[0]);
  EXPECT_EQ(text.c_str(), args.values()[0].as_string);
}

TEST(TraceArguments, ConstructorSingleTraceStringWithCopy) {
  const char kText[] = "Persistent string #2";
  TraceArguments args("foo_tracestring", TraceStringWithCopy(kText));
  EXPECT_EQ(1U, args.size());
  EXPECT_EQ(TRACE_VALUE_TYPE_COPY_STRING, args.types()[0]);
  EXPECT_STREQ("foo_tracestring", args.names()[0]);
  EXPECT_EQ(kText, args.values()[0].as_string);
}

TEST(TraceArguments, ConstructorSinglePointer) {
  bool destroy_flag = false;
  {
    // Simple class that can set a boolean flag on destruction.
    class Foo {
     public:
      Foo(bool* destroy_flag) : destroy_flag_(destroy_flag) {}
      ~Foo() {
        if (destroy_flag_)
          *destroy_flag_ = true;
      }

     private:
      raw_ptr<bool> destroy_flag_;
    };
    auto foo = std::make_unique<Foo>(&destroy_flag);
    EXPECT_FALSE(destroy_flag);
    // This test also verifies that the object is not destroyed by the
    // TraceArguments destructor. This should only be possible for
    // TRACE_VALUE_TYPE_CONVERTABLE instances.
    {
      TraceArguments args("foo_pointer", static_cast<void*>(foo.get()));
      EXPECT_EQ(1U, args.size());
      EXPECT_EQ(TRACE_VALUE_TYPE_POINTER, args.types()[0]);
      EXPECT_STREQ("foo_pointer", args.names()[0]);
      EXPECT_EQ(foo.get(), args.values()[0].as_pointer);
      EXPECT_FALSE(destroy_flag);
    }  // Calls TraceArguments destructor.
    EXPECT_FALSE(destroy_flag);
  }  // Calls Foo destructor.
  EXPECT_TRUE(destroy_flag);
}

TEST(TraceArguments, ConstructorSingleConvertable) {
  bool destroy_flag = false;
  const char kText[] = "Text for MyConvertable instance";
  MyConvertable* ptr = new MyConvertable(kText, &destroy_flag);

  // This test also verifies that the MyConvertable instance is properly
  // destroyed when the TraceArguments destructor is called.
  EXPECT_FALSE(destroy_flag);
  {
    TraceArguments args("foo_convertable", std::unique_ptr<MyConvertable>(ptr));
    EXPECT_EQ(1U, args.size());
    EXPECT_EQ(TRACE_VALUE_TYPE_CONVERTABLE, args.types()[0]);
    EXPECT_STREQ("foo_convertable", args.names()[0]);
    EXPECT_EQ(ptr, args.values()[0].as_convertable);
    EXPECT_FALSE(destroy_flag);
  }  // Calls TraceArguments destructor.
  EXPECT_TRUE(destroy_flag);
}

TEST(TraceArguments, ConstructorWithTwoArguments) {
  const char kText1[] = "First argument";
  const char kText2[] = "Second argument";
  bool destroy_flag = false;

  {
    MyConvertable* ptr = new MyConvertable(kText2, &destroy_flag);
    TraceArguments args1("foo_arg1_cstring", kText1, "foo_arg2_convertable",
                         std::unique_ptr<MyConvertable>(ptr));
    EXPECT_EQ(2U, args1.size());
    EXPECT_STREQ("foo_arg1_cstring", args1.names()[0]);
    EXPECT_STREQ("foo_arg2_convertable", args1.names()[1]);
    EXPECT_EQ(TRACE_VALUE_TYPE_STRING, args1.types()[0]);
    EXPECT_EQ(TRACE_VALUE_TYPE_CONVERTABLE, args1.types()[1]);
    EXPECT_EQ(kText1, args1.values()[0].as_string);
    EXPECT_EQ(ptr, args1.values()[1].as_convertable);
    EXPECT_FALSE(destroy_flag);
  }  // calls |args1| destructor. Should delete |ptr|.
  EXPECT_TRUE(destroy_flag);
}

TEST(TraceArguments, ConstructorLegacyNoConvertables) {
  const char* const kNames[3] = {"legacy_arg1", "legacy_arg2", "legacy_arg3"};
  const unsigned char kTypes[3] = {
      TRACE_VALUE_TYPE_INT,
      TRACE_VALUE_TYPE_STRING,
      TRACE_VALUE_TYPE_POINTER,
  };
  static const char kText[] = "Some text";
  const unsigned long long kValues[3] = {
      1000042ULL,
      reinterpret_cast<unsigned long long>(kText),
      reinterpret_cast<unsigned long long>(kText + 2),
  };
  TraceArguments args(3, kNames, kTypes, kValues);
  // Check that only the first kMaxSize arguments are taken!
  EXPECT_EQ(2U, args.size());
  EXPECT_STREQ(kNames[0], args.names()[0]);
  EXPECT_STREQ(kNames[1], args.names()[1]);
  EXPECT_EQ(TRACE_VALUE_TYPE_INT, args.types()[0]);
  EXPECT_EQ(TRACE_VALUE_TYPE_STRING, args.types()[1]);
  EXPECT_EQ(kValues[0], args.values()[0].as_uint);
  EXPECT_EQ(kText, args.values()[1].as_string);
}

TEST(TraceArguments, ConstructorLegacyWithConvertables) {
  const char* const kNames[3] = {"legacy_arg1", "legacy_arg2", "legacy_arg3"};
  const unsigned char kTypes[3] = {
      TRACE_VALUE_TYPE_CONVERTABLE,
      TRACE_VALUE_TYPE_CONVERTABLE,
      TRACE_VALUE_TYPE_CONVERTABLE,
  };
  std::unique_ptr<MyConvertable> convertables[3] = {
      std::make_unique<MyConvertable>("First one"),
      std::make_unique<MyConvertable>("Second one"),
      std::make_unique<MyConvertable>("Third one"),
  };
  TraceArguments args(3, kNames, kTypes, nullptr, convertables);
  // Check that only the first kMaxSize arguments are taken!
  EXPECT_EQ(2U, args.size());
  EXPECT_STREQ(kNames[0], args.names()[0]);
  EXPECT_STREQ(kNames[1], args.names()[1]);
  EXPECT_EQ(TRACE_VALUE_TYPE_CONVERTABLE, args.types()[0]);
  EXPECT_EQ(TRACE_VALUE_TYPE_CONVERTABLE, args.types()[1]);
  // Check that only the first two items were moved to |args|.
  EXPECT_FALSE(convertables[0].get());
  EXPECT_FALSE(convertables[1].get());
  EXPECT_TRUE(convertables[2].get());
}

TEST(TraceArguments, MoveConstruction) {
  const char kText1[] = "First argument";
  const char kText2[] = "Second argument";
  bool destroy_flag = false;

  {
    MyConvertable* ptr = new MyConvertable(kText2, &destroy_flag);
    TraceArguments args1("foo_arg1_cstring", kText1, "foo_arg2_convertable",
                         std::unique_ptr<MyConvertable>(ptr));
    EXPECT_EQ(2U, args1.size());
    EXPECT_STREQ("foo_arg1_cstring", args1.names()[0]);
    EXPECT_STREQ("foo_arg2_convertable", args1.names()[1]);
    EXPECT_EQ(TRACE_VALUE_TYPE_STRING, args1.types()[0]);
    EXPECT_EQ(TRACE_VALUE_TYPE_CONVERTABLE, args1.types()[1]);
    EXPECT_EQ(kText1, args1.values()[0].as_string);
    EXPECT_EQ(ptr, args1.values()[1].as_convertable);

    {
      TraceArguments args2(std::move(args1));
      EXPECT_FALSE(destroy_flag);

      // |args1| is now empty.
      EXPECT_EQ(0U, args1.size());

      // Check that everything was transferred to |args2|.
      EXPECT_EQ(2U, args2.size());
      EXPECT_STREQ("foo_arg1_cstring", args2.names()[0]);
      EXPECT_STREQ("foo_arg2_convertable", args2.names()[1]);
      EXPECT_EQ(TRACE_VALUE_TYPE_STRING, args2.types()[0]);
      EXPECT_EQ(TRACE_VALUE_TYPE_CONVERTABLE, args2.types()[1]);
      EXPECT_EQ(kText1, args2.values()[0].as_string);
      EXPECT_EQ(ptr, args2.values()[1].as_convertable);
    }  // Calls |args2| destructor. Should delete |ptr|.
    EXPECT_TRUE(destroy_flag);
    destroy_flag = false;
  }  // Calls |args1| destructor. Should not delete |ptr|.
  EXPECT_FALSE(destroy_flag);
}

TEST(TraceArguments, MoveAssignment) {
  const char kText1[] = "First argument";
  const char kText2[] = "Second argument";
  bool destroy_flag = false;

  {
    MyConvertable* ptr = new MyConvertable(kText2, &destroy_flag);
    TraceArguments args1("foo_arg1_cstring", kText1, "foo_arg2_convertable",
                         std::unique_ptr<MyConvertable>(ptr));
    EXPECT_EQ(2U, args1.size());
    EXPECT_STREQ("foo_arg1_cstring", args1.names()[0]);
    EXPECT_STREQ("foo_arg2_convertable", args1.names()[1]);
    EXPECT_EQ(TRACE_VALUE_TYPE_STRING, args1.types()[0]);
    EXPECT_EQ(TRACE_VALUE_TYPE_CONVERTABLE, args1.types()[1]);
    EXPECT_EQ(kText1, args1.values()[0].as_string);
    EXPECT_EQ(ptr, args1.values()[1].as_convertable);

    {
      TraceArguments args2;

      args2 = std::move(args1);
      EXPECT_FALSE(destroy_flag);

      // |args1| is now empty.
      EXPECT_EQ(0U, args1.size());

      // Check that everything was transferred to |args2|.
      EXPECT_EQ(2U, args2.size());
      EXPECT_STREQ("foo_arg1_cstring", args2.names()[0]);
      EXPECT_STREQ("foo_arg2_convertable", args2.names()[1]);
      EXPECT_EQ(TRACE_VALUE_TYPE_STRING, args2.types()[0]);
      EXPECT_EQ(TRACE_VALUE_TYPE_CONVERTABLE, args2.types()[1]);
      EXPECT_EQ(kText1, args2.values()[0].as_string);
      EXPECT_EQ(ptr, args2.values()[1].as_convertable);
    }  // Calls |args2| destructor. Should delete |ptr|.
    EXPECT_TRUE(destroy_flag);
    destroy_flag = false;
  }  // Calls |args1| destructor. Should not delete |ptr|.
  EXPECT_FALSE(destroy_flag);
}

TEST(TraceArguments, Reset) {
  bool destroy_flag = false;
  {
    TraceArguments args(
        "foo_arg1", "Hello", "foo_arg2",
        std::make_unique<MyConvertable>("World", &destroy_flag));

    EXPECT_EQ(2U, args.size());
    EXPECT_FALSE(destroy_flag);
    args.Reset();
    EXPECT_EQ(0U, args.size());
    EXPECT_TRUE(destroy_flag);
    destroy_flag = false;
  }  // Calls |args| destructor. Should not delete twice.
  EXPECT_FALSE(destroy_flag);
}

TEST(TraceArguments, CopyStringsTo_NoStrings) {
  StringStorage storage;

  TraceArguments args("arg1", 10, "arg2", 42);
  args.CopyStringsTo(&storage, false, nullptr, nullptr);
  EXPECT_TRUE(storage.empty());
  EXPECT_EQ(0U, storage.size());
}

TEST(TraceArguments, CopyStringsTo_OnlyArgs) {
  StringStorage storage;

  TraceArguments args("arg1", TraceStringWithCopy("Hello"), "arg2",
                      TraceStringWithCopy("World"));

  const char kExtra1[] = "extra1";
  const char kExtra2[] = "extra2";
  const char* extra1 = kExtra1;
  const char* extra2 = kExtra2;

  // Types should be copyable strings.
  EXPECT_EQ(TRACE_VALUE_TYPE_COPY_STRING, args.types()[0]);
  EXPECT_EQ(TRACE_VALUE_TYPE_COPY_STRING, args.types()[1]);

  args.CopyStringsTo(&storage, false, &extra1, &extra2);

  // Storage should be allocated.
  EXPECT_TRUE(storage.data());
  EXPECT_NE(0U, storage.size());

  // Types should not be changed.
  EXPECT_EQ(TRACE_VALUE_TYPE_COPY_STRING, args.types()[0]);
  EXPECT_EQ(TRACE_VALUE_TYPE_COPY_STRING, args.types()[1]);

  // names should not be copied.
  EXPECT_FALSE(storage.Contains(args.names()[0]));
  EXPECT_FALSE(storage.Contains(args.names()[1]));
  EXPECT_STREQ("arg1", args.names()[0]);
  EXPECT_STREQ("arg2", args.names()[1]);

  // strings should be copied.
  EXPECT_TRUE(storage.Contains(args.values()[0].as_string));
  EXPECT_TRUE(storage.Contains(args.values()[1].as_string));
  EXPECT_STREQ("Hello", args.values()[0].as_string);
  EXPECT_STREQ("World", args.values()[1].as_string);

  // |extra1| and |extra2| should not be copied.
  EXPECT_EQ(kExtra1, extra1);
  EXPECT_EQ(kExtra2, extra2);
}

TEST(TraceArguments, CopyStringsTo_Everything) {
  StringStorage storage;

  TraceArguments args("arg1", "Hello", "arg2", "World");
  const char kExtra1[] = "extra1";
  const char kExtra2[] = "extra2";
  const char* extra1 = kExtra1;
  const char* extra2 = kExtra2;

  // Types should be normal strings.
  EXPECT_EQ(TRACE_VALUE_TYPE_STRING, args.types()[0]);
  EXPECT_EQ(TRACE_VALUE_TYPE_STRING, args.types()[1]);

  args.CopyStringsTo(&storage, true, &extra1, &extra2);

  // Storage should be allocated.
  EXPECT_TRUE(storage.data());
  EXPECT_NE(0U, storage.size());

  // Types should be changed to copyable strings.
  EXPECT_EQ(TRACE_VALUE_TYPE_COPY_STRING, args.types()[0]);
  EXPECT_EQ(TRACE_VALUE_TYPE_COPY_STRING, args.types()[1]);

  // names should be copied.
  EXPECT_TRUE(storage.Contains(args.names()[0]));
  EXPECT_TRUE(storage.Contains(args.names()[1]));
  EXPECT_STREQ("arg1", args.names()[0]);
  EXPECT_STREQ("arg2", args.names()[1]);

  // strings should be copied.
  EXPECT_TRUE(storage.Contains(args.values()[0].as_string));
  EXPECT_TRUE(storage.Contains(args.values()[1].as_string));
  EXPECT_STREQ("Hello", args.values()[0].as_string);
  EXPECT_STREQ("World", args.values()[1].as_string);

  // |extra1| and |extra2| should be copied.
  EXPECT_NE(kExtra1, extra1);
  EXPECT_NE(kExtra2, extra2);
  EXPECT_TRUE(storage.Contains(extra1));
  EXPECT_TRUE(storage.Contains(extra2));
  EXPECT_STREQ(kExtra1, extra1);
  EXPECT_STREQ(kExtra2, extra2);
}

}  // namespace trace_event
}  // namespace base