Fix logging crash

PiperOrigin-RevId: 681591522
This commit is contained in:
Francis Tsui
2024-10-02 14:26:40 -07:00
committed by Copybara-Service
parent 8e68bf346c
commit fdd944e98d
5 changed files with 61 additions and 14 deletions
+13
View File
@@ -862,3 +862,16 @@ cc_test(
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "nearby_connections_service_test",
srcs = ["nearby_connections_service_test.cc"],
deps = [
":connection_types",
":nearby_sharing_service",
"//internal/platform:types",
"//internal/platform/implementation/g3", # fixdeps: keep
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_googletest//:gtest_main",
],
)
+1 -2
View File
@@ -43,8 +43,7 @@ Payload ConvertToPayload(NcPayload payload) {
std::vector<uint8_t>(data.begin(), data.end()));
}
case NcPayloadType::kFile: {
std::filesystem::path file_path =
std::filesystem::u8path(payload.AsFile()->GetFilePath());
std::string file_path = payload.AsFile()->GetFilePath();
std::string parent_folder = payload.GetParentFolder();
NL_VLOG(1) << __func__ << ": Payload file_path=" << file_path
<< ", parent_folder = " << parent_folder;
@@ -0,0 +1,42 @@
// Copyright 2024 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 "sharing/nearby_connections_service.h"
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "internal/platform/file.h"
#include "sharing/nearby_connections_types.h"
namespace nearby::sharing {
namespace {
using ::testing::Eq;
TEST(NearbyConnectionSharingServicePayloadTest, ConvertBytesToPayload) {
Payload payload = ConvertToPayload(NcPayload(1234, NcByteArray("test")));
EXPECT_THAT(payload.id, Eq(1234LL));
EXPECT_THAT(payload.content.type, Eq(PayloadContent::Type::kBytes));
}
TEST(NearbyConnectionSharingServicePayloadTest, ConvertFileToPayload) {
Payload payload = ConvertToPayload(
NcPayload(1234, nearby::InputFile("/为甚么/tmp/test.txt", /*size=*/100)));
EXPECT_THAT(payload.id, Eq(1234LL));
EXPECT_THAT(payload.content.type, Eq(PayloadContent::Type::kFile));
}
} // namespace
} // namespace nearby::sharing
+3 -1
View File
@@ -391,7 +391,9 @@ enum class DistanceInfo {
struct InputFile {
InputFile() = default;
explicit InputFile(std::filesystem::path path) { this->path = path; }
explicit InputFile(std::string path) {
this->path = std::filesystem::u8path(path);
}
std::filesystem::path path;
};
@@ -14,8 +14,6 @@
#include "sharing/nearby_connections_types.h"
#include <filesystem> // NOLINT
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
@@ -24,21 +22,14 @@ namespace nearby::sharing {
using ::testing::Eq;
TEST(NearbyConnectionSharingTypesPayloadTest, FromInputFileUTF8) {
InputFile input_file(std::filesystem::u8path("/为甚么/tmp/test.txt"));
Payload payload(input_file);
EXPECT_THAT(payload.id, Eq(7724502655048749887LL));
EXPECT_THAT(payload.content.type, Eq(PayloadContent::Type::kFile));
}
TEST(NearbyConnectionSharingTypesPayloadTest, FromInputFileUTF16) {
InputFile input_file(std::filesystem::path(L"/为甚么/tmp/test.txt"));
InputFile input_file("/为甚么/tmp/test.txt");
Payload payload(input_file);
EXPECT_THAT(payload.id, Eq(7724502655048749887LL));
EXPECT_THAT(payload.content.type, Eq(PayloadContent::Type::kFile));
}
TEST(NearbyConnectionSharingTypesPayloadTest, FromInputFileWithId) {
InputFile input_file(std::filesystem::u8path("/为甚么/tmp/test.txt"));
InputFile input_file("/为甚么/tmp/test.txt");
Payload payload(1234, input_file);
EXPECT_THAT(payload.id, Eq(1234LL));
EXPECT_THAT(payload.content.type, Eq(PayloadContent::Type::kFile));