#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include <string_view>
#include <utility>
#include <vector>
#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/task_environment.h"
#include "base/test/test_file_util.h"
#include "build/build_config.h"
#include "components/download/public/common/download_create_info.h"
#include "components/download/public/common/download_destination_observer.h"
#include "components/download/public/common/download_file_impl.h"
#include "components/download/public/common/download_interrupt_reasons.h"
#include "components/download/public/common/mock_input_stream.h"
#include "net/base/net_errors.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_WIN)
#include "base/win/scoped_com_initializer.h"
#endif
_;
AnyNumber;
DoAll;
InSequence;
Return;
Sequence;
SetArgPointee;
StrictMock;
namespace download {
namespace {
struct SourceStreamTestData { … };
int64_t GetBuffersLength(const char** buffers, size_t num_buffer) { … }
std::string GetHexEncodedHashValue(crypto::SecureHash* hash_state) { … }
class MockDownloadDestinationObserver : public DownloadDestinationObserver { … };
enum DownloadFileRenameMethodType { … };
class TestDownloadFileImpl : public DownloadFileImpl { … };
}
class DownloadFileTest : public testing::Test { … };
class DownloadFileTestWithRename
: public DownloadFileTest,
public ::testing::WithParamInterface<DownloadFileRenameMethodType> { … };
INSTANTIATE_TEST_SUITE_P(…);
const char DownloadFileTest::kTestData1[] = …;
const char DownloadFileTest::kTestData2[] = …;
const char DownloadFileTest::kTestData3[] = …;
const char DownloadFileTest::kTestData4[] = …;
const char DownloadFileTest::kTestData5[] = …;
const char* DownloadFileTest::kTestData6[] = …;
const char* DownloadFileTest::kTestData7[] = …;
const char* DownloadFileTest::kTestData8[] = …;
const char DownloadFileTest::kDataHash[] = …;
const char DownloadFileTest::kEmptyHash[] = …;
const uint32_t DownloadFileTest::kDummyDownloadId = …;
const int DownloadFileTest::kDummyChildId = …;
const int DownloadFileTest::kDummyRequestId = …;
TEST_P(DownloadFileTestWithRename, RenameFileFinal) { … }
TEST_F(DownloadFileTest, RenameOverwrites) { … }
TEST_F(DownloadFileTest, RenameUniquifies) { … }
TEST_F(DownloadFileTest, RenameRecognizesSelfConflict) { … }
#if BUILDFLAG(IS_MAC)
TEST_F(DownloadFileTest, RenameRemovesHiddenFlag) {
ASSERT_TRUE(CreateDownloadFile(true));
base::FilePath initial_path(download_file_->FullPath());
EXPECT_TRUE(base::PathExists(initial_path));
base::stat_wrapper_t stat;
base::File::Stat(initial_path, &stat);
chflags(initial_path.value().c_str(), stat.st_flags | UF_HIDDEN);
base::FilePath target_path =
initial_path.DirName().Append(FILE_PATH_LITERAL("foo"));
base::FilePath new_path;
EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
RenameAndUniquify(target_path, &new_path));
EXPECT_TRUE(base::PathExists(target_path));
base::File::Stat(initial_path, &stat);
EXPECT_FALSE(stat.st_flags & UF_HIDDEN);
FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true, kEmptyHash);
base::RunLoop().RunUntilIdle();
DestroyDownloadFile(0);
EXPECT_EQ(target_path.value(), new_path.value());
}
#endif
#if BUILDFLAG(IS_FUCHSIA)
#define MAYBE_RenameError …
#else
#define MAYBE_RenameError …
#endif
TEST_P(DownloadFileTestWithRename, MAYBE_RenameError) { … }
namespace {
void TestRenameCompletionCallback(base::OnceClosure closure,
bool* did_run_callback,
DownloadInterruptReason interrupt_reason,
const base::FilePath& new_path) { … }
}
#if BUILDFLAG(IS_FUCHSIA)
#define MAYBE_RenameWithErrorRetry …
#else
#define MAYBE_RenameWithErrorRetry …
#endif
TEST_P(DownloadFileTestWithRename, MAYBE_RenameWithErrorRetry) { … }
TEST_F(DownloadFileTest, StreamEmptySuccess) { … }
TEST_F(DownloadFileTest, StreamEmptyError) { … }
TEST_F(DownloadFileTest, StreamNonEmptySuccess) { … }
TEST_F(DownloadFileTest, StreamNonEmptyError) { … }
TEST_F(DownloadFileTest, FileContentValidationSuccess) { … }
TEST_F(DownloadFileTest, FileContentValidationFail) { … }
TEST_F(DownloadFileTest, MultipleStreamsWrite) { … }
TEST_F(DownloadFileTest, MultipleStreamsLimitedLength) { … }
TEST_F(DownloadFileTest, MultipleStreamsFirstStreamWriteAllData) { … }
TEST_F(DownloadFileTest, SecondStreamStartingOffsetAlreadyWritten) { … }
TEST_F(DownloadFileTest, SecondStreamReadsOffsetWrittenByFirst) { … }
}