Пример #1
0
        public void Parse_Handles_Multiple_Attributes(string username, int token, byte freeUploadSlots, int uploadSpeed, long queueLength)
        {
            var msg = new MessageBuilder()
                      .WriteCode(MessageCode.Peer.SearchResponse)
                      .WriteString(username)
                      .WriteInteger(token)
                      .WriteInteger(1)                               // file count
                      .WriteByte(0x2)                                // code
                      .WriteString("filename")                       // filename
                      .WriteLong(3)                                  // size
                      .WriteString("ext")                            // extension
                      .WriteInteger(2)                               // attribute count
                      .WriteInteger((int)FileAttributeType.BitDepth) // attribute[0].type
                      .WriteInteger(4)                               // attribute[0].value
                      .WriteInteger((int)FileAttributeType.BitRate)  // attribute[0].type
                      .WriteInteger(5)                               // attribute[0].value
                      .WriteByte(freeUploadSlots)
                      .WriteInteger(uploadSpeed)
                      .WriteLong(queueLength)
                      .WriteBytes(new byte[4]) // unknown 4 bytes
                      .Compress()
                      .Build();

            var r = SearchResponse.FromByteArray(msg);

            Assert.Single(r.Files);

            var file = r.Files.ToList()[0];

            Assert.Equal(FileAttributeType.BitDepth, file.Attributes.ToList()[0].Type);
            Assert.Equal(4, file.Attributes.ToList()[0].Value);

            Assert.Equal(FileAttributeType.BitRate, file.Attributes.ToList()[1].Type);
            Assert.Equal(5, file.Attributes.ToList()[1].Value);
        }
Пример #2
0
        public void Parse_Handles_Empty_Responses(string username, int token, byte freeUploadSlots, int uploadSpeed, long queueLength)
        {
            var msg = new MessageBuilder()
                      .WriteCode(MessageCode.Peer.SearchResponse)
                      .WriteString(username)
                      .WriteInteger(token)
                      .WriteInteger(0) // file count
                      .WriteByte(freeUploadSlots)
                      .WriteInteger(uploadSpeed)
                      .WriteLong(queueLength)
                      .WriteBytes(new byte[4]) // unknown 4 bytes
                      .Compress()
                      .Build();

            var r = SearchResponse.FromByteArray(msg);

            Assert.Equal(username, r.Username);
            Assert.Equal(token, r.Token);
            Assert.Equal(0, r.FileCount);
            Assert.Equal(freeUploadSlots, r.FreeUploadSlots);
            Assert.Equal(uploadSpeed, r.UploadSpeed);
            Assert.Equal(queueLength, r.QueueLength);

            Assert.Empty(r.Files);
        }
Пример #3
0
        public void Parse_Throws_MessageReadException_On_File_Count_Mismatch()
        {
            var msg = new MessageBuilder()
                      .WriteCode(MessageCode.Peer.SearchResponse)
                      .WriteString("foo")
                      .WriteInteger(0)
                      .WriteInteger(20)                              // file count
                      .WriteByte(0x2)                                // code
                      .WriteString("filename")                       // filename
                      .WriteLong(3)                                  // size
                      .WriteString("ext")                            // extension
                      .WriteInteger(1)                               // attribute count
                      .WriteInteger((int)FileAttributeType.BitDepth) // attribute[0].type
                      .WriteInteger(4)                               // attribute[0].value
                      .WriteByte(0x20)                               // code
                      .WriteString("filename2")                      // filename
                      .WriteLong(30)                                 // size
                      .WriteString("ext2")                           // extension
                      .WriteInteger(1)                               // attribute count
                      .WriteInteger((int)FileAttributeType.BitRate)  // attribute[0].type
                      .WriteInteger(40)                              // attribute[0].value
                      .WriteByte(0)
                      .WriteInteger(0)
                      .WriteLong(0)
                      .WriteBytes(new byte[4]) // unknown 4 bytes
                      .Compress()
                      .Build();

            var ex = Record.Exception(() => SearchResponse.FromByteArray(msg));

            Assert.NotNull(ex);
            Assert.IsType <MessageReadException>(ex);
        }
