Пример #1
0
        public async Task ShouldPackAndUnpackBytes()
        {
            // Given
            var byteArray = PackStreamBitConverter.GetBytes("hello, world");

            // When
            var session = Server.Driver.AsyncSession();

            try
            {
                var cursor = await session.RunAsync(
                    "CREATE (a {value:{value}}) RETURN a.value", new Dictionary <string, object> {
                    { "value", byteArray }
                });

                var value = await cursor.SingleAsync(r => r["a.value"].As <byte[]>());

                // Then
                value.Should().BeEquivalentTo(byteArray);
            }
            finally
            {
                await session.CloseAsync();
            }
        }
Пример #2
0
 public override string ToString()
 {
     if (OriginalBytes != null && OriginalBytes.Length >= 0)
     {
         return(PackStreamBitConverter.ToString(OriginalBytes));
     }
     return("No original bytes to convert");
 }
Пример #3
0
            public void ShouldThrowExceptionWhenReadBytes32ReturnsBytesSizeLonggerThanIntMax()
            {
                var mockInput = IOExtensions.CreateMockStream(PackStream.Bytes32, PackStreamBitConverter.GetBytes((int)-1));
                var reader    = new PackStreamReader(mockInput.Object, BoltReader.StructHandlers);

                var ex = Xunit.Record.Exception(() => reader.ReadBytes());

                ex.Should().BeOfType <ProtocolException>();
                mockInput.Verify(x => x.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>()), Times.Exactly(2));
            }
Пример #4
0
            public void ShouldReadStruct16()
            {
                var mockInput = IOExtensions.CreateMockStream(PackStream.Struct16, PackStreamBitConverter.GetBytes((short)1));
                var reader    = new PackStreamReader(mockInput.Object, BoltReader.StructHandlers);

                var header = reader.ReadStructHeader();

                header.Should().Be(1);
                mockInput.Verify(x => x.ReadByte(), Times.Once);
                mockInput.Verify(x => x.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>()), Times.Once);
            }
Пример #5
0
            public void ShouldReadString32()
            {
                var mockInput = IOExtensions.CreateMockStream(new byte[] { PackStream.String32 },
                                                              PackStreamBitConverter.GetBytes((int)1), new byte[] { 0x61 });
                var reader = new PackStreamReader(mockInput.Object, BoltReader.StructHandlers);

                var real = reader.ReadString();

                real.Should().Be("a");
                mockInput.Verify(x => x.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>()), Times.Exactly(3));
            }
Пример #6
0
        private static byte[] PackVersions(IEnumerable <int> versions)
        {
            var aLittleBitOfMagic = PackStreamBitConverter.GetBytes(BoltIdentifier);

            var bytes = new List <byte>(aLittleBitOfMagic);

            foreach (var version in versions)
            {
                bytes.AddRange(PackStreamBitConverter.GetBytes(version));
            }
            return(bytes.ToArray());
        }
Пример #7
0
            public void ShouldReadBytes16()
            {
                var mockInput = IOExtensions.CreateMockStream(new byte[] { PackStream.Bytes16 },
                                                              PackStreamBitConverter.GetBytes((short)1), new byte[] { 0x61 });
                var reader = new PackStreamReader(mockInput.Object, BoltReader.StructHandlers);

                var real = reader.ReadBytes();

                real.Length.Should().Be(1);
                real.Should().Contain(0x61);
                mockInput.Verify(x => x.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>()), Times.Exactly(3));
            }
            public async Task ShouldConnectServerAsync()
            {
                var bufferSettings = new BufferSettings(Config.Default);
                var version        = new BoltProtocolVersion(4, 1);
                var connMock       = new Mock <ITcpSocketClient>();

                TcpSocketClientTestSetup.CreateReadStreamMock(connMock, PackStreamBitConverter.GetBytes(version.PackToInt()));
                TcpSocketClientTestSetup.CreateWriteStreamMock(connMock);

                PackStreamBitConverter.GetBytes((int)0x14);

                var client = new SocketClient(FakeUri, null, bufferSettings, socketClient: connMock.Object);

                await client.ConnectAsync(new Dictionary <string, string>());

                // Then
                connMock.Verify(x => x.ConnectAsync(FakeUri), Times.Once);
            }
Пример #9
0
        public void ShouldNotPackBytes()
        {
            // Given
            byte[] byteArray = PackStreamBitConverter.GetBytes("hello, world");

            // When
            using (var session = Server.Driver.Session())
            {
                var exception = Record.Exception(() =>
                                                 session.Run("CREATE (a {value:{value}})",
                                                             new Dictionary <string, object> {
                    { "value", byteArray }
                }));

                // Then
                exception.Should().BeOfType <ProtocolException>();
                exception.Message.Should().Be("Cannot understand values with type System.Byte[]");
            }
        }
Пример #10
0
        public void ShouldPackAndUnpackBytes()
        {
            // Given
            byte[] byteArray = PackStreamBitConverter.GetBytes("hello, world");

            // When
            using (var session = Server.Driver.Session())
            {
                var result = session.Run(
                    "CREATE (a {value:{value}}) RETURN a.value", new Dictionary <string, object> {
                    { "value", byteArray }
                });
                // Then
                foreach (var record in result)
                {
                    var value = record["a.value"].ValueAs <byte[]>();
                    value.Should().BeEquivalentTo(byteArray);
                }
            }
        }
 public static BoltProtocolVersion UnpackAgreedVersion(byte[] data)
 {
     return(BoltProtocolVersion.FromPackedInt(PackStreamBitConverter.ToInt32(data)));
 }
Пример #12
0
 public static int UnpackAgreedVersion(byte[] data)
 {
     return(PackStreamBitConverter.ToInt32(data));
 }
Пример #13
0
            public void ShouldReadDoubleCorrectly()
            {
                const double expected  = 1.12;
                var          mockInput = IOExtensions.CreateMockStream(PackStream.Float64, PackStreamBitConverter.GetBytes(expected));
                var          reader    = new PackStreamReader(mockInput.Object, BoltReader.StructHandlers);

                var real = reader.ReadDouble();

                real.Should().Be(expected);
                mockInput.Verify(x => x.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>()), Times.Exactly(2));
            }
Пример #14
0
            public void ShouldReadLongAsInt()
            {
                const int expected  = 1024;
                var       mockInput = IOExtensions.CreateMockStream(PackStream.Int32, PackStreamBitConverter.GetBytes(expected));
                var       reader    = new PackStreamReader(mockInput.Object, BoltReader.StructHandlers);

                var real = reader.ReadLong();

                real.Should().Be(expected);
                mockInput.Verify(x => x.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>()), Times.Exactly(2));
            }