Revert 97987 - Switching NaCl IRT to be built inside the chrome build.
[chromium-blink-merge.git] / net / url_request / view_cache_helper_unittest.cc
blobbe9b30d0f62db0e0bb748570cfccec36d0588a10
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.
5 #include "net/url_request/view_cache_helper.h"
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"
15 namespace net {
17 namespace {
19 class TestURLRequestContext : public URLRequestContext {
20 public:
21 TestURLRequestContext();
23 // Gets a pointer to the cache backend.
24 disk_cache::Backend* GetBackend();
26 private:
27 HttpCache cache_;
30 TestURLRequestContext::TestURLRequestContext()
31 : cache_(reinterpret_cast<HttpTransactionFactory*>(NULL), NULL,
32 HttpCache::DefaultBackend::InMemory(0)) {
33 set_http_transaction_factory(&cache_);
36 void WriteHeaders(disk_cache::Entry* entry, int flags, const std::string data) {
37 if (data.empty())
38 return;
40 Pickle pickle;
41 pickle.WriteInt(flags | 1); // Version 1.
42 pickle.WriteInt64(0);
43 pickle.WriteInt64(0);
44 pickle.WriteString(data);
46 scoped_refptr<WrappedIOBuffer> buf(new WrappedIOBuffer(
47 reinterpret_cast<const char*>(pickle.data())));
48 int len = static_cast<int>(pickle.size());
50 TestCompletionCallback cb;
51 int rv = entry->WriteData(0, 0, buf, len, &cb, true);
52 ASSERT_EQ(len, cb.GetResult(rv));
55 void WriteData(disk_cache::Entry* entry, int index, const std::string data) {
56 if (data.empty())
57 return;
59 int len = data.length();
60 scoped_refptr<IOBuffer> buf(new IOBuffer(len));
61 memcpy(buf->data(), data.data(), data.length());
63 TestCompletionCallback cb;
64 int rv = entry->WriteData(index, 0, buf, len, &cb, true);
65 ASSERT_EQ(len, cb.GetResult(rv));
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));
80 WriteHeaders(entry, 0, data0);
81 WriteData(entry, 1, data1);
82 WriteData(entry, 2, data2);
84 entry->Close();
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));
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");
100 } // namespace.
102 TEST(ViewCacheHelper, EmptyCache) {
103 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext());
104 ViewCacheHelper helper;
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());
113 TEST(ViewCacheHelper, ListContents) {
114 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext());
115 ViewCacheHelper helper;
117 FillCache(context);
119 std::string prefix, data;
120 TestCompletionCallback cb;
121 int rv = helper.GetContentsHTML(context, prefix, &data, &cb);
122 EXPECT_EQ(OK, cb.GetResult(rv));
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"));
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"));
135 TEST(ViewCacheHelper, DumpEntry) {
136 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext());
137 ViewCacheHelper helper;
139 FillCache(context);
141 std::string data;
142 TestCompletionCallback cb;
143 int rv = helper.GetEntryInfoHTML("second", context, &data, &cb);
144 EXPECT_EQ(OK, cb.GetResult(rv));
146 EXPECT_EQ(0U, data.find("<html>"));
147 EXPECT_NE(std::string::npos, data.find("</html>"));
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"));
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"));
159 // Makes sure the links are correct.
160 TEST(ViewCacheHelper, Prefix) {
161 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext());
162 ViewCacheHelper helper;
164 FillCache(context);
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));
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\">"));
179 TEST(ViewCacheHelper, TruncatedFlag) {
180 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext());
181 ViewCacheHelper helper;
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));
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));
194 // RESPONSE_INFO_TRUNCATED defined on response_info.cc
195 int flags = 1 << 12;
196 WriteHeaders(entry, flags, "something");
197 entry->Close();
199 std::string data;
200 rv = helper.GetEntryInfoHTML(key, context, &data, &cb);
201 EXPECT_EQ(OK, cb.GetResult(rv));
203 EXPECT_NE(std::string::npos, data.find("RESPONSE_INFO_TRUNCATED"));
206 } // namespace net