tls_test_utils.cc
Go to the documentation of this file.
1 //
2 // Copyright 2021 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 <memory>
20 
21 #include "src/core/lib/gprpp/thd.h"
22 #include "test/core/util/port.h"
24 
25 using ::grpc::experimental::ExternalCertificateVerifier;
26 using ::grpc::experimental::TlsCustomVerificationCheckRequest;
27 
28 namespace grpc {
29 namespace testing {
30 
31 bool SyncCertificateVerifier::Verify(TlsCustomVerificationCheckRequest*,
33  grpc::Status* sync_status) {
34  if (!success_) {
36  "SyncCertificateVerifier failed");
37  } else {
38  *sync_status = grpc::Status(grpc::StatusCode::OK, "");
39  }
40  return true;
41 }
42 
44  : success_(success),
45  thread_("AsyncCertificateVerifierWorkerThread", WorkerThread, this) {
46  thread_.Start();
47 }
48 
50  // Tell the thread to shut down.
51  {
52  internal::MutexLock lock(&mu_);
53  queue_.push_back(Request{nullptr, nullptr, true});
54  }
55  // Wait for thread to exit.
56  thread_.Join();
57 }
58 
60  TlsCustomVerificationCheckRequest* request,
62  internal::MutexLock lock(&mu_);
63  queue_.push_back(Request{request, std::move(callback), false});
64  return false; // Asynchronous call
65 }
66 
68  auto* self = static_cast<AsyncCertificateVerifier*>(arg);
69  while (true) {
70  // Check queue for work.
71  bool got_request = false;
73  {
74  internal::MutexLock lock(&self->mu_);
75  if (!self->queue_.empty()) {
76  got_request = true;
77  request = self->queue_.front();
78  self->queue_.pop_front();
79  }
80  }
81  // If nothing found in the queue, sleep for a bit and try again.
82  if (!got_request) {
84  continue;
85  }
86  // If we're being told to shut down, return.
87  if (request.shutdown) return;
88  auto return_status = grpc::Status(grpc::StatusCode::OK, "");
89  // Process the request.
90  if (!self->success_) {
92  "AsyncCertificateVerifier failed");
93  }
94  request.callback(return_status);
95  }
96 }
97 
98 } // namespace testing
99 } // namespace grpc
grpc::testing::AsyncCertificateVerifier::thread_
grpc_core::Thread thread_
Definition: tls_test_utils.h:74
testing
Definition: aws_request_signer_test.cc:25
port.h
grpc
Definition: grpcpp/alarm.h:33
benchmark.request
request
Definition: benchmark.py:77
grpc::testing::SyncCertificateVerifier::Verify
bool Verify(grpc::experimental::TlsCustomVerificationCheckRequest *request, std::function< void(grpc::Status)> callback, grpc::Status *sync_status) override
Definition: tls_test_utils.cc:31
tls_test_utils.h
grpc::internal::MutexLock
Definition: include/grpcpp/impl/codegen/sync.h:86
grpc::testing::AsyncCertificateVerifier::Request
Definition: tls_test_utils.h:65
grpc::testing::AsyncCertificateVerifier::AsyncCertificateVerifier
AsyncCertificateVerifier(bool success)
Definition: tls_test_utils.cc:43
grpc::testing::AsyncCertificateVerifier::mu_
grpc::internal::Mutex mu_
Definition: tls_test_utils.h:75
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
grpc.StatusCode.OK
tuple OK
Definition: src/python/grpcio/grpc/__init__.py:260
grpc_timeout_milliseconds_to_deadline
gpr_timespec grpc_timeout_milliseconds_to_deadline(int64_t time_ms)
Definition: test/core/util/test_config.cc:89
gpr_sleep_until
GPRAPI void gpr_sleep_until(gpr_timespec until)
arg
Definition: cmdline.cc:40
grpc_core::Thread::Join
void Join()
Definition: thd.h:141
grpc.StatusCode.UNAUTHENTICATED
tuple UNAUTHENTICATED
Definition: src/python/grpcio/grpc/__init__.py:280
callback
static void callback(void *arg, int status, int timeouts, struct hostent *host)
Definition: acountry.c:224
grpc::testing::AsyncCertificateVerifier::~AsyncCertificateVerifier
~AsyncCertificateVerifier() override
Definition: tls_test_utils.cc:49
grpc::testing::SyncCertificateVerifier::success_
bool success_
Definition: tls_test_utils.h:46
grpc_core::Thread::Start
void Start()
Definition: thd.h:125
test_config.h
grpc::testing::AsyncCertificateVerifier
Definition: tls_test_utils.h:49
thd.h
grpc::protobuf::util::Status
GRPC_CUSTOM_UTIL_STATUS Status
Definition: include/grpcpp/impl/codegen/config_protobuf.h:93
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
arg
struct arg arg
grpc::testing::AsyncCertificateVerifier::WorkerThread
static void WorkerThread(void *arg)
Definition: tls_test_utils.cc:67
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
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::testing::AsyncCertificateVerifier::Verify
bool Verify(grpc::experimental::TlsCustomVerificationCheckRequest *request, std::function< void(grpc::Status)> callback, grpc::Status *sync_status) override
Definition: tls_test_utils.cc:59
thread_
std::unique_ptr< std::thread > thread_
Definition: settings_timeout_test.cc:104


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:40