mirror of https://github.com/grpc/grpc.git
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.
77 lines
2.5 KiB
77 lines
2.5 KiB
4 years ago
|
//
|
||
|
// 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.
|
||
|
//
|
||
|
|
||
4 years ago
|
#include "test/core/util/tls_utils.h"
|
||
4 years ago
|
|
||
|
#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;
|
||
|
}
|
||
|
|
||
4 years ago
|
PemKeyCertPairList MakeCertKeyPairs(absl::string_view private_key,
|
||
|
absl::string_view certs) {
|
||
|
if (private_key.empty() && certs.empty()) {
|
||
4 years ago
|
return {};
|
||
|
}
|
||
4 years ago
|
return PemKeyCertPairList{PemKeyCertPair(private_key, certs)};
|
||
4 years ago
|
}
|
||
|
|
||
|
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
|