NaCl docs: add sanitizers to GSoC ideas
[chromium-blink-merge.git] / net / base / net_log_util_unittest.cc
blobe052980b0882650b7cd564290fea1b1f95cec42c
1 // Copyright 2014 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_util.h"
7 #include <set>
9 #include "base/files/file_path.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/values.h"
12 #include "net/base/capturing_net_log_observer.h"
13 #include "net/base/net_errors.h"
14 #include "net/base/test_completion_callback.h"
15 #include "net/http/http_cache.h"
16 #include "net/http/http_transaction.h"
17 #include "net/test/spawned_test_server/spawned_test_server.h"
18 #include "net/url_request/url_request_test_util.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 namespace net {
23 namespace {
25 // Make sure GetNetConstants doesn't crash.
26 TEST(NetLogUtil, GetNetConstants) {
27 scoped_ptr<base::Value> constants(GetNetConstants());
30 // Make sure GetNetInfo doesn't crash when called on contexts with and without
31 // caches, and they have the same number of elements.
32 TEST(NetLogUtil, GetNetInfo) {
33 TestURLRequestContext context;
34 net::HttpCache* http_cache = context.http_transaction_factory()->GetCache();
36 // Get NetInfo when there's no cache backend (It's only created on first use).
37 EXPECT_FALSE(http_cache->GetCurrentBackend());
38 scoped_ptr<base::DictionaryValue> net_info_without_cache(
39 GetNetInfo(&context, NET_INFO_ALL_SOURCES));
40 EXPECT_FALSE(http_cache->GetCurrentBackend());
41 EXPECT_GT(net_info_without_cache->size(), 0u);
43 // Fore creation of a cache backend, and get NetInfo again.
44 disk_cache::Backend* backend = NULL;
45 EXPECT_EQ(
46 OK,
47 context.http_transaction_factory()->GetCache()->GetBackend(
48 &backend, TestCompletionCallback().callback()));
49 EXPECT_TRUE(http_cache->GetCurrentBackend());
50 scoped_ptr<base::DictionaryValue> net_info_with_cache(
51 GetNetInfo(&context, NET_INFO_ALL_SOURCES));
52 EXPECT_GT(net_info_with_cache->size(), 0u);
54 EXPECT_EQ(net_info_without_cache->size(), net_info_with_cache->size());
57 // Make sure CreateNetLogEntriesForActiveObjects works for requests from a
58 // single URLRequestContext.
59 TEST(NetLogUtil, CreateNetLogEntriesForActiveObjectsOneContext) {
60 // Using same context for each iteration makes sure deleted requests don't
61 // appear in the list, or result in crashes.
62 TestURLRequestContext context(true);
63 NetLog net_log;
64 context.set_net_log(&net_log);
65 context.Init();
66 TestDelegate delegate;
67 for (size_t num_requests = 0; num_requests < 5; ++num_requests) {
68 ScopedVector<URLRequest> requests;
69 for (size_t i = 0; i < num_requests; ++i) {
70 requests.push_back(context.CreateRequest(
71 GURL("about:life"), DEFAULT_PRIORITY, &delegate, nullptr).release());
73 std::set<URLRequestContext*> contexts;
74 contexts.insert(&context);
75 CapturingNetLogObserver capturing_observer;
76 CreateNetLogEntriesForActiveObjects(contexts, &capturing_observer);
77 CapturedNetLogEntry::List entry_list;
78 capturing_observer.GetEntries(&entry_list);
79 ASSERT_EQ(num_requests, entry_list.size());
81 for (size_t i = 0; i < num_requests; ++i) {
82 EXPECT_EQ(entry_list[i].source.id, requests[i]->net_log().source().id);
87 // Make sure CreateNetLogEntriesForActiveObjects works with multiple
88 // URLRequestContexts.
89 TEST(NetLogUtil, CreateNetLogEntriesForActiveObjectsMultipleContexts) {
90 TestDelegate delegate;
91 for (size_t num_requests = 0; num_requests < 5; ++num_requests) {
92 ScopedVector<TestURLRequestContext> contexts;
93 ScopedVector<URLRequest> requests;
94 NetLog net_log;
95 std::set<URLRequestContext*> context_set;
96 for (size_t i = 0; i < num_requests; ++i) {
97 contexts.push_back(new TestURLRequestContext(true));
98 contexts[i]->set_net_log(&net_log);
99 contexts[i]->Init();
100 context_set.insert(contexts[i]);
101 requests.push_back(contexts[i]->CreateRequest(
102 GURL("about:hats"), DEFAULT_PRIORITY, &delegate, nullptr).release());
104 CapturingNetLogObserver capturing_observer;
105 CreateNetLogEntriesForActiveObjects(context_set, &capturing_observer);
106 CapturedNetLogEntry::List entry_list;
107 capturing_observer.GetEntries(&entry_list);
108 ASSERT_EQ(num_requests, entry_list.size());
110 for (size_t i = 0; i < num_requests; ++i) {
111 EXPECT_EQ(entry_list[i].source.id, requests[i]->net_log().source().id);
116 } // namespace
118 } // namespace net