summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/frame/mhtml_archive_test.cc
blob: ad98278d5a74087006978dfa967f7de263f8346c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
 * Copyright (C) 2014 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "third_party/blink/renderer/platform/mhtml/mhtml_archive.h"

#include "base/test/metrics/histogram_tester.h"
#include "build/build_config.h"
#include "services/network/public/cpp/is_potentially_trustworthy.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/loader/mhtml_load_result.mojom-blink.h"
#include "third_party/blink/renderer/platform/mhtml/mhtml_parser.h"
#include "third_party/blink/renderer/platform/mhtml/serialized_resource.h"
#include "third_party/blink/renderer/platform/testing/testing_platform_support.h"
#include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"
#include "third_party/blink/renderer/platform/testing/url_test_helpers.h"
#include "third_party/blink/renderer/platform/text/date_components.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/weborigin/scheme_registry.h"
#include "third_party/blink/renderer/platform/wtf/shared_buffer.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"

using blink::mojom::MHTMLLoadResult;
using blink::url_test_helpers::ToKURL;

namespace blink {
namespace test {

namespace {

const char kEndOfPartBoundary[] = "--boundary-example";
const char kEndOfDocumentBoundary[] = "--boundary-example--";

}  // namespace

class MHTMLArchiveTest : public testing::Test {
 public:
  MHTMLArchiveTest() {
    file_path_ = test::CoreTestDataPath("frameserializer/css/");
    mhtml_date_ = base::Time::FromJsTime(1520551829000);
    mhtml_date_header_ = String::FromUTF8("Thu, 8 Mar 2018 23:30:29 -0000");
  }

 protected:
  void AddResource(const char* url,
                   const char* mime,
                   scoped_refptr<SharedBuffer> data) {
    SerializedResource resource(ToKURL(url), mime, std::move(data));
    resources_.push_back(resource);
  }

  void AddResource(const char* url, const char* mime, const char* file_name) {
    AddResource(url, mime, ReadFile(file_name));
  }

  void AddTestMainResource() {
    AddResource("http://www.test.com", "text/html", "css_test_page.html");
  }

  void AddTestResources() {
    AddResource("http://www.test.com", "text/html", "css_test_page.html");
    AddResource("http://www.test.com/link_styles.css", "text/css",
                "link_styles.css");
    AddResource("http://www.test.com/import_style_from_link.css", "text/css",
                "import_style_from_link.css");
    AddResource("http://www.test.com/import_styles.css", "text/css",
                "import_styles.css");
    AddResource("http://www.test.com/red_background.png", "image/png",
                "red_background.png");
    AddResource("http://www.test.com/orange_background.png", "image/png",
                "orange_background.png");
    AddResource("http://www.test.com/yellow_background.png", "image/png",
                "yellow_background.png");
    AddResource("http://www.test.com/green_background.png", "image/png",
                "green_background.png");
    AddResource("http://www.test.com/blue_background.png", "image/png",
                "blue_background.png");
    AddResource("http://www.test.com/purple_background.png", "image/png",
                "purple_background.png");
    AddResource("http://www.test.com/ul-dot.png", "image/png", "ul-dot.png");
    AddResource("http://www.test.com/ol-dot.png", "image/png", "ol-dot.png");
  }

  HashMap<String, String> ExtractHeaders(LineReader& line_reader) {
    // Read the data per line until reaching the empty line.
    HashMap<String, String> mhtml_headers;
    String line;
    line_reader.GetNextLine(&line);
    while (line.length()) {
      StringBuilder builder;
      builder.Append(line);

      // Peek next line to see if it starts with soft line break. If yes, append
      // to current line.
      String next_line;
      while (true) {
        line_reader.GetNextLine(&next_line);
        if (next_line.length() > 1 &&
            (next_line[0] == ' ' || next_line[0] == '\t')) {
          builder.Append(next_line, 1, next_line.length() - 1);
          continue;
        }
        break;
      }

      line = builder.ToString();
      wtf_size_t pos = line.Find(":");
      if (pos == kNotFound)
        continue;
      String key = line.Substring(0, pos);
      String value = line.Substring(pos + 2);
      mhtml_headers.insert(key, value);

      line = next_line;
    }
    return mhtml_headers;
  }

  HashMap<String, String> ExtractMHTMLHeaders() {
    LineReader line_reader(String(mhtml_data_.data(), mhtml_data_.size()));
    return ExtractHeaders(line_reader);
  }

