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