| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 | //// Copyright 2020 gRPC authors.//// 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////     http://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 <gmock/gmock.h>#include <grpc/grpc.h>#include <grpc/grpc_security.h>#include <grpcpp/security/server_credentials.h>#include <grpcpp/security/tls_credentials_options.h>#include <gtest/gtest.h>#include <memory>#include "src/cpp/client/secure_credentials.h"#include "test/core/util/port.h"#include "test/core/util/test_config.h"namespace {constexpr const char* kRootCertName = "root_cert_name";constexpr const char* kRootCertContents = "root_cert_contents";constexpr const char* kIdentityCertName = "identity_cert_name";constexpr const char* kIdentityCertPrivateKey = "identity_private_key";constexpr const char* kIdentityCertContents = "identity_cert_contents";using ::grpc::experimental::StaticDataCertificateProvider;}  // namespacenamespace grpc {namespace testing {namespace {TEST(    CredentialsTest,    TlsServerCredentialsWithStaticDataCertificateProviderLoadingRootAndIdentity) {  experimental::IdentityKeyCertPair key_cert_pair;  key_cert_pair.private_key = kIdentityCertPrivateKey;  key_cert_pair.certificate_chain = kIdentityCertContents;  std::vector<experimental::IdentityKeyCertPair> identity_key_cert_pairs;  identity_key_cert_pairs.emplace_back(key_cert_pair);  auto certificate_provider = std::make_shared<StaticDataCertificateProvider>(      kRootCertContents, identity_key_cert_pairs);  grpc::experimental::TlsServerCredentialsOptions options(certificate_provider);  options.watch_root_certs();  options.set_root_cert_name(kRootCertName);  options.watch_identity_key_cert_pairs();  options.set_identity_cert_name(kIdentityCertName);  options.set_cert_request_type(      GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY);  auto server_credentials = grpc::experimental::TlsServerCredentials(options);  GPR_ASSERT(server_credentials.get() != nullptr);}// ServerCredentials should always have identity credential presented.// Otherwise gRPC stack will fail.TEST(CredentialsTest,     TlsServerCredentialsWithStaticDataCertificateProviderLoadingIdentityOnly) {  experimental::IdentityKeyCertPair key_cert_pair;  key_cert_pair.private_key = kIdentityCertPrivateKey;  key_cert_pair.certificate_chain = kIdentityCertContents;  std::vector<experimental::IdentityKeyCertPair> identity_key_cert_pairs;  // Adding two key_cert_pair(s) should still work.  identity_key_cert_pairs.emplace_back(key_cert_pair);  identity_key_cert_pairs.emplace_back(key_cert_pair);  auto certificate_provider =      std::make_shared<StaticDataCertificateProvider>(identity_key_cert_pairs);  grpc::experimental::TlsServerCredentialsOptions options(certificate_provider);  options.watch_identity_key_cert_pairs();  options.set_identity_cert_name(kIdentityCertName);  options.set_cert_request_type(      GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY);  auto server_credentials = grpc::experimental::TlsServerCredentials(options);  GPR_ASSERT(server_credentials.get() != nullptr);}}  // namespace}  // namespace testing}  // namespace grpcint main(int argc, char** argv) {  ::testing::InitGoogleTest(&argc, argv);  grpc::testing::TestEnvironment env(argc, argv);  int ret = RUN_ALL_TESTS();  return ret;}
 |