File: base_file_win_unittest.cc

package info (click to toggle)
qtwebengine-opensource-src 5.15.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,115,536 kB
  • sloc: cpp: 13,170,444; ansic: 4,254,580; javascript: 1,917,440; python: 554,859; asm: 532,901; xml: 496,623; java: 151,702; objc: 80,776; perl: 73,361; sh: 71,244; cs: 30,383; makefile: 21,992; yacc: 9,125; tcl: 8,500; php: 5,896; sql: 5,518; pascal: 4,510; lex: 2,884; lisp: 2,727; ruby: 559; awk: 200; sed: 40
file content (113 lines) | stat: -rw-r--r-- 4,623 bytes parent folder | download | duplicates (6)
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
// 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 "components/download/public/common/base_file.h"

#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "components/download/public/common/download_interrupt_reasons.h"
#include "components/download/public/common/download_item.h"
#include "net/base/filename_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace download {

TEST(BaseFileWin, AnnotateWithSourceInformation) {
  const char kTestFileContents[] = "Hello world!";
  const base::FilePath::CharType kZoneIdentifierStreamName[] =
      FILE_PATH_LITERAL(":Zone.Identifier");

  struct {
    const char* const url;
    const char* const referrer;
    bool expected_internet_zone;
  } kTestCases[] = {
      // Test cases where we expect a MOTW.
      {"http://example.com", "http://example.com", true},
      {"", "http://example.com", true},
      {"", "", true},
      {"http://example.com", "", true},
      {"data:text/plain,Foo", "http://example.com", true},
      {"data:text/plain,Foo", "", true},
      {"data:text/plain,Foo", "data:text/plain,Bar", true},
      {"data:text/plain,Foo", "ftp://localhost/foo", true},
      {"http://example.com", "http://localhost/foo", true},
      {"ftp://example.com/foo", "", true},

      // Test cases where we don't expect a MOTW. These test cases result in
      // different behavior across Windows versions.
      {"ftp://localhost/foo", "", false},
      {"http://localhost/foo", "", false},
      {"", "http://localhost/foo", false},
      {"file:///exists.txt", "", false},
      {"file:///exists.txt", "http://example.com", false},
      {"file:///does-not-exist.txt", "", false},
  };

  base::ScopedTempDir target_directory;
  ASSERT_TRUE(target_directory.CreateUniqueTempDir());

  for (const auto& test_case : kTestCases) {
    GURL url(test_case.url);
    GURL referrer(test_case.referrer);

    // Resolve file:// URLs relative to our temp directory.
    if (url.SchemeIsFile()) {
      base::FilePath relative_path =
          base::FilePath().AppendASCII(url.path().substr(1));
      url = net::FilePathToFileURL(
          target_directory.GetPath().Append(relative_path));
    }

    SCOPED_TRACE(::testing::Message() << "Source URL: " << url.spec()
                                      << " Referrer: " << test_case.referrer);

    BaseFile base_file(download::DownloadItem::kInvalidId);
    int64_t bytes_wasted = 0;  // unused
    ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
              base_file.Initialize(base::FilePath(), target_directory.GetPath(),
                                   base::File(), 0, std::string(),
                                   std::unique_ptr<crypto::SecureHash>(), false,
                                   &bytes_wasted));
    ASSERT_FALSE(base_file.full_path().empty());
    ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
              base_file.Rename(
                  target_directory.GetPath().AppendASCII("test_file.doc")));
    ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
              base_file.AppendDataToFile(kTestFileContents,
                                         base::size(kTestFileContents)));
    ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
              base_file.AnnotateWithSourceInformationSync(
                  "7B2CEE7C-DC81-4160-86F1-9C968597118F", url, referrer));
    base_file.Detach();
    base_file.Finish();

    base::FilePath path = base_file.full_path();
    base::FilePath zone_identifier_stream(path.value() +
                                          kZoneIdentifierStreamName);

    ASSERT_TRUE(base::PathExists(path));

    std::string zone_identifier;
    base::ReadFileToString(zone_identifier_stream, &zone_identifier);

    if (test_case.expected_internet_zone) {
      // The actual assigned zone could be anything and the contents of the zone
      // identifier depends on the version of Windows. So only testing that
      // there is a zone annotation.
      EXPECT_FALSE(zone_identifier.empty());
    } else if (!zone_identifier.empty()) {
      // Seeing an unexpected zone identifier is not an error, but we log a
      // warning just the same so that such cases can be identified during
      // manual testing.
      LOG(WARNING) << "Unexpected zone annotation for Source:" << url.spec()
                   << " Referrer:" << test_case.referrer
                   << " Annotation:" << std::endl
                   << zone_identifier;
    }
    base::DeleteFile(path);
  }
}

}  // namespace download