Commit ebc55b75 authored by Ryan Hansberry's avatar Ryan Hansberry Committed by Commit Bot

[Nearby] Implement unit tests for InputFile.

Two tests related to Read() have been excluded from running
on Windows due to undiagnosed crashes. We can resolve them
in the future when Nearby launches for Windows devices.

Bug: 1124992
Change-Id: I682cf64604344314c40e276042c08dbb8023c800
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2402442
Commit-Queue: Ryan Hansberry <hansberry@chromium.org>
Reviewed-by: default avatarJames Vecore <vecore@google.com>
Cr-Commit-Position: refs/heads/master@{#805889}
parent 9c51ffbd
......@@ -75,6 +75,7 @@ source_set("unit_tests") {
"condition_variable_unittest.cc",
"count_down_latch_unittest.cc",
"crypto_unittest.cc",
"input_file_unittest.cc",
"multi_thread_executor_unittest.cc",
"mutex_unittest.cc",
"recursive_mutex_unittest.cc",
......
......@@ -21,7 +21,7 @@ std::string InputFile::GetFilePath() const {
std::int64_t InputFile::GetTotalSize() const {
if (!file_.IsValid())
return 0;
return -1;
return file_.GetLength();
}
......
// Copyright 2020 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/services/sharing/nearby/platform_v2/input_file.h"
#include <memory>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
namespace location {
namespace nearby {
namespace chrome {
namespace {
// 64KB, the common upper chunk size throughout Nearby Connections.
constexpr int64_t kChunkSize = 1024 * 64;
// 1MB.
constexpr int kTestDataSize = 1024 * 1000;
} // namespace
class InputFileTest : public PlatformTest {
public:
InputFileTest() = default;
~InputFileTest() override = default;
InputFileTest(const InputFileTest&) = delete;
InputFileTest& operator=(const InputFileTest&) = delete;
protected:
void SetUp() override {
PlatformTest::SetUp();
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
CreateValidInputFile(kTestDataSize);
}
void CreateValidInputFile(int file_size) {
if (file_size < 0) {
input_file_ = std::make_unique<InputFile>(base::File());
return;
}
expected_data_ = std::string(file_size, 'a');
base::FilePath file_path =
temp_dir_.GetPath().Append(FILE_PATH_LITERAL("InputFileTest"));
ASSERT_TRUE(WriteFile(file_path, expected_data_));
base::File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ |
base::File::FLAG_WRITE);
ASSERT_TRUE(file.IsValid());
ASSERT_EQ(file.GetLength(), file_size);
input_file_ = std::make_unique<InputFile>(std::move(file));
}
void CreateInvalidInputFile() {
input_file_ = std::make_unique<InputFile>(base::File());
}
void VerifyRead(int64_t chunk_size) {
std::string file_data;
while (true) {
auto exception_or_byte_array = input_file_->Read(chunk_size);
ASSERT_TRUE(exception_or_byte_array.ok());
auto byte_array = exception_or_byte_array.result();
if (byte_array.Empty())
break;
file_data.append(byte_array.data(), byte_array.size());
}
EXPECT_EQ(expected_data_, file_data);
}
std::string expected_data_;
std::unique_ptr<InputFile> input_file_;
base::ScopedTempDir temp_dir_;
};
TEST_F(InputFileTest, TestGetTotalSize_Valid) {
EXPECT_EQ(kTestDataSize, input_file_->GetTotalSize());
}
TEST_F(InputFileTest, TestGetTotalSize_Invalid) {
CreateInvalidInputFile();
EXPECT_EQ(-1, input_file_->GetTotalSize());
}
TEST_F(InputFileTest, TestRead_Valid) {
VerifyRead(kChunkSize);
}
// TODO(crbug.com/1126971): Fix these tests from crashing on Windows.
#if !defined(OS_WIN)
TEST_F(InputFileTest, TestRead_Valid_ChunkLargerThanFileSize) {
VerifyRead(kTestDataSize * 2);
}
TEST_F(InputFileTest, TestRead_Valid_LargeFileSize) {
// 100MB. 1GB does pass, but tcmalloc complains about a large alloc.
CreateValidInputFile(kTestDataSize * 100);
VerifyRead(kChunkSize);
}
#endif // !defined(OS_WIN)
TEST_F(InputFileTest, TestRead_Invalid) {
CreateInvalidInputFile();
EXPECT_FALSE(input_file_->Read(kChunkSize).ok());
}
TEST_F(InputFileTest, TestClose_Valid) {
EXPECT_TRUE(input_file_->Close().Ok());
}
TEST_F(InputFileTest, TestClose_Invalid) {
CreateInvalidInputFile();
EXPECT_FALSE(input_file_->Close().Ok());
}
TEST_F(InputFileTest, TestExtractUnderlyingFile_Valid) {
base::File file = input_file_->ExtractUnderlyingFile();
EXPECT_TRUE(file.IsValid());
EXPECT_EQ(kTestDataSize, file.GetLength());
}
TEST_F(InputFileTest, TestExtractUnderlyingFile_Invalid) {
CreateInvalidInputFile();
base::File file = input_file_->ExtractUnderlyingFile();
EXPECT_FALSE(file.IsValid());
}
} // namespace chrome
} // namespace nearby
} // namespace location
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment