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/test_net_log.h"
17 #include "net/log/test_net_log_entry.h"
18 #include "net/test/spawned_test_server/spawned_test_server.h"
19 #include "net/url_request/url_request_test_util.h"
20 #include "testing/gtest/include/gtest/gtest.h"
26 // Make sure GetNetConstants doesn't crash.
27 TEST(NetLogUtil
, GetNetConstants
) {
28 scoped_ptr
<base::Value
> constants(GetNetConstants());
31 // Make sure GetNetInfo doesn't crash when called on contexts with and without
32 // caches, and they have the same number of elements.
33 TEST(NetLogUtil
, GetNetInfo
) {
34 TestURLRequestContext context
;
35 HttpCache
* http_cache
= context
.http_transaction_factory()->GetCache();
37 // Get NetInfo when there's no cache backend (It's only created on first use).
38 EXPECT_FALSE(http_cache
->GetCurrentBackend());
39 scoped_ptr
<base::DictionaryValue
> net_info_without_cache(
40 GetNetInfo(&context
, NET_INFO_ALL_SOURCES
));
41 EXPECT_FALSE(http_cache
->GetCurrentBackend());
42 EXPECT_GT(net_info_without_cache
->size(), 0u);
44 // Fore creation of a cache backend, and get NetInfo again.
45 disk_cache::Backend
* backend
= NULL
;
46 EXPECT_EQ(OK
, context
.http_transaction_factory()->GetCache()->GetBackend(
47 &backend
, TestCompletionCallback().callback()));
48 EXPECT_TRUE(http_cache
->GetCurrentBackend());
49 scoped_ptr
<base::DictionaryValue
> net_info_with_cache(
50 GetNetInfo(&context
, NET_INFO_ALL_SOURCES
));
51 EXPECT_GT(net_info_with_cache
->size(), 0u);
53 EXPECT_EQ(net_info_without_cache
->size(), net_info_with_cache
->size());
56 // Make sure CreateNetLogEntriesForActiveObjects works for requests from a
57 // single URLRequestContext.
58 TEST(NetLogUtil
, CreateNetLogEntriesForActiveObjectsOneContext
) {
59 // Using same context for each iteration makes sure deleted requests don't
60 // appear in the list, or result in crashes.
61 TestURLRequestContext
context(true);
63 context
.set_net_log(&net_log
);
65 TestDelegate delegate
;
66 for (size_t num_requests
= 0; num_requests
< 5; ++num_requests
) {
67 ScopedVector
<URLRequest
> requests
;
68 for (size_t i
= 0; i
< num_requests
; ++i
) {
69 requests
.push_back(context
.CreateRequest(GURL("about:life"),
71 &delegate
).release());
73 std::set
<URLRequestContext
*> contexts
;
74 contexts
.insert(&context
);
75 TestNetLog test_net_log
;
76 CreateNetLogEntriesForActiveObjects(contexts
, test_net_log
.GetObserver());
77 TestNetLogEntry::List entry_list
;
78 test_net_log
.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
) {
93 ScopedVector
<TestURLRequestContext
> contexts
;
94 ScopedVector
<URLRequest
> requests
;
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
);
100 context_set
.insert(contexts
[i
]);
103 ->CreateRequest(GURL("about:hats"), DEFAULT_PRIORITY
, &delegate
)
106 TestNetLog test_net_log
;
107 CreateNetLogEntriesForActiveObjects(context_set
,
108 test_net_log
.GetObserver());
109 TestNetLogEntry::List entry_list
;
110 test_net_log
.GetEntries(&entry_list
);
111 ASSERT_EQ(num_requests
, entry_list
.size());
113 for (size_t i
= 0; i
< num_requests
; ++i
) {
114 EXPECT_EQ(entry_list
[i
].source
.id
, requests
[i
]->net_log().source().id
);