test/core/util/tls_utils.cc
Go to the documentation of this file.
1 //
2 // Copyright 2020 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
18 
19 #include <grpc/support/log.h>
21 
25 
26 namespace grpc_core {
27 
28 namespace testing {
29 
32  GPR_ASSERT(!name_.empty());
33 }
34 
35 TmpFile::~TmpFile() { GPR_ASSERT(remove(name_.c_str()) == 0); }
36 
38  // Create a new file containing new data.
40  GPR_ASSERT(!new_name.empty());
41  // Remove the old file.
42  GPR_ASSERT(remove(name_.c_str()) == 0);
43  // Rename the new file to the original name.
44  GPR_ASSERT(rename(new_name.c_str(), name_.c_str()) == 0);
45 }
46 
48  char* name = nullptr;
49  FILE* file_descriptor = gpr_tmpfile("test", &name);
50  GPR_ASSERT(fwrite(data.data(), 1, data.size(), file_descriptor) ==
51  data.size());
52  GPR_ASSERT(fclose(file_descriptor) == 0);
53  GPR_ASSERT(file_descriptor != nullptr);
54  GPR_ASSERT(name != nullptr);
55  std::string name_to_return = name;
56  gpr_free(name);
57  return name_to_return;
58 }
59 
61  absl::string_view certs) {
62  if (private_key.empty() && certs.empty()) {
63  return {};
64  }
66 }
67 
73  return data;
74 }
75 
76 int SyncExternalVerifier::Verify(void* user_data,
79  void*, grpc_status_code* sync_status,
80  char** sync_error_details) {
81  auto* self = static_cast<SyncExternalVerifier*>(user_data);
82  if (self->success_) {
83  *sync_status = GRPC_STATUS_OK;
84  return true; // Synchronous call
85  }
86  *sync_status = GRPC_STATUS_UNAUTHENTICATED;
87  *sync_error_details = gpr_strdup("SyncExternalVerifier failed");
88  return true; // Synchronous call
89 }
90 
91 void SyncExternalVerifier::Destruct(void* user_data) {
92  auto* self = static_cast<SyncExternalVerifier*>(user_data);
93  delete self;
94 }
95 
97  // Tell the thread to shut down.
98  {
99  MutexLock lock(&mu_);
100  queue_.push_back(Request{nullptr, nullptr, nullptr, true});
101  }
102  // Wait for thread to exit.
103  thread_.Join();
104  grpc_shutdown();
105 }
106 
110  grpc_status_code*, char**) {
111  auto* self = static_cast<AsyncExternalVerifier*>(user_data);
112  // Add request to queue to be picked up by worker thread.
113  MutexLock lock(&self->mu_);
114  self->queue_.push_back(Request{request, callback, callback_arg, false});
115  return false; // Asynchronous call
116 }
117 
118 namespace {
119 
120 void DestroyExternalVerifier(void* arg) {
121  auto* verifier = static_cast<AsyncExternalVerifier*>(arg);
122  delete verifier;
123 }
124 
125 } // namespace
126 
127 void AsyncExternalVerifier::Destruct(void* user_data) {
128  auto* self = static_cast<AsyncExternalVerifier*>(user_data);
129  // Spawn a detached thread to destroy the verifier, to make sure that we don't
130  // try to join the worker thread from within the worker thread.
131  Thread destroy_thread("DestroyExternalVerifier", DestroyExternalVerifier,
132  self, nullptr, Thread::Options().set_joinable(false));
133  destroy_thread.Start();
134 }
135 
137  auto* self = static_cast<AsyncExternalVerifier*>(arg);
138  while (true) {
139  // Check queue for work.
140  bool got_request = false;
142  {
143  MutexLock lock(&self->mu_);
144  if (!self->queue_.empty()) {
145  got_request = true;
146  request = self->queue_.front();
147  self->queue_.pop_front();
148  }
149  }
150  // If nothing found in the queue, sleep for a bit and try again.
151  if (!got_request) {
153  continue;
154  }
155  // If we're being told to shut down, return.
156  if (request.shutdown) {
157  return;
158  }
159  // Process the request.
160  if (self->success_) {
161  request.callback(request.request, request.callback_arg, GRPC_STATUS_OK,
162  "");
163  } else {
164  request.callback(request.request, request.callback_arg,
166  "AsyncExternalVerifier failed");
167  }
168  }
169 }
170 
171 } // namespace testing
172 
173 } // namespace grpc_core
grpc_core::testing::TmpFile::TmpFile
TmpFile(absl::string_view data)
Definition: test/core/util/tls_utils.cc:30
grpc_slice_unref
GPRAPI void grpc_slice_unref(grpc_slice s)
Definition: slice_api.cc:32
testing
Definition: aws_request_signer_test.cc:25
grpc_core::testing::TmpFile::~TmpFile
~TmpFile()
Definition: test/core/util/tls_utils.cc:35
log.h
grpc_core::testing::SyncExternalVerifier
Definition: test/core/util/tls_utils.h:58
grpc_core::testing::MakeCertKeyPairs
PemKeyCertPairList MakeCertKeyPairs(absl::string_view private_key, absl::string_view certs)
Definition: test/core/util/tls_utils.cc:60
tls_utils.h
grpc_load_file
grpc_error_handle grpc_load_file(const char *filename, int add_null_terminator, grpc_slice *output)
Definition: load_file.cc:33
GRPC_STATUS_UNAUTHENTICATED
@ GRPC_STATUS_UNAUTHENTICATED
Definition: include/grpc/impl/codegen/status.h:72
load_file.h
grpc_core::testing::AsyncExternalVerifier::Verify
static int Verify(void *user_data, grpc_tls_custom_verification_check_request *request, grpc_tls_on_custom_verification_check_done_cb callback, void *callback_arg, grpc_status_code *sync_status, char **sync_error_details)
Definition: test/core/util/tls_utils.cc:107
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::MutexLock
Definition: src/core/lib/gprpp/sync.h:88
grpc_core::testing::TmpFile::CreateTmpFileAndWriteData
std::string CreateTmpFileAndWriteData(absl::string_view data)
Definition: test/core/util/tls_utils.cc:47
benchmark.request
request
Definition: benchmark.py:77
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
grpc_core::StringViewFromSlice
absl::string_view StringViewFromSlice(const grpc_slice &slice)
Definition: slice_internal.h:93
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
grpc_core::testing::AsyncExternalVerifier::~AsyncExternalVerifier
~AsyncExternalVerifier()
Definition: test/core/util/tls_utils.cc:96
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_status_code
grpc_status_code
Definition: include/grpc/impl/codegen/status.h:28
grpc_core::testing::TmpFile::name_
std::string name_
Definition: test/core/util/tls_utils.h:42
gpr_tmpfile
FILE * gpr_tmpfile(const char *prefix, char **tmp_filename)
GRPC_LOG_IF_ERROR
#define GRPC_LOG_IF_ERROR(what, error)
Definition: error.h:398
check_documentation.path
path
Definition: check_documentation.py:57
grpc_tls_on_custom_verification_check_done_cb
void(* grpc_tls_on_custom_verification_check_done_cb)(grpc_tls_custom_verification_check_request *request, void *callback_arg, grpc_status_code status, const char *error_details)
Definition: grpc_security.h:946
verifier
static void verifier(grpc_server *server, grpc_completion_queue *cq, void *)
Definition: badreq.cc:31
grpc_core::testing::AsyncExternalVerifier::thread_
Thread thread_
Definition: test/core/util/tls_utils.h:128
grpc_core::testing::TmpFile::RewriteFile
void RewriteFile(absl::string_view data)
Definition: test/core/util/tls_utils.cc:37
string_util.h
GRPC_STATUS_OK
@ GRPC_STATUS_OK
Definition: include/grpc/impl/codegen/status.h:30
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_timeout_milliseconds_to_deadline
gpr_timespec grpc_timeout_milliseconds_to_deadline(int64_t time_ms)
Definition: test/core/util/test_config.cc:89
slice
grpc_slice slice
Definition: src/core/lib/surface/server.cc:467
gpr_sleep_until
GPRAPI void gpr_sleep_until(gpr_timespec until)
tmpfile.h
arg
Definition: cmdline.cc:40
grpc_empty_slice
GPRAPI grpc_slice grpc_empty_slice(void)
Definition: slice/slice.cc:42
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
grpc_core::testing::AsyncExternalVerifier
Definition: test/core/util/tls_utils.h:92
grpc_core::Thread::Join
void Join()
Definition: thd.h:141
grpc_core::testing::SyncExternalVerifier::Verify
static int Verify(void *user_data, grpc_tls_custom_verification_check_request *request, grpc_tls_on_custom_verification_check_done_cb callback, void *callback_arg, grpc_status_code *sync_status, char **sync_error_details)
Definition: test/core/util/tls_utils.cc:76
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
callback
static void callback(void *arg, int status, int timeouts, struct hostent *host)
Definition: acountry.c:224
grpc_core::PemKeyCertPair
Definition: ssl_utils.h:145
slice_internal.h
grpc_core::Thread::Options
Definition: thd.h:45
grpc_core::testing::AsyncExternalVerifier::Request
Definition: test/core/util/tls_utils.h:108
grpc_core::Thread::Start
void Start()
Definition: thd.h:125
grpc_core::testing::AsyncExternalVerifier::mu_
Mutex mu_
Definition: test/core/util/tls_utils.h:130
grpc_core::testing::TmpFile::name
const std::string & name()
Definition: test/core/util/tls_utils.h:34
grpc_core::testing::AsyncExternalVerifier::Destruct
static void Destruct(void *user_data)
Definition: test/core/util/tls_utils.cc:127
benchmark.FILE
FILE
Definition: benchmark.py:21
grpc::fclose
fclose(creds_file)
grpc_core::PemKeyCertPairList
std::vector< PemKeyCertPair > PemKeyCertPairList
Definition: ssl_utils.h:183
grpc_core::testing::SyncExternalVerifier::Destruct
static void Destruct(void *user_data)
Definition: test/core/util/tls_utils.cc:91
private_key
Definition: hrss.c:1885
arg
struct arg arg
grpc_core::Thread
Definition: thd.h:43
grpc_tls_custom_verification_check_request
Definition: grpc_security.h:907
gpr_strdup
GPRAPI char * gpr_strdup(const char *src)
Definition: string.cc:39
absl::string_view::empty
constexpr bool empty() const noexcept
Definition: abseil-cpp/absl/strings/string_view.h:292
self
PHP_PROTO_OBJECT_FREE_END PHP_PROTO_OBJECT_DTOR_END intern self
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/map.c:543
grpc_core::testing::AsyncExternalVerifier::WorkerThread
static void WorkerThread(void *arg)
Definition: test/core/util/tls_utils.cc:136
grpc_core::testing::GetFileContents
std::string GetFileContents(const char *path)
Definition: test/core/util/tls_utils.cc:68
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:39