Internal change

PiperOrigin-RevId: 380080172
This commit is contained in:
hai007
2021-06-17 17:22:20 -07:00
committed by Copybara-Service
parent 5ed0903d6e
commit 78f19e26be
12 changed files with 547 additions and 32 deletions
+28 -1
View File
@@ -36,6 +36,7 @@ cc_library(
"submittable_executor.h",
],
deps = [
"//base",
"//platform/api:types",
"//platform/base",
],
@@ -88,6 +89,24 @@ cc_library(
"//platform/api:comm",
"//platform/api:platform",
"//platform/api:types",
"//platform/impl/shared:file",
],
)
cc_library(
name = "test_utils",
srcs = [
"test_utils.cc",
],
hdrs = [
"test_utils.h",
],
visibility = [
"//location/nearby/connections/windows:__subpackages__",
],
deps = [
"//platform/base",
"//absl/strings",
],
)
@@ -95,13 +114,21 @@ cc_test(
name = "impl_test",
size = "small",
srcs = [
"atomic_boolean_test.cc",
"atomic_reference_test.cc",
"crypto_test.cc",
"input_file_test.cc",
"output_file_test.cc",
],
deps = [
":comm",
":crypto",
":test_utils",
":types",
"//platform/api:types",
"//platform/api:platform",
"//platform/base",
"//platform/impl/windows",
"//platform/public:logging",
"//testing/base/public:gunit_main",
],
)
+11 -2
View File
@@ -17,6 +17,8 @@
#include "platform/api/atomic_boolean.h"
#include <atomic>
namespace location {
namespace nearby {
namespace windows {
@@ -30,14 +32,21 @@ class AtomicBoolean : public api::AtomicBoolean {
// Atomically read and return current value.
bool Get() const override {
// TODO(b/184975123): replace with real implementation.
return false;
return atomic_boolean_;
};
// Atomically exchange original value with a new one. Return previous value.
bool Set(bool value) override {
// TODO(b/184975123): replace with real implementation.
return false;
bool original = atomic_boolean_;
atomic_boolean_ = value;
return original;
};
private:
std::atomic_bool atomic_boolean_;
};
} // namespace windows
@@ -0,0 +1,31 @@
// 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 "googletest/googletest/include/gtest/gtest.h"
#include "platform/impl/windows/atomic_boolean.h"
TEST(atomic_boolean, SuccessfulCreation) {
// Arrange
location::nearby::windows::AtomicBoolean atomicBoolean;
bool oldValue = true;
bool result = false;
// Act
oldValue = atomicBoolean.Set(true);
result = atomicBoolean.Get();
// Assert
EXPECT_TRUE(result);
EXPECT_FALSE(oldValue);
}
+7 -2
View File
@@ -17,6 +17,8 @@
#include "platform/api/atomic_reference.h"
#include <atomic>
namespace location {
namespace nearby {
namespace windows {
@@ -29,11 +31,14 @@ class AtomicUint32 : public api::AtomicUint32 {
// Atomically reads and returns stored value.
// TODO(b/184975123): replace with real implementation.
std::uint32_t Get() const override { return 0; };
std::uint32_t Get() const override { return atomic_uint32_; };
// Atomically stores value.
// TODO(b/184975123): replace with real implementation.
void Set(std::uint32_t value) override {}
void Set(std::uint32_t value) override { atomic_uint32_ = value; }
private:
std::atomic_int32_t atomic_uint32_ = 0;
};
} // namespace windows
@@ -0,0 +1,71 @@
// 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 "googletest/googletest/include/gtest/gtest.h"
#include "platform/impl/windows/atomic_reference.h"
TEST(atomic_reference, SuccessfulCreation) {
// Arrange
location::nearby::windows::AtomicUint32 atomicUint32;
uint32_t result = UINT32_MAX;
const uint32_t expected = 0;
// Act
result = atomicUint32.Get();
// Assert
EXPECT_EQ(result, expected);
}
TEST(atomic_reference, SuccessfulMaxSet) {
// Arrange
location::nearby::windows::AtomicUint32 atomicUint32;
uint32_t result = 0;
const uint32_t expected = UINT32_MAX;
// Act
atomicUint32.Set(UINT32_MAX);
result = atomicUint32.Get();
// Assert
EXPECT_EQ(result, expected);
}
TEST(atomic_reference, SuccessfulMinSet) {
// Arrange
location::nearby::windows::AtomicUint32 atomicUint32;
uint32_t result = UINT32_MAX;
const uint32_t expected = 0;
// Act
atomicUint32.Set(0);
result = atomicUint32.Get();
// Assert
EXPECT_EQ(result, expected);
}
TEST(atomic_reference, SetNegativeOneReturnsMAXUINT) {
// Arrange
location::nearby::windows::AtomicUint32 atomicUint32;
uint32_t result = 0;
const uint32_t expected = UINT32_MAX;
// Act
atomicUint32.Set(-1); // Try Set -1, should actually store UINT32_MAX
result = atomicUint32.Get();
// Assert
EXPECT_EQ(result, expected);
}
@@ -0,0 +1,154 @@
// 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 "platform/base/payload_id.h"
#include "platform/base/exception.h"
#include "platform/public/logging.h"
#include "platform/impl/windows/input_file.h"
#include "platform/impl/windows/test_utils.h"
#include "googletest/googletest/include/gtest/gtest.h"
class InputFileTests : public testing::Test {
protected:
// You can define per-test set-up logic as usual.
void SetUp() override {
location::nearby::PayloadId payloadId(TEST_PAYLOAD_ID);
hFile_ = CreateFileA(
test_utils::GetPayloadPath(payloadId).c_str(), // name of the write
GENERIC_WRITE, // open for writing
0, // do not share
NULL, // default security
CREATE_ALWAYS, // create new file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hFile_ == INVALID_HANDLE_VALUE) {
NEARBY_LOG(
ERROR, "Failed to create OutputFile with payloadId: %s and error: %d",
test_utils::GetPayloadPath(payloadId).c_str(), GetLastError());
}
const char* buffer = TEST_STRING;
DWORD bytesWritten;
WriteFile(hFile_, buffer, lstrlenA(buffer) * sizeof(char), &bytesWritten,
nullptr);
CloseHandle(hFile_);
}
// You can define per-test tear-down logic as usual.
void TearDown() override {
location::nearby::PayloadId payloadId(TEST_PAYLOAD_ID);
if (FileExists(test_utils::GetPayloadPath(payloadId).c_str())) {
DeleteFileA(test_utils::GetPayloadPath(payloadId).c_str());
}
}
BOOL FileExists(const char* szPath) {
DWORD dwAttrib = GetFileAttributesA(szPath);
return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
private:
HANDLE hFile_ = nullptr;
};
TEST_F(InputFileTests, SuccessfulCreation) {
location::nearby::PayloadId payloadId(TEST_PAYLOAD_ID);
std::unique_ptr<location::nearby::api::InputFile> inputFile = nullptr;
inputFile = location::nearby::api::ImplementationPlatform::CreateInputFile(
payloadId, strlen(TEST_STRING));
EXPECT_NE(inputFile, nullptr);
EXPECT_EQ(inputFile->Close(),
location::nearby::Exception{location::nearby::Exception::kSuccess});
}
TEST_F(InputFileTests, SuccessfulGetFilePath) {
location::nearby::PayloadId payloadId(TEST_PAYLOAD_ID);
std::unique_ptr<location::nearby::api::InputFile> inputFile = nullptr;
std::string fileName;
inputFile = location::nearby::api::ImplementationPlatform::CreateInputFile(
payloadId, strlen(TEST_STRING));
fileName = inputFile->GetFilePath();
EXPECT_EQ(inputFile->Close(),
location::nearby::Exception{location::nearby::Exception::kSuccess});
EXPECT_EQ(fileName, TEST_PATH);
}
TEST_F(InputFileTests, SuccessfulGetTotalSize) {
location::nearby::PayloadId payloadId(TEST_PAYLOAD_ID);
std::unique_ptr<location::nearby::api::InputFile> inputFile = nullptr;
int64_t size = -1;
inputFile = location::nearby::api::ImplementationPlatform::CreateInputFile(
payloadId, strlen(TEST_STRING));
size = inputFile->GetTotalSize();
EXPECT_EQ(inputFile->Close(),
location::nearby::Exception{location::nearby::Exception::kSuccess});
EXPECT_EQ(size, strlen(TEST_STRING));
}
TEST_F(InputFileTests, SuccessfulRead) {
location::nearby::PayloadId payloadId(TEST_PAYLOAD_ID);
std::unique_ptr<location::nearby::api::InputFile> inputFile = nullptr;
inputFile = location::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(),
location::nearby::Exception{location::nearby::Exception::kSuccess});
EXPECT_STREQ(std::string(dataRead.result()).c_str(), TEST_STRING);
}
TEST_F(InputFileTests, FailedRead) {
location::nearby::PayloadId payloadId(TEST_PAYLOAD_ID);
std::unique_ptr<location::nearby::api::InputFile> inputFile = nullptr;
inputFile = location::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(), "");
}
+49 -5
View File
@@ -14,16 +14,60 @@
#include "platform/impl/windows/log_message.h"
#include <algorithm>
#include "base/stringprintf.h"
namespace location {
namespace nearby {
namespace windows {
api::LogMessage::Severity min_log_severity_ = api::LogMessage::Severity::kInfo;
inline absl::LogSeverity ConvertSeverity(api::LogMessage::Severity severity) {
switch (severity) {
// api::LogMessage::Severity kVerbose and kInfo is mapped to
// absl::LogSeverity kInfo since absl::LogSeverity doesn't have kVerbose
// level.
case api::LogMessage::Severity::kVerbose:
case api::LogMessage::Severity::kInfo:
return absl::LogSeverity::kInfo;
case api::LogMessage::Severity::kWarning:
return absl::LogSeverity::kWarning;
case api::LogMessage::Severity::kError:
return absl::LogSeverity::kError;
case api::LogMessage::Severity::kFatal:
return absl::LogSeverity::kFatal;
}
}
LogMessage::LogMessage(const char* file, int line, Severity severity)
: log_streamer_(ConvertSeverity(severity), file, line) {}
LogMessage::~LogMessage() = default;
void LogMessage::Print(const char* format, ...) {
va_list ap;
va_start(ap, format);
std::string result;
StringAppendV(&result, format, ap);
log_streamer_.stream() << result;
va_end(ap);
}
std::ostream& LogMessage::Stream() { return log_streamer_.stream(); }
} // namespace windows
namespace api {
// TODO(b/184975123): replace with real implementation.
void LogMessage::SetMinLogSeverity(Severity severity) {}
// TODO(b/184975123): replace with real implementation.
bool LogMessage::ShouldCreateLogMessage(Severity severity) { return false; }
void LogMessage::SetMinLogSeverity(Severity severity) {
windows::min_log_severity_ = severity;
}
bool LogMessage::ShouldCreateLogMessage(Severity severity) {
return severity >= windows::min_log_severity_;
}
} // namespace api
} // namespace nearby
} // namespace location
+10 -15
View File
@@ -15,32 +15,27 @@
#ifndef PLATFORM_IMPL_WINDOWS_LOG_MESSAGE_H_
#define PLATFORM_IMPL_WINDOWS_LOG_MESSAGE_H_
#include "base/logging.h"
#include "platform/api/log_message.h"
namespace location {
namespace nearby {
namespace windows {
// A log message that prints to appropraite destination when ~LogMessage() is
// called.
//
// note: the Severity enum should map (best effort) to the corresponding level
// id that the platform logging implementation has.
// See documentation in
// cpp/platform/api/log_message.h
class LogMessage : public api::LogMessage {
public:
// TODO(b/184975123): replace with real implementation.
~LogMessage() override = default;
LogMessage(const char* file, int line, Severity severity);
~LogMessage() override;
// Printf like logging.
// TODO(b/184975123): replace with real implementation.
void Print(const char* format, ...) override {}
void Print(const char* format, ...) override;
// Returns a stream for std::cout like logging.
// TODO(b/184975123): replace with real implementation.
std::ostream& Stream() override { return empty_stream_; }
std::ostream& Stream() override;
// TODO(b/184975123): replace with real implementation.
std::ostream empty_stream_;
private:
absl::LogStreamer log_streamer_;
static api::LogMessage::Severity min_log_severity_;
};
} // namespace windows
@@ -0,0 +1,90 @@
// 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 "platform/impl/windows/output_file.h"
#include "platform/api/platform.h"
#include "platform/base/exception.h"
#include "platform/base/payload_id.h"
#include "platform/impl/windows/test_utils.h"
#include "googletest/googletest/include/gtest/gtest.h"
class OutputFileTests : public testing::Test {
protected:
// You can define per-test set-up logic as usual.
void SetUp() override {
location::nearby::PayloadId payloadId(TEST_PAYLOAD_ID);
if (FileExists(test_utils::GetPayloadPath(payloadId).c_str())) {
DeleteFileA(test_utils::GetPayloadPath(payloadId).c_str());
}
}
// You can define per-test tear-down logic as usual.
void TearDown() override {
location::nearby::PayloadId payloadId(TEST_PAYLOAD_ID);
if (FileExists(test_utils::GetPayloadPath(payloadId).c_str())) {
DeleteFileA(test_utils::GetPayloadPath(payloadId).c_str());
}
}
BOOL FileExists(const char* szPath) {
DWORD dwAttrib = GetFileAttributesA(szPath);
return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
};
TEST_F(OutputFileTests, SuccessfulCreation) {
location::nearby::PayloadId payloadId(TEST_PAYLOAD_ID);
std::unique_ptr<location::nearby::api::OutputFile> outputFile = nullptr;
EXPECT_NO_THROW(
outputFile =
location::nearby::api::ImplementationPlatform::CreateOutputFile(
payloadId));
EXPECT_NE(outputFile, nullptr);
EXPECT_NO_THROW(outputFile->Close());
}
TEST_F(OutputFileTests, SuccessfulClose) {
location::nearby::PayloadId payloadId(TEST_PAYLOAD_ID);
std::unique_ptr<location::nearby::api::OutputFile> outputFile = nullptr;
EXPECT_NO_THROW(
outputFile =
location::nearby::api::ImplementationPlatform::CreateOutputFile(
payloadId));
EXPECT_NO_THROW(outputFile->Close());
DeleteFileA(test_utils::GetPayloadPath(payloadId).c_str());
}
TEST_F(OutputFileTests, SuccessfulWrite) {
location::nearby::PayloadId payloadId(TEST_PAYLOAD_ID);
location::nearby::ByteArray data(std::string(TEST_STRING));
std::unique_ptr<location::nearby::api::OutputFile> outputFile = nullptr;
EXPECT_NO_THROW(
outputFile =
location::nearby::api::ImplementationPlatform::CreateOutputFile(
payloadId));
EXPECT_NO_THROW(outputFile->Write(data));
EXPECT_NO_THROW(outputFile->Close());
DeleteFileA(test_utils::GetPayloadPath(payloadId).c_str());
}
+12 -7
View File
@@ -14,6 +14,7 @@
#include "platform/api/platform.h"
#include "platform/impl/shared/file.h"
#include "platform/impl/windows/atomic_boolean.h"
#include "platform/impl/windows/atomic_reference.h"
#include "platform/impl/windows/ble.h"
@@ -24,11 +25,9 @@
#include "platform/impl/windows/count_down_latch.h"
#include "platform/impl/windows/executor.h"
#include "platform/impl/windows/future.h"
#include "platform/impl/windows/input_file.h"
#include "platform/impl/windows/listenable_future.h"
#include "platform/impl/windows/log_message.h"
#include "platform/impl/windows/mutex.h"
#include "platform/impl/windows/output_file.h"
#include "platform/impl/windows/scheduled_executor.h"
#include "platform/impl/windows/server_sync.h"
#include "platform/impl/windows/settable_future.h"
@@ -40,6 +39,12 @@
namespace location {
namespace nearby {
namespace api {
namespace {
std::string GetPayloadPath(PayloadId payload_id) {
return absl::StrCat("/tmp/", payload_id);
}
} // namespace
// TODO(b/184975123): replace with real implementation.
std::unique_ptr<AtomicBoolean> ImplementationPlatform::CreateAtomicBoolean(
@@ -70,22 +75,22 @@ ImplementationPlatform::CreateConditionVariable(Mutex* mutex) {
return std::unique_ptr<ConditionVariable>(new windows::ConditionVariable());
}
// TODO(b/184975123): replace with real implementation.
std::unique_ptr<InputFile> ImplementationPlatform::CreateInputFile(
PayloadId payload_id, std::int64_t total_size) {
return absl::make_unique<windows::InputFile>();
return absl::make_unique<location::nearby::shared::InputFile>(
GetPayloadPath(payload_id), total_size);
}
// TODO(b/184975123): replace with real implementation.
std::unique_ptr<OutputFile> ImplementationPlatform::CreateOutputFile(
PayloadId payload_id) {
return absl::make_unique<windows::OutputFile>();
return absl::make_unique<location::nearby::shared::OutputFile>(
GetPayloadPath(payload_id));
}
// TODO(b/184975123): replace with real implementation.
std::unique_ptr<LogMessage> ImplementationPlatform::CreateLogMessage(
const char* file, int line, LogMessage::Severity severity) {
return nullptr;
return absl::make_unique<windows::LogMessage>(file, line, severity);
}
// TODO(b/184975123): replace with real implementation.
+36
View File
@@ -0,0 +1,36 @@
// 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 "platform/impl/windows/test_utils.h"
#include "absl/strings/str_cat.h"
namespace test_utils {
std::wstring StringToWideString(const std::string& s) {
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
std::string GetPayloadPath(location::nearby::PayloadId payload_id) {
auto returnString = absl::StrCat("/tmp/", payload_id);
return returnString;
}
} // namespace test_utils
+48
View File
@@ -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_WINDOWS_TEST_UTILS_H_
#define PLATFORM_IMPL_WINDOWS_TEST_UTILS_H_
#include <Windows.h>
#include <stdio.h>
#include <xstring>
#include <string>
#include "platform/base/payload_id.h"
#define TEST_BUFFER_SIZE 256
#define TEST_PATH "/tmp/64"
#define TEST_PAYLOAD_ID 64l
#define TEST_STRING \
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas " \
"eleifend nisl at magna maximus, id finibus mauris ultrices. Mauris " \
"interdum efficitur turpis eget auctor. Nullam commodo metus et ante " \
"bibendum molestie. Donec iaculis ante nec diam rutrum egestas. Proin " \
"maximus metus luctus rutrum congue. Integer et eros nunc. Etiam purus " \
"neque, tincidunt eu elementum in, pharetra sit amet magna. Quisque " \
"consequat aliquam aliquam. Vestibulum ante ipsum primis in faucibus orci " \
"luctus et ultrices posuere cubilia curae; Maecenas a semper eros, a " \
"auctor mi. In luctus diam sem, eu pretium nisi porttitor ac. Sed cursus, " \
"arcu in bibendum feugiat, leo erat finibus massa, ut tincidunt magna nunc " \
"eu tellus. Cras feugiat ornare vestibulum. Nullam at ipsum vestibulum " \
"sapien luctus dictum ac vel ligula."
namespace test_utils {
std::wstring StringToWideString(const std::string& s);
std::string GetPayloadPath(location::nearby::PayloadId payload_id);
} // namespace test_utils
#endif // PLATFORM_IMPL_WINDOWS_TEST_UTILS_H_