|
1 | | -use crate::{types::PeerEvent, Network, NetworkRef, Request, Response, Result}; |
| 1 | +use crate::{types::PeerEvent, Config, Network, NetworkRef, Request, Response, Result}; |
2 | 2 | use bytes::{Buf, BufMut, Bytes, BytesMut}; |
3 | 3 | use futures::FutureExt; |
4 | 4 | use std::{convert::Infallible, time::Duration}; |
@@ -868,3 +868,104 @@ async fn network_ref_via_extension() -> Result<()> { |
868 | 868 |
|
869 | 869 | Ok(()) |
870 | 870 | } |
| 871 | + |
| 872 | +fn build_network_with_config(config: Config) -> Result<Network> { |
| 873 | + let network = Network::bind("localhost:0") |
| 874 | + .random_private_key() |
| 875 | + .server_name("test") |
| 876 | + .config(config) |
| 877 | + .start(echo_service())?; |
| 878 | + |
| 879 | + trace!( |
| 880 | + address =% network.local_addr(), |
| 881 | + peer_id =% network.peer_id(), |
| 882 | + "starting network" |
| 883 | + ); |
| 884 | + |
| 885 | + Ok(network) |
| 886 | +} |
| 887 | + |
| 888 | +#[tokio::test] |
| 889 | +async fn server_max_request_frame_size_rejects_large_requests() -> Result<()> { |
| 890 | + let _guard = crate::init_tracing_for_testing(); |
| 891 | + |
| 892 | + let server = build_network_with_config(Config { |
| 893 | + max_request_frame_size: Some(128), |
| 894 | + ..Default::default() |
| 895 | + })?; |
| 896 | + let client = build_network()?; |
| 897 | + |
| 898 | + let peer = client.connect(server.local_addr()).await?; |
| 899 | + |
| 900 | + // Request body within the server's request limit succeeds. |
| 901 | + let small = vec![0u8; 32]; |
| 902 | + let response = client.rpc(peer, Request::new(small.clone().into())).await?; |
| 903 | + assert_eq!(response.into_body().as_ref(), small.as_slice()); |
| 904 | + |
| 905 | + // Request body exceeding the server's request limit is rejected by the server, |
| 906 | + // and the client sees the RPC fail. |
| 907 | + let big = vec![0u8; 4096]; |
| 908 | + client |
| 909 | + .rpc(peer, Request::new(big.into())) |
| 910 | + .await |
| 911 | + .unwrap_err(); |
| 912 | + |
| 913 | + Ok(()) |
| 914 | +} |
| 915 | + |
| 916 | +#[tokio::test] |
| 917 | +async fn client_max_response_frame_size_rejects_large_responses() -> Result<()> { |
| 918 | + let _guard = crate::init_tracing_for_testing(); |
| 919 | + |
| 920 | + let server = build_network()?; |
| 921 | + let client = build_network_with_config(Config { |
| 922 | + max_response_frame_size: Some(128), |
| 923 | + ..Default::default() |
| 924 | + })?; |
| 925 | + |
| 926 | + let peer = client.connect(server.local_addr()).await?; |
| 927 | + |
| 928 | + // Response body within the client's response limit succeeds. |
| 929 | + let small = vec![0u8; 32]; |
| 930 | + let response = client.rpc(peer, Request::new(small.clone().into())).await?; |
| 931 | + assert_eq!(response.into_body().as_ref(), small.as_slice()); |
| 932 | + |
| 933 | + // The echoed response exceeds the client's response limit. The request itself |
| 934 | + // is still within the (default) request limit, so it leaves the client and the |
| 935 | + // server processes it; the failure happens when the client tries to decode the |
| 936 | + // response. |
| 937 | + let big = vec![0u8; 4096]; |
| 938 | + client |
| 939 | + .rpc(peer, Request::new(big.into())) |
| 940 | + .await |
| 941 | + .unwrap_err(); |
| 942 | + |
| 943 | + Ok(()) |
| 944 | +} |
| 945 | + |
| 946 | +#[tokio::test] |
| 947 | +async fn max_frame_size_falls_back_for_both_directions() -> Result<()> { |
| 948 | + let _guard = crate::init_tracing_for_testing(); |
| 949 | + |
| 950 | + // Setting only the legacy `max_frame_size` should constrain both directions, |
| 951 | + // matching pre-existing behavior. |
| 952 | + let server = build_network_with_config(Config { |
| 953 | + max_frame_size: Some(128), |
| 954 | + ..Default::default() |
| 955 | + })?; |
| 956 | + let client = build_network()?; |
| 957 | + |
| 958 | + let peer = client.connect(server.local_addr()).await?; |
| 959 | + |
| 960 | + let small = vec![0u8; 32]; |
| 961 | + let response = client.rpc(peer, Request::new(small.clone().into())).await?; |
| 962 | + assert_eq!(response.into_body().as_ref(), small.as_slice()); |
| 963 | + |
| 964 | + let big = vec![0u8; 4096]; |
| 965 | + client |
| 966 | + .rpc(peer, Request::new(big.into())) |
| 967 | + .await |
| 968 | + .unwrap_err(); |
| 969 | + |
| 970 | + Ok(()) |
| 971 | +} |
0 commit comments