1 // Copyright 2021 gRPC authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef GRPC_TEST_CORE_TEST_UTIL_EVALUATE_ARGS_TEST_UTIL_H 16 #define GRPC_TEST_CORE_TEST_UTIL_EVALUATE_ARGS_TEST_UTIL_H 17 18 #include <grpc/event_engine/memory_allocator.h> 19 #include <grpc/grpc_security.h> 20 #include <grpc/support/port_platform.h> 21 #include <stdlib.h> 22 23 #include <memory> 24 25 #include "absl/strings/string_view.h" 26 #include "src/core/handshaker/endpoint_info/endpoint_info_handshaker.h" 27 #include "src/core/lib/channel/channel_args.h" 28 #include "src/core/lib/resource_quota/arena.h" 29 #include "src/core/lib/resource_quota/memory_quota.h" 30 #include "src/core/lib/resource_quota/resource_quota.h" 31 #include "src/core/lib/security/authorization/evaluate_args.h" 32 #include "src/core/lib/security/context/security_context.h" 33 #include "src/core/lib/slice/slice.h" 34 #include "src/core/lib/transport/metadata_batch.h" 35 #include "src/core/util/ref_counted_ptr.h" 36 37 namespace grpc_core { 38 39 class EvaluateArgsTestUtil final { 40 public: AddPairToMetadata(const char * key,const char * value)41 void AddPairToMetadata(const char* key, const char* value) { 42 metadata_.Append(key, Slice::FromStaticString(value), 43 [](absl::string_view, const Slice&) { 44 // We should never ever see an error here. 45 abort(); 46 }); 47 } 48 SetLocalEndpoint(absl::string_view local_uri)49 void SetLocalEndpoint(absl::string_view local_uri) { 50 args_ = args_.Set(GRPC_ARG_ENDPOINT_LOCAL_ADDRESS, local_uri); 51 } 52 SetPeerEndpoint(absl::string_view peer_uri)53 void SetPeerEndpoint(absl::string_view peer_uri) { 54 args_ = args_.Set(GRPC_ARG_ENDPOINT_PEER_ADDRESS, peer_uri); 55 } 56 AddPropertyToAuthContext(const char * name,const char * value)57 void AddPropertyToAuthContext(const char* name, const char* value) { 58 auth_context_.add_cstring_property(name, value); 59 } 60 MakeEvaluateArgs()61 EvaluateArgs MakeEvaluateArgs() { 62 channel_args_ = 63 std::make_unique<EvaluateArgs::PerChannelArgs>(&auth_context_, args_); 64 return EvaluateArgs(&metadata_, channel_args_.get()); 65 } 66 67 private: 68 MemoryAllocator allocator_ = 69 ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator( 70 "EvaluateArgsTestUtil"); 71 grpc_metadata_batch metadata_; 72 grpc_auth_context auth_context_{nullptr}; 73 ChannelArgs args_; 74 std::unique_ptr<EvaluateArgs::PerChannelArgs> channel_args_; 75 }; 76 77 } // namespace grpc_core 78 79 #endif // GRPC_TEST_CORE_TEST_UTIL_EVALUATE_ARGS_TEST_UTIL_H 80