1 // 2 // 3 // Copyright 2016 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 19 #ifndef GRPC_SRC_CORE_LIB_SECURITY_CREDENTIALS_FAKE_FAKE_CREDENTIALS_H 20 #define GRPC_SRC_CORE_LIB_SECURITY_CREDENTIALS_FAKE_FAKE_CREDENTIALS_H 21 22 #include <grpc/credentials.h> 23 #include <grpc/grpc.h> 24 #include <grpc/grpc_security.h> 25 #include <grpc/grpc_security_constants.h> 26 #include <grpc/support/port_platform.h> 27 28 #include <string> 29 30 #include "absl/status/statusor.h" 31 #include "src/core/lib/channel/channel_args.h" 32 #include "src/core/lib/promise/arena_promise.h" 33 #include "src/core/lib/security/credentials/credentials.h" 34 #include "src/core/lib/security/security_connector/security_connector.h" 35 #include "src/core/lib/slice/slice.h" 36 #include "src/core/lib/transport/transport.h" 37 #include "src/core/util/ref_counted_ptr.h" 38 #include "src/core/util/unique_type_name.h" 39 #include "src/core/util/useful.h" 40 41 #define GRPC_ARG_FAKE_SECURITY_EXPECTED_TARGETS \ 42 "grpc.fake_security.expected_targets" 43 44 // -- Fake transport security credentials. -- 45 46 class grpc_fake_channel_credentials final : public grpc_channel_credentials { 47 public: 48 grpc_core::RefCountedPtr<grpc_channel_security_connector> 49 create_security_connector( 50 grpc_core::RefCountedPtr<grpc_call_credentials> call_creds, 51 const char* target, grpc_core::ChannelArgs* args) override; 52 53 static grpc_core::UniqueTypeName Type(); 54 type()55 grpc_core::UniqueTypeName type() const override { return Type(); } 56 57 private: 58 int cmp_impl(const grpc_channel_credentials* other) const override; 59 }; 60 61 class grpc_fake_server_credentials final : public grpc_server_credentials { 62 public: 63 grpc_core::RefCountedPtr<grpc_server_security_connector> 64 create_security_connector(const grpc_core::ChannelArgs& /*args*/) override; 65 66 static grpc_core::UniqueTypeName Type(); 67 type()68 grpc_core::UniqueTypeName type() const override { return Type(); } 69 }; 70 71 // Creates a fake transport security credentials object for testing. 72 grpc_channel_credentials* grpc_fake_transport_security_credentials_create(void); 73 74 // Creates a fake server transport security credentials object for testing. 75 grpc_server_credentials* grpc_fake_transport_security_server_credentials_create( 76 void); 77 78 // Used to verify the target names given to the fake transport security 79 // connector. 80 // 81 // The syntax of \a expected_targets by example: 82 // For LB channels: 83 // "backend_target_1,backend_target_2,...;lb_target_1,lb_target_2,..." 84 // For regular channels: 85 // "backend_target_1,backend_target_2,..." 86 // 87 // That is to say, LB channels have a heading list of LB targets separated from 88 // the list of backend targets by a semicolon. For non-LB channels, only the 89 // latter is present. 90 grpc_arg grpc_fake_transport_expected_targets_arg(char* expected_targets); 91 92 // -- Metadata-only Test credentials. -- 93 94 class grpc_md_only_test_credentials : public grpc_call_credentials { 95 public: grpc_md_only_test_credentials(const char * md_key,const char * md_value)96 grpc_md_only_test_credentials(const char* md_key, const char* md_value) 97 : grpc_call_credentials(GRPC_SECURITY_NONE), 98 key_(grpc_core::Slice::FromCopiedString(md_key)), 99 value_(grpc_core::Slice::FromCopiedString(md_value)) {} 100 Orphaned()101 void Orphaned() override {} 102 103 grpc_core::ArenaPromise<absl::StatusOr<grpc_core::ClientMetadataHandle>> 104 GetRequestMetadata(grpc_core::ClientMetadataHandle initial_metadata, 105 const GetRequestMetadataArgs* args) override; 106 debug_string()107 std::string debug_string() override { return "MD only Test Credentials"; } 108 109 static grpc_core::UniqueTypeName Type(); 110 type()111 grpc_core::UniqueTypeName type() const override { return Type(); } 112 113 private: cmp_impl(const grpc_call_credentials * other)114 int cmp_impl(const grpc_call_credentials* other) const override { 115 // TODO(yashykt): Check if we can do something better here 116 return grpc_core::QsortCompare( 117 static_cast<const grpc_call_credentials*>(this), other); 118 } 119 120 grpc_core::Slice key_; 121 grpc_core::Slice value_; 122 }; 123 124 #endif // GRPC_SRC_CORE_LIB_SECURITY_CREDENTIALS_FAKE_FAKE_CREDENTIALS_H 125