Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(606)

Unified Diff: third_party/WebKit/Source/core/fetch/MultipartImageResourceParserTest.cpp

Issue 1693183002: Move multipart resource handling to core/fetch (1/2) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@multipart-cleanup-preliminary
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/fetch/MultipartImageResourceParserTest.cpp
diff --git a/third_party/WebKit/Source/core/fetch/MultipartImageResourceParserTest.cpp b/third_party/WebKit/Source/core/fetch/MultipartImageResourceParserTest.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..fdcc630fedab4fb73b1283ce9403c96b9c78c863
--- /dev/null
+++ b/third_party/WebKit/Source/core/fetch/MultipartImageResourceParserTest.cpp
@@ -0,0 +1,419 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "core/fetch/MultipartImageResourceParser.h"
+
+#include "platform/network/ResourceResponse.h"
+#include "public/platform/WebURL.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+#include <stddef.h>
+#include <stdint.h>
+#include <string.h>
+
+namespace blink {
+
+namespace {
+
+String toString(const Vector<char>& data)
+{
+ if (data.isEmpty())
+ return String("");
+ return String(data.data(), data.size());
+}
+
+class MultipartResponseTest : public testing::Test {
+};
+
+class MockClient final : public NoBaseWillBeGarbageCollectedFinalized<MockClient>, public MultipartImageResourceParser::Client {
+ WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(MockClient);
+
+public:
+ void didReceiveResponse(const ResourceResponse& response) override
+ {
+ m_responses.append(response);
+ m_data.append(Vector<char>());
+ }
+ void didReceiveData(const char* bytes, size_t size) override
+ {
+ m_data.last().append(bytes, size);
+ }
+
+ Vector<ResourceResponse> m_responses;
+ Vector<Vector<char>> m_data;
hiroshige 2016/02/25 21:36:27 nit: These tests do not test how many times didRec
yhirano 2016/02/26 22:21:51 I think it's OK because ImageResource doesn't upda
hiroshige 2016/02/29 16:57:07 Acknowledged.
+};
+
+// We can't put this in an anonymous function because it's a friend class for
+// access to private members.
hiroshige 2016/02/25 21:36:27 nit: We can remove this comment as we no longer ha
yhirano 2016/02/26 22:21:51 Done.
+TEST(MultipartResponseTest, PushOverLine)
+{
+ struct {
+ const char* input;
+ const int position;
+ const int expected;
+ } lineTests[] = {
+ { "Line", 0, 0 },
+ { "Line", 2, 0 },
+ { "Line", 10, 0 },
+ { "\r\nLine", 0, 2 },
+ { "\nLine", 0, 1 },
+ { "\n\nLine", 0, 2 },
+ { "\rLine", 0, 1 },
+ { "Line\r\nLine", 4, 2 },
+ { "Line\nLine", 4, 1 },
+ { "Line\n\nLine", 4, 2 },
+ { "Line\rLine", 4, 1 },
+ { "Line\r\rLine", 4, 1 },
+ };
+ for (size_t i = 0; i < WTF_ARRAY_LENGTH(lineTests); ++i) {
+ Vector<char> input;
+ input.append(lineTests[i].input, strlen(lineTests[i].input));
+ EXPECT_EQ(lineTests[i].expected,
+ MultipartImageResourceParser::pushOverLineForTest(input, lineTests[i].position));
+ }
+}
+
+TEST(MultipartResponseTest, FindBoundary)
+{
+ struct {
+ const char* boundary;
+ const char* data;
+ const size_t position;
+ } boundaryTests[] = {
+ { "bound", "bound", 0 },
+ { "bound", "--bound", 0 },
+ { "bound", "junkbound", 4 },
+ { "bound", "junk--bound", 4 },
+ { "foo", "bound", kNotFound },
+ { "bound", "--boundbound", 0 },
+ };
+
+ for (size_t i = 0; i < WTF_ARRAY_LENGTH(boundaryTests); ++i) {
+ Vector<char> boundary, data;
+ boundary.append(boundaryTests[i].boundary, strlen(boundaryTests[i].boundary));
+ data.append(boundaryTests[i].data, strlen(boundaryTests[i].data));
+ EXPECT_EQ(boundaryTests[i].position, MultipartImageResourceParser::findBoundaryForTest(data, &boundary));
+ }
+}
+
+TEST(MultipartResponseTest, NoStartBoundary)
+{
+ ResourceResponse response;
+ response.setMimeType("multipart/x-mixed-replace");
+ response.setHTTPHeaderField("Foo", "Bar");
+ response.setHTTPHeaderField("Content-type", "text/plain");
+ MockClient* client = new MockClient;
+ Vector<char> boundary;
+ boundary.append("bound", 5);
+
+ MultipartImageResourceParser* parser = new MultipartImageResourceParser(response, boundary, client);
+ const char data[] =
+ "Content-type: text/plain\n\n"
+ "This is a sample response\n"
+ "--bound--"
+ "ignore junk after end token --bound\n\nTest2\n";
+ parser->addData(data, strlen(data));
+ ASSERT_EQ(1u, client->m_responses.size());
+ ASSERT_EQ(1u, client->m_data.size());
+ EXPECT_EQ("This is a sample response", toString(client->m_data[0]));
+
+ parser->finish();
+ ASSERT_EQ(1u, client->m_responses.size());
+ ASSERT_EQ(1u, client->m_data.size());
+ EXPECT_EQ("This is a sample response", toString(client->m_data[0]));
+}
+
+TEST(MultipartResponseTest, NoEndBoundary)
+{
+ ResourceResponse response;
+ response.setMimeType("multipart/x-mixed-replace");
+ response.setHTTPHeaderField("Foo", "Bar");
+ response.setHTTPHeaderField("Content-type", "text/plain");
+ MockClient* client = new MockClient;
+ Vector<char> boundary;
+ boundary.append("bound", 5);
+
+ MultipartImageResourceParser* parser = new MultipartImageResourceParser(response, boundary, client);
+ const char data[] =
+ "bound\nContent-type: text/plain\n\n"
+ "This is a sample response\n";
+ parser->addData(data, strlen(data));
+ ASSERT_EQ(1u, client->m_responses.size());
+ ASSERT_EQ(1u, client->m_data.size());
+ EXPECT_EQ("This is a sample response\n", toString(client->m_data[0]));
+
+ parser->finish();
+ ASSERT_EQ(1u, client->m_responses.size());
+ ASSERT_EQ(1u, client->m_data.size());
+ EXPECT_EQ("This is a sample response\n", toString(client->m_data[0]));
+}
+
+TEST(MultipartResponseTest, NoStartAndEndBoundary)
+{
+ ResourceResponse response;
+ response.setMimeType("multipart/x-mixed-replace");
+ response.setHTTPHeaderField("Foo", "Bar");
+ response.setHTTPHeaderField("Content-type", "text/plain");
+ MockClient* client = new MockClient;
+ Vector<char> boundary;
+ boundary.append("bound", 5);
+
+ MultipartImageResourceParser* parser = new MultipartImageResourceParser(response, boundary, client);
+ const char data[] =
+ "Content-type: text/plain\n\n"
+ "This is a sample response\n";
+ parser->addData(data, strlen(data));
+ ASSERT_EQ(1u, client->m_responses.size());
+ ASSERT_EQ(1u, client->m_data.size());
+ EXPECT_EQ("This is a sample response\n", toString(client->m_data[0]));
+
+ parser->finish();
+ ASSERT_EQ(1u, client->m_responses.size());
+ ASSERT_EQ(1u, client->m_data.size());
+ EXPECT_EQ("This is a sample response\n", toString(client->m_data[0]));
+}
+
+TEST(MultipartResponseTest, MalformedBoundary)
+{
+ // Some servers send a boundary that is prefixed by "--". See bug 5786.
+ ResourceResponse response;
+ response.setMimeType("multipart/x-mixed-replace");
+ response.setHTTPHeaderField("Foo", "Bar");
+ response.setHTTPHeaderField("Content-type", "text/plain");
+ MockClient* client = new MockClient;
+ Vector<char> boundary;
+ boundary.append("bound", 5);
hiroshige 2016/02/25 21:36:27 Perhaps this should be "--bound"? (Line 289 of con
yhirano 2016/02/26 22:21:51 Done.
+
+ MultipartImageResourceParser* parser = new MultipartImageResourceParser(response, boundary, client);
+ const char data[] =
+ "--bound\n"
+ "Content-type: text/plain\n\n"
+ "This is a sample response\n"
+ "--bound--"
+ "ignore junk after end token --bound\n\nTest2\n";
+ parser->addData(data, strlen(data));
+ ASSERT_EQ(1u, client->m_responses.size());
+ ASSERT_EQ(1u, client->m_data.size());
+ EXPECT_EQ("This is a sample response", toString(client->m_data[0]));
+
+ parser->finish();
+ ASSERT_EQ(1u, client->m_responses.size());
+ ASSERT_EQ(1u, client->m_data.size());
+ EXPECT_EQ("This is a sample response", toString(client->m_data[0]));
+}
+
+// Used in for tests that break the data in various places.
+struct TestChunk {
+ const int startPosition; // offset in data
+ const int endPosition; // end offset in data
+ const size_t expectedResponses;
+ const char* expectedData;
+};
+
+void VariousChunkSizesTest(const TestChunk chunks[], int chunks_size,
hiroshige 2016/02/25 21:36:27 nit: |variousChunkSizesTest()|, |chunksSize|, |rec
yhirano 2016/02/26 22:21:51 Done.
+ size_t responses, int received_data,
+ const char* completedData)
+{
+ const char data[] =
+ "--bound\n" // 0-7
+ "Content-type: image/png\n\n" // 8-32
+ "datadatadatadatadata" // 33-52
+ "--bound\n" // 53-60
+ "Content-type: image/jpg\n\n" // 61-85
+ "foofoofoofoofoo" // 86-100
+ "--bound--"; // 101-109
+
+ ResourceResponse response;
+ response.setMimeType("multipart/x-mixed-replace");
+ MockClient* client = new MockClient;
+ Vector<char> boundary;
+ boundary.append("bound", 5);
+
+ MultipartImageResourceParser* parser = new MultipartImageResourceParser(response, boundary, client);
+
+ for (int i = 0; i < chunks_size; ++i) {
+ ASSERT_TRUE(chunks[i].startPosition < chunks[i].endPosition);
hiroshige 2016/02/25 21:36:27 nit: ASSERT_LE() can be used.
yhirano 2016/02/26 22:21:51 Done.
+ parser->addData(data + chunks[i].startPosition, chunks[i].endPosition - chunks[i].startPosition);
+ EXPECT_EQ(chunks[i].expectedResponses, client->m_responses.size());
+ EXPECT_EQ(String(chunks[i].expectedData), client->m_data.size() > 0 ? toString(client->m_data.last()) : String(""));
+ }
+ // Check final state
+ parser->finish();
+ EXPECT_EQ(responses, client->m_responses.size());
+ EXPECT_EQ(completedData, toString(client->m_data.last()));
+}
+
+template <size_t N>
+void VariousChunkSizesTest(const TestChunk (&chunks)[N], size_t responses, int received_data, const char* completedData)
hiroshige 2016/02/25 21:36:27 ditto > Line 214.
yhirano 2016/02/26 22:21:51 Done.
+{
+ VariousChunkSizesTest(chunks, N, responses, received_data, completedData);
+}
+
+TEST(MultipartResponseTest, BreakInBoundary)
+{
+ // Break in the first boundary
+ const TestChunk bound1[] = {
+ { 0, 4, 0, "" },
+ { 4, 110, 2, "foofoofoofoofoo" },
+ };
+ VariousChunkSizesTest(bound1, 2, 2, "foofoofoofoofoo");
+
+ // Break in first and second
+ const TestChunk bound2[] = {
+ { 0, 4, 0, "" },
+ { 4, 55, 1, "datadatadatadat" },
+ { 55, 65, 1, "datadatadatadatadata" },
+ { 65, 110, 2, "foofoofoofoofoo" },
+ };
+ VariousChunkSizesTest(bound2, 2, 3, "foofoofoofoofoo");
+
+ // Break in second only
+ const TestChunk bound3[] = {
+ { 0, 55, 1, "datadatadatadat" },
+ { 55, 110, 2, "foofoofoofoofoo" },
+ };
+ VariousChunkSizesTest(bound3, 2, 3, "foofoofoofoofoo");
+}
+
+TEST(MultipartResponseTest, BreakInHeaders)
+{
+ // Break in first header
+ const TestChunk header1[] = {
+ { 0, 10, 0, "" },
+ { 10, 35, 1, "" },
+ { 35, 110, 2, "foofoofoofoofoo" },
+ };
+ VariousChunkSizesTest(header1, 2, 2, "foofoofoofoofoo");
+
+ // Break in both headers
+ const TestChunk header2[] = {
+ { 0, 10, 0, "" },
+ { 10, 65, 1, "datadatadatadatadata" },
+ { 65, 110, 2, "foofoofoofoofoo" },
+ };
+ VariousChunkSizesTest(header2, 2, 2, "foofoofoofoofoo");
+
+ // Break at end of a header
+ const TestChunk header3[] = {
+ { 0, 33, 1, "" },
+ { 33, 65, 1, "datadatadatadatadata" },
+ { 65, 110, 2, "foofoofoofoofoo" },
+ };
+ VariousChunkSizesTest(header3, 2, 2, "foofoofoofoofoo");
+}
+
+TEST(MultipartResponseTest, BreakInData)
+{
+ // All data as one chunk
+ const TestChunk data1[] = {
+ { 0, 110, 2, "foofoofoofoofoo" },
+ };
+ VariousChunkSizesTest(data1, 2, 2, "foofoofoofoofoo");
+
+ // breaks in data segment
+ const TestChunk data2[] = {
+ { 0, 35, 1, "" },
+ { 35, 65, 1, "datadatadatadatadata" },
+ { 65, 90, 2, "" },
+ { 90, 110, 2, "foofoofoofoofoo" },
+ };
+ VariousChunkSizesTest(data2, 2, 2, "foofoofoofoofoo");
+
+ // Incomplete send
+ const TestChunk data3[] = {
+ { 0, 35, 1, "" },
+ { 35, 90, 2, "" },
+ };
+ VariousChunkSizesTest(data3, 2, 2, "foof");
+}
+
+TEST(MultipartResponseTest, SmallChunk)
+{
+ ResourceResponse response;
+ response.setMimeType("multipart/x-mixed-replace");
+ response.setHTTPHeaderField("Content-type", "text/plain");
+ MockClient* client = new MockClient;
+ Vector<char> boundary;
+ boundary.append("bound", 5);
+
+ MultipartImageResourceParser* parser = new MultipartImageResourceParser(response, boundary, client);
+
+ // Test chunks of size 1, 2, and 0.
+ const char data[] =
+ "--boundContent-type: text/plain\n\n"
+ "\n--boundContent-type: text/plain\n\n"
+ "\n\n--boundContent-type: text/plain\n\n"
+ "--boundContent-type: text/plain\n\n"
+ "end--bound--";
+ parser->addData(data, strlen(data));
+ ASSERT_EQ(4u, client->m_responses.size());
+ ASSERT_EQ(4u, client->m_data.size());
+ EXPECT_EQ("", toString(client->m_data[0]));
+ EXPECT_EQ("\n", toString(client->m_data[1]));
+ EXPECT_EQ("", toString(client->m_data[2]));
+ EXPECT_EQ("end", toString(client->m_data[3]));
+
+ parser->finish();
+ ASSERT_EQ(4u, client->m_responses.size());
+ ASSERT_EQ(4u, client->m_data.size());
+ EXPECT_EQ("", toString(client->m_data[0]));
+ EXPECT_EQ("\n", toString(client->m_data[1]));
+ EXPECT_EQ("", toString(client->m_data[2]));
+ EXPECT_EQ("end", toString(client->m_data[3]));
+}
+
+TEST(MultipartResponseTest, MultipleBoundaries)
+{
+ // Test multiple boundaries back to back
+ ResourceResponse response;
+ response.setMimeType("multipart/x-mixed-replace");
+ MockClient* client = new MockClient;
+ Vector<char> boundary;
+ boundary.append("bound", 5);
+
+ MultipartImageResourceParser* parser = new MultipartImageResourceParser(response, boundary, client);
+
+ const char data[] = "--bound\r\n\r\n--bound\r\n\r\nfoofoo--bound--";
+ parser->addData(data, strlen(data));
+ ASSERT_EQ(2u, client->m_responses.size());
+ ASSERT_EQ(2u, client->m_data.size());
+ EXPECT_EQ("", toString(client->m_data[0]));
+ EXPECT_EQ("foofoo", toString(client->m_data[1]));
+}
+
+TEST(MultipartResponseTest, MultipartPayloadSet)
+{
+ ResourceResponse response;
+ response.setMimeType("multipart/x-mixed-replace");
+ MockClient* client = new MockClient;
+ Vector<char> boundary;
+ boundary.append("bound", 5);
+
+ MultipartImageResourceParser* parser = new MultipartImageResourceParser(response, boundary, client);
+
+ const char data[] =
+ "--bound\n"
+ "Content-type: text/plain\n\n"
+ "response data\n"
+ "--bound\n";
+ parser->addData(data, strlen(data));
+ ASSERT_EQ(1u, client->m_responses.size());
+ ASSERT_EQ(1u, client->m_data.size());
+ EXPECT_EQ("response data", toString(client->m_data[0]));
+ EXPECT_FALSE(client->m_responses[0].isMultipartPayload());
+
+ const char data2[] =
+ "Content-type: text/plain\n\n"
+ "response data2\n"
+ "--bound\n";
+ parser->addData(data2, strlen(data2));
+ ASSERT_EQ(2u, client->m_responses.size());
+ ASSERT_EQ(2u, client->m_data.size());
+ EXPECT_EQ("response data2", toString(client->m_data[1]));
+ EXPECT_TRUE(client->m_responses[1].isMultipartPayload());
+}
+
+} // namespace
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698