generalize TmpFile interface (#27848)

pull/28225/head
Ashitha Santhosh 3 years ago committed by GitHub
parent 1290364374
commit f04eedd922
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      test/core/util/tls_utils.cc
  2. 10
      test/core/util/tls_utils.h

@ -26,16 +26,16 @@ namespace grpc_core {
namespace testing {
TmpFile::TmpFile(absl::string_view credential_data) {
name_ = CreateTmpFileAndWriteData(credential_data);
TmpFile::TmpFile(absl::string_view data) {
name_ = CreateTmpFileAndWriteData(data);
GPR_ASSERT(!name_.empty());
}
TmpFile::~TmpFile() { GPR_ASSERT(remove(name_.c_str()) == 0); }
void TmpFile::RewriteFile(absl::string_view credential_data) {
void TmpFile::RewriteFile(absl::string_view data) {
// Create a new file containing new data.
std::string new_name = CreateTmpFileAndWriteData(credential_data);
std::string new_name = CreateTmpFileAndWriteData(data);
GPR_ASSERT(!new_name.empty());
// Remove the old file.
GPR_ASSERT(remove(name_.c_str()) == 0);
@ -43,12 +43,11 @@ void TmpFile::RewriteFile(absl::string_view credential_data) {
GPR_ASSERT(rename(new_name.c_str(), name_.c_str()) == 0);
}
std::string TmpFile::CreateTmpFileAndWriteData(
absl::string_view credential_data) {
std::string TmpFile::CreateTmpFileAndWriteData(absl::string_view 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());
FILE* file_descriptor = gpr_tmpfile("test", &name);
GPR_ASSERT(fwrite(data.data(), 1, data.size(), file_descriptor) ==
data.size());
GPR_ASSERT(fclose(file_descriptor) == 0);
GPR_ASSERT(file_descriptor != nullptr);
GPR_ASSERT(name != nullptr);
@ -68,9 +67,9 @@ PemKeyCertPairList MakeCertKeyPairs(absl::string_view private_key,
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));
std::string data = std::string(StringViewFromSlice(slice));
grpc_slice_unref(slice);
return credential;
return data;
}
int SyncExternalVerifier::Verify(void* user_data,

@ -26,18 +26,18 @@ namespace testing {
class TmpFile {
public:
// Create a temporary file with |credential_data| written in.
explicit TmpFile(absl::string_view credential_data);
// Create a temporary file with |data| written in.
explicit TmpFile(absl::string_view data);
~TmpFile();
const std::string& name() { return name_; }
// Rewrite |credential_data| to the temporary file, in an atomic way.
void RewriteFile(absl::string_view credential_data);
// Rewrite |data| to the temporary file, in an atomic way.
void RewriteFile(absl::string_view data);
private:
std::string CreateTmpFileAndWriteData(absl::string_view credential_data);
std::string CreateTmpFileAndWriteData(absl::string_view data);
std::string name_;
};

Loading…
Cancel
Save