| 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 |
#include <grpc/support/port_platform.h> |
| 20 |
|
| 21 |
#include "src/core/lib/security/credentials/fake/fake_credentials.h" |
| 22 |
|
| 23 |
#include <stdlib.h> |
| 24 |
|
| 25 |
#include <memory> |
| 26 |
#include <utility> |
| 27 |
|
| 28 |
#include "absl/strings/string_view.h" |
| 29 |
|
| 30 |
#include "src/core/lib/channel/channel_args.h" |
| 31 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 32 |
#include "src/core/lib/promise/promise.h" |
| 33 |
#include "src/core/lib/security/security_connector/fake/fake_security_connector.h" |
| 34 |
#include "src/core/lib/security/security_connector/security_connector.h" |
| 35 |
#include "src/core/lib/transport/metadata_batch.h" |
| 36 |
|
| 37 |
/* -- Fake transport security credentials. -- */ |
| 38 |
|
| 39 |
namespace { |
| 40 |
|
| 41 |
class grpc_fake_channel_credentials final : public grpc_channel_credentials { |
| 42 |
public: |
| 43 |
grpc_core::RefCountedPtr<grpc_channel_security_connector> |
| 44 |
create_security_connector( |
| 45 |
grpc_core::RefCountedPtr<grpc_call_credentials> call_creds, |
| 46 |
const char* target, grpc_core::ChannelArgs* args) override { |
| 47 |
return grpc_fake_channel_security_connector_create( |
| 48 |
this->Ref(), std::move(call_creds), target, *args); |
| 49 |
} |
| 50 |
|
| 51 |
grpc_core::UniqueTypeName type() const override { |
| 52 |
static grpc_core::UniqueTypeName::Factory kFactory("Fake"); |
| 53 |
return kFactory.Create(); |
| 54 |
} |
| 55 |
|
| 56 |
private: |
| 57 |
int cmp_impl(const grpc_channel_credentials* other) const override { |
| 58 |
// TODO(yashykt): Check if we can do something better here |
| 59 |
return grpc_core::QsortCompare( |
| 60 |
static_cast<const grpc_channel_credentials*>(this), other); |
| 61 |
} |
| 62 |
}; |
| 63 |
|
| 64 |
class grpc_fake_server_credentials final : public grpc_server_credentials { |
| 65 |
public: |
| 66 |
grpc_core::RefCountedPtr<grpc_server_security_connector> |
| 67 |
create_security_connector(const grpc_core::ChannelArgs& /*args*/) override { |
| 68 |
return grpc_fake_server_security_connector_create(this->Ref()); |
| 69 |
} |
| 70 |
|
| 71 |
grpc_core::UniqueTypeName type() const override { |
| 72 |
static grpc_core::UniqueTypeName::Factory kFactory("Fake"); |
| 73 |
return kFactory.Create(); |
| 74 |
} |
| 75 |
}; |
| 76 |
} // namespace |
| 77 |
|
| 78 |
grpc_channel_credentials* grpc_fake_transport_security_credentials_create() { |
| 79 |
return new grpc_fake_channel_credentials(); |
| 80 |
} |
| 81 |
|
| 82 |
grpc_server_credentials* |
| 83 |
grpc_fake_transport_security_server_credentials_create() { |
| 84 |
return new grpc_fake_server_credentials(); |
| 85 |
} |
| 86 |
|
| 87 |
grpc_arg grpc_fake_transport_expected_targets_arg(char* expected_targets) { |
| 88 |
return grpc_channel_arg_string_create( |
| 89 |
const_cast<char*>(GRPC_ARG_FAKE_SECURITY_EXPECTED_TARGETS), |
| 90 |
expected_targets); |
| 91 |
} |
| 92 |
|
| 93 |
/* -- Metadata-only test credentials. -- */ |
| 94 |
|
| 95 |
grpc_core::ArenaPromise<absl::StatusOr<grpc_core::ClientMetadataHandle>> |
| 96 |
grpc_md_only_test_credentials::GetRequestMetadata( |
| 97 |
grpc_core::ClientMetadataHandle initial_metadata, |
| 98 |
const grpc_call_credentials::GetRequestMetadataArgs*) { |
| 99 |
initial_metadata->Append( |
| 100 |
key_.as_string_view(), value_.Ref(), |
| 101 |
[](absl::string_view, const grpc_core::Slice&) { abort(); }); |
| 102 |
return grpc_core::Immediate(std::move(initial_metadata)); |
| 103 |
} |
| 104 |
|
| 105 |
grpc_core::UniqueTypeName grpc_md_only_test_credentials::Type() { |
| 106 |
static grpc_core::UniqueTypeName::Factory kFactory("MdOnlyTest"); |
| 107 |
return kFactory.Create(); |
| 108 |
} |
| 109 |
|
| 110 |
grpc_call_credentials* grpc_md_only_test_credentials_create( |
| 111 |
const char* md_key, const char* md_value) { |
| 112 |
return new grpc_md_only_test_credentials(md_key, md_value); |
| 113 |
} |