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/log/net_log_util.h"
9 #include "base/files/file_path.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/values.h"
12 #include "net/base/net_errors.h"
13 #include "net/base/test_completion_callback.h"
14 #include "net/http/http_cache.h"
15 #include "net/http/http_transaction.h"
16 #include "net/log/capturing_net_log_observer.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"
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(OK
, context
.http_transaction_factory()->GetCache()->GetBackend(
46 &backend
, TestCompletionCallback().callback()));
47 EXPECT_TRUE(http_cache
->GetCurrentBackend());
48 scoped_ptr
<base::DictionaryValue
> net_info_with_cache(
49 GetNetInfo(&context
, NET_INFO_ALL_SOURCES
));
50 EXPECT_GT(net_info_with_cache
->size(), 0u);
52 EXPECT_EQ(net_info_without_cache
->size(), net_info_with_cache
->size());
55 // Make sure CreateNetLogEntriesForActiveObjects works for requests from a
56 // single URLRequestContext.
57 TEST(NetLogUtil
, CreateNetLogEntriesForActiveObjectsOneContext
) {
58 // Using same context for each iteration makes sure deleted requests don't
59 // appear in the list, or result in crashes.
60 TestURLRequestContext
context(true);
62 context
.set_net_log(&net_log
);
64 TestDelegate delegate
;
65 for (size_t num_requests
= 0; num_requests
< 5; ++num_requests
) {
66 ScopedVector
<URLRequest
> requests
;
67 for (size_t i
= 0; i
< num_requests
; ++i
) {
68 requests
.push_back(context
.CreateRequest(GURL("about:life"),
70 &delegate
).release());
72 std::set
<URLRequestContext
*> contexts
;
73 contexts
.insert(&context
);
74 CapturingNetLogObserver capturing_observer
;
75 CreateNetLogEntriesForActiveObjects(contexts
, &capturing_observer
);
76 CapturedNetLogEntry::List entry_list
;
77 capturing_observer
.GetEntries(&entry_list
);
78 ASSERT_EQ(num_requests
, entry_list
.size());
80 for (size_t i
= 0; i
< num_requests
; ++i
) {
81 EXPECT_EQ(entry_list
[i
].source
.id
, requests
[i
]->net_log().source().id
);
86 // Make sure CreateNetLogEntriesForActiveObjects works with multiple
87 // URLRequestContexts.
88 TEST(NetLogUtil
, CreateNetLogEntriesForActiveObjectsMultipleContexts
) {
89 TestDelegate delegate
;
90 for (size_t num_requests
= 0; num_requests
< 5; ++num_requests
) {
91 ScopedVector
<TestURLRequestContext
> contexts
;
92 ScopedVector
<URLRequest
> requests
;
94 std::set
<URLRequestContext
*> context_set
;
95 for (size_t i
= 0; i
< num_requests
; ++i
) {
96 contexts
.push_back(new TestURLRequestContext(true));
97 contexts
[i
]->set_net_log(&net_log
);
99 context_set
.insert(contexts
[i
]);
102 ->CreateRequest(GURL("about:hats"), DEFAULT_PRIORITY
, &delegate
)
105 CapturingNetLogObserver capturing_observer
;
106 CreateNetLogEntriesForActiveObjects(context_set
, &capturing_observer
);
107 CapturedNetLogEntry::List entry_list
;
108 capturing_observer
.GetEntries(&entry_list
);
109 ASSERT_EQ(num_requests
, entry_list
.size());
111 for (size_t i
= 0; i
< num_requests
; ++i
) {
112 EXPECT_EQ(entry_list
[i
].source
.id
, requests
[i
]->net_log().source().id
);