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