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/support/port_platform.h>
23 
24 #include <string>
25 
26 #include "absl/status/statusor.h"
27 
28 #include <grpc/grpc.h>
29 #include <grpc/grpc_security.h>
30 #include <grpc/grpc_security_constants.h>
31 
32 #include "src/core/lib/gpr/useful.h"
33 #include "src/core/lib/gprpp/unique_type_name.h"
34 #include "src/core/lib/promise/arena_promise.h"
35 #include "src/core/lib/security/credentials/credentials.h"
36 #include "src/core/lib/slice/slice.h"
37 #include "src/core/lib/transport/transport.h"
38 
39 #define GRPC_ARG_FAKE_SECURITY_EXPECTED_TARGETS \
40   "grpc.fake_security.expected_targets"
41 
42 // -- Fake transport security credentials. --
43 
44 // Creates a fake transport security credentials object for testing.
45 grpc_channel_credentials* grpc_fake_transport_security_credentials_create(void);
46 
47 // Creates a fake server transport security credentials object for testing.
48 grpc_server_credentials* grpc_fake_transport_security_server_credentials_create(
49     void);
50 
51 // Used to verify the target names given to the fake transport security
52 // connector.
53 //
54 // The syntax of \a expected_targets by example:
55 // For LB channels:
56 //     "backend_target_1,backend_target_2,...;lb_target_1,lb_target_2,..."
57 // For regular channels:
58 //     "backend_taget_1,backend_target_2,..."
59 //
60 // That is to say, LB channels have a heading list of LB targets separated from
61 // the list of backend targets by a semicolon. For non-LB channels, only the
62 // latter is present.
63 grpc_arg grpc_fake_transport_expected_targets_arg(char* expected_targets);
64 
65 // --  Metadata-only Test credentials. --
66 
67 class grpc_md_only_test_credentials : public grpc_call_credentials {
68  public:
grpc_md_only_test_credentials(const char * md_key,const char * md_value)69   grpc_md_only_test_credentials(const char* md_key, const char* md_value)
70       : grpc_call_credentials(GRPC_SECURITY_NONE),
71         key_(grpc_core::Slice::FromCopiedString(md_key)),
72         value_(grpc_core::Slice::FromCopiedString(md_value)) {}
73 
74   grpc_core::ArenaPromise<absl::StatusOr<grpc_core::ClientMetadataHandle>>
75   GetRequestMetadata(grpc_core::ClientMetadataHandle initial_metadata,
76                      const GetRequestMetadataArgs* args) override;
77 
debug_string()78   std::string debug_string() override { return "MD only Test Credentials"; }
79 
80   static grpc_core::UniqueTypeName Type();
81 
type()82   grpc_core::UniqueTypeName type() const override { return Type(); }
83 
84  private:
cmp_impl(const grpc_call_credentials * other)85   int cmp_impl(const grpc_call_credentials* other) const override {
86     // TODO(yashykt): Check if we can do something better here
87     return grpc_core::QsortCompare(
88         static_cast<const grpc_call_credentials*>(this), other);
89   }
90 
91   grpc_core::Slice key_;
92   grpc_core::Slice value_;
93 };
94 
95 #endif  // GRPC_SRC_CORE_LIB_SECURITY_CREDENTIALS_FAKE_FAKE_CREDENTIALS_H
96