diff --git a/fastpair/common/BUILD b/fastpair/common/BUILD index 27ed6b32..5415a907 100644 --- a/fastpair/common/BUILD +++ b/fastpair/common/BUILD @@ -8,6 +8,7 @@ cc_library( "fast_pair_device.cc", "fast_pair_http_result.cc", "fast_pair_prefs.cc", + "fast_pair_switches.cc", "pair_failure.cc", "protocol.cc", ], @@ -19,6 +20,7 @@ cc_library( "fast_pair_device.h", "fast_pair_http_result.h", "fast_pair_prefs.h", + "fast_pair_switches.h", "non_discoverable_advertisement.h", "pair_failure.h", "protocol.h", @@ -113,3 +115,18 @@ cc_test( "@com_google_googletest//:gtest_main", ], ) + +cc_test( + name = "fast_pair_switches_test", + size = "small", + srcs = [ + "fast_pair_switches_test.cc", + ], + shard_count = 16, + deps = [ + ":common", + "//internal/platform/implementation/g3", # build_cleaner: keep + "@com_github_protobuf_matchers//protobuf-matchers", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/fastpair/common/fast_pair_switches.cc b/fastpair/common/fast_pair_switches.cc new file mode 100644 index 00000000..5391958c --- /dev/null +++ b/fastpair/common/fast_pair_switches.cc @@ -0,0 +1,45 @@ +// Copyright 2023 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 "fastpair/common/fast_pair_switches.h" + +#include + +namespace nearby { +namespace fastpair { +namespace switches { + +namespace { + +// Switch value for nearby fast pair global host. +int global_host_size = 0; +char global_host[256]; + +} // namespace + +void SetNearbyFastPairHttpHost(const std::string& host) { + if (host.size() > 256) { + return; + } + global_host_size = host.size(); + memcpy(global_host, host.c_str(), global_host_size); +} + +std::string GetNearbyFastPairHttpHost() { + return std::string(global_host, global_host_size); +} + +} // namespace switches +} // namespace fastpair +} // namespace nearby diff --git a/fastpair/common/fast_pair_switches.h b/fastpair/common/fast_pair_switches.h new file mode 100644 index 00000000..1b3a51ca --- /dev/null +++ b/fastpair/common/fast_pair_switches.h @@ -0,0 +1,37 @@ +// Copyright 2023 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 THIRD_PARTY_NEARBY_FASTPAIR_COMMON_FASTPAIR_SWITCHES_H_ +#define THIRD_PARTY_NEARBY_FASTPAIR_COMMON_FASTPAIR_SWITCHES_H_ + +#include + +#include "fastpair/common/fast_pair_switches.h" + +namespace nearby { +namespace fastpair { +namespace switches { + +// All switches in alphabetical order. The switches should be documented +// alongside the definition of their values in the .cc file. + +// Setter and Getter for Nearby Fast Pair http host. +void SetNearbyFastPairHttpHost(const std::string& host); +std::string GetNearbyFastPairHttpHost(); + +} // namespace switches +} // namespace fastpair +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_FASTPAIR_COMMON_FASTPAIR_SWITCHES_H_ diff --git a/fastpair/common/fast_pair_switches_test.cc b/fastpair/common/fast_pair_switches_test.cc new file mode 100644 index 00000000..b085cb2b --- /dev/null +++ b/fastpair/common/fast_pair_switches_test.cc @@ -0,0 +1,51 @@ +// Copyright 2023 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 "fastpair/common/fast_pair_switches.h" + +#include + +#include "gmock/gmock.h" +#include "protobuf-matchers/protocol-buffer-matchers.h" +#include "gtest/gtest.h" + +namespace nearby { +namespace fastpair { +namespace switches { +namespace { + +TEST(FastPairSwitches, DefautEmptyHttpHost) { + EXPECT_THAT(GetNearbyFastPairHttpHost(), testing::IsEmpty()); +} + +TEST(FastPairSwitches, SetLongSizedHttpHostSkipped) { + std::string s(5000, '*'); + SetNearbyFastPairHttpHost(s); + EXPECT_THAT(GetNearbyFastPairHttpHost(), testing::IsEmpty()); +} + +TEST(FastPairSwitches, SetNonEmptyHttpHost) { + SetNearbyFastPairHttpHost("fakehost.com"); + EXPECT_THAT(GetNearbyFastPairHttpHost(), "fakehost.com"); +} + +TEST(FastPairSwitches, SetEmptyHttpHost) { + SetNearbyFastPairHttpHost(""); + EXPECT_THAT(GetNearbyFastPairHttpHost(), testing::IsEmpty()); +} + +} // namespace +} // namespace switches +} // namespace fastpair +} // namespace nearby diff --git a/fastpair/repository/fast_pair_metadata_repository_impl_test.cc b/fastpair/repository/fast_pair_metadata_repository_impl_test.cc index 98a0abbe..be8acc6b 100644 --- a/fastpair/repository/fast_pair_metadata_repository_impl_test.cc +++ b/fastpair/repository/fast_pair_metadata_repository_impl_test.cc @@ -91,7 +91,6 @@ class FastPairMetadataRepositoryImplTest : public ::testing::Test { void SetUp() override { http_client_ = std::make_unique(); - SetNearbySharedHttpHost(kTestGoogleApisUrl); auto fetcher = std::make_unique(); fetcher_ = fetcher.get(); @@ -100,16 +99,6 @@ class FastPairMetadataRepositoryImplTest : public ::testing::Test { std::move(fetcher), std::move(http_client_)); } - void SetNearbySharedHttpHost(const std::string& host) { - int global_host_size = 0; - char global_host[256]; - if (host.size() > 256) { - return; - } - global_host_size = host.size(); - memcpy(global_host, host.c_str(), global_host_size); - } - const FastPairMetadataFetcher::QueryParameters& request_as_query_parameters() { return fetcher_->request_as_query_parameters_;