summaryrefslogtreecommitdiffstats
path: root/chrome/browser/net/net_log_logger_unittest.cc
blob: d9f764272c8f4e19790a4a94b6329fcba06558b4 (plain)
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
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/net/net_log_logger.h"

#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/json/json_reader.h"
#include "base/values.h"
#include "net/base/net_log.h"
#include "testing/gtest/include/gtest/gtest.h"


class NetLogLoggerTest : public testing::Test {
 public:
  virtual void SetUp() {
    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
    log_path_ = temp_dir_.path().AppendASCII("NetLogFile");
  }

 protected:
  base::ScopedTempDir temp_dir_;
  base::FilePath log_path_;
};


TEST_F(NetLogLoggerTest, GeneratesValidJSONForNoEvents) {
  {
    // Create and destroy a logger.
    NetLogLogger logger(log_path_);
  }

  std::string input;
  ASSERT_TRUE(file_util::ReadFileToString(log_path_, &input));

  base::JSONReader reader;
  scoped_ptr<base::Value> root(reader.ReadToValue(input));
  EXPECT_TRUE(root.get()) << reader.GetErrorMessage();
}

TEST_F(NetLogLoggerTest, GeneratesValidJSONWithOneEvent) {
  {
    NetLogLogger logger(log_path_);

    const int kDummyId = 1;
    net::NetLog::Source source(net::NetLog::SOURCE_SPDY_SESSION, kDummyId);
    net::NetLog::Entry entry(net::NetLog::TYPE_PROXY_SERVICE,
                             source,
                             net::NetLog::PHASE_BEGIN,
                             base::TimeTicks::Now(),
                             NULL,
                             net::NetLog::LOG_BASIC);
    logger.OnAddEntry(entry);
  }

  std::string input;
  ASSERT_TRUE(file_util::ReadFileToString(log_path_, &input));

  base::JSONReader reader;
  scoped_ptr<base::Value> root(reader.ReadToValue(input));
  EXPECT_TRUE(root.get()) << reader.GetErrorMessage();
}

TEST_F(NetLogLoggerTest, GeneratesValidJSONWithMultipleEvents) {
  {
    NetLogLogger logger(log_path_);

    const int kDummyId = 1;
    net::NetLog::Source source(net::NetLog::SOURCE_SPDY_SESSION, kDummyId);
    net::NetLog::Entry entry(net::NetLog::TYPE_PROXY_SERVICE,
                             source,
                             net::NetLog::PHASE_BEGIN,
                             base::TimeTicks::Now(),
                             NULL,
                             net::NetLog::LOG_BASIC);

    // Add the entry multiple times.
    logger.OnAddEntry(entry);
    logger.OnAddEntry(entry);
  }

  std::string input;
  ASSERT_TRUE(file_util::ReadFileToString(log_path_, &input));

  base::JSONReader reader;
  scoped_ptr<base::Value> root(reader.ReadToValue(input));
  EXPECT_TRUE(root.get()) << reader.GetErrorMessage();
}