tls_test_utils.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #ifndef GRPC_TEST_CPP_UTIL_TLS_TEST_UTILS_H
  17. #define GRPC_TEST_CPP_UTIL_TLS_TEST_UTILS_H
  18. #include <deque>
  19. #include <grpc/grpc.h>
  20. #include <grpc/grpc_security.h>
  21. #include <grpcpp/security/server_credentials.h>
  22. #include "src/core/lib/gprpp/thd.h"
  23. namespace grpc {
  24. namespace testing {
  25. class SyncCertificateVerifier
  26. : public grpc::experimental::ExternalCertificateVerifier {
  27. public:
  28. explicit SyncCertificateVerifier(bool success) : success_(success) {}
  29. ~SyncCertificateVerifier() override {}
  30. bool Verify(grpc::experimental::TlsCustomVerificationCheckRequest* request,
  31. std::function<void(grpc::Status)> callback,
  32. grpc::Status* sync_status) override;
  33. void Cancel(grpc::experimental::TlsCustomVerificationCheckRequest*) override {
  34. }
  35. private:
  36. bool success_ = false;
  37. };
  38. class AsyncCertificateVerifier
  39. : public grpc::experimental::ExternalCertificateVerifier {
  40. public:
  41. explicit AsyncCertificateVerifier(bool success);
  42. ~AsyncCertificateVerifier() override;
  43. bool Verify(grpc::experimental::TlsCustomVerificationCheckRequest* request,
  44. std::function<void(grpc::Status)> callback,
  45. grpc::Status* sync_status) override;
  46. void Cancel(grpc::experimental::TlsCustomVerificationCheckRequest*) override {
  47. }
  48. private:
  49. // A request to pass to the worker thread.
  50. struct Request {
  51. grpc::experimental::TlsCustomVerificationCheckRequest* request;
  52. std::function<void(grpc::Status)> callback;
  53. bool shutdown; // If true, thread will exit.
  54. };
  55. static void WorkerThread(void* arg);
  56. bool success_ = false;
  57. grpc_core::Thread thread_;
  58. grpc::internal::Mutex mu_;
  59. std::deque<Request> queue_ ABSL_GUARDED_BY(mu_);
  60. };
  61. } // namespace testing
  62. } // namespace grpc
  63. #endif // GRPC_TEST_CPP_UTIL_TLS_TEST_UTILS_H