diff --git a/presence/BUILD b/presence/BUILD index c4b38578..8969a12c 100644 --- a/presence/BUILD +++ b/presence/BUILD @@ -34,9 +34,9 @@ cc_library( "//internal/platform:types", "//internal/proto:metadata_cc_proto", "//presence/implementation:internal", # build_cleaner: keep + "//presence/implementation/mediums", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", - "@com_google_absl//absl/types:variant", ], ) diff --git a/presence/implementation/BUILD b/presence/implementation/BUILD index a7b9de53..52b96d63 100644 --- a/presence/implementation/BUILD +++ b/presence/implementation/BUILD @@ -1,3 +1,5 @@ +load("@bazel_skylib//lib:selects.bzl", "selects") + # Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -12,7 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") -load("@bazel_skylib//lib:selects.bzl", "selects") licenses(["notice"]) @@ -136,6 +137,7 @@ cc_library( srcs = [ ], hdrs = [ + "mock_credential_manager.h", "mock_service_controller.h", ], visibility = [ @@ -143,8 +145,9 @@ cc_library( ], deps = [ ":internal", - "//presence", + "//internal/platform/implementation:comm", "@com_github_protobuf_matchers//protobuf-matchers", + "@com_google_absl//absl/strings:string_view", "@com_google_googletest//:gtest_main", ], ) @@ -352,3 +355,34 @@ cc_test( ], }), ) + +cc_test( + name = "service_controller_impl_test", + size = "small", + srcs = ["service_controller_impl_test.cc"], + deps = [ + ":internal", + ":internal_test", + "//internal/platform:comm", + "//internal/platform:test_util", + "//internal/platform:types", + "//internal/platform/implementation:comm", + "//internal/platform/implementation:types", + "//internal/proto:credential_cc_proto", + "//net/proto2/contrib/parse_proto:testing", + "//presence/implementation/mediums", + "@com_github_protobuf_matchers//protobuf-matchers", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/time", + "@com_google_googletest//:gtest_main", + ] + select({ + "@platforms//os:windows": [ + "//internal/platform/implementation/windows", + ], + "//conditions:default": [ + "//internal/platform/implementation/g3", + ], + }), +) diff --git a/presence/implementation/mediums/BUILD b/presence/implementation/mediums/BUILD index e30cdad4..a2592bc7 100644 --- a/presence/implementation/mediums/BUILD +++ b/presence/implementation/mediums/BUILD @@ -23,7 +23,7 @@ cc_library( "mediums.h", ], visibility = [ - "//presence/implementation:__subpackages__", + "//presence:__subpackages__", ], deps = [ "//internal/platform:comm", diff --git a/presence/implementation/mock_credential_manager.h b/presence/implementation/mock_credential_manager.h new file mode 100644 index 00000000..9334a70a --- /dev/null +++ b/presence/implementation/mock_credential_manager.h @@ -0,0 +1,85 @@ +// 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_PRESENCE_IMPLEMENTATION_MOCK_CREDENTIAL_MANAGER_H_ +#define THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_MOCK_CREDENTIAL_MANAGER_H_ + +#include +#include + +#include "gmock/gmock.h" +#include "absl/strings/string_view.h" +#include "internal/platform/implementation/credential_callbacks.h" +#include "presence/implementation/credential_manager.h" + +namespace nearby { +namespace presence { + +class MockCredentialManager : public CredentialManager { + public: + MOCK_METHOD( + void, GenerateCredentials, + (const nearby::internal::Metadata& metadata, + absl::string_view manager_app_id, + const std::vector& identity_types, + int credential_life_cycle_days, int contiguous_copy_of_credentials, + GenerateCredentialsResultCallback credentials_generated_cb), + (override)); + MOCK_METHOD(void, UpdateRemotePublicCredentials, + (absl::string_view manager_app_id, absl::string_view account_name, + const std::vector& + remote_public_creds, + UpdateRemotePublicCredentialsCallback credentials_updated_cb), + (override)); + MOCK_METHOD(void, UpdateLocalCredential, + (const CredentialSelector& credential_selector, + nearby::internal::LocalCredential credential, + SaveCredentialsResultCallback result_callback), + (override)); + MOCK_METHOD(void, GetLocalCredentials, + (const CredentialSelector& credential_selector, + GetLocalCredentialsResultCallback callback), + (override)); + MOCK_METHOD(void, GetPublicCredentials, + (const CredentialSelector& credential_selector, + PublicCredentialType public_credential_type, + GetPublicCredentialsResultCallback callback), + (override)); + MOCK_METHOD(SubscriberId, SubscribeForPublicCredentials, + (const CredentialSelector& credential_selector, + PublicCredentialType public_credential_type, + GetPublicCredentialsResultCallback callback), + (override)); + MOCK_METHOD(void, UnsubscribeFromPublicCredentials, (SubscriberId id), + (override)); + MOCK_METHOD(std::string, DecryptMetadata, + (absl::string_view metadata_encryption_key, + absl::string_view key_seed, absl::string_view metadata_string), + (override)); + MOCK_METHOD( + void, SetLocalDeviceMetadata, + (const ::nearby::internal::Metadata& metadata, bool regen_credentials, + absl::string_view manager_app_id, + const std::vector& identity_types, + int credential_life_cycle_days, int contiguous_copy_of_credentials, + GenerateCredentialsResultCallback credentials_generated_cb), + (override)); + MOCK_METHOD(::nearby::internal::Metadata, GetLocalDeviceMetadata, (), + (override)); +}; + +} // namespace presence +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_MOCK_CREDENTIAL_MANAGER_H_ diff --git a/presence/implementation/service_controller.h b/presence/implementation/service_controller.h index bd27c003..cb9587a3 100644 --- a/presence/implementation/service_controller.h +++ b/presence/implementation/service_controller.h @@ -19,6 +19,7 @@ #include #include "absl/status/statusor.h" +#include "internal/platform/implementation/credential_callbacks.h" #include "internal/proto/metadata.pb.h" #include "presence/broadcast_request.h" #include "presence/data_types.h" @@ -57,6 +58,9 @@ class ServiceController { const std::vector& remote_public_creds, UpdateRemotePublicCredentialsCallback credentials_updated_cb) = 0; + virtual void GetLocalCredentials( + const CredentialSelector& credential_selector, + GetLocalCredentialsResultCallback callback) = 0; }; } // namespace presence diff --git a/presence/implementation/service_controller_impl.cc b/presence/implementation/service_controller_impl.cc index 60efdc31..9c806f79 100644 --- a/presence/implementation/service_controller_impl.cc +++ b/presence/implementation/service_controller_impl.cc @@ -18,7 +18,9 @@ #include #include "absl/status/statusor.h" +#include "internal/platform/implementation/credential_callbacks.h" #include "presence/data_types.h" +#include "presence/implementation/credential_manager.h" namespace nearby { namespace presence { @@ -70,5 +72,12 @@ void ServiceControllerImpl::UpdateRemotePublicCredentials( std::move(credentials_updated_cb)); } +void ServiceControllerImpl::GetLocalCredentials( + const CredentialSelector& credential_selector, + GetLocalCredentialsResultCallback callback) { + credential_manager_.GetLocalCredentials(credential_selector, + std::move(callback)); +} + } // namespace presence } // namespace nearby diff --git a/presence/implementation/service_controller_impl.h b/presence/implementation/service_controller_impl.h index 3590d34d..641ced7a 100644 --- a/presence/implementation/service_controller_impl.h +++ b/presence/implementation/service_controller_impl.h @@ -20,9 +20,10 @@ #include #include "absl/status/statusor.h" +#include "absl/strings/string_view.h" #include "internal/proto/metadata.pb.h" #include "presence/implementation/broadcast_manager.h" -#include "presence/implementation/credential_manager_impl.h" +#include "presence/implementation/credential_manager.h" #include "presence/implementation/mediums/mediums.h" #include "presence/implementation/scan_manager.h" #include "presence/implementation/service_controller.h" @@ -39,6 +40,14 @@ class ServiceControllerImpl : public ServiceController { public: using SingleThreadExecutor = ::nearby::SingleThreadExecutor; + ServiceControllerImpl(SingleThreadExecutor* executor, + CredentialManager* credential_manager, + ScanManager* scan_manager, + BroadcastManager* broadcast_manager) + : executor_(*executor), + credential_manager_(*credential_manager), + scan_manager_(*scan_manager), + broadcast_manager_(*broadcast_manager) {} ~ServiceControllerImpl() override { executor_.Shutdown(); } absl::StatusOr StartScan(ScanRequest scan_request, @@ -65,24 +74,21 @@ class ServiceControllerImpl : public ServiceController { const std::vector& remote_public_creds, UpdateRemotePublicCredentialsCallback credentials_updated_cb) override; + void GetLocalCredentials(const CredentialSelector& credential_selector, + GetLocalCredentialsResultCallback callback) override; SingleThreadExecutor& GetBackgroundExecutor() { return executor_; } - // Gives tests access to mediums. - Mediums& GetMediums() { return mediums_; } - private: - SingleThreadExecutor executor_; void NotifyStartCallbackStatus(BroadcastSessionId id, absl::Status status); void RunOnServiceControllerThread(absl::string_view name, Runnable runnable) { executor_.Execute(std::string(name), std::move(runnable)); } - Mediums mediums_; // NOLINT: further impl will use it. - CredentialManagerImpl credential_manager_{ - &executor_}; // NOLINT: further impl will use it. - ScanManager scan_manager_{mediums_, credential_manager_, - executor_}; // NOLINT: further impl will use it. - BroadcastManager broadcast_manager_{mediums_, credential_manager_, executor_}; + + SingleThreadExecutor& executor_; + CredentialManager& credential_manager_; + ScanManager& scan_manager_; + BroadcastManager& broadcast_manager_; }; } // namespace presence diff --git a/presence/implementation/service_controller_impl_test.cc b/presence/implementation/service_controller_impl_test.cc new file mode 100644 index 00000000..84bafd8f --- /dev/null +++ b/presence/implementation/service_controller_impl_test.cc @@ -0,0 +1,102 @@ +// 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 "presence/implementation/service_controller_impl.h" + +#include +#include +#include +#include + +#include "gmock/gmock.h" +#include "protobuf-matchers/protocol-buffer-matchers.h" +#include "gtest/gtest.h" +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" +#include "internal/platform/implementation/credential_callbacks.h" +#include "internal/platform/single_thread_executor.h" +#include "internal/proto/credential.pb.h" +#include "presence/implementation/broadcast_manager.h" +#include "presence/implementation/mediums/mediums.h" +#include "presence/implementation/mock_credential_manager.h" +#include "presence/implementation/scan_manager.h" + +namespace nearby { +namespace presence { +namespace { + +constexpr absl::string_view kManagerAppId = "TEST_MANAGER_APP"; +constexpr absl::string_view kAccountName = "test account"; +constexpr absl::string_view kSecretId1 = "1111111"; +constexpr absl::string_view kSecretId2 = "2222222"; +constexpr absl::string_view kSecretId3 = "3333333"; + +CredentialSelector BuildDefaultCredentialSelector() { + CredentialSelector credential_selector; + credential_selector.manager_app_id = std::string(kManagerAppId); + credential_selector.account_name = std::string(kAccountName); + credential_selector.identity_type = + ::nearby::internal::IdentityType::IDENTITY_TYPE_PRIVATE; + return credential_selector; +} + +std::vector BuildLocalCredentials() { + internal::LocalCredential local_credential1; + local_credential1.set_secret_id(kSecretId1); + internal::LocalCredential local_credential2; + local_credential2.set_secret_id(kSecretId2); + internal::LocalCredential local_credential3; + local_credential3.set_secret_id(kSecretId3); + return {local_credential1, local_credential2, local_credential3}; +} + +TEST(ServiceControllerImplTest, GetLocalCredentials) { + auto mock_credential_manager = std::make_unique(); + EXPECT_CALL(*mock_credential_manager.get(), GetLocalCredentials) + .WillOnce([&](const CredentialSelector& credential_selector, + GetLocalCredentialsResultCallback callback) { + callback.credentials_fetched_cb(BuildLocalCredentials()); + }); + + Mediums mediums; + SingleThreadExecutor executor; + ScanManager scan_manager{mediums, *mock_credential_manager, executor}; + BroadcastManager broadcast_manager{mediums, *mock_credential_manager, + executor}; + + auto service_controller = std::make_unique( + &executor, mock_credential_manager.get(), &scan_manager, + &broadcast_manager); + CredentialSelector credential_selector = BuildDefaultCredentialSelector(); + + absl::StatusOr> + private_credentials; + service_controller->GetLocalCredentials( + credential_selector, + {.credentials_fetched_cb = + [&](absl::StatusOr> + credentials) { + private_credentials = std::move(credentials); + }}); + + EXPECT_OK(private_credentials); + ASSERT_EQ(3u, private_credentials->size()); + ASSERT_EQ(private_credentials->at(0).secret_id(), kSecretId1); + ASSERT_EQ(private_credentials->at(1).secret_id(), kSecretId2); + ASSERT_EQ(private_credentials->at(2).secret_id(), kSecretId3); +} + +} // namespace +} // namespace presence +} // namespace nearby diff --git a/presence/presence_service_impl.cc b/presence/presence_service_impl.cc index a40eb6d1..8b14d936 100644 --- a/presence/presence_service_impl.cc +++ b/presence/presence_service_impl.cc @@ -27,7 +27,8 @@ namespace nearby { namespace presence { PresenceServiceImpl::PresenceServiceImpl() { - service_controller_ = std::make_unique(); + service_controller_ = std::make_unique( + &executor_, &credential_manager_, &scan_manager_, &broadcast_manager_); provider_ = std::make_unique( service_controller_->GetLocalDeviceMetadata()); } diff --git a/presence/presence_service_impl.h b/presence/presence_service_impl.h index ef8afeec..76dc6e86 100644 --- a/presence/presence_service_impl.h +++ b/presence/presence_service_impl.h @@ -22,6 +22,10 @@ #include "internal/platform/borrowable.h" #include "internal/proto/metadata.pb.h" #include "presence/data_types.h" +#include "presence/implementation/broadcast_manager.h" +#include "presence/implementation/credential_manager_impl.h" +#include "presence/implementation/mediums/mediums.h" +#include "presence/implementation/scan_manager.h" #include "presence/implementation/service_controller.h" #include "presence/presence_client.h" #include "presence/presence_device_provider.h" @@ -77,6 +81,11 @@ class PresenceServiceImpl : public PresenceService { UpdateRemotePublicCredentialsCallback credentials_updated_cb) override; private: + SingleThreadExecutor executor_; + Mediums mediums_; + CredentialManagerImpl credential_manager_{&executor_}; + ScanManager scan_manager_{mediums_, credential_manager_, executor_}; + BroadcastManager broadcast_manager_{mediums_, credential_manager_, executor_}; std::unique_ptr service_controller_; ::nearby::Lender lender_{this}; std::unique_ptr provider_;