|
| 1 | +using System.Net; |
| 2 | +using System.Net.Security; |
| 3 | +using System.Security.Authentication; |
| 4 | +using Akka.Actor; |
| 5 | +using Akka.Event; |
| 6 | +using Servus.Akka.Transport; |
| 7 | +using TurboHTTP.Protocol; |
| 8 | +using TurboHTTP.Protocol.Syntax.Http11.Server; |
| 9 | +using TurboHTTP.Protocol.Syntax.Http2.Server; |
| 10 | +using TurboHTTP.Server; |
| 11 | +using TurboHTTP.Streams.Stages.Server; |
| 12 | + |
| 13 | +namespace TurboHTTP.Tests.Protocol; |
| 14 | + |
| 15 | +public sealed class ProtocolNegotiatingStateMachineSpec |
| 16 | +{ |
| 17 | + private sealed class FakeServerOps : IServerStageOperations |
| 18 | + { |
| 19 | + public List<HttpRequestMessage> EmittedRequests { get; } = []; |
| 20 | + public List<ITransportOutbound> EmittedOutbound { get; } = []; |
| 21 | + public List<string> ScheduledTimers { get; } = []; |
| 22 | + public ILoggingAdapter Log { get; } = NoLogger.Instance; |
| 23 | + public IActorRef StageActor { get; set; } = ActorRefs.Nobody; |
| 24 | + |
| 25 | + public void OnRequest(HttpRequestMessage request) => EmittedRequests.Add(request); |
| 26 | + public void OnOutbound(ITransportOutbound item) => EmittedOutbound.Add(item); |
| 27 | + public void OnScheduleTimer(string name, TimeSpan delay) => ScheduledTimers.Add(name); |
| 28 | + public void OnCancelTimer(string name) { } |
| 29 | + } |
| 30 | + |
| 31 | + private static TransportConnected MakeConnected(SslApplicationProtocol? alpn = null) |
| 32 | + { |
| 33 | + SecurityInfo? security = alpn is not null |
| 34 | + ? new SecurityInfo(SslProtocols.Tls13, alpn.Value) |
| 35 | + : null; |
| 36 | + |
| 37 | + var info = new ConnectionInfo( |
| 38 | + new IPEndPoint(IPAddress.Loopback, 443), |
| 39 | + new IPEndPoint(IPAddress.Loopback, 50000), |
| 40 | + alpn is not null ? TransportProtocol.Tls : TransportProtocol.Tcp, |
| 41 | + security); |
| 42 | + |
| 43 | + return new TransportConnected(info); |
| 44 | + } |
| 45 | + |
| 46 | + private static TransportData MakeData(byte[] data) |
| 47 | + { |
| 48 | + var buffer = TransportBuffer.Rent(data.Length); |
| 49 | + data.CopyTo(buffer.FullMemory.Span); |
| 50 | + buffer.Length = data.Length; |
| 51 | + return new TransportData(buffer); |
| 52 | + } |
| 53 | + |
| 54 | + // Task 2: ALPN Detection Tests |
| 55 | + |
| 56 | + [Fact(Timeout = 5000)] |
| 57 | + public void DecodeClientData_should_select_http2_for_alpn_h2() |
| 58 | + { |
| 59 | + var ops = new FakeServerOps(); |
| 60 | + var sm = new ProtocolNegotiatingStateMachine(new TurboServerOptions(), ops); |
| 61 | + |
| 62 | + sm.DecodeClientData(MakeConnected(SslApplicationProtocol.Http2)); |
| 63 | + |
| 64 | + Assert.True(sm.CanAcceptResponse || !sm.ShouldComplete); |
| 65 | + Assert.Contains("keep-alive-timeout", ops.ScheduledTimers); |
| 66 | + } |
| 67 | + |
| 68 | + [Fact(Timeout = 5000)] |
| 69 | + public void DecodeClientData_should_select_http11_for_alpn_http11() |
| 70 | + { |
| 71 | + var ops = new FakeServerOps(); |
| 72 | + var sm = new ProtocolNegotiatingStateMachine(new TurboServerOptions(), ops); |
| 73 | + |
| 74 | + sm.DecodeClientData(MakeConnected(SslApplicationProtocol.Http11)); |
| 75 | + |
| 76 | + Assert.False(sm.CanAcceptResponse); |
| 77 | + Assert.False(sm.ShouldComplete); |
| 78 | + } |
| 79 | + |
| 80 | + [Fact(Timeout = 5000)] |
| 81 | + public void DecodeClientData_should_select_http11_for_default_alpn() |
| 82 | + { |
| 83 | + var ops = new FakeServerOps(); |
| 84 | + var sm = new ProtocolNegotiatingStateMachine(new TurboServerOptions(), ops); |
| 85 | + |
| 86 | + sm.DecodeClientData(MakeConnected(default(SslApplicationProtocol))); |
| 87 | + |
| 88 | + Assert.False(sm.CanAcceptResponse); |
| 89 | + Assert.False(sm.ShouldComplete); |
| 90 | + } |
| 91 | + |
| 92 | + // Task 3: Preface Sniffing Tests |
| 93 | + |
| 94 | + [Fact(Timeout = 5000)] |
| 95 | + public void DecodeClientData_should_select_http2_for_pri_preface() |
| 96 | + { |
| 97 | + var ops = new FakeServerOps(); |
| 98 | + var sm = new ProtocolNegotiatingStateMachine(new TurboServerOptions(), ops); |
| 99 | + |
| 100 | + sm.DecodeClientData(MakeConnected()); |
| 101 | + |
| 102 | + var preface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"u8.ToArray(); |
| 103 | + sm.DecodeClientData(MakeData(preface)); |
| 104 | + |
| 105 | + Assert.Contains("keep-alive-timeout", ops.ScheduledTimers); |
| 106 | + } |
| 107 | + |
| 108 | + [Fact(Timeout = 5000)] |
| 109 | + public void DecodeClientData_should_select_http11_for_get_request() |
| 110 | + { |
| 111 | + var ops = new FakeServerOps(); |
| 112 | + var sm = new ProtocolNegotiatingStateMachine(new TurboServerOptions(), ops); |
| 113 | + |
| 114 | + sm.DecodeClientData(MakeConnected()); |
| 115 | + |
| 116 | + var request = "GET / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 0\r\n\r\n"u8.ToArray(); |
| 117 | + sm.DecodeClientData(MakeData(request)); |
| 118 | + |
| 119 | + Assert.Single(ops.EmittedRequests); |
| 120 | + Assert.Equal("GET", ops.EmittedRequests[0].Method.Method); |
| 121 | + } |
| 122 | + |
| 123 | + [Fact(Timeout = 5000)] |
| 124 | + public void DecodeClientData_should_select_http11_for_post_request() |
| 125 | + { |
| 126 | + var ops = new FakeServerOps(); |
| 127 | + var sm = new ProtocolNegotiatingStateMachine(new TurboServerOptions(), ops); |
| 128 | + |
| 129 | + sm.DecodeClientData(MakeConnected()); |
| 130 | + |
| 131 | + var request = "POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 0\r\n\r\n"u8.ToArray(); |
| 132 | + sm.DecodeClientData(MakeData(request)); |
| 133 | + |
| 134 | + Assert.Single(ops.EmittedRequests); |
| 135 | + Assert.Equal("POST", ops.EmittedRequests[0].Method.Method); |
| 136 | + } |
| 137 | + |
| 138 | + [Fact(Timeout = 5000)] |
| 139 | + public void DecodeClientData_should_stay_sniffing_for_insufficient_data() |
| 140 | + { |
| 141 | + var ops = new FakeServerOps(); |
| 142 | + var sm = new ProtocolNegotiatingStateMachine(new TurboServerOptions(), ops); |
| 143 | + |
| 144 | + sm.DecodeClientData(MakeConnected()); |
| 145 | + sm.DecodeClientData(MakeData("PR"u8.ToArray())); |
| 146 | + |
| 147 | + Assert.False(sm.CanAcceptResponse); |
| 148 | + Assert.False(sm.ShouldComplete); |
| 149 | + Assert.Empty(ops.EmittedRequests); |
| 150 | + Assert.Empty(ops.ScheduledTimers); |
| 151 | + } |
| 152 | + |
| 153 | + [Fact(Timeout = 5000)] |
| 154 | + public void Cleanup_should_dispose_buffered_data() |
| 155 | + { |
| 156 | + var ops = new FakeServerOps(); |
| 157 | + var sm = new ProtocolNegotiatingStateMachine(new TurboServerOptions(), ops); |
| 158 | + |
| 159 | + sm.DecodeClientData(MakeConnected()); |
| 160 | + sm.Cleanup(); |
| 161 | + |
| 162 | + Assert.False(sm.ShouldComplete); |
| 163 | + } |
| 164 | +} |
0 commit comments