OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 ServiceWorkerStorage::GetAllRegistrationInfosCallback MakeGetAllCallback( | 76 ServiceWorkerStorage::GetAllRegistrationInfosCallback MakeGetAllCallback( |
77 bool* was_called, | 77 bool* was_called, |
78 std::vector<ServiceWorkerRegistrationInfo>* all) { | 78 std::vector<ServiceWorkerRegistrationInfo>* all) { |
79 return base::Bind(&GetAllCallback, was_called, all); | 79 return base::Bind(&GetAllCallback, was_called, all); |
80 } | 80 } |
81 | 81 |
82 void OnIOComplete(int* rv_out, int rv) { | 82 void OnIOComplete(int* rv_out, int rv) { |
83 *rv_out = rv; | 83 *rv_out = rv; |
84 } | 84 } |
85 | 85 |
86 void WriteBasicResponse(ServiceWorkerStorage* storage, int64 id) { | 86 void OnCompareComplete( |
| 87 ServiceWorkerStatusCode* status_out, int* compare_result_out, |
| 88 ServiceWorkerStatusCode status, int compare_result) { |
| 89 *status_out = status; |
| 90 *compare_result_out = compare_result; |
| 91 } |
| 92 |
| 93 void WriteResponse( |
| 94 ServiceWorkerStorage* storage, int64 id, |
| 95 const std::string& headers, |
| 96 IOBuffer* body, int length) { |
87 scoped_ptr<ServiceWorkerResponseWriter> writer = | 97 scoped_ptr<ServiceWorkerResponseWriter> writer = |
88 storage->CreateResponseWriter(id); | 98 storage->CreateResponseWriter(id); |
89 | 99 |
90 const char kHttpHeaders[] = | |
91 "HTTP/1.0 200 HONKYDORY\0Content-Length: 6\0\0"; | |
92 const char kHttpBody[] = "Hello\0"; | |
93 scoped_refptr<IOBuffer> body(new WrappedIOBuffer(kHttpBody)); | |
94 std::string raw_headers(kHttpHeaders, arraysize(kHttpHeaders)); | |
95 scoped_ptr<net::HttpResponseInfo> info(new net::HttpResponseInfo); | 100 scoped_ptr<net::HttpResponseInfo> info(new net::HttpResponseInfo); |
96 info->request_time = base::Time::Now(); | 101 info->request_time = base::Time::Now(); |
97 info->response_time = base::Time::Now(); | 102 info->response_time = base::Time::Now(); |
98 info->was_cached = false; | 103 info->was_cached = false; |
99 info->headers = new net::HttpResponseHeaders(raw_headers); | 104 info->headers = new net::HttpResponseHeaders(headers); |
100 scoped_refptr<HttpResponseInfoIOBuffer> info_buffer = | 105 scoped_refptr<HttpResponseInfoIOBuffer> info_buffer = |
101 new HttpResponseInfoIOBuffer(info.release()); | 106 new HttpResponseInfoIOBuffer(info.release()); |
102 | 107 |
103 int rv = -1234; | 108 int rv = -1234; |
104 writer->WriteInfo(info_buffer, base::Bind(&OnIOComplete, &rv)); | 109 writer->WriteInfo(info_buffer, base::Bind(&OnIOComplete, &rv)); |
105 base::RunLoop().RunUntilIdle(); | 110 base::RunLoop().RunUntilIdle(); |
106 EXPECT_LT(0, rv); | 111 EXPECT_LT(0, rv); |
107 | 112 |
108 rv = -1234; | 113 rv = -1234; |
109 writer->WriteData(body, arraysize(kHttpBody), | 114 writer->WriteData(body, length, |
110 base::Bind(&OnIOComplete, &rv)); | 115 base::Bind(&OnIOComplete, &rv)); |
111 base::RunLoop().RunUntilIdle(); | 116 base::RunLoop().RunUntilIdle(); |
112 EXPECT_EQ(static_cast<int>(arraysize(kHttpBody)), rv); | 117 EXPECT_EQ(length, rv); |
| 118 } |
| 119 |
| 120 void WriteStringResponse( |
| 121 ServiceWorkerStorage* storage, int64 id, |
| 122 const std::string& headers, |
| 123 const std::string& body) { |
| 124 scoped_refptr<IOBuffer> body_buffer(new WrappedIOBuffer(body.data())); |
| 125 WriteResponse(storage, id, headers, body_buffer, body.length()); |
| 126 } |
| 127 |
| 128 void WriteBasicResponse(ServiceWorkerStorage* storage, int64 id) { |
| 129 scoped_ptr<ServiceWorkerResponseWriter> writer = |
| 130 storage->CreateResponseWriter(id); |
| 131 |
| 132 const char kHttpHeaders[] = |
| 133 "HTTP/1.0 200 HONKYDORY\0Content-Length: 5\0\0"; |
| 134 const char kHttpBody[] = "Hello"; |
| 135 std::string headers(kHttpHeaders, arraysize(kHttpHeaders)); |
| 136 WriteStringResponse(storage, id, headers, std::string(kHttpBody)); |
113 } | 137 } |
114 | 138 |
115 bool VerifyBasicResponse(ServiceWorkerStorage* storage, int64 id, | 139 bool VerifyBasicResponse(ServiceWorkerStorage* storage, int64 id, |
116 bool expected_positive_result) { | 140 bool expected_positive_result) { |
117 const char kExpectedHttpBody[] = "Hello\0"; | 141 const std::string kExpectedHttpBody("Hello"); |
118 scoped_ptr<ServiceWorkerResponseReader> reader = | 142 scoped_ptr<ServiceWorkerResponseReader> reader = |
119 storage->CreateResponseReader(id); | 143 storage->CreateResponseReader(id); |
120 scoped_refptr<HttpResponseInfoIOBuffer> info_buffer = | 144 scoped_refptr<HttpResponseInfoIOBuffer> info_buffer = |
121 new HttpResponseInfoIOBuffer(); | 145 new HttpResponseInfoIOBuffer(); |
122 { | 146 { |
123 TestCompletionCallback cb; | 147 TestCompletionCallback cb; |
124 reader->ReadInfo(info_buffer, cb.callback()); | 148 reader->ReadInfo(info_buffer, cb.callback()); |
125 int rv = cb.WaitForResult(); | 149 int rv = cb.WaitForResult(); |
126 if (expected_positive_result) | 150 if (expected_positive_result) |
127 EXPECT_LT(0, rv); | 151 EXPECT_LT(0, rv); |
128 if (rv <= 0) | 152 if (rv <= 0) |
129 return false; | 153 return false; |
130 } | 154 } |
131 | 155 |
132 const int kBigEnough = 512; | 156 std::string received_body; |
133 scoped_refptr<net::IOBuffer> buffer = new IOBuffer(kBigEnough); | |
134 { | 157 { |
| 158 const int kBigEnough = 512; |
| 159 scoped_refptr<net::IOBuffer> buffer = new IOBuffer(kBigEnough); |
135 TestCompletionCallback cb; | 160 TestCompletionCallback cb; |
136 reader->ReadData(buffer, kBigEnough, cb.callback()); | 161 reader->ReadData(buffer, kBigEnough, cb.callback()); |
137 int rv = cb.WaitForResult(); | 162 int rv = cb.WaitForResult(); |
138 EXPECT_EQ(static_cast<int>(arraysize(kExpectedHttpBody)), rv); | 163 EXPECT_EQ(static_cast<int>(kExpectedHttpBody.size()), rv); |
139 if (rv <= 0) | 164 if (rv <= 0) |
140 return false; | 165 return false; |
| 166 received_body.assign(buffer->data(), rv); |
141 } | 167 } |
142 | 168 |
143 bool status_match = | 169 bool status_match = |
144 std::string("HONKYDORY") == | 170 std::string("HONKYDORY") == |
145 info_buffer->http_info->headers->GetStatusText(); | 171 info_buffer->http_info->headers->GetStatusText(); |
146 bool data_match = | 172 bool data_match = kExpectedHttpBody == received_body; |
147 std::string(kExpectedHttpBody) == std::string(buffer->data()); | |
148 | 173 |
149 EXPECT_TRUE(status_match); | 174 EXPECT_TRUE(status_match); |
150 EXPECT_TRUE(data_match); | 175 EXPECT_TRUE(data_match); |
151 return status_match && data_match; | 176 return status_match && data_match; |
152 } | 177 } |
153 | 178 |
| 179 void WriteResponseOfSize(ServiceWorkerStorage* storage, int64 id, |
| 180 char val, int size) { |
| 181 const char kHttpHeaders[] = "HTTP/1.0 200 HONKYDORY\00"; |
| 182 std::string headers(kHttpHeaders, arraysize(kHttpHeaders)); |
| 183 |
| 184 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(size); |
| 185 memset(buffer->data(), val, size); |
| 186 WriteResponse(storage, id, headers, buffer, size); |
| 187 } |
| 188 |
154 } // namespace | 189 } // namespace |
155 | 190 |
156 class ServiceWorkerStorageTest : public testing::Test { | 191 class ServiceWorkerStorageTest : public testing::Test { |
157 public: | 192 public: |
158 ServiceWorkerStorageTest() | 193 ServiceWorkerStorageTest() |
159 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) { | 194 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) { |
160 } | 195 } |
161 | 196 |
162 virtual void SetUp() OVERRIDE { | 197 virtual void SetUp() OVERRIDE { |
163 context_.reset( | 198 context_.reset( |
(...skipping 761 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
925 live_registration2, NULL, SERVICE_WORKER_OK); | 960 live_registration2, NULL, SERVICE_WORKER_OK); |
926 storage()->NotifyDoneInstallingRegistration( | 961 storage()->NotifyDoneInstallingRegistration( |
927 live_registration3, NULL, SERVICE_WORKER_OK); | 962 live_registration3, NULL, SERVICE_WORKER_OK); |
928 | 963 |
929 // Find a registration among installed ones. | 964 // Find a registration among installed ones. |
930 EXPECT_EQ(SERVICE_WORKER_OK, | 965 EXPECT_EQ(SERVICE_WORKER_OK, |
931 FindRegistrationForDocument(kDocumentUrl, &found_registration)); | 966 FindRegistrationForDocument(kDocumentUrl, &found_registration)); |
932 EXPECT_EQ(live_registration2, found_registration); | 967 EXPECT_EQ(live_registration2, found_registration); |
933 } | 968 } |
934 | 969 |
| 970 TEST_F(ServiceWorkerStorageTest, CompareResources) { |
| 971 // Compare two small responses containing the same data. |
| 972 WriteBasicResponse(storage(), 1); |
| 973 WriteBasicResponse(storage(), 2); |
| 974 ServiceWorkerStatusCode status = static_cast<ServiceWorkerStatusCode>(-1); |
| 975 int compare_result = -1234; |
| 976 storage()->CompareScriptResources( |
| 977 1, 2, |
| 978 base::Bind(&OnCompareComplete, &status, &compare_result)); |
| 979 base::RunLoop().RunUntilIdle(); |
| 980 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 981 EXPECT_EQ(0, compare_result); |
| 982 |
| 983 // Compare two small responses with different data. |
| 984 const char kHttpHeaders[] = |
| 985 "HTTP/1.0 200 HONKYDORY\0\0"; |
| 986 const char kHttpBody[] = "Goodbye"; |
| 987 std::string headers(kHttpHeaders, arraysize(kHttpHeaders)); |
| 988 WriteStringResponse(storage(), 3, headers, std::string(kHttpBody)); |
| 989 status = static_cast<ServiceWorkerStatusCode>(-1); |
| 990 compare_result = -1234; |
| 991 storage()->CompareScriptResources( |
| 992 1, 3, |
| 993 base::Bind(&OnCompareComplete, &status, &compare_result)); |
| 994 base::RunLoop().RunUntilIdle(); |
| 995 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 996 EXPECT_NE(0, compare_result); |
| 997 |
| 998 // Compare two large responses with the same data. |
| 999 const int k32K = 32 * 1024; |
| 1000 WriteResponseOfSize(storage(), 4, 'a', k32K); |
| 1001 WriteResponseOfSize(storage(), 5, 'a', k32K); |
| 1002 status = static_cast<ServiceWorkerStatusCode>(-1); |
| 1003 compare_result = -1234; |
| 1004 storage()->CompareScriptResources( |
| 1005 4, 5, |
| 1006 base::Bind(&OnCompareComplete, &status, &compare_result)); |
| 1007 base::RunLoop().RunUntilIdle(); |
| 1008 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 1009 EXPECT_EQ(0, compare_result); |
| 1010 |
| 1011 // Compare a large and small response. |
| 1012 status = static_cast<ServiceWorkerStatusCode>(-1); |
| 1013 compare_result = -1234; |
| 1014 storage()->CompareScriptResources( |
| 1015 1, 5, |
| 1016 base::Bind(&OnCompareComplete, &status, &compare_result)); |
| 1017 base::RunLoop().RunUntilIdle(); |
| 1018 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 1019 EXPECT_NE(0, compare_result); |
| 1020 |
| 1021 // Compare two large responses with different data. |
| 1022 WriteResponseOfSize(storage(), 6, 'b', k32K); |
| 1023 status = static_cast<ServiceWorkerStatusCode>(-1); |
| 1024 compare_result = -1234; |
| 1025 storage()->CompareScriptResources( |
| 1026 5, 6, |
| 1027 base::Bind(&OnCompareComplete, &status, &compare_result)); |
| 1028 base::RunLoop().RunUntilIdle(); |
| 1029 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 1030 EXPECT_NE(0, compare_result); |
| 1031 } |
| 1032 |
935 } // namespace content | 1033 } // namespace content |
OLD | NEW |