OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/log/net_log_util.h" | 5 #include "net/log/net_log_util.h" |
6 | 6 |
| 7 #include <memory> |
7 #include <set> | 8 #include <set> |
8 #include <vector> | 9 #include <vector> |
9 | 10 |
10 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/ptr_util.h" |
12 #include "base/values.h" | 13 #include "base/values.h" |
13 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
14 #include "net/base/test_completion_callback.h" | 15 #include "net/base/test_completion_callback.h" |
15 #include "net/http/http_cache.h" | 16 #include "net/http/http_cache.h" |
16 #include "net/http/http_transaction.h" | 17 #include "net/http/http_transaction.h" |
17 #include "net/log/test_net_log.h" | 18 #include "net/log/test_net_log.h" |
18 #include "net/log/test_net_log_entry.h" | 19 #include "net/log/test_net_log_entry.h" |
19 #include "net/url_request/url_request_test_util.h" | 20 #include "net/url_request/url_request_test_util.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 | 22 |
22 namespace net { | 23 namespace net { |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 // Make sure GetNetConstants doesn't crash. | 27 // Make sure GetNetConstants doesn't crash. |
27 TEST(NetLogUtil, GetNetConstants) { | 28 TEST(NetLogUtil, GetNetConstants) { |
28 scoped_ptr<base::Value> constants(GetNetConstants()); | 29 std::unique_ptr<base::Value> constants(GetNetConstants()); |
29 } | 30 } |
30 | 31 |
31 // Make sure GetNetInfo doesn't crash when called on contexts with and without | 32 // Make sure GetNetInfo doesn't crash when called on contexts with and without |
32 // caches, and they have the same number of elements. | 33 // caches, and they have the same number of elements. |
33 TEST(NetLogUtil, GetNetInfo) { | 34 TEST(NetLogUtil, GetNetInfo) { |
34 TestURLRequestContext context; | 35 TestURLRequestContext context; |
35 HttpCache* http_cache = context.http_transaction_factory()->GetCache(); | 36 HttpCache* http_cache = context.http_transaction_factory()->GetCache(); |
36 | 37 |
37 // Get NetInfo when there's no cache backend (It's only created on first use). | 38 // Get NetInfo when there's no cache backend (It's only created on first use). |
38 EXPECT_FALSE(http_cache->GetCurrentBackend()); | 39 EXPECT_FALSE(http_cache->GetCurrentBackend()); |
39 scoped_ptr<base::DictionaryValue> net_info_without_cache( | 40 std::unique_ptr<base::DictionaryValue> net_info_without_cache( |
40 GetNetInfo(&context, NET_INFO_ALL_SOURCES)); | 41 GetNetInfo(&context, NET_INFO_ALL_SOURCES)); |
41 EXPECT_FALSE(http_cache->GetCurrentBackend()); | 42 EXPECT_FALSE(http_cache->GetCurrentBackend()); |
42 EXPECT_GT(net_info_without_cache->size(), 0u); | 43 EXPECT_GT(net_info_without_cache->size(), 0u); |
43 | 44 |
44 // Fore creation of a cache backend, and get NetInfo again. | 45 // Fore creation of a cache backend, and get NetInfo again. |
45 disk_cache::Backend* backend = NULL; | 46 disk_cache::Backend* backend = NULL; |
46 EXPECT_EQ(OK, context.http_transaction_factory()->GetCache()->GetBackend( | 47 EXPECT_EQ(OK, context.http_transaction_factory()->GetCache()->GetBackend( |
47 &backend, TestCompletionCallback().callback())); | 48 &backend, TestCompletionCallback().callback())); |
48 EXPECT_TRUE(http_cache->GetCurrentBackend()); | 49 EXPECT_TRUE(http_cache->GetCurrentBackend()); |
49 scoped_ptr<base::DictionaryValue> net_info_with_cache( | 50 std::unique_ptr<base::DictionaryValue> net_info_with_cache( |
50 GetNetInfo(&context, NET_INFO_ALL_SOURCES)); | 51 GetNetInfo(&context, NET_INFO_ALL_SOURCES)); |
51 EXPECT_GT(net_info_with_cache->size(), 0u); | 52 EXPECT_GT(net_info_with_cache->size(), 0u); |
52 | 53 |
53 EXPECT_EQ(net_info_without_cache->size(), net_info_with_cache->size()); | 54 EXPECT_EQ(net_info_without_cache->size(), net_info_with_cache->size()); |
54 } | 55 } |
55 | 56 |
56 // Make sure CreateNetLogEntriesForActiveObjects works for requests from a | 57 // Make sure CreateNetLogEntriesForActiveObjects works for requests from a |
57 // single URLRequestContext. | 58 // single URLRequestContext. |
58 TEST(NetLogUtil, CreateNetLogEntriesForActiveObjectsOneContext) { | 59 TEST(NetLogUtil, CreateNetLogEntriesForActiveObjectsOneContext) { |
59 // Using same context for each iteration makes sure deleted requests don't | 60 // Using same context for each iteration makes sure deleted requests don't |
60 // appear in the list, or result in crashes. | 61 // appear in the list, or result in crashes. |
61 TestURLRequestContext context(true); | 62 TestURLRequestContext context(true); |
62 NetLog net_log; | 63 NetLog net_log; |
63 context.set_net_log(&net_log); | 64 context.set_net_log(&net_log); |
64 context.Init(); | 65 context.Init(); |
65 TestDelegate delegate; | 66 TestDelegate delegate; |
66 for (size_t num_requests = 0; num_requests < 5; ++num_requests) { | 67 for (size_t num_requests = 0; num_requests < 5; ++num_requests) { |
67 std::vector<scoped_ptr<URLRequest>> requests; | 68 std::vector<std::unique_ptr<URLRequest>> requests; |
68 for (size_t i = 0; i < num_requests; ++i) { | 69 for (size_t i = 0; i < num_requests; ++i) { |
69 requests.push_back(context.CreateRequest(GURL("about:life"), | 70 requests.push_back(context.CreateRequest(GURL("about:life"), |
70 DEFAULT_PRIORITY, &delegate)); | 71 DEFAULT_PRIORITY, &delegate)); |
71 } | 72 } |
72 std::set<URLRequestContext*> contexts; | 73 std::set<URLRequestContext*> contexts; |
73 contexts.insert(&context); | 74 contexts.insert(&context); |
74 TestNetLog test_net_log; | 75 TestNetLog test_net_log; |
75 CreateNetLogEntriesForActiveObjects(contexts, test_net_log.GetObserver()); | 76 CreateNetLogEntriesForActiveObjects(contexts, test_net_log.GetObserver()); |
76 TestNetLogEntry::List entry_list; | 77 TestNetLogEntry::List entry_list; |
77 test_net_log.GetEntries(&entry_list); | 78 test_net_log.GetEntries(&entry_list); |
78 ASSERT_EQ(num_requests, entry_list.size()); | 79 ASSERT_EQ(num_requests, entry_list.size()); |
79 | 80 |
80 for (size_t i = 0; i < num_requests; ++i) { | 81 for (size_t i = 0; i < num_requests; ++i) { |
81 EXPECT_EQ(entry_list[i].source.id, requests[i]->net_log().source().id); | 82 EXPECT_EQ(entry_list[i].source.id, requests[i]->net_log().source().id); |
82 } | 83 } |
83 } | 84 } |
84 } | 85 } |
85 | 86 |
86 // Make sure CreateNetLogEntriesForActiveObjects works with multiple | 87 // Make sure CreateNetLogEntriesForActiveObjects works with multiple |
87 // URLRequestContexts. | 88 // URLRequestContexts. |
88 TEST(NetLogUtil, CreateNetLogEntriesForActiveObjectsMultipleContexts) { | 89 TEST(NetLogUtil, CreateNetLogEntriesForActiveObjectsMultipleContexts) { |
89 TestDelegate delegate; | 90 TestDelegate delegate; |
90 for (size_t num_requests = 0; num_requests < 5; ++num_requests) { | 91 for (size_t num_requests = 0; num_requests < 5; ++num_requests) { |
91 NetLog net_log; | 92 NetLog net_log; |
92 std::vector<scoped_ptr<TestURLRequestContext>> contexts; | 93 std::vector<std::unique_ptr<TestURLRequestContext>> contexts; |
93 std::vector<scoped_ptr<URLRequest>> requests; | 94 std::vector<std::unique_ptr<URLRequest>> requests; |
94 std::set<URLRequestContext*> context_set; | 95 std::set<URLRequestContext*> context_set; |
95 for (size_t i = 0; i < num_requests; ++i) { | 96 for (size_t i = 0; i < num_requests; ++i) { |
96 contexts.push_back(make_scoped_ptr(new TestURLRequestContext(true))); | 97 contexts.push_back(base::WrapUnique(new TestURLRequestContext(true))); |
97 contexts[i]->set_net_log(&net_log); | 98 contexts[i]->set_net_log(&net_log); |
98 contexts[i]->Init(); | 99 contexts[i]->Init(); |
99 context_set.insert(contexts[i].get()); | 100 context_set.insert(contexts[i].get()); |
100 requests.push_back(contexts[i]->CreateRequest( | 101 requests.push_back(contexts[i]->CreateRequest( |
101 GURL("about:hats"), DEFAULT_PRIORITY, &delegate)); | 102 GURL("about:hats"), DEFAULT_PRIORITY, &delegate)); |
102 } | 103 } |
103 TestNetLog test_net_log; | 104 TestNetLog test_net_log; |
104 CreateNetLogEntriesForActiveObjects(context_set, | 105 CreateNetLogEntriesForActiveObjects(context_set, |
105 test_net_log.GetObserver()); | 106 test_net_log.GetObserver()); |
106 TestNetLogEntry::List entry_list; | 107 TestNetLogEntry::List entry_list; |
107 test_net_log.GetEntries(&entry_list); | 108 test_net_log.GetEntries(&entry_list); |
108 ASSERT_EQ(num_requests, entry_list.size()); | 109 ASSERT_EQ(num_requests, entry_list.size()); |
109 | 110 |
110 for (size_t i = 0; i < num_requests; ++i) { | 111 for (size_t i = 0; i < num_requests; ++i) { |
111 EXPECT_EQ(entry_list[i].source.id, requests[i]->net_log().source().id); | 112 EXPECT_EQ(entry_list[i].source.id, requests[i]->net_log().source().id); |
112 } | 113 } |
113 } | 114 } |
114 } | 115 } |
115 | 116 |
116 } // namespace | 117 } // namespace |
117 | 118 |
118 } // namespace net | 119 } // namespace net |
OLD | NEW |