summaryrefslogtreecommitdiffstats
path: root/chromeos/network/network_event_log_unittest.cc
blob: 3d44f6c983433b7f80aeba18d7e83ff5358adbb4 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) 2012 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 "chromeos/network/network_event_log.h"

#include <algorithm>

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/format_macros.h"
#include "base/string_split.h"
#include "base/stringprintf.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace chromeos {

class NetworkEventLogTest : public testing::Test {
 public:
  NetworkEventLogTest() {
  }

  virtual void SetUp() OVERRIDE {
    network_event_log::Initialize();
  }

  virtual void TearDown() OVERRIDE {
    network_event_log::Shutdown();
  }

 protected:
  std::string SkipTime(const std::string& input) {
    std::string output;
    std::vector<std::string> lines;
    base::SplitString(input, '\n', &lines);
    for (size_t i = 0; i < lines.size(); ++i) {
      size_t n = lines[i].find(']');
      if (n != std::string::npos)
        output += lines[i].substr(n+2) + '\n';
    }
    return output;
  }

  size_t CountLines(const std::string& input) {
    return std::count(input.begin(), input.end(), '\n');
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(NetworkEventLogTest);
};

TEST_F(NetworkEventLogTest, TestNetworkEvents) {
  std::string output_none = network_event_log::GetAsString(
      network_event_log::OLDEST_FIRST, 0);
  EXPECT_EQ("No Log Entries.", output_none);

  network_event_log::AddEntry("module1", "event1", "description1");
  network_event_log::AddEntry("module2", "event2", "description2");
  network_event_log::AddEntry("module3", "event3", "description3");
  network_event_log::AddEntry("module3", "event3", "description3");

  const std::string expected_output_oldest_first(
      "module1.event1: description1\n"
      "module2.event2: description2\n"
      "module3.event3: description3 (2)\n");
  std::string output_oldest_first = network_event_log::GetAsString(
      network_event_log::OLDEST_FIRST, 0);
  output_oldest_first = SkipTime(output_oldest_first);
  EXPECT_EQ(expected_output_oldest_first, output_oldest_first);

  const std::string expected_output_oldest_first_short(
      "module2.event2: description2\n"
      "module3.event3: description3 (2)\n");
  std::string output_oldest_first_short = network_event_log::GetAsString(
      network_event_log::OLDEST_FIRST, 2);
  output_oldest_first_short = SkipTime(output_oldest_first_short);
  EXPECT_EQ(expected_output_oldest_first_short, output_oldest_first_short);

  const std::string expected_output_newest_first(
      "module3.event3: description3 (2)\n"
      "module2.event2: description2\n"
      "module1.event1: description1\n");
  std::string output_newest_first = network_event_log::GetAsString(
      network_event_log::NEWEST_FIRST, 0);
  output_newest_first = SkipTime(output_newest_first);
  EXPECT_EQ(expected_output_newest_first, output_newest_first);

  const std::string expected_output_newest_first_short(
      "module3.event3: description3 (2)\n"
      "module2.event2: description2\n");
  std::string output_newest_first_short = network_event_log::GetAsString(
      network_event_log::NEWEST_FIRST, 2);
  output_newest_first_short = SkipTime(output_newest_first_short);
  EXPECT_EQ(expected_output_newest_first_short, output_newest_first_short);
}

TEST_F(NetworkEventLogTest, TestMaxNetworkEvents) {
  const size_t entries_to_add =
      network_event_log::kMaxNetworkEventLogEntries + 3;
  for (size_t i = 0; i < entries_to_add; ++i)
    network_event_log::AddEntry("test", StringPrintf("event_%"PRIuS, i), "");

  std::string output = GetAsString(network_event_log::OLDEST_FIRST, 0);
  size_t output_lines = CountLines(output);
  EXPECT_EQ(network_event_log::kMaxNetworkEventLogEntries, output_lines);
}

}  // namespace chromeos