| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "core/fetch/Resource.h" | 5 #include "core/fetch/Resource.h" |
| 6 | 6 |
| 7 #include "core/fetch/MemoryCache.h" | 7 #include "core/fetch/MemoryCache.h" |
| 8 #include "platform/SharedBuffer.h" | 8 #include "platform/SharedBuffer.h" |
| 9 #include "platform/network/ResourceRequest.h" | 9 #include "platform/network/ResourceRequest.h" |
| 10 #include "platform/network/ResourceResponse.h" | 10 #include "platform/network/ResourceResponse.h" |
| 11 #include "platform/testing/TestingPlatformSupport.h" | 11 #include "platform/testing/TestingPlatformSupport.h" |
| 12 #include "platform/testing/URLTestHelpers.h" | 12 #include "platform/testing/URLTestHelpers.h" |
| 13 #include "public/platform/Platform.h" | 13 #include "public/platform/Platform.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "wtf/Vector.h" | 15 #include "wtf/Vector.h" |
| 16 | 16 |
| 17 namespace blink { | 17 namespace blink { |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 class UnlockableResource : public Resource { | |
| 22 public: | |
| 23 static UnlockableResource* create(const KURL& url) | |
| 24 { | |
| 25 return new UnlockableResource(ResourceRequest(url), Resource::Raw); | |
| 26 } | |
| 27 | |
| 28 private: | |
| 29 UnlockableResource(const ResourceRequest& request, Type type) | |
| 30 : Resource(request, type, ResourceLoaderOptions()) | |
| 31 { | |
| 32 } | |
| 33 | |
| 34 bool isSafeToUnlock() const override { return true; } | |
| 35 }; | |
| 36 | |
| 37 class MockPlatform final : public TestingPlatformSupport { | 21 class MockPlatform final : public TestingPlatformSupport { |
| 38 public: | 22 public: |
| 39 MockPlatform() { } | 23 MockPlatform() { } |
| 40 ~MockPlatform() override { } | 24 ~MockPlatform() override { } |
| 41 | 25 |
| 42 // From blink::Platform: | 26 // From blink::Platform: |
| 43 void cacheMetadata(const WebURL& url, int64_t, const char*, size_t) override | 27 void cacheMetadata(const WebURL& url, int64_t, const char*, size_t) override |
| 44 { | 28 { |
| 45 m_cachedURLs.append(url); | 29 m_cachedURLs.append(url); |
| 46 } | 30 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 | 67 |
| 84 TEST(ResourceTest, SetCachedMetadata_DoesNotSendMetadataToPlatformWhenFetchedVia
ServiceWorker) | 68 TEST(ResourceTest, SetCachedMetadata_DoesNotSendMetadataToPlatformWhenFetchedVia
ServiceWorker) |
| 85 { | 69 { |
| 86 MockPlatform mock; | 70 MockPlatform mock; |
| 87 ResourceResponse response(createTestResourceResponse()); | 71 ResourceResponse response(createTestResourceResponse()); |
| 88 response.setWasFetchedViaServiceWorker(true); | 72 response.setWasFetchedViaServiceWorker(true); |
| 89 createTestResourceAndSetCachedMetadata(response); | 73 createTestResourceAndSetCachedMetadata(response); |
| 90 EXPECT_EQ(0u, mock.cachedURLs().size()); | 74 EXPECT_EQ(0u, mock.cachedURLs().size()); |
| 91 } | 75 } |
| 92 | 76 |
| 93 TEST(ResourceTest, LockFailureNoCrash) | |
| 94 { | |
| 95 ResourceResponse response(createTestResourceResponse()); | |
| 96 UnlockableResource* resource = UnlockableResource::create(response.url()); | |
| 97 memoryCache()->add(resource); | |
| 98 resource->setResponse(response); | |
| 99 | |
| 100 // A Resource won't be put in DiscardableMemory unless it is at least 16KiB. | |
| 101 Vector<char> dataVector(4*4096); | |
| 102 for (int i = 0; i < 4096; i++) | |
| 103 dataVector.append("test", 4); | |
| 104 resource->setResourceBuffer(SharedBuffer::adoptVector(dataVector)); | |
| 105 | |
| 106 resource->finish(currentTime()); | |
| 107 resource->prune(); | |
| 108 ASSERT_TRUE(resource->isPurgeable()); | |
| 109 bool didLock = resource->lock(); | |
| 110 ASSERT_FALSE(didLock); | |
| 111 EXPECT_EQ(nullptr, resource->resourceBuffer()); | |
| 112 EXPECT_EQ(size_t(0), resource->encodedSize()); | |
| 113 } | |
| 114 | |
| 115 TEST(ResourceTest, RevalidateWithFragment) | 77 TEST(ResourceTest, RevalidateWithFragment) |
| 116 { | 78 { |
| 117 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo.html"); | 79 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo.html"); |
| 118 ResourceResponse response; | 80 ResourceResponse response; |
| 119 response.setURL(url); | 81 response.setURL(url); |
| 120 response.setHTTPStatusCode(200); | 82 response.setHTTPStatusCode(200); |
| 121 Resource* resource = Resource::create(url, Resource::Raw); | 83 Resource* resource = Resource::create(url, Resource::Raw); |
| 122 resource->responseReceived(response, nullptr); | 84 resource->responseReceived(response, nullptr); |
| 123 resource->finish(); | 85 resource->finish(); |
| 124 | 86 |
| 125 // Revalidating with a url that differs by only the fragment | 87 // Revalidating with a url that differs by only the fragment |
| 126 // shouldn't trigger a securiy check. | 88 // shouldn't trigger a securiy check. |
| 127 url.setFragmentIdentifier("bar"); | 89 url.setFragmentIdentifier("bar"); |
| 128 resource->setRevalidatingRequest(ResourceRequest(url)); | 90 resource->setRevalidatingRequest(ResourceRequest(url)); |
| 129 ResourceResponse revalidatingResponse; | 91 ResourceResponse revalidatingResponse; |
| 130 revalidatingResponse.setURL(url); | 92 revalidatingResponse.setURL(url); |
| 131 revalidatingResponse.setHTTPStatusCode(304); | 93 revalidatingResponse.setHTTPStatusCode(304); |
| 132 resource->responseReceived(revalidatingResponse, nullptr); | 94 resource->responseReceived(revalidatingResponse, nullptr); |
| 133 } | 95 } |
| 134 | 96 |
| 135 } // namespace blink | 97 } // namespace blink |
| OLD | NEW |