public void TestInvalidInputWithMaxArrayLengthAsStringLength() { using (MemoryStream iostr = new MemoryStream()) { Encoder e = new BinaryEncoder(iostr); #if NETCOREAPP3_1_OR_GREATER const int maximumArrayLength = 0x7FFFFFC7; #else const int maximumArrayLength = 0x7FFFFFFF / 2; #endif e.WriteLong(maximumArrayLength); e.WriteBytes(Encoding.UTF8.GetBytes("SomeSmallString")); iostr.Flush(); iostr.Position = 0; Decoder d = new BinaryDecoder(iostr); var exception = Assert.Throws <AvroException>(() => d.ReadString()); Assert.NotNull(exception); Assert.AreEqual("Could not read as many bytes from stream as expected!", exception.Message); iostr.Close(); } }
public async Task <int> RespondAsync(GenericContext rpcContext, CancellationToken token) { using (var responseData = new FrameStream()) using (var encoder = new BinaryEncoder(responseData)) { if (DoHandshake()) { HANDSHAKE_RESPONSE_WRITER.Write(encoder, rpcContext.HandshakeResponse); _handshakePending = rpcContext.HandshakeResponse.match == HandshakeMatch.BOTH; } META_WRITER.Write(encoder, EMPTY_META); encoder.WriteBoolean(rpcContext.IsError); if (rpcContext.IsError) { _protocol.WriteError(encoder, rpcContext.MessageName, rpcContext.Error); } else { _protocol.WriteReponse(encoder, rpcContext.MessageName, rpcContext.Response); } encoder.WriteBytes(END_OF_FRAME); responseData.Seek(0, SeekOrigin.Begin); return(await _tranceiver.SendAsync(responseData, token)); } }
public void EncodeBytes(byte[] expectedValue, int expectedLength, byte[] value) { using (var stream = new MemoryStream()) using (var encoder = new BinaryEncoder(stream)) { encoder.WriteBytes(value); Assert.AreEqual(expectedLength, stream.Position); Assert.AreEqual(expectedValue, stream.GetBuffer().AsSpan(0, expectedLength).ToArray()); } }
public void TestBytes(byte[] expected) { using (var stream = new MemoryStream()) using (var encoder = new BinaryEncoder(stream)) using (var decoder = new BinaryDecoder(stream)) { encoder.WriteBytes(expected); stream.Seek(0, SeekOrigin.Begin); var actual = decoder.ReadBytes(); Assert.AreEqual(expected, actual); } }
public void TestInvalidInputWithMaxIntAsStringLength() { using (MemoryStream iostr = new MemoryStream()) { Encoder e = new BinaryEncoder(iostr); e.WriteLong(int.MaxValue); e.WriteBytes(Encoding.UTF8.GetBytes("SomeSmallString")); iostr.Flush(); iostr.Position = 0; Decoder d = new BinaryDecoder(iostr); var exception = Assert.Throws <AvroException>(() => d.ReadString()); Assert.NotNull(exception); Assert.AreEqual("String length is not supported!", exception.Message); iostr.Close(); } }
private async Task <GenericContext> Request(GenericContext rpcContext, CancellationToken token) { using (var requestData = new FrameStream()) using (var encoder = new BinaryEncoder(requestData)) { if (DoHandshake()) { if (rpcContext.HandshakeRequest == null) { rpcContext.HandshakeRequest = NewHandshakeRequest((MD5)Protocol.MD5, (MD5)RemoteProtocol.MD5); } HANDSHAKE_REQUEST_WRITER.Write(encoder, rpcContext.HandshakeRequest); } META_WRITER.Write(encoder, EMPTY_META); encoder.WriteString(rpcContext.MessageName); _protocol.WriteRequest(encoder, rpcContext.MessageName, rpcContext.RequestParameters); encoder.WriteBytes(END_OF_FRAME); requestData.Seek(0, SeekOrigin.Begin); using (var responseData = await _tranceiver.RequestAsync(rpcContext.MessageName, requestData, token)) using (var decode = new BinaryDecoder(responseData)) { responseData.Seek(0, SeekOrigin.Begin); if (DoHandshake()) { rpcContext.HandshakeResponse = HANDSHAKE_RESPONSE_READER.Read(decode); _handshakePending = rpcContext.HandshakeResponse.match == HandshakeMatch.NONE; var remoteProtocol = default(AvroProtocol); if (rpcContext.HandshakeResponse.match == HandshakeMatch.CLIENT || rpcContext.HandshakeResponse.match == HandshakeMatch.NONE) { remoteProtocol = AvroParser.ReadProtocol(rpcContext.HandshakeResponse.serverProtocol); _protocol = new GenericRequestor(Protocol, remoteProtocol); } if (rpcContext.HandshakeResponse.match == HandshakeMatch.NONE) { rpcContext.HandshakeRequest.serverHash = (MD5)remoteProtocol.MD5; rpcContext.HandshakeRequest.clientProtocol = Protocol.ToAvroCanonical(); _protocol = new GenericRequestor(Protocol, remoteProtocol); rpcContext = await Request(rpcContext, token); } } rpcContext.Metadata = META_READER.Read(decode); rpcContext.IsError = decode.ReadBoolean(); if (rpcContext.IsError) { rpcContext.Error = _protocol.ReadError <object>(decode, rpcContext.MessageName); } else { rpcContext.Response = _protocol.ReadResponse <object>(decode, rpcContext.MessageName); } return(rpcContext); } } }