chromium/components/download/internal/common/base_file_unittest.cc

// Copyright 2012 The Chromium Authors
// 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 <stddef.h>
#include <stdint.h>

#include <memory>
#include <utility>

#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/test_file_util.h"
#include "build/build_config.h"
#include "components/download/public/common/download_interrupt_reasons.h"
#include "components/download/public/common/download_item.h"
#include "crypto/secure_hash.h"
#include "crypto/sha2.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(IS_WIN)
#include "base/win/scoped_com_initializer.h"
#endif

namespace download {
namespace {

const char kTestData1[] =;
const char kTestData2[] =;
const char kTestData3[] =;
const char kTestData4[] =;
const int kTestDataLength1 =;
const int kTestDataLength2 =;
const int kTestDataLength3 =;
const int kTestDataLength4 =;
int64_t kTestDataBytesWasted =;

// SHA-256 hash of kTestData1 (excluding terminating NUL).
const uint8_t kHashOfTestData1[] =;

// SHA-256 hash of kTestData1 ++ kTestData2 ++ kTestData3 (excluding terminating
// NUL).
const uint8_t kHashOfTestData1To3[] =;

}  // namespace

class BaseFileTest : public testing::Test {};

// This will initialize the entire array to zero.
const unsigned char BaseFileTest::kEmptySha256Hash[] =;

// Test the most basic scenario: just create the object and do a sanity check
// on all its accessors. This is actually a case that rarely happens
// in production, where we would at least Initialize it.
TEST_F(BaseFileTest, CreateDestroy) {}

// Cancel the download explicitly.
TEST_F(BaseFileTest, Cancel) {}

// Write data to the file and detach it, so it doesn't get deleted
// automatically when base_file_ is destructed.
TEST_F(BaseFileTest, WriteAndDetach) {}

// Write data to the file and detach it, and calculate its sha256 hash.
TEST_F(BaseFileTest, WriteWithHashAndDetach) {}

// Rename the file after writing to it, then detach.
TEST_F(BaseFileTest, WriteThenRenameAndDetach) {}

// Write data to the file once.
TEST_F(BaseFileTest, SingleWrite) {}

// Write data to the file multiple times.
TEST_F(BaseFileTest, MultipleWrites) {}

// Write data to the file multiple times, interrupt it, and continue using
// another file.  Calculate the resulting combined sha256 hash.
TEST_F(BaseFileTest, MultipleWritesInterruptedWithHash) {}

// Rename the file after all writes to it.
TEST_F(BaseFileTest, WriteThenRename) {}

// Rename the file while the download is still in progress.
TEST_F(BaseFileTest, RenameWhileInProgress) {}

#if BUILDFLAG(IS_FUCHSIA)
// TODO(crbug.com/40221266): Re-enable when RenameWithError works on Fuchsia.
#define MAYBE_RenameWithError
#else
#define MAYBE_RenameWithError
#endif
// Test that a failed rename reports the correct error.
TEST_F(BaseFileTest, MAYBE_RenameWithError) {}

#if BUILDFLAG(IS_FUCHSIA)
// TODO(crbug.com/40221266): Re-enable when RenameWithErrorInProgress works on
// Fuchsia.
#define MAYBE_RenameWithErrorInProgress
#else
#define MAYBE_RenameWithErrorInProgress
#endif
// Test that if a rename fails for an in-progress BaseFile, it remains writeable
// and renameable.
TEST_F(BaseFileTest, MAYBE_RenameWithErrorInProgress) {}

#if BUILDFLAG(IS_FUCHSIA)
// TODO(crbug.com/40221270): Re-enable when WriteWithError works on Fuchsia.
#define MAYBE_WriteWithError
#else
#define MAYBE_WriteWithError
#endif
// Test that a failed write reports an error.
TEST_F(BaseFileTest, MAYBE_WriteWithError) {}

// Try to write to uninitialized file.
TEST_F(BaseFileTest, UninitializedFile) {}

// Create two |BaseFile|s with the same file, and attempt to write to both.
// Overwrite base_file_ with another file with the same name and
// non-zero contents, and make sure the last file to close 'wins'.
TEST_F(BaseFileTest, DuplicateBaseFile) {}

// Create a file and append to it.
TEST_F(BaseFileTest, AppendToBaseFile) {}

#if BUILDFLAG(IS_FUCHSIA)
// TODO(crbug.com/40221265): Re-enable when ReadonlyBaseFile works on Fuchsia.
#define MAYBE_ReadonlyBaseFile
#else
#define MAYBE_ReadonlyBaseFile
#endif
// Create a read-only file and attempt to write to it.
TEST_F(BaseFileTest, MAYBE_ReadonlyBaseFile) {}

// Open an existing file and continue writing to it. The hash of the partial
// file is known and matches the existing contents.
TEST_F(BaseFileTest, ExistingBaseFileKnownHash) {}

// Open an existing file and continue writing to it. The hash of the partial
// file is unknown.
TEST_F(BaseFileTest, ExistingBaseFileUnknownHash) {}

// Open an existing file. The contentsof the file doesn't match the known hash.
TEST_F(BaseFileTest, ExistingBaseFileIncorrectHash) {}

// Open a large existing file with a known hash and continue writing to it.
TEST_F(BaseFileTest, ExistingBaseFileLargeSizeKnownHash) {}

// Open a large existing file. The contents doesn't match the known hash.
TEST_F(BaseFileTest, ExistingBaseFileLargeSizeIncorrectHash) {}

// Open an existing file. The size of the file is too short.
TEST_F(BaseFileTest, ExistingBaseFileTooShort) {}

// Open an existing file. The size is larger than expected.
TEST_F(BaseFileTest, ExistingBaseFileKnownHashTooLong) {}

// Open an existing file. The size is large than expected and the hash is
// unknown.
TEST_F(BaseFileTest, ExistingBaseFileUnknownHashTooLong) {}

// Similar to ExistingBaseFileKnownHashTooLong test, but with a file large
// enough to requre multiple Read()s to complete. This provides additional code
// coverage for the CalculatePartialHash() logic.
TEST_F(BaseFileTest, ExistingBaseFileUnknownHashTooLongForLargeFile) {}

// Test that a temporary file is created in the default download directory.
TEST_F(BaseFileTest, CreatedInDefaultDirectory) {}

TEST_F(BaseFileTest, NoDoubleDeleteAfterCancel) {}

// Test that writing data to a sparse file works.
TEST_F(BaseFileTest, WriteDataToSparseFile) {}

// Test that validating data in a file works.
TEST_F(BaseFileTest, ValidateDataInFile) {}

}  // namespace download