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_UTIL_EVALUATE_ARGS_TEST_UTIL_H 16 #define GRPC_TEST_CORE_UTIL_EVALUATE_ARGS_TEST_UTIL_H 17 18 #include <stdlib.h> 19 20 #include <memory> 21 22 #include "absl/strings/string_view.h" 23 24 #include <grpc/event_engine/memory_allocator.h> 25 #include <grpc/grpc_security.h> 26 #include <grpc/support/port_platform.h> 27 28 #include "src/core/lib/channel/channel_args.h" 29 #include "src/core/lib/gprpp/ref_counted_ptr.h" 30 #include "src/core/lib/resource_quota/arena.h" 31 #include "src/core/lib/resource_quota/memory_quota.h" 32 #include "src/core/lib/resource_quota/resource_quota.h" 33 #include "src/core/lib/security/authorization/evaluate_args.h" 34 #include "src/core/lib/security/context/security_context.h" 35 #include "src/core/lib/slice/slice.h" 36 #include "src/core/lib/transport/endpoint_info_handshaker.h" 37 #include "src/core/lib/transport/metadata_batch.h" 38 39 namespace grpc_core { 40 41 class EvaluateArgsTestUtil final { 42 public: AddPairToMetadata(const char * key,const char * value)43 void AddPairToMetadata(const char* key, const char* value) { 44 metadata_.Append(key, Slice::FromStaticString(value), 45 [](absl::string_view, const Slice&) { 46 // We should never ever see an error here. 47 abort(); 48 }); 49 } 50 SetLocalEndpoint(absl::string_view local_uri)51 void SetLocalEndpoint(absl::string_view local_uri) { 52 args_ = args_.Set(GRPC_ARG_ENDPOINT_LOCAL_ADDRESS, local_uri); 53 } 54 SetPeerEndpoint(absl::string_view peer_uri)55 void SetPeerEndpoint(absl::string_view peer_uri) { 56 args_ = args_.Set(GRPC_ARG_ENDPOINT_PEER_ADDRESS, peer_uri); 57 } 58 AddPropertyToAuthContext(const char * name,const char * value)59 void AddPropertyToAuthContext(const char* name, const char* value) { 60 auth_context_.add_cstring_property(name, value); 61 } 62 MakeEvaluateArgs()63 EvaluateArgs MakeEvaluateArgs() { 64 channel_args_ = 65 std::make_unique<EvaluateArgs::PerChannelArgs>(&auth_context_, args_); 66 return EvaluateArgs(&metadata_, channel_args_.get()); 67 } 68 69 private: 70 MemoryAllocator allocator_ = 71 ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator( 72 "EvaluateArgsTestUtil"); 73 grpc_metadata_batch metadata_; 74 grpc_auth_context auth_context_{nullptr}; 75 ChannelArgs args_; 76 std::unique_ptr<EvaluateArgs::PerChannelArgs> channel_args_; 77 }; 78 79 } // namespace grpc_core 80 81 #endif // GRPC_TEST_CORE_UTIL_EVALUATE_ARGS_TEST_UTIL_H 82