Пример #4
0
        public void Parse_Throws_MessageException_On_Code_Mismatch()
        {
            var msg = new MessageBuilder()
                      .WriteCode(MessageCode.Peer.BrowseRequest)
                      .Build();

            var ex = Record.Exception(() => SearchResponse.FromByteArray(msg));

            Assert.NotNull(ex);
            Assert.IsType <MessageException>(ex);
        }
Пример #5
0
        public void Parse_Throws_MessageReadException_On_Missing_Data()
        {
            var msg = new MessageBuilder()
                      .WriteCode(MessageCode.Peer.SearchResponse)
                      .WriteString("foo")
                      .Compress()
                      .Build();

            var ex = Record.Exception(() => SearchResponse.FromByteArray(msg));

            Assert.NotNull(ex);
            Assert.IsType <MessageReadException>(ex);
        }
Пример #6
0
        public void Parse_Throws_MessageCompressionException_On_Uncompressed_Payload()
        {
            var msg = new MessageBuilder()
                      .WriteCode(MessageCode.Peer.SearchResponse)
                      .WriteBytes(new byte[] { 0x0, 0x1, 0x2, 0x3 })
                      .Build();

            var ex = Record.Exception(() => SearchResponse.FromByteArray(msg));

            Assert.NotNull(ex);
            Assert.IsType <MessageCompressionException>(ex);
            Assert.IsType <ZStreamException>(ex.InnerException);
        }
Пример #7
0
        public void Parse_Returns_Expected_Data(string username, int token, byte freeUploadSlots, int uploadSpeed, long queueLength)
        {
            var msg = new MessageBuilder()
                      .WriteCode(MessageCode.Peer.SearchResponse)
                      .WriteString(username)
                      .WriteInteger(token)
                      .WriteInteger(1)                               // file count
                      .WriteByte(0x2)                                // code
                      .WriteString("filename")                       // filename
                      .WriteLong(3)                                  // size
                      .WriteString("ext")                            // extension
                      .WriteInteger(1)                               // attribute count
                      .WriteInteger((int)FileAttributeType.BitDepth) // attribute[0].type
                      .WriteInteger(4)                               // attribute[0].value
                      .WriteByte(freeUploadSlots)
                      .WriteInteger(uploadSpeed)
                      .WriteLong(queueLength)
                      .WriteBytes(new byte[4]) // unknown 4 bytes
                      .Compress()
                      .Build();

            var r = SearchResponse.FromByteArray(msg);

            Assert.Equal(username, r.Username);
            Assert.Equal(token, r.Token);
            Assert.Equal(1, r.FileCount);
            Assert.Equal(freeUploadSlots, r.FreeUploadSlots);
            Assert.Equal(uploadSpeed, r.UploadSpeed);
            Assert.Equal(queueLength, r.QueueLength);

            Assert.Single(r.Files);

            var file = r.Files.ToList()[0];

            Assert.Equal(0x2, file.Code);
            Assert.Equal("filename", file.Filename);
            Assert.Equal(3, file.Size);
            Assert.Equal("ext", file.Extension);
            Assert.Equal(1, file.AttributeCount);
            Assert.Single(file.Attributes);
            Assert.Equal(FileAttributeType.BitDepth, file.Attributes.ToList()[0].Type);
            Assert.Equal(4, file.Attributes.ToList()[0].Value);
        }
Пример #8
0
        public void Parse_Handles_Empty_Attributes(string username, int token, byte freeUploadSlots, int uploadSpeed, long queueLength)
        {
            var msg = new MessageBuilder()
                      .WriteCode(MessageCode.Peer.SearchResponse)
                      .WriteString(username)
                      .WriteInteger(token)
                      .WriteInteger(1)         // file count
                      .WriteByte(0x2)          // code
                      .WriteString("filename") // filename
                      .WriteLong(3)            // size
                      .WriteString("ext")      // extension
                      .WriteInteger(0)         // attribute count
                      .WriteByte(freeUploadSlots)
                      .WriteInteger(uploadSpeed)
                      .WriteLong(queueLength)
                      .WriteBytes(new byte[4]) // unknown 4 bytes
                      .Compress()
                      .Build();

            var r = SearchResponse.FromByteArray(msg);

            Assert.Single(r.Files);
            Assert.Empty(r.Files.ToList()[0].Attributes);
        }