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