[tls] Add copy constructor for TlsCredentialsOptions (#35499)

<!--

If you know who should review your pull request, please assign it to that
person, otherwise the pull request would get assigned randomly.

If your pull request is for a specific language, please add the appropriate
lang label.

-->

Closes #35499

COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/35499 from dawidcha:cred_opts_copy_constr 330165930f
PiperOrigin-RevId: 599977221
pull/35620/head
David Chamberlin 1 year ago committed by Copybara-Service
parent d4f1f8543e
commit 5b724c09c5
  1. 6
      include/grpcpp/security/tls_credentials_options.h
  2. 6
      src/cpp/common/tls_credentials_options.cc
  3. 18
      test/cpp/client/credentials_test.cc

@ -45,6 +45,12 @@ class TlsCredentialsOptions {
// will be used in the TLS handshake
TlsCredentialsOptions();
~TlsCredentialsOptions();
// Copy constructor does a deep copy of the underlying pointer. No assignment
// permitted
TlsCredentialsOptions(const TlsCredentialsOptions& other);
TlsCredentialsOptions& operator=(const TlsCredentialsOptions& other) = delete;
// ---- Setters for member fields ----
// Sets the certificate provider used to store root certs and identity certs.
void set_certificate_provider(

@ -39,6 +39,12 @@ TlsCredentialsOptions::~TlsCredentialsOptions() {
grpc_tls_credentials_options_destroy(c_credentials_options_);
}
TlsCredentialsOptions::TlsCredentialsOptions(
const TlsCredentialsOptions& other) {
c_credentials_options_ =
grpc_tls_credentials_options_copy(other.c_credentials_options_);
}
void TlsCredentialsOptions::set_certificate_provider(
std::shared_ptr<CertificateProviderInterface> certificate_provider) {
certificate_provider_ = certificate_provider;

@ -423,6 +423,24 @@ TEST(CredentialsTest, TlsChannelCredentialsWithCrlProviderAndDirectory) {
GPR_ASSERT(channel_credentials.get() != nullptr);
}
TEST(CredentialsTest, TlsCredentialsOptionsCopyConstructor) {
// define a class that grants access to the internal
// grpc_tls_credentials_options pointer
class TlsTestCredentialsOptions
: public grpc::experimental::TlsCredentialsOptions {
public:
grpc_tls_credentials_options* internal_cred_opts() {
return mutable_c_credentials_options();
}
};
TlsTestCredentialsOptions options;
TlsTestCredentialsOptions copied_options = options;
// Make sure the copy constructor cloned the internal pointer
GPR_ASSERT(options.internal_cred_opts() !=
copied_options.internal_cred_opts());
}
TEST(CredentialsTest, TlsCredentialsOptionsDoesNotLeak) {
TlsCredentialsOptions options;
(void)options;

Loading…
Cancel
Save