  void GenerateMHTMLData(const Vector<SerializedResource>& resources,
                         MHTMLArchive::EncodingPolicy encoding_policy,
                         const KURL& url,
                         const String& title,
                         const String& mime_type,
                         bool validate) {
    // This boundary is as good as any other.  Plus it gets used in almost
    // all the examples in the MHTML spec - RFC 2557.
    String boundary = String::FromUTF8("boundary-example");

    MHTMLArchive::GenerateMHTMLHeader(boundary, url, title, mime_type,
                                      mhtml_date_, mhtml_data_);
    for (const auto& resource : resources) {
      MHTMLArchive::GenerateMHTMLPart(boundary, String(), encoding_policy,
                                      resource, mhtml_data_);
    }
    MHTMLArchive::GenerateMHTMLFooterForTesting(boundary, mhtml_data_);

    if (validate) {
      // Validate the generated MHTML.
      MHTMLParser parser(
          SharedBuffer::Create(mhtml_data_.data(), mhtml_data_.size()));
      EXPECT_FALSE(parser.ParseArchive().IsEmpty())
          << "Generated MHTML is malformed";
    }
  }

  void Serialize(const KURL& url,
                 const String& title,
                 const String& mime,
                 MHTMLArchive::EncodingPolicy encoding_policy) {
    return GenerateMHTMLData(resources_, encoding_policy, url, title, mime,
                             true);
  }

  Vector<char>& mhtml_data() { return mhtml_data_; }

  base::Time mhtml_date() const { return mhtml_date_; }
  const String& mhtml_date_header() const { return mhtml_date_header_; }

  void CheckLoadResult(const KURL url,
                       scoped_refptr<const SharedBuffer> data,
                       MHTMLLoadResult expected_result) {
    // Set up histogram testing (takes snapshot of histogram data).
    base::HistogramTester histogram_tester;

    // Attempt loading the archive and check the returned pointer.
    MHTMLArchive* archive = MHTMLArchive::Create(url, data);
    ASSERT_TRUE(archive);

    EXPECT_EQ(archive->LoadResult(), expected_result);

    // Check that the correct count, and only the correct count, increased.
    histogram_tester.ExpectUniqueSample(
        "PageSerialization.MhtmlLoading.LoadResult", expected_result, 1);
  }

 private:
  scoped_refptr<SharedBuffer> ReadFile(const char* file_name) {
    String file_path = file_path_ + file_name;
    return test::ReadFromFile(file_path);
  }

