tls_test_utils.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include "test/cpp/util/tls_test_utils.h"
  17. #include <memory>
  18. #include "src/core/lib/gprpp/thd.h"
  19. #include "test/core/util/port.h"
  20. #include "test/core/util/test_config.h"
  21. using ::grpc::experimental::ExternalCertificateVerifier;
  22. using ::grpc::experimental::TlsCustomVerificationCheckRequest;
  23. namespace grpc {
  24. namespace testing {
  25. bool SyncCertificateVerifier::Verify(TlsCustomVerificationCheckRequest*,
  26. std::function<void(grpc::Status)>,
  27. grpc::Status* sync_status) {
  28. if (!success_) {
  29. *sync_status = grpc::Status(grpc::StatusCode::UNAUTHENTICATED,
  30. "SyncCertificateVerifier failed");
  31. } else {
  32. *sync_status = grpc::Status(grpc::StatusCode::OK, "");
  33. }
  34. return true;
  35. }
  36. AsyncCertificateVerifier::AsyncCertificateVerifier(bool success)
  37. : success_(success),
  38. thread_("AsyncCertificateVerifierWorkerThread", WorkerThread, this) {
  39. thread_.Start();
  40. }
  41. AsyncCertificateVerifier::~AsyncCertificateVerifier() {
  42. // Tell the thread to shut down.
  43. {
  44. internal::MutexLock lock(&mu_);
  45. queue_.push_back(Request{nullptr, nullptr, true});
  46. }
  47. // Wait for thread to exit.
  48. thread_.Join();
  49. }
  50. bool AsyncCertificateVerifier::Verify(
  51. TlsCustomVerificationCheckRequest* request,
  52. std::function<void(grpc::Status)> callback, grpc::Status*) {
  53. internal::MutexLock lock(&mu_);
  54. queue_.push_back(Request{request, std::move(callback), false});
  55. return false; // Asynchronous call
  56. }
  57. void AsyncCertificateVerifier::WorkerThread(void* arg) {
  58. auto* self = static_cast<AsyncCertificateVerifier*>(arg);
  59. while (true) {
  60. // Check queue for work.
  61. bool got_request = false;
  62. Request request;
  63. {
  64. internal::MutexLock lock(&self->mu_);
  65. if (!self->queue_.empty()) {
  66. got_request = true;
  67. request = self->queue_.front();
  68. self->queue_.pop_front();
  69. }
  70. }
  71. // If nothing found in the queue, sleep for a bit and try again.
  72. if (!got_request) {
  73. gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(100));
  74. continue;
  75. }
  76. // If we're being told to shut down, return.
  77. if (request.shutdown) return;
  78. auto return_status = grpc::Status(grpc::StatusCode::OK, "");
  79. // Process the request.
  80. if (!self->success_) {
  81. return_status = grpc::Status(grpc::StatusCode::UNAUTHENTICATED,
  82. "AsyncCertificateVerifier failed");
  83. }
  84. request.callback(return_status);
  85. }
  86. }
  87. } // namespace testing
  88. } // namespace grpc