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 <string.h>
24
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 #include <grpc/support/string_util.h>
28
29 #include "src/core/lib/channel/channel_args.h"
30 #include "src/core/lib/gpr/string.h"
31 #include "src/core/lib/iomgr/executor.h"
32
33 /* -- Fake transport security credentials. -- */
34
fake_transport_security_create_security_connector(grpc_channel_credentials * c,grpc_call_credentials * call_creds,const char * target,const grpc_channel_args * args,grpc_channel_security_connector ** sc,grpc_channel_args ** new_args)35 static grpc_security_status fake_transport_security_create_security_connector(
36 grpc_channel_credentials* c, grpc_call_credentials* call_creds,
37 const char* target, const grpc_channel_args* args,
38 grpc_channel_security_connector** sc, grpc_channel_args** new_args) {
39 *sc =
40 grpc_fake_channel_security_connector_create(c, call_creds, target, args);
41 return GRPC_SECURITY_OK;
42 }
43
44 static grpc_security_status
fake_transport_security_server_create_security_connector(grpc_server_credentials * c,grpc_server_security_connector ** sc)45 fake_transport_security_server_create_security_connector(
46 grpc_server_credentials* c, grpc_server_security_connector** sc) {
47 *sc = grpc_fake_server_security_connector_create(c);
48 return GRPC_SECURITY_OK;
49 }
50
51 static grpc_channel_credentials_vtable
52 fake_transport_security_credentials_vtable = {
53 nullptr, fake_transport_security_create_security_connector, nullptr};
54
55 static grpc_server_credentials_vtable
56 fake_transport_security_server_credentials_vtable = {
57 nullptr, fake_transport_security_server_create_security_connector};
58
grpc_fake_transport_security_credentials_create(void)59 grpc_channel_credentials* grpc_fake_transport_security_credentials_create(
60 void) {
61 grpc_channel_credentials* c = static_cast<grpc_channel_credentials*>(
62 gpr_zalloc(sizeof(grpc_channel_credentials)));
63 c->type = GRPC_CHANNEL_CREDENTIALS_TYPE_FAKE_TRANSPORT_SECURITY;
64 c->vtable = &fake_transport_security_credentials_vtable;
65 gpr_ref_init(&c->refcount, 1);
66 return c;
67 }
68
grpc_fake_transport_security_server_credentials_create(void)69 grpc_server_credentials* grpc_fake_transport_security_server_credentials_create(
70 void) {
71 grpc_server_credentials* c = static_cast<grpc_server_credentials*>(
72 gpr_malloc(sizeof(grpc_server_credentials)));
73 memset(c, 0, sizeof(grpc_server_credentials));
74 c->type = GRPC_CHANNEL_CREDENTIALS_TYPE_FAKE_TRANSPORT_SECURITY;
75 gpr_ref_init(&c->refcount, 1);
76 c->vtable = &fake_transport_security_server_credentials_vtable;
77 return c;
78 }
79
grpc_fake_transport_expected_targets_arg(char * expected_targets)80 grpc_arg grpc_fake_transport_expected_targets_arg(char* expected_targets) {
81 return grpc_channel_arg_string_create(
82 (char*)GRPC_ARG_FAKE_SECURITY_EXPECTED_TARGETS, expected_targets);
83 }
84
grpc_fake_transport_get_expected_targets(const grpc_channel_args * args)85 const char* grpc_fake_transport_get_expected_targets(
86 const grpc_channel_args* args) {
87 const grpc_arg* expected_target_arg =
88 grpc_channel_args_find(args, GRPC_ARG_FAKE_SECURITY_EXPECTED_TARGETS);
89 return grpc_channel_arg_get_string(expected_target_arg);
90 }
91
92 /* -- Metadata-only test credentials. -- */
93
md_only_test_destruct(grpc_call_credentials * creds)94 static void md_only_test_destruct(grpc_call_credentials* creds) {
95 grpc_md_only_test_credentials* c =
96 reinterpret_cast<grpc_md_only_test_credentials*>(creds);
97 GRPC_MDELEM_UNREF(c->md);
98 }
99
md_only_test_get_request_metadata(grpc_call_credentials * creds,grpc_polling_entity * pollent,grpc_auth_metadata_context context,grpc_credentials_mdelem_array * md_array,grpc_closure * on_request_metadata,grpc_error ** error)100 static bool md_only_test_get_request_metadata(
101 grpc_call_credentials* creds, grpc_polling_entity* pollent,
102 grpc_auth_metadata_context context, grpc_credentials_mdelem_array* md_array,
103 grpc_closure* on_request_metadata, grpc_error** error) {
104 grpc_md_only_test_credentials* c =
105 reinterpret_cast<grpc_md_only_test_credentials*>(creds);
106 grpc_credentials_mdelem_array_add(md_array, c->md);
107 if (c->is_async) {
108 GRPC_CLOSURE_SCHED(on_request_metadata, GRPC_ERROR_NONE);
109 return false;
110 }
111 return true;
112 }
113
md_only_test_cancel_get_request_metadata(grpc_call_credentials * c,grpc_credentials_mdelem_array * md_array,grpc_error * error)114 static void md_only_test_cancel_get_request_metadata(
115 grpc_call_credentials* c, grpc_credentials_mdelem_array* md_array,
116 grpc_error* error) {
117 GRPC_ERROR_UNREF(error);
118 }
119
120 static grpc_call_credentials_vtable md_only_test_vtable = {
121 md_only_test_destruct, md_only_test_get_request_metadata,
122 md_only_test_cancel_get_request_metadata};
123
grpc_md_only_test_credentials_create(const char * md_key,const char * md_value,bool is_async)124 grpc_call_credentials* grpc_md_only_test_credentials_create(
125 const char* md_key, const char* md_value, bool is_async) {
126 grpc_md_only_test_credentials* c =
127 static_cast<grpc_md_only_test_credentials*>(
128 gpr_zalloc(sizeof(grpc_md_only_test_credentials)));
129 c->base.type = GRPC_CALL_CREDENTIALS_TYPE_OAUTH2;
130 c->base.vtable = &md_only_test_vtable;
131 gpr_ref_init(&c->base.refcount, 1);
132 c->md = grpc_mdelem_from_slices(grpc_slice_from_copied_string(md_key),
133 grpc_slice_from_copied_string(md_value));
134 c->is_async = is_async;
135 return &c->base;
136 }
137