Chromium Code Reviews| Index: content/browser/service_worker/service_worker_storage_unittest.cc |
| diff --git a/content/browser/service_worker/service_worker_storage_unittest.cc b/content/browser/service_worker/service_worker_storage_unittest.cc |
| index 36ffb5b77e0aca22c56d8c9bdc6344085f3b3440..8060c0394b47bae94e32685bc6be4c8113029002 100644 |
| --- a/content/browser/service_worker/service_worker_storage_unittest.cc |
| +++ b/content/browser/service_worker/service_worker_storage_unittest.cc |
| @@ -83,20 +83,25 @@ void OnIOComplete(int* rv_out, int rv) { |
| *rv_out = rv; |
| } |
| -void WriteBasicResponse(ServiceWorkerStorage* storage, int64 id) { |
| +void OnCompareComplete( |
| + ServiceWorkerStatusCode* status_out, int* compare_result_out, |
| + ServiceWorkerStatusCode status, int compare_result) { |
| + *status_out = status; |
| + *compare_result_out = compare_result; |
| +} |
| + |
| +void WriteResponse( |
| + ServiceWorkerStorage* storage, int64 id, |
| + const std::string& headers, |
| + IOBuffer* body, int length) { |
| scoped_ptr<ServiceWorkerResponseWriter> writer = |
| storage->CreateResponseWriter(id); |
| - const char kHttpHeaders[] = |
| - "HTTP/1.0 200 HONKYDORY\0Content-Length: 6\0\0"; |
| - const char kHttpBody[] = "Hello\0"; |
| - scoped_refptr<IOBuffer> body(new WrappedIOBuffer(kHttpBody)); |
| - std::string raw_headers(kHttpHeaders, arraysize(kHttpHeaders)); |
| scoped_ptr<net::HttpResponseInfo> info(new net::HttpResponseInfo); |
| info->request_time = base::Time::Now(); |
| info->response_time = base::Time::Now(); |
| info->was_cached = false; |
| - info->headers = new net::HttpResponseHeaders(raw_headers); |
| + info->headers = new net::HttpResponseHeaders(headers); |
| scoped_refptr<HttpResponseInfoIOBuffer> info_buffer = |
| new HttpResponseInfoIOBuffer(info.release()); |
| @@ -106,15 +111,34 @@ void WriteBasicResponse(ServiceWorkerStorage* storage, int64 id) { |
| EXPECT_LT(0, rv); |
| rv = -1234; |
| - writer->WriteData(body, arraysize(kHttpBody), |
| + writer->WriteData(body, length, |
| base::Bind(&OnIOComplete, &rv)); |
|
falken
2014/07/17 06:33:43
now this fits on one line
michaeln
2014/07/18 03:40:49
Done.
|
| base::RunLoop().RunUntilIdle(); |
| - EXPECT_EQ(static_cast<int>(arraysize(kHttpBody)), rv); |
| + EXPECT_EQ(length, rv); |
| +} |
| + |
| +void WriteStringResponse( |
| + ServiceWorkerStorage* storage, int64 id, |
| + const std::string& headers, |
| + const std::string& body) { |
| + scoped_refptr<IOBuffer> body_buffer(new WrappedIOBuffer(body.data())); |
| + WriteResponse(storage, id, headers, body_buffer, body.length()); |
| +} |
| + |
| +void WriteBasicResponse(ServiceWorkerStorage* storage, int64 id) { |
| + scoped_ptr<ServiceWorkerResponseWriter> writer = |
| + storage->CreateResponseWriter(id); |
| + |
| + const char kHttpHeaders[] = |
| + "HTTP/1.0 200 HONKYDORY\0Content-Length: 5\0\0"; |
|
falken
2014/07/17 06:33:43
this fits on one line
michaeln
2014/07/18 03:40:49
Done.
|
| + const char kHttpBody[] = "Hello"; |
| + std::string headers(kHttpHeaders, arraysize(kHttpHeaders)); |
| + WriteStringResponse(storage, id, headers, std::string(kHttpBody)); |
| } |
| bool VerifyBasicResponse(ServiceWorkerStorage* storage, int64 id, |
| bool expected_positive_result) { |
| - const char kExpectedHttpBody[] = "Hello\0"; |
| + const std::string kExpectedHttpBody("Hello"); |
| scoped_ptr<ServiceWorkerResponseReader> reader = |
| storage->CreateResponseReader(id); |
| scoped_refptr<HttpResponseInfoIOBuffer> info_buffer = |
| @@ -129,28 +153,39 @@ bool VerifyBasicResponse(ServiceWorkerStorage* storage, int64 id, |
| return false; |
| } |
| - const int kBigEnough = 512; |
| - scoped_refptr<net::IOBuffer> buffer = new IOBuffer(kBigEnough); |
| + std::string received_body; |
| { |
| + const int kBigEnough = 512; |
| + scoped_refptr<net::IOBuffer> buffer = new IOBuffer(kBigEnough); |
| TestCompletionCallback cb; |
| reader->ReadData(buffer, kBigEnough, cb.callback()); |
| int rv = cb.WaitForResult(); |
| - EXPECT_EQ(static_cast<int>(arraysize(kExpectedHttpBody)), rv); |
| + EXPECT_EQ(static_cast<int>(kExpectedHttpBody.size()), rv); |
| if (rv <= 0) |
| return false; |
| + received_body.assign(buffer->data(), rv); |
| } |
| bool status_match = |
| std::string("HONKYDORY") == |
| info_buffer->http_info->headers->GetStatusText(); |
| - bool data_match = |
| - std::string(kExpectedHttpBody) == std::string(buffer->data()); |
| + bool data_match = kExpectedHttpBody == received_body; |
| EXPECT_TRUE(status_match); |
| EXPECT_TRUE(data_match); |
| return status_match && data_match; |
| } |
| +void WriteResponseOfSize(ServiceWorkerStorage* storage, int64 id, |
| + char val, int size) { |
| + const char kHttpHeaders[] = "HTTP/1.0 200 HONKYDORY\00"; |
| + std::string headers(kHttpHeaders, arraysize(kHttpHeaders)); |
| + |
| + scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(size); |
| + memset(buffer->data(), val, size); |
| + WriteResponse(storage, id, headers, buffer, size); |
| +} |
| + |
| } // namespace |
| class ServiceWorkerStorageTest : public testing::Test { |
| @@ -932,4 +967,67 @@ TEST_F(ServiceWorkerStorageTest, FindRegistration_LongestScopeMatch) { |
| EXPECT_EQ(live_registration2, found_registration); |
| } |
| +TEST_F(ServiceWorkerStorageTest, CompareResources) { |
| + // Compare two small responses containing the same data. |
| + WriteBasicResponse(storage(), 1); |
| + WriteBasicResponse(storage(), 2); |
| + ServiceWorkerStatusCode status = static_cast<ServiceWorkerStatusCode>(-1); |
| + int compare_result = -1234; |
| + storage()->CompareScriptResources( |
| + 1, 2, |
| + base::Bind(&OnCompareComplete, &status, &compare_result)); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(SERVICE_WORKER_OK, status); |
| + EXPECT_EQ(0, compare_result); |
| + |
| + // Compare two small responses with different data. |
| + const char kHttpHeaders[] = |
| + "HTTP/1.0 200 HONKYDORY\0\0"; |
|
falken
2014/07/17 06:33:43
fits on one line
michaeln
2014/07/18 03:40:49
Done.
|
| + const char kHttpBody[] = "Goodbye"; |
| + std::string headers(kHttpHeaders, arraysize(kHttpHeaders)); |
| + WriteStringResponse(storage(), 3, headers, std::string(kHttpBody)); |
| + status = static_cast<ServiceWorkerStatusCode>(-1); |
| + compare_result = -1234; |
| + storage()->CompareScriptResources( |
| + 1, 3, |
| + base::Bind(&OnCompareComplete, &status, &compare_result)); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(SERVICE_WORKER_OK, status); |
| + EXPECT_NE(0, compare_result); |
| + |
| + // Compare two large responses with the same data. |
| + const int k32K = 32 * 1024; |
| + WriteResponseOfSize(storage(), 4, 'a', k32K); |
| + WriteResponseOfSize(storage(), 5, 'a', k32K); |
| + status = static_cast<ServiceWorkerStatusCode>(-1); |
| + compare_result = -1234; |
| + storage()->CompareScriptResources( |
| + 4, 5, |
| + base::Bind(&OnCompareComplete, &status, &compare_result)); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(SERVICE_WORKER_OK, status); |
| + EXPECT_EQ(0, compare_result); |
| + |
| + // Compare a large and small response. |
| + status = static_cast<ServiceWorkerStatusCode>(-1); |
| + compare_result = -1234; |
| + storage()->CompareScriptResources( |
| + 1, 5, |
| + base::Bind(&OnCompareComplete, &status, &compare_result)); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(SERVICE_WORKER_OK, status); |
| + EXPECT_NE(0, compare_result); |
| + |
| + // Compare two large responses with different data. |
| + WriteResponseOfSize(storage(), 6, 'b', k32K); |
| + status = static_cast<ServiceWorkerStatusCode>(-1); |
| + compare_result = -1234; |
| + storage()->CompareScriptResources( |
| + 5, 6, |
| + base::Bind(&OnCompareComplete, &status, &compare_result)); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(SERVICE_WORKER_OK, status); |
| + EXPECT_NE(0, compare_result); |
| +} |
| + |
| } // namespace content |