| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/url_request/view_cache_helper.h" | 5 #include "net/url_request/view_cache_helper.h" |
| 6 | 6 |
| 7 #include "base/pickle.h" | 7 #include "base/pickle.h" |
| 8 #include "net/base/net_errors.h" | 8 #include "net/base/net_errors.h" |
| 9 #include "net/base/test_completion_callback.h" | 9 #include "net/base/test_completion_callback.h" |
| 10 #include "net/disk_cache/disk_cache.h" | 10 #include "net/disk_cache/disk_cache.h" |
| 11 #include "net/http/http_cache.h" | 11 #include "net/http/http_cache.h" |
| 12 #include "net/url_request/url_request_context.h" | 12 #include "net/url_request/url_request_context.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace net { |
| 16 |
| 15 namespace { | 17 namespace { |
| 16 | 18 |
| 17 class TestURLRequestContext : public net::URLRequestContext { | 19 class TestURLRequestContext : public URLRequestContext { |
| 18 public: | 20 public: |
| 19 TestURLRequestContext(); | 21 TestURLRequestContext(); |
| 20 | 22 |
| 21 // Gets a pointer to the cache backend. | 23 // Gets a pointer to the cache backend. |
| 22 disk_cache::Backend* GetBackend(); | 24 disk_cache::Backend* GetBackend(); |
| 23 | 25 |
| 24 private: | 26 private: |
| 25 net::HttpCache cache_; | 27 HttpCache cache_; |
| 26 }; | 28 }; |
| 27 | 29 |
| 28 TestURLRequestContext::TestURLRequestContext() | 30 TestURLRequestContext::TestURLRequestContext() |
| 29 : cache_(reinterpret_cast<net::HttpTransactionFactory*>(NULL), NULL, | 31 : cache_(reinterpret_cast<HttpTransactionFactory*>(NULL), NULL, |
| 30 net::HttpCache::DefaultBackend::InMemory(0)) { | 32 HttpCache::DefaultBackend::InMemory(0)) { |
| 31 set_http_transaction_factory(&cache_); | 33 set_http_transaction_factory(&cache_); |
| 32 } | 34 } |
| 33 | 35 |
| 34 void WriteHeaders(disk_cache::Entry* entry, int flags, const std::string data) { | 36 void WriteHeaders(disk_cache::Entry* entry, int flags, const std::string data) { |
| 35 if (data.empty()) | 37 if (data.empty()) |
| 36 return; | 38 return; |
| 37 | 39 |
| 38 Pickle pickle; | 40 Pickle pickle; |
| 39 pickle.WriteInt(flags | 1); // Version 1. | 41 pickle.WriteInt(flags | 1); // Version 1. |
| 40 pickle.WriteInt64(0); | 42 pickle.WriteInt64(0); |
| 41 pickle.WriteInt64(0); | 43 pickle.WriteInt64(0); |
| 42 pickle.WriteString(data); | 44 pickle.WriteString(data); |
| 43 | 45 |
| 44 scoped_refptr<net::WrappedIOBuffer> buf(new net::WrappedIOBuffer( | 46 scoped_refptr<WrappedIOBuffer> buf(new WrappedIOBuffer( |
| 45 reinterpret_cast<const char*>(pickle.data()))); | 47 reinterpret_cast<const char*>(pickle.data()))); |
| 46 int len = static_cast<int>(pickle.size()); | 48 int len = static_cast<int>(pickle.size()); |
| 47 | 49 |
| 48 TestCompletionCallback cb; | 50 TestCompletionCallback cb; |
| 49 int rv = entry->WriteData(0, 0, buf, len, &cb, true); | 51 int rv = entry->WriteData(0, 0, buf, len, &cb, true); |
| 50 ASSERT_EQ(len, cb.GetResult(rv)); | 52 ASSERT_EQ(len, cb.GetResult(rv)); |
| 51 } | 53 } |
| 52 | 54 |
| 53 void WriteData(disk_cache::Entry* entry, int index, const std::string data) { | 55 void WriteData(disk_cache::Entry* entry, int index, const std::string data) { |
| 54 if (data.empty()) | 56 if (data.empty()) |
| 55 return; | 57 return; |
| 56 | 58 |
| 57 int len = data.length(); | 59 int len = data.length(); |
| 58 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(len)); | 60 scoped_refptr<IOBuffer> buf(new IOBuffer(len)); |
| 59 memcpy(buf->data(), data.data(), data.length()); | 61 memcpy(buf->data(), data.data(), data.length()); |
| 60 | 62 |
| 61 TestCompletionCallback cb; | 63 TestCompletionCallback cb; |
| 62 int rv = entry->WriteData(index, 0, buf, len, &cb, true); | 64 int rv = entry->WriteData(index, 0, buf, len, &cb, true); |
| 63 ASSERT_EQ(len, cb.GetResult(rv)); | 65 ASSERT_EQ(len, cb.GetResult(rv)); |
| 64 } | 66 } |
| 65 | 67 |
| 66 void WriteToEntry(disk_cache::Backend* cache, const std::string key, | 68 void WriteToEntry(disk_cache::Backend* cache, const std::string key, |
| 67 const std::string data0, const std::string data1, | 69 const std::string data0, const std::string data1, |
| 68 const std::string data2) { | 70 const std::string data2) { |
| 69 TestCompletionCallback cb; | 71 TestCompletionCallback cb; |
| 70 disk_cache::Entry* entry; | 72 disk_cache::Entry* entry; |
| 71 int rv = cache->CreateEntry(key, &entry, &cb); | 73 int rv = cache->CreateEntry(key, &entry, &cb); |
| 72 rv = cb.GetResult(rv); | 74 rv = cb.GetResult(rv); |
| 73 if (rv != net::OK) { | 75 if (rv != OK) { |
| 74 rv = cache->OpenEntry(key, &entry, &cb); | 76 rv = cache->OpenEntry(key, &entry, &cb); |
| 75 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 77 ASSERT_EQ(OK, cb.GetResult(rv)); |
| 76 } | 78 } |
| 77 | 79 |
| 78 WriteHeaders(entry, 0, data0); | 80 WriteHeaders(entry, 0, data0); |
| 79 WriteData(entry, 1, data1); | 81 WriteData(entry, 1, data1); |
| 80 WriteData(entry, 2, data2); | 82 WriteData(entry, 2, data2); |
| 81 | 83 |
| 82 entry->Close(); | 84 entry->Close(); |
| 83 } | 85 } |
| 84 | 86 |
| 85 void FillCache(net::URLRequestContext* context) { | 87 void FillCache(URLRequestContext* context) { |
| 86 TestCompletionCallback cb; | 88 TestCompletionCallback cb; |
| 87 disk_cache::Backend* cache; | 89 disk_cache::Backend* cache; |
| 88 int rv = | 90 int rv = |
| 89 context->http_transaction_factory()->GetCache()->GetBackend(&cache, &cb); | 91 context->http_transaction_factory()->GetCache()->GetBackend(&cache, &cb); |
| 90 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 92 ASSERT_EQ(OK, cb.GetResult(rv)); |
| 91 | 93 |
| 92 std::string empty; | 94 std::string empty; |
| 93 WriteToEntry(cache, "first", "some", empty, empty); | 95 WriteToEntry(cache, "first", "some", empty, empty); |
| 94 WriteToEntry(cache, "second", "only hex_dumped", "same", "kind"); | 96 WriteToEntry(cache, "second", "only hex_dumped", "same", "kind"); |
| 95 WriteToEntry(cache, "third", empty, "another", "thing"); | 97 WriteToEntry(cache, "third", empty, "another", "thing"); |
| 96 } | 98 } |
| 97 | 99 |
| 98 } // namespace. | 100 } // namespace. |
| 99 | 101 |
| 100 TEST(ViewCacheHelper, EmptyCache) { | 102 TEST(ViewCacheHelper, EmptyCache) { |
| 101 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); | 103 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); |
| 102 net::ViewCacheHelper helper; | 104 ViewCacheHelper helper; |
| 103 | 105 |
| 104 TestCompletionCallback cb; | 106 TestCompletionCallback cb; |
| 105 std::string prefix, data; | 107 std::string prefix, data; |
| 106 int rv = helper.GetContentsHTML(context, prefix, &data, &cb); | 108 int rv = helper.GetContentsHTML(context, prefix, &data, &cb); |
| 107 EXPECT_EQ(net::OK, cb.GetResult(rv)); | 109 EXPECT_EQ(OK, cb.GetResult(rv)); |
| 108 EXPECT_FALSE(data.empty()); | 110 EXPECT_FALSE(data.empty()); |
| 109 } | 111 } |
| 110 | 112 |
| 111 TEST(ViewCacheHelper, ListContents) { | 113 TEST(ViewCacheHelper, ListContents) { |
| 112 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); | 114 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); |
| 113 net::ViewCacheHelper helper; | 115 ViewCacheHelper helper; |
| 114 | 116 |
| 115 FillCache(context); | 117 FillCache(context); |
| 116 | 118 |
| 117 std::string prefix, data; | 119 std::string prefix, data; |
| 118 TestCompletionCallback cb; | 120 TestCompletionCallback cb; |
| 119 int rv = helper.GetContentsHTML(context, prefix, &data, &cb); | 121 int rv = helper.GetContentsHTML(context, prefix, &data, &cb); |
| 120 EXPECT_EQ(net::OK, cb.GetResult(rv)); | 122 EXPECT_EQ(OK, cb.GetResult(rv)); |
| 121 | 123 |
| 122 EXPECT_EQ(0U, data.find("<html>")); | 124 EXPECT_EQ(0U, data.find("<html>")); |
| 123 EXPECT_NE(std::string::npos, data.find("</html>")); | 125 EXPECT_NE(std::string::npos, data.find("</html>")); |
| 124 EXPECT_NE(std::string::npos, data.find("first")); | 126 EXPECT_NE(std::string::npos, data.find("first")); |
| 125 EXPECT_NE(std::string::npos, data.find("second")); | 127 EXPECT_NE(std::string::npos, data.find("second")); |
| 126 EXPECT_NE(std::string::npos, data.find("third")); | 128 EXPECT_NE(std::string::npos, data.find("third")); |
| 127 | 129 |
| 128 EXPECT_EQ(std::string::npos, data.find("some")); | 130 EXPECT_EQ(std::string::npos, data.find("some")); |
| 129 EXPECT_EQ(std::string::npos, data.find("same")); | 131 EXPECT_EQ(std::string::npos, data.find("same")); |
| 130 EXPECT_EQ(std::string::npos, data.find("thing")); | 132 EXPECT_EQ(std::string::npos, data.find("thing")); |
| 131 } | 133 } |
| 132 | 134 |
| 133 TEST(ViewCacheHelper, DumpEntry) { | 135 TEST(ViewCacheHelper, DumpEntry) { |
| 134 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); | 136 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); |
| 135 net::ViewCacheHelper helper; | 137 ViewCacheHelper helper; |
| 136 | 138 |
| 137 FillCache(context); | 139 FillCache(context); |
| 138 | 140 |
| 139 std::string data; | 141 std::string data; |
| 140 TestCompletionCallback cb; | 142 TestCompletionCallback cb; |
| 141 int rv = helper.GetEntryInfoHTML("second", context, &data, &cb); | 143 int rv = helper.GetEntryInfoHTML("second", context, &data, &cb); |
| 142 EXPECT_EQ(net::OK, cb.GetResult(rv)); | 144 EXPECT_EQ(OK, cb.GetResult(rv)); |
| 143 | 145 |
| 144 EXPECT_EQ(0U, data.find("<html>")); | 146 EXPECT_EQ(0U, data.find("<html>")); |
| 145 EXPECT_NE(std::string::npos, data.find("</html>")); | 147 EXPECT_NE(std::string::npos, data.find("</html>")); |
| 146 | 148 |
| 147 EXPECT_NE(std::string::npos, data.find("hex_dumped")); | 149 EXPECT_NE(std::string::npos, data.find("hex_dumped")); |
| 148 EXPECT_NE(std::string::npos, data.find("same")); | 150 EXPECT_NE(std::string::npos, data.find("same")); |
| 149 EXPECT_NE(std::string::npos, data.find("kind")); | 151 EXPECT_NE(std::string::npos, data.find("kind")); |
| 150 | 152 |
| 151 EXPECT_EQ(std::string::npos, data.find("first")); | 153 EXPECT_EQ(std::string::npos, data.find("first")); |
| 152 EXPECT_EQ(std::string::npos, data.find("third")); | 154 EXPECT_EQ(std::string::npos, data.find("third")); |
| 153 EXPECT_EQ(std::string::npos, data.find("some")); | 155 EXPECT_EQ(std::string::npos, data.find("some")); |
| 154 EXPECT_EQ(std::string::npos, data.find("another")); | 156 EXPECT_EQ(std::string::npos, data.find("another")); |
| 155 } | 157 } |
| 156 | 158 |
| 157 // Makes sure the links are correct. | 159 // Makes sure the links are correct. |
| 158 TEST(ViewCacheHelper, Prefix) { | 160 TEST(ViewCacheHelper, Prefix) { |
| 159 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); | 161 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); |
| 160 net::ViewCacheHelper helper; | 162 ViewCacheHelper helper; |
| 161 | 163 |
| 162 FillCache(context); | 164 FillCache(context); |
| 163 | 165 |
| 164 std::string key, data; | 166 std::string key, data; |
| 165 std::string prefix("prefix:"); | 167 std::string prefix("prefix:"); |
| 166 TestCompletionCallback cb; | 168 TestCompletionCallback cb; |
| 167 int rv = helper.GetContentsHTML(context, prefix, &data, &cb); | 169 int rv = helper.GetContentsHTML(context, prefix, &data, &cb); |
| 168 EXPECT_EQ(net::OK, cb.GetResult(rv)); | 170 EXPECT_EQ(OK, cb.GetResult(rv)); |
| 169 | 171 |
| 170 EXPECT_EQ(0U, data.find("<html>")); | 172 EXPECT_EQ(0U, data.find("<html>")); |
| 171 EXPECT_NE(std::string::npos, data.find("</html>")); | 173 EXPECT_NE(std::string::npos, data.find("</html>")); |
| 172 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:first\">")); | 174 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:first\">")); |
| 173 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:second\">")); | 175 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:second\">")); |
| 174 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:third\">")); | 176 EXPECT_NE(std::string::npos, data.find("<a href=\"prefix:third\">")); |
| 175 } | 177 } |
| 176 | 178 |
| 177 TEST(ViewCacheHelper, TruncatedFlag) { | 179 TEST(ViewCacheHelper, TruncatedFlag) { |
| 178 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); | 180 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext()); |
| 179 net::ViewCacheHelper helper; | 181 ViewCacheHelper helper; |
| 180 | 182 |
| 181 TestCompletionCallback cb; | 183 TestCompletionCallback cb; |
| 182 disk_cache::Backend* cache; | 184 disk_cache::Backend* cache; |
| 183 int rv = | 185 int rv = |
| 184 context->http_transaction_factory()->GetCache()->GetBackend(&cache, &cb); | 186 context->http_transaction_factory()->GetCache()->GetBackend(&cache, &cb); |
| 185 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 187 ASSERT_EQ(OK, cb.GetResult(rv)); |
| 186 | 188 |
| 187 std::string key("the key"); | 189 std::string key("the key"); |
| 188 disk_cache::Entry* entry; | 190 disk_cache::Entry* entry; |
| 189 rv = cache->CreateEntry(key, &entry, &cb); | 191 rv = cache->CreateEntry(key, &entry, &cb); |
| 190 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 192 ASSERT_EQ(OK, cb.GetResult(rv)); |
| 191 | 193 |
| 192 // RESPONSE_INFO_TRUNCATED defined on response_info.cc | 194 // RESPONSE_INFO_TRUNCATED defined on response_info.cc |
| 193 int flags = 1 << 12; | 195 int flags = 1 << 12; |
| 194 WriteHeaders(entry, flags, "something"); | 196 WriteHeaders(entry, flags, "something"); |
| 195 entry->Close(); | 197 entry->Close(); |
| 196 | 198 |
| 197 std::string data; | 199 std::string data; |
| 198 rv = helper.GetEntryInfoHTML(key, context, &data, &cb); | 200 rv = helper.GetEntryInfoHTML(key, context, &data, &cb); |
| 199 EXPECT_EQ(net::OK, cb.GetResult(rv)); | 201 EXPECT_EQ(OK, cb.GetResult(rv)); |
| 200 | 202 |
| 201 EXPECT_NE(std::string::npos, data.find("RESPONSE_INFO_TRUNCATED")); | 203 EXPECT_NE(std::string::npos, data.find("RESPONSE_INFO_TRUNCATED")); |
| 202 } | 204 } |
| 205 |
| 206 } // namespace net |
| OLD | NEW |