  String file_path_;
  Vector<SerializedResource> resources_;
  Vector<char> mhtml_data_;
  base::Time mhtml_date_;
  String mhtml_date_header_;
};

TEST_F(MHTMLArchiveTest,
       TestMHTMLHeadersWithTitleContainingAllPrintableCharacters) {
  const char kURL[] = "http://www.example.com/";
  const char kTitle[] = "abc";
  AddTestMainResource();
  Serialize(ToKURL(kURL), String::FromUTF8(kTitle), "text/html",
            MHTMLArchive::kUseDefaultEncoding);

  HashMap<String, String> mhtml_headers = ExtractMHTMLHeaders();

  EXPECT_EQ("<Saved by Blink>", mhtml_headers.find("From")->value);
  EXPECT_FALSE(mhtml_headers.find("Date")->value.IsEmpty());
  EXPECT_EQ(
      "multipart/related;type=\"text/html\";boundary=\"boundary-example\"",
      mhtml_headers.find("Content-Type")->value);
  EXPECT_EQ("abc", mhtml_headers.find("Subject")->value);
  EXPECT_EQ(kURL, mhtml_headers.find("Snapshot-Content-Location")->value);
}

TEST_F(MHTMLArchiveTest,
       TestMHTMLHeadersWithTitleContainingNonPrintableCharacters) {
  const char kURL[] = "http://www.example.com/";
  const char kTitle[] = "abc \t=\xe2\x98\x9d\xf0\x9f\x8f\xbb";
  AddTestMainResource();
  Serialize(ToKURL(kURL), String::FromUTF8(kTitle), "text/html",
            MHTMLArchive::kUseDefaultEncoding);

  HashMap<String, String> mhtml_headers = ExtractMHTMLHeaders();

  EXPECT_EQ("<Saved by Blink>", mhtml_headers.find("From")->value);
  EXPECT_FALSE(mhtml_headers.find("Date")->value.IsEmpty());
  EXPECT_EQ(
      "multipart/related;type=\"text/html\";boundary=\"boundary-example\"",
      mhtml_headers.find("Content-Type")->value);
  EXPECT_EQ("=?utf-8?Q?abc=20=09=3D=E2=98=9D=F0=9F=8F=BB?=",
            mhtml_headers.find("Subject")->value);
  EXPECT_EQ(kURL, mhtml_headers.find("Snapshot-Content-Location")->value);
}

TEST_F(MHTMLArchiveTest,
       TestMHTMLHeadersWithLongTitleContainingNonPrintableCharacters) {
  const char kURL[] = "http://www.example.com/";
  const char kTitle[] =
      "01234567890123456789012345678901234567890123456789"
      "01234567890123456789012345678901234567890123456789"
      " \t=\xe2\x98\x9d\xf0\x9f\x8f\xbb";
  AddTestMainResource();
  Serialize(ToKURL(kURL), String::FromUTF8(kTitle), "text/html",
            MHTMLArchive::kUseDefaultEncoding);

  HashMap<String, String> mhtml_headers = ExtractMHTMLHeaders();

  EXPECT_EQ("<Saved by Blink>", mhtml_headers.find("From")->value);
  EXPECT_FALSE(mhtml_headers.find("Date")->value.IsEmpty());
  EXPECT_EQ(
      "multipart/related;type=\"text/html\";boundary=\"boundary-example\"",
      mhtml_headers.find("Content-Type")->value);
  EXPECT_EQ(
      "=?utf-8?Q?012345678901234567890123456789"
      "012345678901234567890123456789012?="
      "=?utf-8?Q?345678901234567890123456789"
      "0123456789=20=09=3D=E2=98=9D=F0=9F?="
      "=?utf-8?Q?=8F=BB?=",
      mhtml_headers.find("Subject")->value);
  EXPECT_EQ(kURL, mhtml_headers.find("Snapshot-Content-Location")->value);
}

TEST_F(MHTMLArchiveTest, TestMHTMLPartsWithBinaryEncoding) {
  const char kURL[] = "http://www.example.com";
  AddTestResources();
  Serialize(ToKURL(kURL), "Test Serialization", "text/html",
            MHTMLArchive::kUseBinaryEncoding);

  // Read the MHTML data line per line and do some pseudo-parsing to make sure
  // the right encoding is used for the different sections.
  LineReader line_reader(String(mhtml_data().data(), mhtml_data().size()));
  int part_count = 0;
  String line, last_line;
  while (line_reader.GetNextLine(&line)) {
    last_line = line;
    if (line != kEndOfPartBoundary)
      continue;
    part_count++;

    HashMap<String, String> part_headers = ExtractHeaders(line_reader);
    EXPECT_FALSE(part_headers.find("Content-Type")->value.IsEmpty());
    EXPECT_EQ("binary", part_headers.find("Content-Transfer-Encoding")->value);
    EXPECT_FALSE(part_headers.find("Content-Location")->value.IsEmpty());
  }
  EXPECT_EQ(12, part_count);

  // Last line should be the end-of-document boundary.
  EXPECT_EQ(kEndOfDocumentBoundary, last_line);
}

TEST_F(MHTMLArchiveTest, TestMHTMLPartsWithDefaultEncoding) {
  const char kURL[] = "http://www.example.com";
  AddTestResources();
  Serialize(ToKURL(kURL), "Test Serialization", "text/html",
            MHTMLArchive::kUseDefaultEncoding);

  // Read the MHTML data line per line and do some pseudo-parsing to make sure
  // the right encoding is used for the different sections.
  LineReader line_reader(String(mhtml_data().data(), mhtml_data().size()));
  int part_count = 0;
  String line, last_line;
  while (line_reader.GetNextLine(&line)) {
    last_line = line;
    if (line != kEndOfPartBoundary)
      continue;
    part_count++;

    HashMap<String, String> part_headers = ExtractHeaders(line_reader);

    String content_type = part_headers.find("Content-Type")->value;
    EXPECT_FALSE(content_type.IsEmpty());

    String encoding = part_headers.find("Content-Transfer-Encoding")->value;
    EXPECT_FALSE(encoding.IsEmpty());

    if (content_type.StartsWith("text/"))
      EXPECT_EQ("quoted-printable", encoding);
    else if (content_type.StartsWith("image/"))
      EXPECT_EQ("base64", encoding);
    else
      FAIL() << "Unexpected Content-Type: " << content_type;
  }
  EXPECT_EQ(12, part_count);

  // Last line should be the end-of-document boundary.
  EXPECT_EQ(kEndOfDocumentBoundary, last_line);
}

TEST_F(MHTMLArchiveTest, MHTMLFromScheme) {
  const char kURL[] = "http://www.example.com";
  AddTestMainResource();
  Serialize(ToKURL(kURL), "Test Serialization", "text/html",
            MHTMLArchive::kUseDefaultEncoding);

  scoped_refptr<SharedBuffer> data =
      SharedBuffer::Create(mhtml_data().data(), mhtml_data().size());

  // MHTMLArchives can only be initialized from local schemes, http/https
  // schemes, and content scheme(Android specific).
  CheckLoadResult(ToKURL("http://www.example.com"), data.get(),
                  MHTMLLoadResult::kSuccess);

#if defined(OS_ANDROID)
  CheckLoadResult(ToKURL("content://foo"), data.get(),
                  MHTMLLoadResult::kSuccess);
#else
  CheckLoadResult(ToKURL("content://foo"), data.get(),
                  MHTMLLoadResult::kUrlSchemeNotAllowed);
#endif
  CheckLoadResult(ToKURL("file://foo"), data.get(), MHTMLLoadResult::kSuccess);
  CheckLoadResult(ToKURL("fooscheme://bar"), data.get(),
                  MHTMLLoadResult::kUrlSchemeNotAllowed);

  url::ScopedSchemeRegistryForTests scoped_registry;
  url::AddLocalScheme("fooscheme");
  CheckLoadResult(ToKURL("fooscheme://bar"), data.get(),
                  MHTMLLoadResult::kSuccess);
}

TEST_F(MHTMLArchiveTest, MHTMLDate) {
  const char kURL[] = "http://www.example.com";
  AddTestMainResource();
  Serialize(ToKURL(kURL), "Test Serialization", "text/html",
            MHTMLArchive::kUseDefaultEncoding);
  // The serialization process should have added a date header corresponding to
  // mhtml_date().
  HashMap<String, String> mhtml_headers = ExtractMHTMLHeaders();
  ASSERT_EQ(mhtml_date_header(), mhtml_headers.find("Date")->value);

  scoped_refptr<SharedBuffer> data =
      SharedBuffer::Create(mhtml_data().data(), mhtml_data().size());
  KURL http_url = ToKURL("http://www.example.com");
  MHTMLArchive* archive = MHTMLArchive::Create(http_url, data.get());
  ASSERT_NE(nullptr, archive);

  // The deserialization process should have parsed the header into a Date.
  EXPECT_EQ(mhtml_date(), archive->Date());
}

TEST_F(MHTMLArchiveTest, EmptyArchive) {
  // Test failure to load when |data| is null.
  KURL http_url = ToKURL("http://www.example.com");
  CheckLoadResult(http_url, nullptr, MHTMLLoadResult::kEmptyFile);

  // Test failure to load when |data| is non-null but empty.
  const char* buf = "";
  scoped_refptr<SharedBuffer> data =
      SharedBuffer::Create(buf, static_cast<size_t>(0u));
  CheckLoadResult(http_url, data.get(), MHTMLLoadResult::kEmptyFile);
}

TEST_F(MHTMLArchiveTest, NoMainResource) {
  const char kURL[] = "http://www.example.com";
  // Only add a resource to a CSS file, so no main resource is valid for
  // rendering.
  AddResource("http://www.example.com/link_styles.css", "text/css",
              "link_styles.css");
  Serialize(ToKURL(kURL), "Test Serialization", "text/html",
            MHTMLArchive::kUseDefaultEncoding);

  scoped_refptr<SharedBuffer> data =
      SharedBuffer::Create(mhtml_data().data(), mhtml_data().size());
  KURL http_url = ToKURL("http://www.example.com");

  CheckLoadResult(http_url, data.get(), MHTMLLoadResult::kMissingMainResource);
}

TEST_F(MHTMLArchiveTest, InvalidMHTML) {
  const char kURL[] = "http://www.example.com";
  // Intentionally create MHTML data with no resources.
  Vector<SerializedResource> resources;
  GenerateMHTMLData(resources, MHTMLArchive::kUseDefaultEncoding, ToKURL(kURL),
                    "Test invalid mhtml", "text/html", false);

  scoped_refptr<SharedBuffer> data =
      SharedBuffer::Create(mhtml_data().data(), mhtml_data().size());

  CheckLoadResult(ToKURL(kURL), data.get(), MHTMLLoadResult::kInvalidArchive);
}

}  // namespace test

}  // namespace blink