mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 07:36:10 -04:00
Added base implementation for input_file and output_file
This are unimplemented currently, but will be in the future.
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef PLATFORM_IMPL_LINUX_INPUT_FILE_H_
|
||||
#define PLATFORM_IMPL_LINUX_INPUT_FILE_H_
|
||||
|
||||
#include "internal/platform/implementation/input_file.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/exception.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace linux {
|
||||
|
||||
// An InputFile represents a readable file on the system.
|
||||
class InputFile : public api::InputFile {
|
||||
public:
|
||||
// TODO(b/184975123): replace with real implementation.
|
||||
~InputFile() override = default;
|
||||
// TODO(b/184975123): replace with real implementation.
|
||||
std::string GetFilePath() const override { return "Un-implemented"; }
|
||||
// TODO(b/184975123): replace with real implementation.
|
||||
std::int64_t GetTotalSize() const override { return 0; }
|
||||
|
||||
// throws Exception::kIo
|
||||
// TODO(b/184975123): replace with real implementation.
|
||||
ExceptionOr<ByteArray> Read(std::int64_t size) override {
|
||||
return ExceptionOr<ByteArray>(Exception::kFailed);
|
||||
}
|
||||
// throws Exception::kIo
|
||||
// TODO(b/184975123): replace with real implementation.
|
||||
Exception Close() override { return Exception{}; }
|
||||
};
|
||||
|
||||
} // namespace linux
|
||||
} // namespace nearby
|
||||
|
||||
#endif // PLATFORM_IMPL_LINUX_INPUT_FILE_H_
|
||||
@@ -0,0 +1,135 @@
|
||||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "internal/platform/implementation/linux/input_file.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "internal/platform/exception.h"
|
||||
#include "internal/platform/payload_id.h"
|
||||
#include "internal/platform/implementation/linux/test_utils.h"
|
||||
#include "internal/platform/logging.h"
|
||||
|
||||
class InputFileTests : public testing::Test {
|
||||
protected:
|
||||
// You can define per-test set-up logic as usual.
|
||||
void SetUp() override {
|
||||
nearby::PayloadId payloadId(TEST_PAYLOAD_ID);
|
||||
auto path = test_utils::GetPayloadPath(payloadId);
|
||||
|
||||
file_.open(path, std::ios::out);
|
||||
|
||||
if (!file_) {
|
||||
NEARBY_LOG(ERROR,
|
||||
"Failed to create OutputFile with payloadId: %s and error: %d",
|
||||
test_utils::GetPayloadPath(payloadId).c_str(), std::strerror(errno));
|
||||
}
|
||||
|
||||
const char* buffer = TEST_STRING;
|
||||
|
||||
file_.write(buffer, std::strlen(buffer));
|
||||
|
||||
file_.close();
|
||||
}
|
||||
|
||||
// You can define per-test tear-down logic as usual.
|
||||
void TearDown() override {
|
||||
nearby::PayloadId payloadId(TEST_PAYLOAD_ID);
|
||||
if (std::filesystem::exists(test_utils::GetPayloadPath(payloadId))) {
|
||||
std::filesystem::remove(test_utils::GetPayloadPath(payloadId));
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::fstream file_;
|
||||
};
|
||||
|
||||
TEST_F(InputFileTests, SuccessfulCreation) {
|
||||
nearby::PayloadId payloadId(TEST_PAYLOAD_ID);
|
||||
std::unique_ptr<nearby::api::InputFile> inputFile = nullptr;
|
||||
|
||||
inputFile = nearby::api::ImplementationPlatform::CreateInputFile(
|
||||
payloadId, strlen(TEST_STRING));
|
||||
|
||||
EXPECT_NE(inputFile, nullptr);
|
||||
EXPECT_EQ(inputFile->Close(), nearby::Exception{nearby::Exception::kSuccess});
|
||||
}
|
||||
|
||||
TEST_F(InputFileTests, SuccessfulGetFilePath) {
|
||||
nearby::PayloadId payloadId(TEST_PAYLOAD_ID);
|
||||
std::unique_ptr<nearby::api::InputFile> inputFile = nullptr;
|
||||
std::string fileName;
|
||||
|
||||
inputFile = nearby::api::ImplementationPlatform::CreateInputFile(
|
||||
payloadId, strlen(TEST_STRING));
|
||||
|
||||
fileName = inputFile->GetFilePath();
|
||||
|
||||
EXPECT_EQ(inputFile->Close(), nearby::Exception{nearby::Exception::kSuccess});
|
||||
|
||||
EXPECT_EQ(fileName, test_utils::GetPayloadPath(payloadId).c_str());
|
||||
}
|
||||
|
||||
TEST_F(InputFileTests, SuccessfulGetTotalSize) {
|
||||
nearby::PayloadId payloadId(TEST_PAYLOAD_ID);
|
||||
std::unique_ptr<nearby::api::InputFile> inputFile = nullptr;
|
||||
int64_t size = -1;
|
||||
|
||||
inputFile = nearby::api::ImplementationPlatform::CreateInputFile(
|
||||
payloadId, strlen(TEST_STRING));
|
||||
|
||||
size = inputFile->GetTotalSize();
|
||||
|
||||
EXPECT_EQ(inputFile->Close(), nearby::Exception{nearby::Exception::kSuccess});
|
||||
|
||||
EXPECT_EQ(size, strlen(TEST_STRING));
|
||||
}
|
||||
|
||||
TEST_F(InputFileTests, SuccessfulRead) {
|
||||
nearby::PayloadId payloadId(TEST_PAYLOAD_ID);
|
||||
std::unique_ptr<nearby::api::InputFile> inputFile = nullptr;
|
||||
|
||||
inputFile = nearby::api::ImplementationPlatform::CreateInputFile(
|
||||
payloadId, strlen(TEST_STRING));
|
||||
|
||||
auto fileSize = inputFile->GetTotalSize();
|
||||
auto dataRead = inputFile->Read(fileSize);
|
||||
|
||||
EXPECT_TRUE(dataRead.ok());
|
||||
EXPECT_EQ(inputFile->Close(), nearby::Exception{nearby::Exception::kSuccess});
|
||||
|
||||
EXPECT_STREQ(std::string(dataRead.result()).c_str(), TEST_STRING);
|
||||
}
|
||||
|
||||
TEST_F(InputFileTests, FailedRead) {
|
||||
nearby::PayloadId payloadId(TEST_PAYLOAD_ID);
|
||||
std::unique_ptr<nearby::api::InputFile> inputFile = nullptr;
|
||||
|
||||
inputFile = nearby::api::ImplementationPlatform::CreateInputFile(
|
||||
payloadId, strlen(TEST_STRING));
|
||||
|
||||
auto fileSize = inputFile->GetTotalSize();
|
||||
EXPECT_NE(fileSize, -1);
|
||||
|
||||
auto dataRead = inputFile->Read(fileSize);
|
||||
EXPECT_TRUE(dataRead.ok());
|
||||
|
||||
dataRead = inputFile->Read(fileSize);
|
||||
std::string data = std::string(dataRead.result());
|
||||
|
||||
inputFile->Close();
|
||||
|
||||
EXPECT_STREQ(data.c_str(), "");
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef PLATFORM_IMPL_LINUX_OUTPUT_FILE_H_
|
||||
#define PLATFORM_IMPL_LINUX_OUTPUT_FILE_H_
|
||||
|
||||
#include "internal/platform/implementation/output_file.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/exception.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace linux {
|
||||
|
||||
// An OutputFile represents a writable file on the system.
|
||||
class OutputFile : public api::OutputFile {
|
||||
public:
|
||||
// TODO(b/184975123): replace with real implementation.
|
||||
~OutputFile() override = default;
|
||||
|
||||
// throws Exception::kIo
|
||||
// TODO(b/184975123): replace with real implementation.
|
||||
Exception Write(const ByteArray& data) override { return Exception{}; }
|
||||
// throws Exception::kIo
|
||||
// TODO(b/184975123): replace with real implementation.
|
||||
Exception Flush() override { return Exception{}; }
|
||||
// throws Exception::kIo
|
||||
// TODO(b/184975123): replace with real implementation.
|
||||
Exception Close() override { return Exception{}; }
|
||||
};
|
||||
|
||||
} // namespace linux
|
||||
} // namespace nearby
|
||||
|
||||
#endif // PLATFORM_IMPL_LINUX_OUTPUT_FILE_H_
|
||||
@@ -0,0 +1,80 @@
|
||||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "internal/platform/implementation/linux/output_file.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "internal/platform/implementation/platform.h"
|
||||
#include "internal/platform/exception.h"
|
||||
#include "internal/platform/payload_id.h"
|
||||
#include "internal/platform/implementation/windows/test_utils.h"
|
||||
|
||||
class OutputFileTests : public testing::Test {
|
||||
protected:
|
||||
// You can define per-test set-up logic as usual.
|
||||
void SetUp() override {
|
||||
nearby::PayloadId payloadId(TEST_PAYLOAD_ID);
|
||||
if (std::filesystem::exists(test_utils::GetPayloadPath(payloadId))) {
|
||||
std::filesystem::remove(test_utils::GetPayloadPath(payloadId));
|
||||
}
|
||||
}
|
||||
|
||||
// You can define per-test tear-down logic as usual.
|
||||
void TearDown() override {
|
||||
nearby::PayloadId payloadId(TEST_PAYLOAD_ID);
|
||||
if (std::filesystem::exists(test_utils::GetPayloadPath(payloadId).c_str())) {
|
||||
std::filesystem::remove(test_utils::GetPayloadPath(payloadId).c_str());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(OutputFileTests, SuccessfulCreation) {
|
||||
nearby::PayloadId payloadId(TEST_PAYLOAD_ID);
|
||||
std::unique_ptr<nearby::api::OutputFile> outputFile = nullptr;
|
||||
|
||||
EXPECT_NO_THROW(
|
||||
outputFile =
|
||||
nearby::api::ImplementationPlatform::CreateOutputFile(payloadId));
|
||||
|
||||
EXPECT_NE(outputFile, nullptr);
|
||||
EXPECT_NO_THROW(outputFile->Close());
|
||||
}
|
||||
|
||||
TEST_F(OutputFileTests, SuccessfulClose) {
|
||||
nearby::PayloadId payloadId(TEST_PAYLOAD_ID);
|
||||
std::unique_ptr<nearby::api::OutputFile> outputFile = nullptr;
|
||||
|
||||
EXPECT_NO_THROW(
|
||||
outputFile =
|
||||
nearby::api::ImplementationPlatform::CreateOutputFile(payloadId));
|
||||
|
||||
EXPECT_NO_THROW(outputFile->Close());
|
||||
|
||||
std::filesystem::remove(test_utils::GetPayloadPath(payloadId).c_str());
|
||||
}
|
||||
|
||||
TEST_F(OutputFileTests, SuccessfulWrite) {
|
||||
nearby::PayloadId payloadId(TEST_PAYLOAD_ID);
|
||||
nearby::ByteArray data(std::string(TEST_STRING));
|
||||
std::unique_ptr<nearby::api::OutputFile> outputFile = nullptr;
|
||||
|
||||
EXPECT_NO_THROW(
|
||||
outputFile =
|
||||
nearby::api::ImplementationPlatform::CreateOutputFile(payloadId));
|
||||
|
||||
EXPECT_NO_THROW(outputFile->Write(data));
|
||||
EXPECT_NO_THROW(outputFile->Close());
|
||||
|
||||
std::filesystem::remove(test_utils::GetPayloadPath(payloadId).c_str());
|
||||
}
|
||||
Reference in New Issue
Block a user