The C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#) https://grpc.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

83 lines
2.8 KiB

//
// 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 "test/core/security/tls_utils.h"
#include "src/core/lib/gpr/tmpfile.h"
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/slice/slice_internal.h"
namespace grpc_core {
namespace testing {
TmpFile::TmpFile(absl::string_view credential_data) {
name_ = CreateTmpFileAndWriteData(credential_data);
GPR_ASSERT(!name_.empty());
}
TmpFile::~TmpFile() { GPR_ASSERT(remove(name_.c_str()) == 0); }
void TmpFile::RewriteFile(absl::string_view credential_data) {
// Create a new file containing new data.
std::string new_name = CreateTmpFileAndWriteData(credential_data);
GPR_ASSERT(!new_name.empty());
// Remove the old file.
GPR_ASSERT(remove(name_.c_str()) == 0);
// Rename the new file to the original name.
GPR_ASSERT(rename(new_name.c_str(), name_.c_str()) == 0);
}
std::string TmpFile::CreateTmpFileAndWriteData(
absl::string_view credential_data) {
char* name = nullptr;
FILE* file_descriptor = gpr_tmpfile("GrpcTlsCertificateProviderTest", &name);
GPR_ASSERT(fwrite(credential_data.data(), 1, credential_data.size(),
file_descriptor) == credential_data.size());
GPR_ASSERT(fclose(file_descriptor) == 0);
GPR_ASSERT(file_descriptor != nullptr);
GPR_ASSERT(name != nullptr);
std::string name_to_return = name;
gpr_free(name);
return name_to_return;
}
PemKeyCertPairList MakeCertKeyPairs(const char* private_key,
const char* certs) {
if (strcmp(private_key, "") == 0 && strcmp(certs, "") == 0) {
return {};
}
grpc_ssl_pem_key_cert_pair* ssl_pair =
static_cast<grpc_ssl_pem_key_cert_pair*>(
gpr_malloc(sizeof(grpc_ssl_pem_key_cert_pair)));
ssl_pair->private_key = gpr_strdup(private_key);
ssl_pair->cert_chain = gpr_strdup(certs);
PemKeyCertPairList pem_key_cert_pairs;
pem_key_cert_pairs.emplace_back(ssl_pair);
return pem_key_cert_pairs;
}
std::string GetFileContents(const char* path) {
grpc_slice slice = grpc_empty_slice();
GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file", grpc_load_file(path, 0, &slice)));
std::string credential = std::string(StringViewFromSlice(slice));
grpc_slice_unref(slice);
return credential;
}
} // namespace testing
} // namespace grpc_core