File: file_downloader_unittest.cc

package info (click to toggle)
chromium 73.0.3683.75-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,792,156 kB
  • sloc: cpp: 13,473,466; ansic: 1,577,080; python: 898,539; javascript: 655,737; xml: 341,883; asm: 306,070; java: 289,969; perl: 80,911; objc: 67,198; sh: 43,184; cs: 27,853; makefile: 12,092; php: 11,064; yacc: 10,373; tcl: 8,875; ruby: 3,941; lex: 1,800; pascal: 1,473; lisp: 812; awk: 41; jsp: 39; sed: 19; sql: 3
file content (123 lines) | stat: -rw-r--r-- 4,296 bytes parent folder | download | duplicates (2)
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
// Copyright 2015 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 "chrome/browser/net/file_downloader.h"

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread_task_runner_handle.h"
#include "content/public/test/test_utils.h"
#include "net/http/http_status_code.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

const char kURL[] = "https://www.url.com/path";
const char kFilename[] = "filename.ext";
const char kFileContents1[] = "file contents";
const char kFileContents2[] = "different contents";

class FileDownloaderTest : public testing::Test {
 public:
  FileDownloaderTest()
      : test_shared_loader_factory_(
            base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
                &test_url_loader_factory_)) {}

  void SetUp() override {
    ASSERT_TRUE(dir_.CreateUniqueTempDir());
    path_ = dir_.GetPath().AppendASCII(kFilename);
    ASSERT_FALSE(base::PathExists(path_));
  }

  MOCK_METHOD1(OnDownloadFinished, void(FileDownloader::Result));

 protected:
  const base::FilePath& path() const { return path_; }

  void SetValidResponse() {
    test_url_loader_factory_.AddResponse(kURL, kFileContents1);
  }

  void SetValidResponse2() {
    test_url_loader_factory_.AddResponse(kURL, kFileContents2);
  }

  void SetFailedResponse() {
    test_url_loader_factory_.AddResponse(
        GURL(kURL), network::ResourceResponseHead(), std::string(),
        network::URLLoaderCompletionStatus(net::HTTP_NOT_FOUND));
  }

  void Download(bool overwrite, FileDownloader::Result expected_result) {
    FileDownloader downloader(
        GURL(kURL), path_, overwrite, test_shared_loader_factory_,
        base::BindOnce(&FileDownloaderTest::OnDownloadFinished,
                       base::Unretained(this)),
        TRAFFIC_ANNOTATION_FOR_TESTS);
    EXPECT_CALL(*this, OnDownloadFinished(expected_result));
    // Wait for the FileExists check to happen if necessary.
    content::RunAllTasksUntilIdle();
  }

 private:
  base::ScopedTempDir dir_;
  base::FilePath path_;

  base::test::ScopedTaskEnvironment scoped_task_environment_;
  network::TestURLLoaderFactory test_url_loader_factory_;
  scoped_refptr<network::SharedURLLoaderFactory> test_shared_loader_factory_;
};

TEST_F(FileDownloaderTest, Success) {
  SetValidResponse();
  Download(true, FileDownloader::DOWNLOADED);
  EXPECT_TRUE(base::PathExists(path()));
  std::string contents;
  ASSERT_TRUE(base::ReadFileToString(path(), &contents));
  EXPECT_EQ(std::string(kFileContents1), contents);
}

TEST_F(FileDownloaderTest, Failure) {
  SetFailedResponse();
  Download(true, FileDownloader::FAILED);
  EXPECT_FALSE(base::PathExists(path()));
}

TEST_F(FileDownloaderTest, Overwrite) {
  SetValidResponse();
  Download(true, FileDownloader::DOWNLOADED);
  ASSERT_TRUE(base::PathExists(path()));
  std::string contents;
  ASSERT_TRUE(base::ReadFileToString(path(), &contents));
  ASSERT_EQ(std::string(kFileContents1), contents);

  SetValidResponse2();
  Download(true, FileDownloader::DOWNLOADED);
  // The file should have been overwritten with the new contents.
  EXPECT_TRUE(base::PathExists(path()));
  ASSERT_TRUE(base::ReadFileToString(path(), &contents));
  EXPECT_EQ(std::string(kFileContents2), contents);
}

TEST_F(FileDownloaderTest, DontOverwrite) {
  SetValidResponse();
  Download(true, FileDownloader::DOWNLOADED);
  ASSERT_TRUE(base::PathExists(path()));
  std::string contents;
  ASSERT_TRUE(base::ReadFileToString(path(), &contents));
  EXPECT_EQ(std::string(kFileContents1), contents);

  SetValidResponse2();
  Download(false, FileDownloader::EXISTS);
  // The file should still have the old contents.
  EXPECT_TRUE(base::PathExists(path()));
  ASSERT_TRUE(base::ReadFileToString(path(), &contents));
  EXPECT_EQ(std::string(kFileContents1), contents);
}