| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "modules/fetch/FetchBlobDataConsumerHandle.h" |
| 7 |
| 8 #include "core/dom/ExecutionContext.h" |
| 9 #include "core/fetch/ResourceLoaderOptions.h" |
| 10 #include "core/loader/ThreadableLoader.h" |
| 11 #include "core/loader/ThreadableLoaderClient.h" |
| 12 #include "core/testing/DummyPageHolder.h" |
| 13 #include "modules/fetch/DataConsumerHandleTestUtil.h" |
| 14 #include "platform/blob/BlobData.h" |
| 15 #include "platform/blob/BlobURL.h" |
| 16 #include "platform/network/ResourceError.h" |
| 17 #include "platform/network/ResourceRequest.h" |
| 18 #include "platform/network/ResourceResponse.h" |
| 19 #include "platform/testing/UnitTestHelpers.h" |
| 20 #include "wtf/PassRefPtr.h" |
| 21 #include "wtf/RefPtr.h" |
| 22 |
| 23 #include <gmock/gmock.h> |
| 24 #include <gtest/gtest.h> |
| 25 #include <string.h> |
| 26 |
| 27 namespace blink { |
| 28 namespace { |
| 29 |
| 30 using Result = WebDataConsumerHandle::Result; |
| 31 const Result kUnexpectedError = WebDataConsumerHandle::UnexpectedError; |
| 32 const Result kDone = WebDataConsumerHandle::Done; |
| 33 using Flags = WebDataConsumerHandle::Flags; |
| 34 const Flags kNone = WebDataConsumerHandle::FlagNone; |
| 35 using Thread = DataConsumerHandleTestUtil::Thread; |
| 36 using HandleReader = DataConsumerHandleTestUtil::HandleReader; |
| 37 using HandleTwoPhaseReader = DataConsumerHandleTestUtil::HandleTwoPhaseReader; |
| 38 using HandleReadResult = DataConsumerHandleTestUtil::HandleReadResult; |
| 39 using ReplayingHandle = DataConsumerHandleTestUtil::ReplayingHandle; |
| 40 using Command = DataConsumerHandleTestUtil::Command; |
| 41 template <typename T> |
| 42 using HandleReaderRunner = DataConsumerHandleTestUtil::HandleReaderRunner<T>; |
| 43 |
| 44 using ::testing::_; |
| 45 using ::testing::DoAll; |
| 46 using ::testing::InSequence; |
| 47 using ::testing::Ref; |
| 48 using ::testing::Return; |
| 49 using ::testing::SaveArg; |
| 50 using ::testing::StrictMock; |
| 51 using Checkpoint = StrictMock<::testing::MockFunction<void(int)>>; |
| 52 |
| 53 class MockLoaderFactory : public FetchBlobDataConsumerHandle::LoaderFactory { |
| 54 public: |
| 55 MOCK_METHOD5(create, PassRefPtr<ThreadableLoader>(ExecutionContext&, Threada
bleLoaderClient*, const ResourceRequest&, const ThreadableLoaderOptions&, const
ResourceLoaderOptions&)); |
| 56 }; |
| 57 |
| 58 class MockThreadableLoader : public ThreadableLoader { |
| 59 public: |
| 60 static PassRefPtr<MockThreadableLoader> create() { return adoptRef(new Stric
tMock<MockThreadableLoader>); } |
| 61 |
| 62 MOCK_METHOD1(overrideTimeout, void(unsigned long)); |
| 63 MOCK_METHOD0(cancel, void()); |
| 64 |
| 65 protected: |
| 66 MockThreadableLoader() = default; |
| 67 }; |
| 68 |
| 69 PassRefPtr<BlobDataHandle> createBlobDataHandle(const char* s) |
| 70 { |
| 71 OwnPtr<BlobData> data = BlobData::create(); |
| 72 data->appendText(s, false); |
| 73 auto size = data->length(); |
| 74 return BlobDataHandle::create(data.release(), size); |
| 75 } |
| 76 |
| 77 String toString(const Vector<char>& data) |
| 78 { |
| 79 return String(data.data(), data.size()); |
| 80 } |
| 81 |
| 82 class FetchBlobDataConsumerHandleTest : public ::testing::Test { |
| 83 public: |
| 84 FetchBlobDataConsumerHandleTest() |
| 85 : m_dummyPageHolder(DummyPageHolder::create(IntSize(1, 1))) {} |
| 86 ~FetchBlobDataConsumerHandleTest() override |
| 87 { |
| 88 m_dummyPageHolder = nullptr; |
| 89 // We need this to collect garbage-collected mocks. |
| 90 Heap::collectAllGarbage(); |
| 91 } |
| 92 |
| 93 Document& document() { return m_dummyPageHolder->document(); } |
| 94 |
| 95 private: |
| 96 OwnPtr<DummyPageHolder> m_dummyPageHolder; |
| 97 }; |
| 98 |
| 99 TEST_F(FetchBlobDataConsumerHandleTest, CreateLoader) |
| 100 { |
| 101 auto factory = new StrictMock<MockLoaderFactory>; |
| 102 Checkpoint checkpoint; |
| 103 |
| 104 ResourceRequest request; |
| 105 ThreadableLoaderOptions options; |
| 106 ResourceLoaderOptions resourceLoaderOptions; |
| 107 |
| 108 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create(); |
| 109 |
| 110 InSequence s; |
| 111 EXPECT_CALL(checkpoint, Call(1)); |
| 112 EXPECT_CALL(*factory, create(Ref(document()), _, _, _, _)).WillOnce(DoAll( |
| 113 SaveArg<2>(&request), |
| 114 SaveArg<3>(&options), |
| 115 SaveArg<4>(&resourceLoaderOptions), |
| 116 Return(loader.get()))); |
| 117 EXPECT_CALL(checkpoint, Call(2)); |
| 118 EXPECT_CALL(*loader, cancel()); |
| 119 |
| 120 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti
me"); |
| 121 OwnPtr<WebDataConsumerHandle> handle |
| 122 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto
ry); |
| 123 testing::runPendingTasks(); |
| 124 |
| 125 size_t size = 0; |
| 126 handle->obtainReader(nullptr)->read(nullptr, 0, kNone, &size); |
| 127 checkpoint.Call(1); |
| 128 testing::runPendingTasks(); |
| 129 checkpoint.Call(2); |
| 130 |
| 131 EXPECT_TRUE(request.url().string().startsWith("blob:")); |
| 132 EXPECT_TRUE(request.useStreamOnResponse()); |
| 133 |
| 134 EXPECT_EQ(ConsiderPreflight, options.preflightPolicy); |
| 135 EXPECT_EQ(DenyCrossOriginRequests, options.crossOriginRequestPolicy); |
| 136 EXPECT_EQ(DoNotEnforceContentSecurityPolicy, options.contentSecurityPolicyEn
forcement); |
| 137 |
| 138 EXPECT_EQ(DoNotBufferData, resourceLoaderOptions.dataBufferingPolicy); |
| 139 EXPECT_EQ(DoNotAllowStoredCredentials, resourceLoaderOptions.allowCredential
s); |
| 140 EXPECT_EQ(ClientDidNotRequestCredentials, resourceLoaderOptions.credentialsR
equested); |
| 141 EXPECT_EQ(CheckContentSecurityPolicy, resourceLoaderOptions.contentSecurityP
olicyOption); |
| 142 EXPECT_EQ(DocumentContext, resourceLoaderOptions.requestInitiatorContext); |
| 143 EXPECT_EQ(RequestAsynchronously, resourceLoaderOptions.synchronousPolicy); |
| 144 EXPECT_EQ(NotCORSEnabled, resourceLoaderOptions.corsEnabled); |
| 145 } |
| 146 |
| 147 TEST_F(FetchBlobDataConsumerHandleTest, CancelLoaderWhenStopped) |
| 148 { |
| 149 auto factory = new StrictMock<MockLoaderFactory>; |
| 150 Checkpoint checkpoint; |
| 151 |
| 152 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create(); |
| 153 |
| 154 InSequence s; |
| 155 EXPECT_CALL(checkpoint, Call(1)); |
| 156 EXPECT_CALL(*factory, create(Ref(document()), _, _, _, _)).WillOnce(Return(l
oader.get())); |
| 157 EXPECT_CALL(checkpoint, Call(2)); |
| 158 EXPECT_CALL(*loader, cancel()); |
| 159 EXPECT_CALL(checkpoint, Call(3)); |
| 160 |
| 161 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti
me"); |
| 162 OwnPtr<WebDataConsumerHandle> handle |
| 163 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto
ry); |
| 164 testing::runPendingTasks(); |
| 165 |
| 166 size_t size = 0; |
| 167 handle->obtainReader(nullptr)->read(nullptr, 0, kNone, &size); |
| 168 checkpoint.Call(1); |
| 169 testing::runPendingTasks(); |
| 170 checkpoint.Call(2); |
| 171 document().stopActiveDOMObjects(); |
| 172 checkpoint.Call(3); |
| 173 } |
| 174 |
| 175 TEST_F(FetchBlobDataConsumerHandleTest, CancelLoaderWhenDestinationDetached) |
| 176 { |
| 177 auto factory = new StrictMock<MockLoaderFactory>; |
| 178 Checkpoint checkpoint; |
| 179 |
| 180 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create(); |
| 181 |
| 182 InSequence s; |
| 183 EXPECT_CALL(checkpoint, Call(1)); |
| 184 EXPECT_CALL(*factory, create(Ref(document()), _, _, _, _)).WillOnce(Return(l
oader.get())); |
| 185 EXPECT_CALL(checkpoint, Call(2)); |
| 186 EXPECT_CALL(checkpoint, Call(3)); |
| 187 EXPECT_CALL(*loader, cancel()); |
| 188 EXPECT_CALL(checkpoint, Call(4)); |
| 189 |
| 190 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti
me"); |
| 191 OwnPtr<WebDataConsumerHandle> handle |
| 192 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto
ry); |
| 193 OwnPtr<WebDataConsumerHandle::Reader> reader = handle->obtainReader(nullptr)
; |
| 194 testing::runPendingTasks(); |
| 195 |
| 196 size_t size = 0; |
| 197 reader->read(nullptr, 0, kNone, &size); |
| 198 checkpoint.Call(1); |
| 199 testing::runPendingTasks(); |
| 200 checkpoint.Call(2); |
| 201 handle = nullptr; |
| 202 reader = nullptr; |
| 203 checkpoint.Call(3); |
| 204 Heap::collectAllGarbage(); |
| 205 checkpoint.Call(4); |
| 206 } |
| 207 |
| 208 TEST_F(FetchBlobDataConsumerHandleTest, ReadTest) |
| 209 { |
| 210 auto factory = new StrictMock<MockLoaderFactory>; |
| 211 Checkpoint checkpoint; |
| 212 |
| 213 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create(); |
| 214 ThreadableLoaderClient* client = nullptr; |
| 215 |
| 216 InSequence s; |
| 217 EXPECT_CALL(checkpoint, Call(1)); |
| 218 EXPECT_CALL(*factory, create(Ref(document()), _, _, _, _)).WillOnce(DoAll(Sa
veArg<1>(&client), Return(loader.get()))); |
| 219 EXPECT_CALL(checkpoint, Call(2)); |
| 220 EXPECT_CALL(*loader, cancel()); |
| 221 |
| 222 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti
me"); |
| 223 OwnPtr<WebDataConsumerHandle> handle |
| 224 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto
ry); |
| 225 |
| 226 OwnPtr<ReplayingHandle> src = ReplayingHandle::create(); |
| 227 src->add(Command(Command::Wait)); |
| 228 src->add(Command(Command::Data, "hello, ")); |
| 229 src->add(Command(Command::Data, "world")); |
| 230 src->add(Command(Command::Wait)); |
| 231 src->add(Command(Command::Done)); |
| 232 |
| 233 size_t size = 0; |
| 234 handle->obtainReader(nullptr)->read(nullptr, 0, kNone, &size); |
| 235 checkpoint.Call(1); |
| 236 testing::runPendingTasks(); |
| 237 checkpoint.Call(2); |
| 238 client->didReceiveResponse(0, ResourceResponse(), src.release()); |
| 239 HandleReaderRunner<HandleReader> runner(handle.release()); |
| 240 OwnPtr<HandleReadResult> r = runner.wait(); |
| 241 EXPECT_EQ(kDone, r->result()); |
| 242 EXPECT_EQ("hello, world", toString(r->data())); |
| 243 } |
| 244 |
| 245 TEST_F(FetchBlobDataConsumerHandleTest, TwoPhaseReadTest) |
| 246 { |
| 247 auto factory = new StrictMock<MockLoaderFactory>; |
| 248 Checkpoint checkpoint; |
| 249 |
| 250 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create(); |
| 251 ThreadableLoaderClient* client = nullptr; |
| 252 |
| 253 InSequence s; |
| 254 EXPECT_CALL(checkpoint, Call(1)); |
| 255 EXPECT_CALL(*factory, create(Ref(document()), _, _, _, _)).WillOnce(DoAll(Sa
veArg<1>(&client), Return(loader.get()))); |
| 256 EXPECT_CALL(checkpoint, Call(2)); |
| 257 EXPECT_CALL(*loader, cancel()); |
| 258 |
| 259 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti
me"); |
| 260 OwnPtr<WebDataConsumerHandle> handle |
| 261 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto
ry); |
| 262 |
| 263 OwnPtr<ReplayingHandle> src = ReplayingHandle::create(); |
| 264 src->add(Command(Command::Wait)); |
| 265 src->add(Command(Command::Data, "hello, ")); |
| 266 src->add(Command(Command::Data, "world")); |
| 267 src->add(Command(Command::Wait)); |
| 268 src->add(Command(Command::Done)); |
| 269 |
| 270 size_t size = 0; |
| 271 handle->obtainReader(nullptr)->read(nullptr, 0, kNone, &size); |
| 272 checkpoint.Call(1); |
| 273 testing::runPendingTasks(); |
| 274 checkpoint.Call(2); |
| 275 client->didReceiveResponse(0, ResourceResponse(), src.release()); |
| 276 HandleReaderRunner<HandleTwoPhaseReader> runner(handle.release()); |
| 277 OwnPtr<HandleReadResult> r = runner.wait(); |
| 278 EXPECT_EQ(kDone, r->result()); |
| 279 EXPECT_EQ("hello, world", toString(r->data())); |
| 280 } |
| 281 |
| 282 TEST_F(FetchBlobDataConsumerHandleTest, LoadErrorTest) |
| 283 { |
| 284 auto factory = new StrictMock<MockLoaderFactory>; |
| 285 Checkpoint checkpoint; |
| 286 |
| 287 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create(); |
| 288 ThreadableLoaderClient* client = nullptr; |
| 289 |
| 290 InSequence s; |
| 291 EXPECT_CALL(checkpoint, Call(1)); |
| 292 EXPECT_CALL(*factory, create(Ref(document()), _, _, _, _)).WillOnce(DoAll(Sa
veArg<1>(&client), Return(loader.get()))); |
| 293 EXPECT_CALL(checkpoint, Call(2)); |
| 294 |
| 295 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti
me"); |
| 296 OwnPtr<WebDataConsumerHandle> handle |
| 297 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto
ry); |
| 298 |
| 299 size_t size = 0; |
| 300 handle->obtainReader(nullptr)->read(nullptr, 0, kNone, &size); |
| 301 checkpoint.Call(1); |
| 302 testing::runPendingTasks(); |
| 303 checkpoint.Call(2); |
| 304 client->didFail(ResourceError()); |
| 305 HandleReaderRunner<HandleReader> runner(handle.release()); |
| 306 OwnPtr<HandleReadResult> r = runner.wait(); |
| 307 EXPECT_EQ(kUnexpectedError, r->result()); |
| 308 } |
| 309 |
| 310 TEST_F(FetchBlobDataConsumerHandleTest, BodyLoadErrorTest) |
| 311 { |
| 312 auto factory = new StrictMock<MockLoaderFactory>; |
| 313 Checkpoint checkpoint; |
| 314 |
| 315 RefPtr<MockThreadableLoader> loader = MockThreadableLoader::create(); |
| 316 ThreadableLoaderClient* client = nullptr; |
| 317 |
| 318 InSequence s; |
| 319 EXPECT_CALL(checkpoint, Call(1)); |
| 320 EXPECT_CALL(*factory, create(Ref(document()), _, _, _, _)).WillOnce(DoAll(Sa
veArg<1>(&client), Return(loader.get()))); |
| 321 EXPECT_CALL(checkpoint, Call(2)); |
| 322 EXPECT_CALL(*loader, cancel()); |
| 323 |
| 324 RefPtr<BlobDataHandle> blobDataHandle = createBlobDataHandle("Once upon a ti
me"); |
| 325 OwnPtr<WebDataConsumerHandle> handle |
| 326 = FetchBlobDataConsumerHandle::create(&document(), blobDataHandle, facto
ry); |
| 327 |
| 328 OwnPtr<ReplayingHandle> src = ReplayingHandle::create(); |
| 329 src->add(Command(Command::Wait)); |
| 330 src->add(Command(Command::Data, "hello, ")); |
| 331 src->add(Command(Command::Error)); |
| 332 |
| 333 size_t size = 0; |
| 334 handle->obtainReader(nullptr)->read(nullptr, 0, kNone, &size); |
| 335 checkpoint.Call(1); |
| 336 testing::runPendingTasks(); |
| 337 checkpoint.Call(2); |
| 338 client->didReceiveResponse(0, ResourceResponse(), src.release()); |
| 339 HandleReaderRunner<HandleReader> runner(handle.release()); |
| 340 OwnPtr<HandleReadResult> r = runner.wait(); |
| 341 EXPECT_EQ(kUnexpectedError, r->result()); |
| 342 } |
| 343 |
| 344 } // namespace |
| 345 } // namespace blink |
| OLD | NEW |