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