| Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1 | #include "quiche/http2/adapter/mock_nghttp2_callbacks.h" |
| 2 | |
| 3 | #include "quiche/http2/adapter/nghttp2_util.h" |
| 4 | |
| 5 | namespace http2 { |
| 6 | namespace adapter { |
| 7 | namespace test { |
| 8 | |
| 9 | /* static */ |
| 10 | nghttp2_session_callbacks_unique_ptr MockNghttp2Callbacks::GetCallbacks() { |
| 11 | nghttp2_session_callbacks* callbacks; |
| 12 | nghttp2_session_callbacks_new(&callbacks); |
| 13 | |
| 14 | // All of the callback implementations below just delegate to the mock methods |
| 15 | // of |user_data|, which is assumed to be a MockNghttp2Callbacks*. |
| 16 | nghttp2_session_callbacks_set_send_callback( |
| 17 | callbacks, |
| 18 | [](nghttp2_session*, const uint8_t* data, size_t length, int flags, |
| 19 | void* user_data) -> ssize_t { |
| 20 | return static_cast<MockNghttp2Callbacks*>(user_data)->Send(data, length, |
| 21 | flags); |
| 22 | }); |
| 23 | |
| 24 | nghttp2_session_callbacks_set_send_data_callback( |
| 25 | callbacks, |
| 26 | [](nghttp2_session*, nghttp2_frame* frame, const uint8_t* framehd, |
| 27 | size_t length, nghttp2_data_source* source, void* user_data) -> int { |
| 28 | return static_cast<MockNghttp2Callbacks*>(user_data)->SendData( |
| 29 | frame, framehd, length, source); |
| 30 | }); |
| 31 | |
| 32 | nghttp2_session_callbacks_set_on_begin_headers_callback( |
| 33 | callbacks, |
| 34 | [](nghttp2_session*, const nghttp2_frame* frame, void* user_data) -> int { |
| 35 | return static_cast<MockNghttp2Callbacks*>(user_data)->OnBeginHeaders( |
| 36 | frame); |
| 37 | }); |
| 38 | |
| 39 | nghttp2_session_callbacks_set_on_header_callback( |
| 40 | callbacks, |
| 41 | [](nghttp2_session*, const nghttp2_frame* frame, const uint8_t* raw_name, |
| 42 | size_t name_length, const uint8_t* raw_value, size_t value_length, |
| 43 | uint8_t flags, void* user_data) -> int { |
| 44 | absl::string_view name = ToStringView(raw_name, name_length); |
| 45 | absl::string_view value = ToStringView(raw_value, value_length); |
| 46 | return static_cast<MockNghttp2Callbacks*>(user_data)->OnHeader( |
| 47 | frame, name, value, flags); |
| 48 | }); |
| 49 | |
| 50 | nghttp2_session_callbacks_set_on_data_chunk_recv_callback( |
| 51 | callbacks, |
| 52 | [](nghttp2_session*, uint8_t flags, int32_t stream_id, |
| 53 | const uint8_t* data, size_t len, void* user_data) -> int { |
| 54 | absl::string_view chunk = ToStringView(data, len); |
| 55 | return static_cast<MockNghttp2Callbacks*>(user_data)->OnDataChunkRecv( |
| 56 | flags, stream_id, chunk); |
| 57 | }); |
| 58 | |
| 59 | nghttp2_session_callbacks_set_on_begin_frame_callback( |
| 60 | callbacks, |
| 61 | [](nghttp2_session*, const nghttp2_frame_hd* hd, void* user_data) -> int { |
| 62 | return static_cast<MockNghttp2Callbacks*>(user_data)->OnBeginFrame(hd); |
| 63 | }); |
| 64 | |
| 65 | nghttp2_session_callbacks_set_on_frame_recv_callback( |
| 66 | callbacks, |
| 67 | [](nghttp2_session*, const nghttp2_frame* frame, void* user_data) -> int { |
| 68 | return static_cast<MockNghttp2Callbacks*>(user_data)->OnFrameRecv( |
| 69 | frame); |
| 70 | }); |
| 71 | |
| 72 | nghttp2_session_callbacks_set_on_stream_close_callback( |
| 73 | callbacks, |
| 74 | [](nghttp2_session*, int32_t stream_id, uint32_t error_code, |
| 75 | void* user_data) -> int { |
| 76 | return static_cast<MockNghttp2Callbacks*>(user_data)->OnStreamClose( |
| 77 | stream_id, error_code); |
| 78 | }); |
| 79 | |
| 80 | nghttp2_session_callbacks_set_on_frame_send_callback( |
| 81 | callbacks, |
| 82 | [](nghttp2_session*, const nghttp2_frame* frame, void* user_data) -> int { |
| 83 | return static_cast<MockNghttp2Callbacks*>(user_data)->OnFrameSend( |
| 84 | frame); |
| 85 | }); |
| 86 | |
| 87 | nghttp2_session_callbacks_set_before_frame_send_callback( |
| 88 | callbacks, |
| 89 | [](nghttp2_session*, const nghttp2_frame* frame, void* user_data) -> int { |
| 90 | return static_cast<MockNghttp2Callbacks*>(user_data)->BeforeFrameSend( |
| 91 | frame); |
| 92 | }); |
| 93 | |
| 94 | nghttp2_session_callbacks_set_on_frame_not_send_callback( |
| 95 | callbacks, |
| 96 | [](nghttp2_session*, const nghttp2_frame* frame, int lib_error_code, |
| 97 | void* user_data) -> int { |
| 98 | return static_cast<MockNghttp2Callbacks*>(user_data)->OnFrameNotSend( |
| 99 | frame, lib_error_code); |
| 100 | }); |
| 101 | |
| 102 | nghttp2_session_callbacks_set_on_invalid_frame_recv_callback( |
| 103 | callbacks, |
| 104 | [](nghttp2_session*, const nghttp2_frame* frame, int error_code, |
| 105 | void* user_data) -> int { |
| 106 | return static_cast<MockNghttp2Callbacks*>(user_data) |
| 107 | ->OnInvalidFrameRecv(frame, error_code); |
| 108 | }); |
| 109 | |
| 110 | nghttp2_session_callbacks_set_error_callback2( |
| 111 | callbacks, |
| 112 | [](nghttp2_session* /*session*/, int lib_error_code, const char* msg, |
| 113 | size_t len, void* user_data) -> int { |
| 114 | return static_cast<MockNghttp2Callbacks*>(user_data)->OnErrorCallback2( |
| 115 | lib_error_code, msg, len); |
| 116 | }); |
| 117 | |
| 118 | nghttp2_session_callbacks_set_pack_extension_callback( |
| 119 | callbacks, |
| 120 | [](nghttp2_session*, uint8_t* buf, size_t len, const nghttp2_frame* frame, |
| 121 | void* user_data) -> ssize_t { |
| 122 | return static_cast<MockNghttp2Callbacks*>(user_data)->OnPackExtension( |
| 123 | buf, len, frame); |
| 124 | }); |
| 125 | return MakeCallbacksPtr(callbacks); |
| 126 | } |
| 127 | |
| 128 | } // namespace test |
| 129 | } // namespace adapter |
| 130 | } // namespace http2 |