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_TEST_CORE_TEST_UTIL_MOCK_ENDPOINT_H 20 #define GRPC_TEST_CORE_TEST_UTIL_MOCK_ENDPOINT_H 21 22 #include <grpc/event_engine/event_engine.h> 23 #include <grpc/slice.h> 24 25 #include <memory> 26 27 #include "src/core/lib/iomgr/endpoint.h" 28 29 namespace grpc_event_engine { 30 namespace experimental { 31 32 // Internal controller object for mock endpoint operations. 33 // 34 // This helps avoid shared ownership issues. The endpoint itself may destroyed 35 // while a fuzzer is still attempting to use it (e.g., the transport is closed, 36 // and a fuzzer still wants to schedule reads). 37 class MockEndpointController 38 : public std::enable_shared_from_this<MockEndpointController> { 39 public: 40 // Factory method ensures this class is always a shared_ptr. 41 static std::shared_ptr<MockEndpointController> Create( 42 std::shared_ptr<EventEngine> engine); 43 44 ~MockEndpointController(); 45 46 // ---- mock methods ---- 47 void TriggerReadEvent(Slice read_data); 48 void NoMoreReads(); 49 void Read(absl::AnyInvocable<void(absl::Status)> on_read, 50 SliceBuffer* buffer); 51 // Takes ownership of the grpc_endpoint object from the controller. 52 grpc_endpoint* TakeCEndpoint(); 53 54 // ---- accessors ---- engine()55 EventEngine* engine() { return engine_.get(); } 56 57 private: 58 explicit MockEndpointController(std::shared_ptr<EventEngine> engine); 59 60 std::shared_ptr<EventEngine> engine_; 61 grpc_core::Mutex mu_; 62 bool reads_done_ ABSL_GUARDED_BY(mu_) = false; 63 SliceBuffer read_buffer_ ABSL_GUARDED_BY(mu_); 64 absl::AnyInvocable<void(absl::Status)> on_read_ ABSL_GUARDED_BY(mu_); 65 SliceBuffer* on_read_slice_buffer_ ABSL_GUARDED_BY(mu_) = nullptr; 66 grpc_endpoint* mock_grpc_endpoint_; 67 }; 68 69 class MockEndpoint : public EventEngine::Endpoint { 70 public: 71 MockEndpoint(); 72 ~MockEndpoint() override = default; 73 74 // ---- mock methods ---- SetController(std::shared_ptr<MockEndpointController> endpoint_control)75 void SetController(std::shared_ptr<MockEndpointController> endpoint_control) { 76 endpoint_control_ = std::move(endpoint_control); 77 } 78 79 // ---- overrides ---- 80 bool Read(absl::AnyInvocable<void(absl::Status)> on_read, SliceBuffer* buffer, 81 const ReadArgs* args) override; 82 bool Write(absl::AnyInvocable<void(absl::Status)> on_writable, 83 SliceBuffer* data, const WriteArgs* args) override; 84 const EventEngine::ResolvedAddress& GetPeerAddress() const override; 85 const EventEngine::ResolvedAddress& GetLocalAddress() const override; 86 87 private: 88 std::shared_ptr<MockEndpointController> endpoint_control_; 89 EventEngine::ResolvedAddress peer_addr_; 90 EventEngine::ResolvedAddress local_addr_; 91 }; 92 93 } // namespace experimental 94 } // namespace grpc_event_engine 95 96 #endif // GRPC_TEST_CORE_TEST_UTIL_MOCK_ENDPOINT_H 97