server_helper.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include "test/cpp/interop/server_helper.h"
  19. #include <memory>
  20. #include "absl/flags/declare.h"
  21. #include "absl/flags/flag.h"
  22. #include <grpcpp/security/server_credentials.h>
  23. #include "src/core/lib/surface/call_test_only.h"
  24. #include "src/core/lib/transport/byte_stream.h"
  25. #include "test/cpp/util/test_credentials_provider.h"
  26. ABSL_DECLARE_FLAG(bool, use_alts);
  27. ABSL_DECLARE_FLAG(bool, use_tls);
  28. ABSL_DECLARE_FLAG(std::string, custom_credentials_type);
  29. namespace grpc {
  30. namespace testing {
  31. std::shared_ptr<ServerCredentials> CreateInteropServerCredentials() {
  32. if (!absl::GetFlag(FLAGS_custom_credentials_type).empty()) {
  33. return GetCredentialsProvider()->GetServerCredentials(
  34. absl::GetFlag(FLAGS_custom_credentials_type));
  35. } else if (absl::GetFlag(FLAGS_use_alts)) {
  36. return GetCredentialsProvider()->GetServerCredentials(kAltsCredentialsType);
  37. } else if (absl::GetFlag(FLAGS_use_tls)) {
  38. return GetCredentialsProvider()->GetServerCredentials(kTlsCredentialsType);
  39. } else {
  40. return GetCredentialsProvider()->GetServerCredentials(
  41. kInsecureCredentialsType);
  42. }
  43. }
  44. InteropServerContextInspector::InteropServerContextInspector(
  45. const grpc::ServerContext& context)
  46. : context_(context) {}
  47. grpc_compression_algorithm
  48. InteropServerContextInspector::GetCallCompressionAlgorithm() const {
  49. return grpc_call_test_only_get_compression_algorithm(context_.call_.call);
  50. }
  51. uint32_t InteropServerContextInspector::GetEncodingsAcceptedByClient() const {
  52. return grpc_call_test_only_get_encodings_accepted_by_peer(
  53. context_.call_.call);
  54. }
  55. bool InteropServerContextInspector::WasCompressed() const {
  56. return (grpc_call_test_only_get_message_flags(context_.call_.call) &
  57. GRPC_WRITE_INTERNAL_COMPRESS) ||
  58. (grpc_call_test_only_get_message_flags(context_.call_.call) &
  59. GRPC_WRITE_INTERNAL_TEST_ONLY_WAS_COMPRESSED);
  60. }
  61. std::shared_ptr<const AuthContext>
  62. InteropServerContextInspector::GetAuthContext() const {
  63. return context_.auth_context();
  64. }
  65. bool InteropServerContextInspector::IsCancelled() const {
  66. return context_.IsCancelled();
  67. }
  68. } // namespace testing
  69. } // namespace grpc