chunked_data_pipe_upload_data_stream_unittest.cc 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. // Copyright 2017 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. #include "services/network/chunked_data_pipe_upload_data_stream.h"
  5. #include <stdint.h>
  6. #include <limits>
  7. #include <memory>
  8. #include "base/memory/ref_counted.h"
  9. #include "base/run_loop.h"
  10. #include "base/strings/string_number_conversions.h"
  11. #include "base/test/task_environment.h"
  12. #include "mojo/public/cpp/system/data_pipe.h"
  13. #include "mojo/public/cpp/system/data_pipe_utils.h"
  14. #include "net/base/completion_once_callback.h"
  15. #include "net/base/io_buffer.h"
  16. #include "net/base/test_completion_callback.h"
  17. #include "net/log/net_log_with_source.h"
  18. #include "services/network/public/cpp/resource_request_body.h"
  19. #include "services/network/public/mojom/chunked_data_pipe_getter.mojom.h"
  20. #include "services/network/test_chunked_data_pipe_getter.h"
  21. #include "testing/gtest/include/gtest/gtest.h"
  22. // Most tests of this class are at the URLLoader layer. These tests focus on
  23. // things too difficult to cover with integration tests.
  24. namespace network {
  25. namespace {
  26. net::CompletionOnceCallback NoCallback() {
  27. return base::BindOnce([](int result) {
  28. NOTREACHED() << "This callback should not be called. result=" << result;
  29. });
  30. }
  31. class ChunkedDataPipeUploadDataStreamTest : public testing::Test {
  32. public:
  33. ChunkedDataPipeUploadDataStreamTest() { CreateAndInitChunkedUploadStream(); }
  34. void CreateAndInitChunkedUploadStream() {
  35. CreateChunkedUploadStream();
  36. InitChunkedUploadStream();
  37. }
  38. void CreateChunkedUploadStream() {
  39. chunked_data_pipe_getter_ = std::make_unique<TestChunkedDataPipeGetter>();
  40. chunked_upload_stream_ = std::make_unique<ChunkedDataPipeUploadDataStream>(
  41. base::MakeRefCounted<network::ResourceRequestBody>(),
  42. chunked_data_pipe_getter_->GetDataPipeGetterRemote());
  43. }
  44. void InitChunkedUploadStream() {
  45. // Nothing interesting happens before Init, so always wait for it in the
  46. // test fixture.
  47. EXPECT_EQ(net::OK, chunked_upload_stream_->Init(NoCallback(),
  48. net::NetLogWithSource()));
  49. get_size_callback_ = chunked_data_pipe_getter_->WaitForGetSize();
  50. write_pipe_ = chunked_data_pipe_getter_->WaitForStartReading();
  51. EXPECT_TRUE(chunked_upload_stream_->is_chunked());
  52. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  53. EXPECT_FALSE(chunked_upload_stream_->IsInMemory());
  54. }
  55. protected:
  56. base::test::SingleThreadTaskEnvironment task_environment_{
  57. base::test::SingleThreadTaskEnvironment::MainThreadType::IO};
  58. std::unique_ptr<TestChunkedDataPipeGetter> chunked_data_pipe_getter_;
  59. std::unique_ptr<ChunkedDataPipeUploadDataStream> chunked_upload_stream_;
  60. mojom::ChunkedDataPipeGetter::GetSizeCallback get_size_callback_;
  61. mojo::ScopedDataPipeProducerHandle write_pipe_;
  62. };
  63. // Test just reading through the response body once, where reads are initiated
  64. // before data is received.
  65. TEST_F(ChunkedDataPipeUploadDataStreamTest, ReadBeforeDataReady) {
  66. const std::string kData = "1234567890";
  67. for (int consumer_read_size : {5, 10, 20}) {
  68. for (int num_writes : {0, 1, 2}) {
  69. CreateAndInitChunkedUploadStream();
  70. for (int i = 0; i < num_writes; ++i) {
  71. std::string read_data;
  72. while (read_data.size() < kData.size()) {
  73. net::TestCompletionCallback callback;
  74. auto io_buffer =
  75. base::MakeRefCounted<net::IOBufferWithSize>(consumer_read_size);
  76. int result = chunked_upload_stream_->Read(
  77. io_buffer.get(), io_buffer->size(), callback.callback());
  78. if (read_data.size() == 0)
  79. mojo::BlockingCopyFromString(kData, write_pipe_);
  80. result = callback.GetResult(result);
  81. ASSERT_LT(0, result);
  82. EXPECT_LE(result, consumer_read_size);
  83. read_data.append(std::string(io_buffer->data(), result));
  84. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  85. }
  86. EXPECT_EQ(read_data, kData);
  87. }
  88. auto io_buffer =
  89. base::MakeRefCounted<net::IOBufferWithSize>(consumer_read_size);
  90. net::TestCompletionCallback callback;
  91. int result = chunked_upload_stream_->Read(
  92. io_buffer.get(), io_buffer->size(), callback.callback());
  93. EXPECT_EQ(net::ERR_IO_PENDING, result);
  94. std::move(get_size_callback_).Run(net::OK, num_writes * kData.size());
  95. EXPECT_EQ(net::OK, callback.GetResult(result));
  96. EXPECT_TRUE(chunked_upload_stream_->IsEOF());
  97. }
  98. }
  99. }
  100. // Test just reading through the response body once, where reads are initiated
  101. // after data has been received.
  102. TEST_F(ChunkedDataPipeUploadDataStreamTest, ReadAfterDataReady) {
  103. const std::string kData = "1234567890";
  104. for (int consumer_read_size : {5, 10, 20}) {
  105. for (int num_writes : {0, 1, 2}) {
  106. CreateAndInitChunkedUploadStream();
  107. for (int i = 0; i < num_writes; ++i) {
  108. mojo::BlockingCopyFromString(kData, write_pipe_);
  109. base::RunLoop().RunUntilIdle();
  110. std::string read_data;
  111. while (read_data.size() < kData.size()) {
  112. net::TestCompletionCallback callback;
  113. auto io_buffer =
  114. base::MakeRefCounted<net::IOBufferWithSize>(consumer_read_size);
  115. int result = chunked_upload_stream_->Read(
  116. io_buffer.get(), io_buffer->size(), callback.callback());
  117. ASSERT_LT(0, result);
  118. EXPECT_LE(result, consumer_read_size);
  119. read_data.append(std::string(io_buffer->data(), result));
  120. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  121. }
  122. EXPECT_EQ(read_data, kData);
  123. }
  124. std::move(get_size_callback_).Run(net::OK, num_writes * kData.size());
  125. base::RunLoop().RunUntilIdle();
  126. net::TestCompletionCallback callback;
  127. auto io_buffer =
  128. base::MakeRefCounted<net::IOBufferWithSize>(consumer_read_size);
  129. EXPECT_EQ(net::OK,
  130. chunked_upload_stream_->Read(io_buffer.get(), io_buffer->size(),
  131. callback.callback()));
  132. EXPECT_TRUE(chunked_upload_stream_->IsEOF());
  133. }
  134. }
  135. }
  136. // Test the case where the URLRequest reads through the request body multiple
  137. // times, as can happen in the case of redirects or retries.
  138. TEST_F(ChunkedDataPipeUploadDataStreamTest, MultipleReadThrough) {
  139. const std::string kData = "1234567890";
  140. // Send size up front - cases where the size isn't initially known are checked
  141. // elsewhere, and have to have size before the first read through can complete
  142. // successfully.
  143. std::move(get_size_callback_).Run(net::OK, kData.size());
  144. base::RunLoop().RunUntilIdle();
  145. // 3 is an arbitrary number greater than 1.
  146. for (int i = 0; i < 3; i++) {
  147. // Already initialized before loop runs for the first time.
  148. if (i != 0) {
  149. net::TestCompletionCallback callback;
  150. EXPECT_EQ(net::OK, chunked_upload_stream_->Init(callback.callback(),
  151. net::NetLogWithSource()));
  152. write_pipe_ = chunked_data_pipe_getter_->WaitForStartReading();
  153. }
  154. mojo::BlockingCopyFromString(kData, write_pipe_);
  155. std::string read_data;
  156. while (read_data.size() < kData.size()) {
  157. net::TestCompletionCallback callback;
  158. auto io_buffer =
  159. base::MakeRefCounted<net::IOBufferWithSize>(kData.size());
  160. int result = chunked_upload_stream_->Read(
  161. io_buffer.get(), io_buffer->size(), callback.callback());
  162. result = callback.GetResult(result);
  163. ASSERT_LT(0, result);
  164. EXPECT_LE(static_cast<size_t>(result), kData.size());
  165. read_data.append(std::string(io_buffer->data(), result));
  166. if (read_data.size() == kData.size()) {
  167. EXPECT_TRUE(chunked_upload_stream_->IsEOF());
  168. } else {
  169. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  170. }
  171. }
  172. EXPECT_EQ(kData, read_data);
  173. net::TestCompletionCallback callback;
  174. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(1);
  175. int result =
  176. chunked_upload_stream_->Read(io_buffer.get(), 1, callback.callback());
  177. EXPECT_EQ(net::OK, callback.GetResult(result));
  178. EXPECT_TRUE(chunked_upload_stream_->IsEOF());
  179. chunked_upload_stream_->Reset();
  180. }
  181. }
  182. // Test the case where the URLRequest partially reads through the request body
  183. // multiple times, as can happen in the case of retries. The size is known from
  184. // the start.
  185. TEST_F(ChunkedDataPipeUploadDataStreamTest,
  186. MultiplePartialReadThroughWithKnownSize) {
  187. const std::string kData = "1234567890";
  188. std::move(get_size_callback_).Run(net::OK, kData.size());
  189. // Wait for the stream to learn the size, to avoid any races.
  190. base::RunLoop().RunUntilIdle();
  191. // In each iteration, read through more of the body. Reset the stream after
  192. // each loop iteration but the last, which reads the entire body.
  193. for (size_t num_bytes_to_read = 0; num_bytes_to_read <= kData.size();
  194. num_bytes_to_read++) {
  195. // Already initialized before loop runs for the first time.
  196. if (num_bytes_to_read != 0) {
  197. net::TestCompletionCallback callback;
  198. EXPECT_EQ(net::OK, chunked_upload_stream_->Init(callback.callback(),
  199. net::NetLogWithSource()));
  200. write_pipe_ = chunked_data_pipe_getter_->WaitForStartReading();
  201. }
  202. mojo::BlockingCopyFromString(kData.substr(0, num_bytes_to_read),
  203. write_pipe_);
  204. std::string read_data;
  205. while (read_data.size() < num_bytes_to_read) {
  206. net::TestCompletionCallback callback;
  207. auto io_buffer =
  208. base::MakeRefCounted<net::IOBufferWithSize>(kData.size());
  209. int result = chunked_upload_stream_->Read(
  210. io_buffer.get(), io_buffer->size(), callback.callback());
  211. result = callback.GetResult(result);
  212. ASSERT_LT(0, result);
  213. EXPECT_LE(static_cast<size_t>(result), kData.size());
  214. read_data.append(std::string(io_buffer->data(), result));
  215. if (read_data.size() == kData.size()) {
  216. EXPECT_TRUE(chunked_upload_stream_->IsEOF());
  217. } else {
  218. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  219. }
  220. }
  221. EXPECT_EQ(kData.substr(0, num_bytes_to_read), read_data);
  222. if (num_bytes_to_read != kData.size())
  223. chunked_upload_stream_->Reset();
  224. }
  225. net::TestCompletionCallback callback;
  226. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(1);
  227. int result =
  228. chunked_upload_stream_->Read(io_buffer.get(), 1, callback.callback());
  229. EXPECT_EQ(net::OK, callback.GetResult(result));
  230. EXPECT_TRUE(chunked_upload_stream_->IsEOF());
  231. }
  232. // Test the case where the URLRequest partially reads through the request body
  233. // multiple times, as can happen in the case of retries. The size isn't known
  234. // until the end.
  235. TEST_F(ChunkedDataPipeUploadDataStreamTest,
  236. MultiplePartialReadThroughSizeNotKnown) {
  237. const std::string kData = "1234567890";
  238. // In each iteration, read through more of the body. Reset the stream after
  239. // each loop iteration but the last, which reads the entire body.
  240. for (size_t num_bytes_to_read = 0; num_bytes_to_read <= kData.size();
  241. num_bytes_to_read++) {
  242. // Already initialized before loop runs for the first time.
  243. if (num_bytes_to_read != 0) {
  244. net::TestCompletionCallback callback;
  245. EXPECT_EQ(net::OK, chunked_upload_stream_->Init(callback.callback(),
  246. net::NetLogWithSource()));
  247. write_pipe_ = chunked_data_pipe_getter_->WaitForStartReading();
  248. }
  249. mojo::BlockingCopyFromString(kData.substr(0, num_bytes_to_read),
  250. write_pipe_);
  251. std::string read_data;
  252. while (read_data.size() < num_bytes_to_read) {
  253. net::TestCompletionCallback callback;
  254. auto io_buffer =
  255. base::MakeRefCounted<net::IOBufferWithSize>(kData.size());
  256. int result = chunked_upload_stream_->Read(
  257. io_buffer.get(), io_buffer->size(), callback.callback());
  258. result = callback.GetResult(result);
  259. ASSERT_LT(0, result);
  260. EXPECT_LE(static_cast<size_t>(result), kData.size());
  261. read_data.append(std::string(io_buffer->data(), result));
  262. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  263. }
  264. EXPECT_EQ(kData.substr(0, num_bytes_to_read), read_data);
  265. if (num_bytes_to_read != kData.size())
  266. chunked_upload_stream_->Reset();
  267. }
  268. std::move(get_size_callback_).Run(net::OK, kData.size());
  269. base::RunLoop().RunUntilIdle();
  270. net::TestCompletionCallback callback;
  271. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(1);
  272. int result =
  273. chunked_upload_stream_->Read(io_buffer.get(), 1, callback.callback());
  274. EXPECT_EQ(net::OK, callback.GetResult(result));
  275. EXPECT_TRUE(chunked_upload_stream_->IsEOF());
  276. }
  277. // Test where GetSize() is invoked before the upload is initialized.
  278. TEST_F(ChunkedDataPipeUploadDataStreamTest, GetSizeSucceedsBeforeInit) {
  279. const std::string kData = "1234567890";
  280. chunked_data_pipe_getter_ = std::make_unique<TestChunkedDataPipeGetter>();
  281. chunked_upload_stream_ = std::make_unique<ChunkedDataPipeUploadDataStream>(
  282. base::MakeRefCounted<network::ResourceRequestBody>(),
  283. chunked_data_pipe_getter_->GetDataPipeGetterRemote());
  284. get_size_callback_ = chunked_data_pipe_getter_->WaitForGetSize();
  285. std::move(get_size_callback_).Run(net::OK, kData.size());
  286. // Wait for the ChunkedUploadStream to receive the size.
  287. base::RunLoop().RunUntilIdle();
  288. net::TestCompletionCallback callback;
  289. ASSERT_EQ(net::OK, chunked_upload_stream_->Init(callback.callback(),
  290. net::NetLogWithSource()));
  291. write_pipe_ = chunked_data_pipe_getter_->WaitForStartReading();
  292. EXPECT_TRUE(chunked_upload_stream_->is_chunked());
  293. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  294. mojo::BlockingCopyFromString(kData, write_pipe_);
  295. std::string read_data;
  296. while (read_data.size() < kData.size()) {
  297. int read_size = kData.size() - read_data.size();
  298. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(read_size);
  299. int result = chunked_upload_stream_->Read(
  300. io_buffer.get(), io_buffer->size(), callback.callback());
  301. result = callback.GetResult(result);
  302. ASSERT_LT(0, result);
  303. EXPECT_LE(result, read_size);
  304. read_data.append(std::string(io_buffer->data(), result));
  305. EXPECT_EQ(read_data.size() == kData.size(),
  306. chunked_upload_stream_->IsEOF());
  307. }
  308. }
  309. // Test where GetSize() is only invoked after the upload is reset.
  310. TEST_F(ChunkedDataPipeUploadDataStreamTest, GetSizeSucceedsAfterReset) {
  311. const std::string kData = "1234567890";
  312. // Read through the body once, without a size
  313. std::string read_data;
  314. mojo::BlockingCopyFromString(kData, write_pipe_);
  315. while (read_data.size() < kData.size()) {
  316. net::TestCompletionCallback callback;
  317. int read_size = kData.size() - read_data.size();
  318. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(read_size);
  319. int result = chunked_upload_stream_->Read(
  320. io_buffer.get(), io_buffer->size(), callback.callback());
  321. result = callback.GetResult(result);
  322. ASSERT_LT(0, result);
  323. EXPECT_LE(result, read_size);
  324. read_data.append(std::string(io_buffer->data(), result));
  325. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  326. }
  327. // Reset, get the size, and read through the body again.
  328. chunked_upload_stream_->Reset();
  329. std::move(get_size_callback_).Run(net::OK, kData.size());
  330. // Wait for the ChunkedUploadStream to receive the size.
  331. base::RunLoop().RunUntilIdle();
  332. net::TestCompletionCallback callback;
  333. ASSERT_EQ(net::OK, chunked_upload_stream_->Init(callback.callback(),
  334. net::NetLogWithSource()));
  335. write_pipe_ = chunked_data_pipe_getter_->WaitForStartReading();
  336. read_data.erase();
  337. mojo::BlockingCopyFromString(kData, write_pipe_);
  338. while (read_data.size() < kData.size()) {
  339. int read_size = kData.size() - read_data.size();
  340. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(read_size);
  341. int result = chunked_upload_stream_->Read(
  342. io_buffer.get(), io_buffer->size(), callback.callback());
  343. result = callback.GetResult(result);
  344. ASSERT_LT(0, result);
  345. EXPECT_LE(result, read_size);
  346. read_data.append(std::string(io_buffer->data(), result));
  347. EXPECT_EQ(read_data.size() == kData.size(),
  348. chunked_upload_stream_->IsEOF());
  349. }
  350. }
  351. // Test where GetSize() is invoked with an error before the upload is
  352. // initialized.
  353. TEST_F(ChunkedDataPipeUploadDataStreamTest, GetSizeFailsBeforeInit) {
  354. const std::string kData = "1234567890";
  355. chunked_data_pipe_getter_ = std::make_unique<TestChunkedDataPipeGetter>();
  356. chunked_upload_stream_ = std::make_unique<ChunkedDataPipeUploadDataStream>(
  357. base::MakeRefCounted<network::ResourceRequestBody>(),
  358. chunked_data_pipe_getter_->GetDataPipeGetterRemote());
  359. get_size_callback_ = chunked_data_pipe_getter_->WaitForGetSize();
  360. std::move(get_size_callback_).Run(net::ERR_ACCESS_DENIED, 0);
  361. // Wait for the ChunkedUploadStream to receive the size.
  362. base::RunLoop().RunUntilIdle();
  363. net::TestCompletionCallback callback;
  364. EXPECT_EQ(net::ERR_ACCESS_DENIED,
  365. chunked_upload_stream_->Init(callback.callback(),
  366. net::NetLogWithSource()));
  367. }
  368. // Test where GetSize() is only invoked with an error after the upload is reset.
  369. TEST_F(ChunkedDataPipeUploadDataStreamTest, GetSizeFailsAfterReset) {
  370. const std::string kData = "1234567890";
  371. // Read through the body once, without a size
  372. std::string read_data;
  373. mojo::BlockingCopyFromString(kData, write_pipe_);
  374. while (read_data.size() < kData.size()) {
  375. net::TestCompletionCallback callback;
  376. int read_size = kData.size() - read_data.size();
  377. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(read_size);
  378. int result = chunked_upload_stream_->Read(
  379. io_buffer.get(), io_buffer->size(), callback.callback());
  380. result = callback.GetResult(result);
  381. ASSERT_LT(0, result);
  382. EXPECT_LE(result, read_size);
  383. read_data.append(std::string(io_buffer->data(), result));
  384. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  385. }
  386. // Reset, get the size, and read through the body again.
  387. chunked_upload_stream_->Reset();
  388. std::move(get_size_callback_).Run(net::ERR_ACCESS_DENIED, kData.size());
  389. // Wait for the ChunkedUploadStream to receive the size.
  390. base::RunLoop().RunUntilIdle();
  391. net::TestCompletionCallback callback;
  392. ASSERT_EQ(net::ERR_ACCESS_DENIED,
  393. chunked_upload_stream_->Init(callback.callback(),
  394. net::NetLogWithSource()));
  395. }
  396. // Three variations on when the stream can be closed before a request succeeds.
  397. // Stream is closed, then a read attempted, then the GetSizeCallback is invoked.
  398. // The read should notice the close body pipe, but not report anything until the
  399. // GetSizeCallback is invoked.
  400. TEST_F(ChunkedDataPipeUploadDataStreamTest, CloseBodyPipeBeforeSuccess1) {
  401. write_pipe_.reset();
  402. base::RunLoop().RunUntilIdle();
  403. net::TestCompletionCallback callback;
  404. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(1);
  405. int result =
  406. chunked_upload_stream_->Read(io_buffer.get(), 1, callback.callback());
  407. EXPECT_EQ(result, net::ERR_IO_PENDING);
  408. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  409. std::move(get_size_callback_).Run(net::OK, 0);
  410. EXPECT_EQ(net::OK, callback.GetResult(result));
  411. EXPECT_TRUE(chunked_upload_stream_->IsEOF());
  412. }
  413. // A read attempt, stream is closed, then the GetSizeCallback is invoked. The
  414. // watcher should see the close, but not report anything until the
  415. // GetSizeCallback is invoked.
  416. TEST_F(ChunkedDataPipeUploadDataStreamTest, CloseBodyPipeBeforeSuccess2) {
  417. net::TestCompletionCallback callback;
  418. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(1);
  419. int result =
  420. chunked_upload_stream_->Read(io_buffer.get(), 1, callback.callback());
  421. EXPECT_EQ(result, net::ERR_IO_PENDING);
  422. write_pipe_.reset();
  423. base::RunLoop().RunUntilIdle();
  424. EXPECT_FALSE(callback.have_result());
  425. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  426. std::move(get_size_callback_).Run(net::OK, 0);
  427. EXPECT_EQ(net::OK, callback.GetResult(result));
  428. EXPECT_TRUE(chunked_upload_stream_->IsEOF());
  429. }
  430. // The stream is closed, the GetSizeCallback is invoked, and then a read
  431. // attempt. The read attempt should notice the request already successfully
  432. // completed.
  433. TEST_F(ChunkedDataPipeUploadDataStreamTest, CloseBodyPipeBeforeSuccess3) {
  434. write_pipe_.reset();
  435. base::RunLoop().RunUntilIdle();
  436. std::move(get_size_callback_).Run(net::OK, 0);
  437. base::RunLoop().RunUntilIdle();
  438. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  439. net::TestCompletionCallback callback;
  440. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(1);
  441. int result =
  442. chunked_upload_stream_->Read(io_buffer.get(), 1, callback.callback());
  443. EXPECT_EQ(net::OK, callback.GetResult(result));
  444. EXPECT_TRUE(chunked_upload_stream_->IsEOF());
  445. }
  446. // Same cases as above, but the GetSizeCallback indicates the response was
  447. // truncated.
  448. TEST_F(ChunkedDataPipeUploadDataStreamTest, CloseBodyPipeBeforeTruncation1) {
  449. write_pipe_.reset();
  450. base::RunLoop().RunUntilIdle();
  451. net::TestCompletionCallback callback;
  452. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(1);
  453. int result =
  454. chunked_upload_stream_->Read(io_buffer.get(), 1, callback.callback());
  455. EXPECT_EQ(result, net::ERR_IO_PENDING);
  456. std::move(get_size_callback_).Run(net::OK, 1);
  457. EXPECT_EQ(net::ERR_FAILED, callback.GetResult(result));
  458. }
  459. TEST_F(ChunkedDataPipeUploadDataStreamTest, CloseBodyPipeBeforeTruncation2) {
  460. net::TestCompletionCallback callback;
  461. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(1);
  462. int result =
  463. chunked_upload_stream_->Read(io_buffer.get(), 1, callback.callback());
  464. EXPECT_EQ(result, net::ERR_IO_PENDING);
  465. write_pipe_.reset();
  466. base::RunLoop().RunUntilIdle();
  467. EXPECT_FALSE(callback.have_result());
  468. std::move(get_size_callback_).Run(net::OK, 1);
  469. EXPECT_EQ(net::ERR_FAILED, callback.GetResult(result));
  470. }
  471. TEST_F(ChunkedDataPipeUploadDataStreamTest, CloseBodyPipeBeforeTruncation3) {
  472. write_pipe_.reset();
  473. base::RunLoop().RunUntilIdle();
  474. std::move(get_size_callback_).Run(net::OK, 1);
  475. net::TestCompletionCallback callback;
  476. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(1);
  477. int result =
  478. chunked_upload_stream_->Read(io_buffer.get(), 1, callback.callback());
  479. EXPECT_EQ(net::ERR_FAILED, callback.GetResult(result));
  480. }
  481. // Same cases as above, but the GetSizeCallback indicates the read failed.
  482. TEST_F(ChunkedDataPipeUploadDataStreamTest, CloseBodyPipeBeforeFailure1) {
  483. write_pipe_.reset();
  484. base::RunLoop().RunUntilIdle();
  485. net::TestCompletionCallback callback;
  486. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(1);
  487. int result =
  488. chunked_upload_stream_->Read(io_buffer.get(), 1, callback.callback());
  489. EXPECT_EQ(result, net::ERR_IO_PENDING);
  490. std::move(get_size_callback_).Run(net::ERR_ACCESS_DENIED, 0);
  491. EXPECT_EQ(net::ERR_ACCESS_DENIED, callback.GetResult(result));
  492. }
  493. TEST_F(ChunkedDataPipeUploadDataStreamTest, CloseBodyPipeBeforeFailure2) {
  494. net::TestCompletionCallback callback;
  495. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(1);
  496. int result =
  497. chunked_upload_stream_->Read(io_buffer.get(), 1, callback.callback());
  498. EXPECT_EQ(result, net::ERR_IO_PENDING);
  499. write_pipe_.reset();
  500. base::RunLoop().RunUntilIdle();
  501. EXPECT_FALSE(callback.have_result());
  502. std::move(get_size_callback_).Run(net::ERR_ACCESS_DENIED, 0);
  503. EXPECT_EQ(net::ERR_ACCESS_DENIED, callback.GetResult(result));
  504. }
  505. TEST_F(ChunkedDataPipeUploadDataStreamTest, CloseBodyPipeBeforeFailure3) {
  506. write_pipe_.reset();
  507. base::RunLoop().RunUntilIdle();
  508. std::move(get_size_callback_).Run(net::ERR_ACCESS_DENIED, 0);
  509. net::TestCompletionCallback callback;
  510. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(1);
  511. int result =
  512. chunked_upload_stream_->Read(io_buffer.get(), 1, callback.callback());
  513. EXPECT_EQ(net::ERR_ACCESS_DENIED, callback.GetResult(result));
  514. }
  515. // Same cases as above, but ChunkedBodyGetter pipe is destroyed without invoking
  516. // the GetSizeCallback.
  517. TEST_F(ChunkedDataPipeUploadDataStreamTest, CloseBodyPipeBeforeCloseGetter1) {
  518. write_pipe_.reset();
  519. base::RunLoop().RunUntilIdle();
  520. net::TestCompletionCallback callback;
  521. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(1);
  522. int result =
  523. chunked_upload_stream_->Read(io_buffer.get(), 1, callback.callback());
  524. EXPECT_EQ(result, net::ERR_IO_PENDING);
  525. chunked_data_pipe_getter_.reset();
  526. EXPECT_EQ(net::ERR_FAILED, callback.GetResult(result));
  527. }
  528. TEST_F(ChunkedDataPipeUploadDataStreamTest, CloseBodyPipeBeforeCloseGetter2) {
  529. net::TestCompletionCallback callback;
  530. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(1);
  531. int result =
  532. chunked_upload_stream_->Read(io_buffer.get(), 1, callback.callback());
  533. EXPECT_EQ(result, net::ERR_IO_PENDING);
  534. write_pipe_.reset();
  535. base::RunLoop().RunUntilIdle();
  536. EXPECT_FALSE(callback.have_result());
  537. chunked_data_pipe_getter_.reset();
  538. EXPECT_EQ(net::ERR_FAILED, callback.GetResult(result));
  539. }
  540. TEST_F(ChunkedDataPipeUploadDataStreamTest, CloseBodyPipeBeforeCloseGetter3) {
  541. write_pipe_.reset();
  542. base::RunLoop().RunUntilIdle();
  543. chunked_data_pipe_getter_.reset();
  544. net::TestCompletionCallback callback;
  545. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(1);
  546. int result =
  547. chunked_upload_stream_->Read(io_buffer.get(), 1, callback.callback());
  548. EXPECT_EQ(net::ERR_FAILED, callback.GetResult(result));
  549. }
  550. // Test uses same order as CloseBodyPipeBeforeTruncation1, but in this test the
  551. // GetSizeCallback indicates too many bytes were received.
  552. TEST_F(ChunkedDataPipeUploadDataStreamTest, ExtraBytes1) {
  553. const std::string kData = "123";
  554. mojo::BlockingCopyFromString(kData, write_pipe_);
  555. std::string read_data;
  556. while (read_data.size() < kData.size()) {
  557. net::TestCompletionCallback callback;
  558. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(kData.size());
  559. int result = chunked_upload_stream_->Read(
  560. io_buffer.get(), io_buffer->size(), callback.callback());
  561. result = callback.GetResult(result);
  562. ASSERT_LT(0, result);
  563. read_data.append(std::string(io_buffer->data(), result));
  564. EXPECT_LE(read_data.size(), kData.size());
  565. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  566. }
  567. net::TestCompletionCallback callback;
  568. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(1);
  569. int result =
  570. chunked_upload_stream_->Read(io_buffer.get(), 1, callback.callback());
  571. EXPECT_EQ(result, net::ERR_IO_PENDING);
  572. std::move(get_size_callback_).Run(net::OK, kData.size() - 1);
  573. EXPECT_EQ(net::ERR_FAILED, callback.GetResult(result));
  574. }
  575. // Extra bytes are received after getting the size notification. No error should
  576. // be reported,
  577. TEST_F(ChunkedDataPipeUploadDataStreamTest, ExtraBytes2) {
  578. const std::string kData = "123";
  579. // Read first byte.
  580. mojo::BlockingCopyFromString(kData.substr(0, 1), write_pipe_);
  581. net::TestCompletionCallback callback;
  582. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(1);
  583. int result = chunked_upload_stream_->Read(io_buffer.get(), io_buffer->size(),
  584. callback.callback());
  585. result = callback.GetResult(result);
  586. ASSERT_EQ(1, result);
  587. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  588. // Start another read.
  589. net::TestCompletionCallback callback2;
  590. ASSERT_EQ(net::ERR_IO_PENDING, chunked_upload_stream_->Read(
  591. io_buffer.get(), 1, callback2.callback()));
  592. // Learn the size was only one byte. Read should complete, indicating the end
  593. // of the stream was reached.
  594. std::move(get_size_callback_).Run(net::OK, 1);
  595. EXPECT_EQ(net::OK, callback2.WaitForResult());
  596. EXPECT_TRUE(chunked_upload_stream_->IsEOF());
  597. // More data is copied to the stream unexpectedly. It should be ignored.
  598. mojo::BlockingCopyFromString(kData.substr(1), write_pipe_);
  599. base::RunLoop().RunUntilIdle();
  600. EXPECT_TRUE(chunked_upload_stream_->IsEOF());
  601. }
  602. TEST_F(ChunkedDataPipeUploadDataStreamTest, ClosePipeGetterBeforeInit) {
  603. chunked_data_pipe_getter_ = std::make_unique<TestChunkedDataPipeGetter>();
  604. chunked_upload_stream_ = std::make_unique<ChunkedDataPipeUploadDataStream>(
  605. base::MakeRefCounted<network::ResourceRequestBody>(),
  606. chunked_data_pipe_getter_->GetDataPipeGetterRemote());
  607. // Destroy the DataPipeGetter pipe, which is the pipe used for
  608. // GetSizeCallback.
  609. chunked_data_pipe_getter_->ClosePipe();
  610. base::RunLoop().RunUntilIdle();
  611. // Init should fail in this case.
  612. net::TestCompletionCallback callback;
  613. EXPECT_EQ(net::ERR_FAILED, chunked_upload_stream_->Init(
  614. callback.callback(), net::NetLogWithSource()));
  615. }
  616. TEST_F(ChunkedDataPipeUploadDataStreamTest,
  617. ClosePipeGetterWithoutCallingGetSizeCallbackNoPendingRead) {
  618. // Destroy the DataPipeGetter pipe, which is the pipe used for
  619. // GetSizeCallback.
  620. chunked_data_pipe_getter_->ClosePipe();
  621. base::RunLoop().RunUntilIdle();
  622. net::TestCompletionCallback callback;
  623. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(1);
  624. int result =
  625. chunked_upload_stream_->Read(io_buffer.get(), 1, callback.callback());
  626. EXPECT_EQ(net::ERR_FAILED, callback.GetResult(result));
  627. }
  628. TEST_F(ChunkedDataPipeUploadDataStreamTest,
  629. ClosePipeGetterWithoutCallingGetSizeCallbackPendingRead) {
  630. net::TestCompletionCallback callback;
  631. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(1);
  632. int result =
  633. chunked_upload_stream_->Read(io_buffer.get(), 1, callback.callback());
  634. EXPECT_EQ(net::ERR_IO_PENDING, result);
  635. // Destroy the DataPipeGetter pipe, which is the pipe used for
  636. // GetSizeCallback.
  637. chunked_data_pipe_getter_->ClosePipe();
  638. EXPECT_EQ(net::ERR_FAILED, callback.GetResult(result));
  639. }
  640. TEST_F(ChunkedDataPipeUploadDataStreamTest,
  641. ClosePipeGetterAfterCallingGetSizeCallback) {
  642. const char kData[] = "1";
  643. const int kDataLen = strlen(kData);
  644. net::TestCompletionCallback callback;
  645. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(kDataLen);
  646. std::move(get_size_callback_).Run(net::OK, kDataLen);
  647. // Destroy the DataPipeGetter pipe, which is the pipe used for
  648. // GetSizeCallback.
  649. chunked_data_pipe_getter_->ClosePipe();
  650. base::RunLoop().RunUntilIdle();
  651. int result = chunked_upload_stream_->Read(io_buffer.get(), kDataLen,
  652. callback.callback());
  653. EXPECT_EQ(net::ERR_IO_PENDING, result);
  654. mojo::BlockingCopyFromString(kData, write_pipe_);
  655. // Since the pipe was closed the GetSizeCallback was invoked, reading the
  656. // upload body should succeed once.
  657. EXPECT_EQ(kDataLen, callback.GetResult(result));
  658. EXPECT_TRUE(chunked_upload_stream_->IsEOF());
  659. net::TestCompletionCallback callback2;
  660. EXPECT_EQ(0, chunked_upload_stream_->Read(io_buffer.get(), kDataLen,
  661. callback2.callback()));
  662. // But trying again will result in failure.
  663. chunked_upload_stream_->Reset();
  664. net::TestCompletionCallback callback3;
  665. EXPECT_EQ(net::ERR_FAILED,
  666. chunked_upload_stream_->Init(callback3.callback(),
  667. net::NetLogWithSource()));
  668. }
  669. #define EXPECT_READ(chunked_upload_stream, io_buffer, expected) \
  670. { \
  671. int read_result = chunked_upload_stream->Read( \
  672. io_buffer.get(), io_buffer->size(), NoCallback()); \
  673. EXPECT_GT(read_result, 0); \
  674. EXPECT_EQ(std::string(io_buffer->data(), read_result), expected); \
  675. }
  676. #define EXPECT_EOF(chunked_upload_stream, size) \
  677. { \
  678. net::TestCompletionCallback test_callback; \
  679. auto one_byte_io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(1); \
  680. int read_result = chunked_upload_stream->Read( \
  681. one_byte_io_buffer.get(), 1u, test_callback.callback()); \
  682. EXPECT_EQ(net::ERR_IO_PENDING, read_result); \
  683. std::move(get_size_callback_).Run(net::OK, size); \
  684. EXPECT_EQ(net::OK, test_callback.GetResult(read_result)); \
  685. EXPECT_TRUE(chunked_upload_stream->IsEOF()); \
  686. }
  687. #define WRITE_DATA_SYNC(write_pipe, str) \
  688. { \
  689. std::string data(str); \
  690. uint32_t num_size = data.size(); \
  691. EXPECT_EQ(write_pipe->WriteData((void*)data.c_str(), &num_size, \
  692. MOJO_WRITE_DATA_FLAG_NONE), \
  693. MOJO_RESULT_OK); \
  694. EXPECT_EQ(num_size, data.size()); \
  695. }
  696. TEST_F(ChunkedDataPipeUploadDataStreamTest, CacheNotUsed) {
  697. chunked_upload_stream_->EnableCache();
  698. const std::string kData = "1234567890";
  699. WRITE_DATA_SYNC(write_pipe_, kData);
  700. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(7);
  701. EXPECT_READ(chunked_upload_stream_, io_buffer, "1234567");
  702. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  703. EXPECT_READ(chunked_upload_stream_, io_buffer, "890");
  704. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  705. EXPECT_EOF(chunked_upload_stream_, kData.size());
  706. }
  707. TEST_F(ChunkedDataPipeUploadDataStreamTest, CacheEnableBeforeInit1) {
  708. CreateChunkedUploadStream();
  709. chunked_upload_stream_->EnableCache();
  710. InitChunkedUploadStream();
  711. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(7);
  712. int result = chunked_upload_stream_->Read(io_buffer.get(), io_buffer->size(),
  713. NoCallback());
  714. EXPECT_EQ(result, net::ERR_IO_PENDING);
  715. // Destroy the DataPipeGetter pipe, which is the pipe used for
  716. // GetSizeCallback.
  717. chunked_data_pipe_getter_->ClosePipe();
  718. }
  719. TEST_F(ChunkedDataPipeUploadDataStreamTest, CacheEnableBeforeInit2) {
  720. CreateChunkedUploadStream();
  721. chunked_upload_stream_->EnableCache();
  722. InitChunkedUploadStream();
  723. WRITE_DATA_SYNC(write_pipe_, "1234567890");
  724. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(7);
  725. EXPECT_READ(chunked_upload_stream_, io_buffer, "1234567");
  726. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  727. EXPECT_EQ(chunked_upload_stream_->Init(NoCallback(), net::NetLogWithSource()),
  728. net::OK);
  729. EXPECT_READ(chunked_upload_stream_, io_buffer, "1234567");
  730. EXPECT_READ(chunked_upload_stream_, io_buffer, "890");
  731. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  732. EXPECT_EOF(chunked_upload_stream_, 10);
  733. }
  734. TEST_F(ChunkedDataPipeUploadDataStreamTest, CacheRead) {
  735. chunked_upload_stream_->EnableCache();
  736. WRITE_DATA_SYNC(write_pipe_, "1234567890");
  737. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(7);
  738. EXPECT_READ(chunked_upload_stream_, io_buffer, "1234567");
  739. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  740. EXPECT_EQ(chunked_upload_stream_->Init(NoCallback(), net::NetLogWithSource()),
  741. net::OK);
  742. EXPECT_READ(chunked_upload_stream_, io_buffer, "1234567");
  743. EXPECT_READ(chunked_upload_stream_, io_buffer, "890");
  744. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  745. EXPECT_EOF(chunked_upload_stream_, 10);
  746. }
  747. TEST_F(ChunkedDataPipeUploadDataStreamTest, CacheOverWindowOnce) {
  748. const size_t kMaxSize = 4u;
  749. chunked_upload_stream_->EnableCache(kMaxSize);
  750. WRITE_DATA_SYNC(write_pipe_, "1234567890");
  751. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(7);
  752. EXPECT_READ(chunked_upload_stream_, io_buffer, "1234567");
  753. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  754. EXPECT_EQ(chunked_upload_stream_->Init(NoCallback(), net::NetLogWithSource()),
  755. net::OK);
  756. EXPECT_READ(chunked_upload_stream_, io_buffer, "1234567");
  757. EXPECT_READ(chunked_upload_stream_, io_buffer, "890");
  758. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  759. EXPECT_EOF(chunked_upload_stream_, 10);
  760. }
  761. TEST_F(ChunkedDataPipeUploadDataStreamTest, CacheOverWindowTwice) {
  762. const size_t kMaxSize = 4u;
  763. chunked_upload_stream_->EnableCache(kMaxSize);
  764. WRITE_DATA_SYNC(write_pipe_, "1234567890");
  765. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(7);
  766. EXPECT_READ(chunked_upload_stream_, io_buffer, "1234567");
  767. EXPECT_READ(chunked_upload_stream_, io_buffer, "890");
  768. int result =
  769. chunked_upload_stream_->Init(NoCallback(), net::NetLogWithSource());
  770. EXPECT_EQ(net::ERR_FAILED, result);
  771. // Destroy the DataPipeGetter pipe, which is the pipe used for
  772. // GetSizeCallback.
  773. chunked_data_pipe_getter_->ClosePipe();
  774. }
  775. TEST_F(ChunkedDataPipeUploadDataStreamTest, CacheInitBeforeRead) {
  776. chunked_upload_stream_->EnableCache();
  777. WRITE_DATA_SYNC(write_pipe_, "1234567890");
  778. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(7);
  779. EXPECT_EQ(chunked_upload_stream_->Init(NoCallback(), net::NetLogWithSource()),
  780. net::OK);
  781. EXPECT_READ(chunked_upload_stream_, io_buffer, "1234567");
  782. EXPECT_READ(chunked_upload_stream_, io_buffer, "890");
  783. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  784. EXPECT_EOF(chunked_upload_stream_, 10);
  785. }
  786. TEST_F(ChunkedDataPipeUploadDataStreamTest, CacheInitWhileRead) {
  787. chunked_upload_stream_->EnableCache();
  788. WRITE_DATA_SYNC(write_pipe_, "1234567890");
  789. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(7);
  790. EXPECT_READ(chunked_upload_stream_, io_buffer, "1234567");
  791. io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(3);
  792. EXPECT_EQ(chunked_upload_stream_->Init(NoCallback(), net::NetLogWithSource()),
  793. net::OK);
  794. EXPECT_READ(chunked_upload_stream_, io_buffer, "123");
  795. EXPECT_READ(chunked_upload_stream_, io_buffer, "456");
  796. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  797. // Re-init in the middle of reading from the cache
  798. EXPECT_EQ(chunked_upload_stream_->Init(NoCallback(), net::NetLogWithSource()),
  799. net::OK);
  800. EXPECT_READ(chunked_upload_stream_, io_buffer, "123");
  801. EXPECT_READ(chunked_upload_stream_, io_buffer, "456");
  802. EXPECT_READ(chunked_upload_stream_, io_buffer, "7");
  803. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  804. // Re-init exactly after reading the last cached bit.
  805. EXPECT_EQ(chunked_upload_stream_->Init(NoCallback(), net::NetLogWithSource()),
  806. net::OK);
  807. EXPECT_READ(chunked_upload_stream_, io_buffer, "123");
  808. EXPECT_READ(chunked_upload_stream_, io_buffer, "456");
  809. EXPECT_READ(chunked_upload_stream_, io_buffer, "7");
  810. EXPECT_READ(chunked_upload_stream_, io_buffer, "890");
  811. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  812. // Reading "890" over the first cache should append "890" to the cache.
  813. io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(7);
  814. EXPECT_EQ(chunked_upload_stream_->Init(NoCallback(), net::NetLogWithSource()),
  815. net::OK);
  816. EXPECT_READ(chunked_upload_stream_, io_buffer, "1234567");
  817. EXPECT_READ(chunked_upload_stream_, io_buffer, "890");
  818. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  819. EXPECT_EOF(chunked_upload_stream_, 10);
  820. }
  821. TEST_F(ChunkedDataPipeUploadDataStreamTest, CacheReadAppendDataBeforeInit) {
  822. chunked_upload_stream_->EnableCache();
  823. WRITE_DATA_SYNC(write_pipe_, "1234567890");
  824. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(7);
  825. EXPECT_READ(chunked_upload_stream_, io_buffer, "1234567");
  826. EXPECT_READ(chunked_upload_stream_, io_buffer, "890");
  827. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  828. net::TestCompletionCallback callback;
  829. int result = chunked_upload_stream_->Read(io_buffer.get(), io_buffer->size(),
  830. callback.callback());
  831. EXPECT_EQ(net::ERR_IO_PENDING, result);
  832. WRITE_DATA_SYNC(write_pipe_, "abc");
  833. EXPECT_EQ(chunked_upload_stream_->Init(NoCallback(), net::NetLogWithSource()),
  834. net::OK);
  835. // Wait mojo.
  836. base::RunLoop().RunUntilIdle();
  837. // We should not receive the second mojo data.
  838. EXPECT_FALSE(callback.have_result());
  839. EXPECT_READ(chunked_upload_stream_, io_buffer, "1234567");
  840. EXPECT_READ(chunked_upload_stream_, io_buffer, "890");
  841. EXPECT_READ(chunked_upload_stream_, io_buffer, "abc");
  842. EXPECT_EOF(chunked_upload_stream_, 13);
  843. }
  844. TEST_F(ChunkedDataPipeUploadDataStreamTest, CacheReadAppendDataAfterInit) {
  845. chunked_upload_stream_->EnableCache();
  846. WRITE_DATA_SYNC(write_pipe_, "1234567890");
  847. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(7);
  848. EXPECT_READ(chunked_upload_stream_, io_buffer, "1234567");
  849. EXPECT_READ(chunked_upload_stream_, io_buffer, "890");
  850. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  851. net::TestCompletionCallback callback;
  852. int result = chunked_upload_stream_->Read(io_buffer.get(), io_buffer->size(),
  853. callback.callback());
  854. EXPECT_EQ(net::ERR_IO_PENDING, result);
  855. EXPECT_EQ(chunked_upload_stream_->Init(NoCallback(), net::NetLogWithSource()),
  856. net::OK);
  857. WRITE_DATA_SYNC(write_pipe_, "abc");
  858. // Wait mojo.
  859. base::RunLoop().RunUntilIdle();
  860. // We should not receive the second mojo data.
  861. EXPECT_FALSE(callback.have_result());
  862. EXPECT_READ(chunked_upload_stream_, io_buffer, "1234567");
  863. EXPECT_READ(chunked_upload_stream_, io_buffer, "890");
  864. EXPECT_READ(chunked_upload_stream_, io_buffer, "abc");
  865. EXPECT_EOF(chunked_upload_stream_, 13);
  866. }
  867. TEST_F(ChunkedDataPipeUploadDataStreamTest, CacheReadAppendDataDuringRead) {
  868. chunked_upload_stream_->EnableCache();
  869. WRITE_DATA_SYNC(write_pipe_, "1234567890");
  870. auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(7);
  871. EXPECT_READ(chunked_upload_stream_, io_buffer, "1234567");
  872. EXPECT_READ(chunked_upload_stream_, io_buffer, "890");
  873. EXPECT_FALSE(chunked_upload_stream_->IsEOF());
  874. net::TestCompletionCallback callback;
  875. int result = chunked_upload_stream_->Read(io_buffer.get(), io_buffer->size(),
  876. callback.callback());
  877. EXPECT_EQ(net::ERR_IO_PENDING, result);
  878. EXPECT_EQ(chunked_upload_stream_->Init(NoCallback(), net::NetLogWithSource()),
  879. net::OK);
  880. // We should not receive the second mojo data.
  881. EXPECT_FALSE(callback.have_result());
  882. EXPECT_READ(chunked_upload_stream_, io_buffer, "1234567");
  883. WRITE_DATA_SYNC(write_pipe_, "abc");
  884. // Wait mojo.
  885. base::RunLoop().RunUntilIdle();
  886. EXPECT_READ(chunked_upload_stream_, io_buffer, "890");
  887. EXPECT_READ(chunked_upload_stream_, io_buffer, "abc");
  888. EXPECT_EOF(chunked_upload_stream_, 13);
  889. }
  890. TEST_F(ChunkedDataPipeUploadDataStreamTest, ErrorAndDetach) {
  891. chunked_data_pipe_getter_ = std::make_unique<TestChunkedDataPipeGetter>();
  892. chunked_upload_stream_ = std::make_unique<ChunkedDataPipeUploadDataStream>(
  893. base::MakeRefCounted<network::ResourceRequestBody>(),
  894. chunked_data_pipe_getter_->GetDataPipeGetterRemote());
  895. get_size_callback_ = chunked_data_pipe_getter_->WaitForGetSize();
  896. std::move(get_size_callback_).Run(net::ERR_FAILED, 0);
  897. base::RunLoop().RunUntilIdle();
  898. chunked_data_pipe_getter_->ClosePipe();
  899. base::RunLoop().RunUntilIdle();
  900. }
  901. } // namespace
  902. } // namespace network