[tls] Add copy constructor for TlsCredentialsOptions

pull/35499/head
David Chamberlin 1 year ago
parent 1751f1043e
commit 330165930f
  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