histograms: Added Platform.TPM.DictionaryAttackResetStatus.
[chromium-blink-merge.git] / net / base / net_log_unittest.cc
blob0f5b0ebf46c9e3515009838276ec5f613e1275e7
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/base/net_log_unittest.h"
7 #include "base/bind.h"
8 #include "base/memory/scoped_vector.h"
9 #include "base/synchronization/waitable_event.h"
10 #include "base/threading/simple_thread.h"
11 #include "base/values.h"
12 #include "net/base/net_errors.h"
14 namespace net {
16 namespace {
18 const int kThreads = 10;
19 const int kEvents = 100;
21 base::Value* NetLogLevelCallback(NetLog::LogLevel log_level) {
22 base::DictionaryValue* dict = new base::DictionaryValue();
23 dict->SetInteger("log_level", log_level);
24 return dict;
27 TEST(NetLogTest, Basic) {
28 CapturingNetLog net_log;
29 CapturingNetLog::CapturedEntryList entries;
30 net_log.GetEntries(&entries);
31 EXPECT_EQ(0u, entries.size());
33 net_log.AddGlobalEntry(NetLog::TYPE_CANCELLED);
35 net_log.GetEntries(&entries);
36 ASSERT_EQ(1u, entries.size());
37 EXPECT_EQ(NetLog::TYPE_CANCELLED, entries[0].type);
38 EXPECT_EQ(NetLog::SOURCE_NONE, entries[0].source.type);
39 EXPECT_NE(NetLog::Source::kInvalidId, entries[0].source.id);
40 EXPECT_EQ(NetLog::PHASE_NONE, entries[0].phase);
41 EXPECT_GE(base::TimeTicks::Now(), entries[0].time);
42 EXPECT_FALSE(entries[0].params);
45 // Check that the correct LogLevel is sent to NetLog Value callbacks.
46 TEST(NetLogTest, LogLevels) {
47 CapturingNetLog net_log;
48 for (int log_level = NetLog::LOG_ALL; log_level < NetLog::LOG_NONE;
49 ++log_level) {
50 net_log.SetLogLevel(static_cast<NetLog::LogLevel>(log_level));
51 EXPECT_EQ(log_level, net_log.GetLogLevel());
53 net_log.AddGlobalEntry(NetLog::TYPE_SOCKET_ALIVE,
54 base::Bind(NetLogLevelCallback));
56 CapturingNetLog::CapturedEntryList entries;
57 net_log.GetEntries(&entries);
59 ASSERT_EQ(1u, entries.size());
60 EXPECT_EQ(NetLog::TYPE_SOCKET_ALIVE, entries[0].type);
61 EXPECT_EQ(NetLog::SOURCE_NONE, entries[0].source.type);
62 EXPECT_NE(NetLog::Source::kInvalidId, entries[0].source.id);
63 EXPECT_EQ(NetLog::PHASE_NONE, entries[0].phase);
64 EXPECT_GE(base::TimeTicks::Now(), entries[0].time);
66 int logged_log_level;
67 ASSERT_TRUE(entries[0].GetIntegerValue("log_level", &logged_log_level));
68 EXPECT_EQ(log_level, logged_log_level);
70 net_log.Clear();
74 class CountingObserver : public NetLog::ThreadSafeObserver {
75 public:
76 CountingObserver() : count_(0) {}
78 ~CountingObserver() override {
79 if (net_log())
80 net_log()->RemoveThreadSafeObserver(this);
83 void OnAddEntry(const NetLog::Entry& entry) override { ++count_; }
85 int count() const { return count_; }
87 private:
88 int count_;
91 class LoggingObserver : public NetLog::ThreadSafeObserver {
92 public:
93 LoggingObserver() {}
95 ~LoggingObserver() override {
96 if (net_log())
97 net_log()->RemoveThreadSafeObserver(this);
100 void OnAddEntry(const NetLog::Entry& entry) override {
101 base::Value* value = entry.ToValue();
102 base::DictionaryValue* dict = NULL;
103 ASSERT_TRUE(value->GetAsDictionary(&dict));
104 values_.push_back(dict);
107 size_t GetNumValues() const { return values_.size(); }
108 base::DictionaryValue* GetValue(size_t index) const { return values_[index]; }
110 private:
111 ScopedVector<base::DictionaryValue> values_;
114 base::Value* LogLevelToValue(NetLog::LogLevel log_level) {
115 return new base::FundamentalValue(log_level);
118 void AddEvent(NetLog* net_log) {
119 net_log->AddGlobalEntry(NetLog::TYPE_CANCELLED, base::Bind(LogLevelToValue));
122 // A thread that waits until an event has been signalled before calling
123 // RunTestThread.
124 class NetLogTestThread : public base::SimpleThread {
125 public:
126 NetLogTestThread()
127 : base::SimpleThread("NetLogTest"),
128 net_log_(NULL),
129 start_event_(NULL) {
132 // We'll wait for |start_event| to be triggered before calling a subclass's
133 // subclass's RunTestThread() function.
134 void Init(NetLog* net_log, base::WaitableEvent* start_event) {
135 start_event_ = start_event;
136 net_log_ = net_log;
139 void Run() override {
140 start_event_->Wait();
141 RunTestThread();
144 // Subclasses must override this with the code they want to run on their
145 // thread.
146 virtual void RunTestThread() = 0;
148 protected:
149 NetLog* net_log_;
151 private:
152 // Only triggered once all threads have been created, to make it less likely
153 // each thread completes before the next one starts.
154 base::WaitableEvent* start_event_;
156 DISALLOW_COPY_AND_ASSIGN(NetLogTestThread);
159 // A thread that adds a bunch of events to the NetLog.
160 class AddEventsTestThread : public NetLogTestThread {
161 public:
162 AddEventsTestThread() {}
163 ~AddEventsTestThread() override {}
165 private:
166 void RunTestThread() override {
167 for (int i = 0; i < kEvents; ++i)
168 AddEvent(net_log_);
171 DISALLOW_COPY_AND_ASSIGN(AddEventsTestThread);
174 // A thread that adds and removes an observer from the NetLog repeatedly.
175 class AddRemoveObserverTestThread : public NetLogTestThread {
176 public:
177 AddRemoveObserverTestThread() {}
179 ~AddRemoveObserverTestThread() override { EXPECT_TRUE(!observer_.net_log()); }
181 private:
182 void RunTestThread() override {
183 for (int i = 0; i < kEvents; ++i) {
184 ASSERT_FALSE(observer_.net_log());
186 net_log_->AddThreadSafeObserver(&observer_, NetLog::LOG_ALL_BUT_BYTES);
187 ASSERT_EQ(net_log_, observer_.net_log());
188 ASSERT_EQ(NetLog::LOG_ALL_BUT_BYTES, observer_.log_level());
189 ASSERT_LE(net_log_->GetLogLevel(), NetLog::LOG_ALL_BUT_BYTES);
191 net_log_->SetObserverLogLevel(&observer_, NetLog::LOG_ALL);
192 ASSERT_EQ(net_log_, observer_.net_log());
193 ASSERT_EQ(NetLog::LOG_ALL, observer_.log_level());
194 ASSERT_LE(net_log_->GetLogLevel(), NetLog::LOG_ALL);
196 net_log_->RemoveThreadSafeObserver(&observer_);
197 ASSERT_TRUE(!observer_.net_log());
201 CountingObserver observer_;
203 DISALLOW_COPY_AND_ASSIGN(AddRemoveObserverTestThread);
206 // Creates |kThreads| threads of type |ThreadType| and then runs them all
207 // to completion.
208 template<class ThreadType>
209 void RunTestThreads(NetLog* net_log) {
210 ThreadType threads[kThreads];
211 base::WaitableEvent start_event(true, false);
213 for (size_t i = 0; i < arraysize(threads); ++i) {
214 threads[i].Init(net_log, &start_event);
215 threads[i].Start();
218 start_event.Signal();
220 for (size_t i = 0; i < arraysize(threads); ++i)
221 threads[i].Join();
224 // Makes sure that events on multiple threads are dispatched to all observers.
225 TEST(NetLogTest, NetLogEventThreads) {
226 NetLog net_log;
228 // Attach some observers. Since they're created after |net_log|, they'll
229 // safely detach themselves on destruction.
230 CountingObserver observers[3];
231 for (size_t i = 0; i < arraysize(observers); ++i)
232 net_log.AddThreadSafeObserver(&observers[i], NetLog::LOG_ALL);
234 // Run a bunch of threads to completion, each of which will emit events to
235 // |net_log|.
236 RunTestThreads<AddEventsTestThread>(&net_log);
238 // Check that each observer saw the emitted events.
239 const int kTotalEvents = kThreads * kEvents;
240 for (size_t i = 0; i < arraysize(observers); ++i)
241 EXPECT_EQ(kTotalEvents, observers[i].count());
244 // Test adding and removing a single observer.
245 TEST(NetLogTest, NetLogAddRemoveObserver) {
246 NetLog net_log;
247 CountingObserver observer;
249 AddEvent(&net_log);
250 EXPECT_EQ(0, observer.count());
251 EXPECT_EQ(NULL, observer.net_log());
252 EXPECT_EQ(NetLog::LOG_NONE, net_log.GetLogLevel());
254 // Add the observer and add an event.
255 net_log.AddThreadSafeObserver(&observer, NetLog::LOG_ALL_BUT_BYTES);
256 EXPECT_EQ(&net_log, observer.net_log());
257 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES, observer.log_level());
258 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES, net_log.GetLogLevel());
260 AddEvent(&net_log);
261 EXPECT_EQ(1, observer.count());
263 // Change the observer's logging level and add an event.
264 net_log.SetObserverLogLevel(&observer, NetLog::LOG_ALL);
265 EXPECT_EQ(&net_log, observer.net_log());
266 EXPECT_EQ(NetLog::LOG_ALL, observer.log_level());
267 EXPECT_EQ(NetLog::LOG_ALL, net_log.GetLogLevel());
269 AddEvent(&net_log);
270 EXPECT_EQ(2, observer.count());
272 // Remove observer and add an event.
273 net_log.RemoveThreadSafeObserver(&observer);
274 EXPECT_EQ(NULL, observer.net_log());
275 EXPECT_EQ(NetLog::LOG_NONE, net_log.GetLogLevel());
277 AddEvent(&net_log);
278 EXPECT_EQ(2, observer.count());
280 // Add the observer a final time, and add an event.
281 net_log.AddThreadSafeObserver(&observer, NetLog::LOG_ALL);
282 EXPECT_EQ(&net_log, observer.net_log());
283 EXPECT_EQ(NetLog::LOG_ALL, observer.log_level());
284 EXPECT_EQ(NetLog::LOG_ALL, net_log.GetLogLevel());
286 AddEvent(&net_log);
287 EXPECT_EQ(3, observer.count());
290 // Test adding and removing two observers at different log levels.
291 TEST(NetLogTest, NetLogTwoObservers) {
292 NetLog net_log;
293 LoggingObserver observer[2];
295 // Add first observer.
296 net_log.AddThreadSafeObserver(&observer[0], NetLog::LOG_ALL_BUT_BYTES);
297 EXPECT_EQ(&net_log, observer[0].net_log());
298 EXPECT_EQ(NULL, observer[1].net_log());
299 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES, observer[0].log_level());
300 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES, net_log.GetLogLevel());
302 // Add second observer observer.
303 net_log.AddThreadSafeObserver(&observer[1], NetLog::LOG_ALL);
304 EXPECT_EQ(&net_log, observer[0].net_log());
305 EXPECT_EQ(&net_log, observer[1].net_log());
306 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES, observer[0].log_level());
307 EXPECT_EQ(NetLog::LOG_ALL, observer[1].log_level());
308 EXPECT_EQ(NetLog::LOG_ALL, net_log.GetLogLevel());
310 // Add event and make sure both observers receive it at their respective log
311 // levels.
312 int param;
313 AddEvent(&net_log);
314 ASSERT_EQ(1U, observer[0].GetNumValues());
315 ASSERT_TRUE(observer[0].GetValue(0)->GetInteger("params", &param));
316 EXPECT_EQ(observer[0].log_level(), param);
317 ASSERT_EQ(1U, observer[1].GetNumValues());
318 ASSERT_TRUE(observer[1].GetValue(0)->GetInteger("params", &param));
319 EXPECT_EQ(observer[1].log_level(), param);
321 // Remove second observer.
322 net_log.RemoveThreadSafeObserver(&observer[1]);
323 EXPECT_EQ(&net_log, observer[0].net_log());
324 EXPECT_EQ(NULL, observer[1].net_log());
325 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES, observer[0].log_level());
326 EXPECT_EQ(NetLog::LOG_ALL_BUT_BYTES, net_log.GetLogLevel());
328 // Add event and make sure only second observer gets it.
329 AddEvent(&net_log);
330 EXPECT_EQ(2U, observer[0].GetNumValues());
331 EXPECT_EQ(1U, observer[1].GetNumValues());
333 // Remove first observer.
334 net_log.RemoveThreadSafeObserver(&observer[0]);
335 EXPECT_EQ(NULL, observer[0].net_log());
336 EXPECT_EQ(NULL, observer[1].net_log());
337 EXPECT_EQ(NetLog::LOG_NONE, net_log.GetLogLevel());
339 // Add event and make sure neither observer gets it.
340 AddEvent(&net_log);
341 EXPECT_EQ(2U, observer[0].GetNumValues());
342 EXPECT_EQ(1U, observer[1].GetNumValues());
345 // Makes sure that adding and removing observers simultaneously on different
346 // threads works.
347 TEST(NetLogTest, NetLogAddRemoveObserverThreads) {
348 NetLog net_log;
350 // Run a bunch of threads to completion, each of which will repeatedly add
351 // and remove an observer, and set its logging level.
352 RunTestThreads<AddRemoveObserverTestThread>(&net_log);
355 } // namespace
357 } // namespace net