Debian Codegraph @

test/core/util/evaluate_args_test_util.h

raw
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 <grpc/support/port_platform.h>
19
20 #include <stdlib.h>
21
22 #include <memory>
23
24 #include "absl/strings/string_view.h"
25
26 #include <grpc/event_engine/memory_allocator.h>
27 #include <grpc/grpc_security.h>
28
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/metadata_batch.h"
37 #include "test/core/util/mock_authorization_endpoint.h"
38
39 namespace grpc_core {
40
41 class EvaluateArgsTestUtil {
42 public:
43 EvaluateArgsTestUtil() = default;
44
45 ~EvaluateArgsTestUtil() { delete channel_args_; }
46
47 void AddPairToMetadata(const char* key, const char* value) {
48 metadata_.Append(key, Slice::FromStaticString(value),
49 [](absl::string_view, const Slice&) {
50 // We should never ever see an error here.
51 abort();
52 });
53 }
54
55 void SetLocalEndpoint(absl::string_view local_uri) {
56 endpoint_.SetLocalAddress(local_uri);
57 }
58
59 void SetPeerEndpoint(absl::string_view peer_uri) {
60 endpoint_.SetPeer(peer_uri);
61 }
62
63 void AddPropertyToAuthContext(const char* name, const char* value) {
64 auth_context_.add_cstring_property(name, value);
65 }
66
67 EvaluateArgs MakeEvaluateArgs() {
68 channel_args_ =
69 new EvaluateArgs::PerChannelArgs(&auth_context_, &endpoint_);
70 return EvaluateArgs(&metadata_, channel_args_);
71 }
72
73 private:
74 MemoryAllocator allocator_ =
75 ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator(
76 "EvaluateArgsTestUtil");
77 ScopedArenaPtr arena_ = MakeScopedArena(1024, &allocator_);
78 grpc_metadata_batch metadata_{arena_.get()};
79 MockAuthorizationEndpoint endpoint_{/*local_uri=*/"", /*peer_uri=*/""};
80 grpc_auth_context auth_context_{nullptr};
81 EvaluateArgs::PerChannelArgs* channel_args_ = nullptr;
82 };
83
84 } // namespace grpc_core
85
86 #endif // GRPC_TEST_CORE_UTIL_EVALUATE_ARGS_TEST_UTIL_H