Add Fast Pair Switches.

PiperOrigin-RevId: 538605609
This commit is contained in:
Chun Zhang
2023-06-07 15:17:00 -07:00
committed by Copybara-Service
parent 4095b7978d
commit e101795cac
5 changed files with 150 additions and 11 deletions
+17
View File
@@ -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",
],
)
+45
View File
@@ -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 <string>
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
+37
View File
@@ -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 <string>
#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_
@@ -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 <string>
#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
@@ -91,7 +91,6 @@ class FastPairMetadataRepositoryImplTest : public ::testing::Test {
void SetUp() override {
http_client_ = std::make_unique<network::FastPairFakeHttpClient>();
SetNearbySharedHttpHost(kTestGoogleApisUrl);
auto fetcher = std::make_unique<FakeFastPairMetadataFetcher>();
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_;