Пример #1
0
 public void TestUnpackObject_ByteArray_Int32_OffsetIsNegative()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => Unpacking.UnpackObject(new byte[] { 0x1 }, -1));
 }
Пример #2
0
 public void TestUnpackDictionary_DictionaryCountIsGreaterThanInt32MaxValue()
 {
     Assert.Throws <MessageNotSupportedException>(() => Unpacking.UnpackDictionary(new byte[] { 0xDF, 0x80, 0x00, 0x00, 0x00, 0xFF }));
 }
Пример #3
0
 public void TestUnpackObject_ByteArray_Int32_ByteArrayIsEmpty()
 {
     Assert.Throws <ArgumentException>(() => Unpacking.UnpackObject(new byte[0], 0));
 }
Пример #4
0
 public void TestUnpackCharStream_Stream_Null()
 {
     Assert.Throws <ArgumentNullException>(() => Unpacking.UnpackCharStream(null));
 }
Пример #5
0
 public void TestUnpackCharStream_Stream_Encoding_EncodingIsNull()
 {
     Assert.Throws <ArgumentNullException>(() => Unpacking.UnpackCharStream(new MemoryStream(new byte[] { 0xA1, ( byte )'A' }), null));
 }
Пример #6
0
 public void TestUnpackString_ByteArray_Int32_Encoding_OffsetIsTooBig()
 {
     Assert.Throws <ArgumentException>(() => Unpacking.UnpackString(new byte[] { 0x1 }, 1, Encoding.UTF8));
 }
Пример #7
0
 public void TestUnpackString_ByteArray_Int32_Encoding_EncodingIsNull()
 {
     Assert.Throws <ArgumentNullException>(() => Unpacking.UnpackString(new byte[] { 0x1 }, 0, null));
 }
Пример #8
0
		/// <summary>
		///		Transit current stage to unpackContextCollection stage with cleanuping states.
		/// </summary>
		private void TransitToUnpackContextCollection()
		{
			this._next = this._unpackHeader;
			this._isInCollection = true;
			this._contextValueHeader = MessagePackHeader.Null;
			this._bytesBuffer = BytesBuffer.Null;
		}
 public void TestUnpackDictionaryCount_ByteArray_Offset_Null()
 {
     Assert.Throws <ArgumentNullException>(() => Unpacking.UnpackDictionaryCount(default(byte[]), 0));
 }
Пример #10
0
		/// <summary>
		///		Try unpack object from specified source.
		/// </summary>
		/// <param name="source">Input source to unpack.</param>
		/// <param name="unpackingMode"><see cref="UnpackingMode"/> that controls unpacking flow.</param>
		/// <returns>
		///		Unpacked entry. The mean of the entry depends on specified <paramref name="unpackingMode"/>.
		/// </returns>
		/// <remarks>
		///		<para>
		///			When this method returns null, caller can feed extra bytes to <paramref name="source"/> and invoke this again. 
		///			It could succeed because this instance preserves previous invocation state, and required bytes are supplied.
		///		</para>
		///		<para>
		///			When this method completes unpackaging single <see cref="MessagePackObject"/> tree,
		///			this method stops iterating <paramref name="source"/> (via <see cref="IEnumerator&lt;T&gt;"/>.
		///			This behavior is notified via <see cref="IDisposable.Dispose">IEnumerator&lt;T&gt;.Dispose()</see> method.
		///		</para>
		/// </remarks>
		public MessagePackObject? Unpack( Stream source, UnpackingMode unpackingMode )
		{
			// FIXME:BULK LOAD
			Contract.Assert( source != null );

			this._lastEmptyCollection = EmptyCollectionType.None;

			MessagePackObject? collectionItemOrRoot;
			while ( this._next( source, unpackingMode, out collectionItemOrRoot ) )
			{
				if ( collectionItemOrRoot != null )
				{
					int depth = this._collectionState.Depth;
					var root = this.AddToContextCollection( collectionItemOrRoot.Value );
					this._hasMoreEntries = depth <= this._collectionState.Depth;

					if ( root != null )
					{
#if DEBUG
						Contract.Assert( this._collectionState.IsEmpty );
#endif
						this._next = this._unpackHeader;
						this._isInCollection = false;
#if DEBUG
						Contract.Assert( this._contextValueHeader.Type == MessageType.Unknown, this._contextValueHeader.ToString() );// null
						Contract.Assert( this._bytesBuffer.BackingStore == null, this._bytesBuffer.ToString() ); // null
#endif
						if ( unpackingMode == UnpackingMode.PerEntry )
						{
							if ( this._lastEmptyCollection != EmptyCollectionType.None )
							{
								// It was empty collection.
								return 0;
							}
							else
							{
								// Last item
								return collectionItemOrRoot.Value;
							}
						}
						else
						{
							// Length
							var readByteLength = this._readByteLength;
							this._readByteLength = 0L;
							return readByteLength;
						}
					}
				}
				else
				{
					this._hasMoreEntries = true;
				}

				// There are more entries in the tree.
				if ( unpackingMode == UnpackingMode.PerEntry )
				{
					// The unpacker may return unpacked collection item.
					if ( this._isInCollection )
					{
						if ( this._collectionState.UnpackedItemsCount == 0 )
						{
							// Count
							return this._collectionState.UnpackingItemsCount;
						}
						else if ( this._lastEmptyCollection != EmptyCollectionType.None )
						{
							// Empty nested collection.
							return 0;
						}
						else
						{
							Contract.Assert( collectionItemOrRoot.HasValue );
							return collectionItemOrRoot.Value;
						}
					}
				}
			}

			return null;
		}
Пример #11
0
		/// <summary>
		///		Transit current stage to unpackRawBytes stage with cleanuping states.
		/// </summary>
		/// <param name="length">The known length of the source.</param>
		private void TransitToUnpackRawBytes( uint length )
		{
			this._next = this._unpackRawBytes;
			this._isInCollection = false;
			// Allocate buffer to store raw binaries.
			this._bytesBuffer = new BytesBuffer( length );
		}
Пример #12
0
		/// <summary>
		///		Initializes a new instance of the <see cref="StreamingUnpacker"/> class.
		/// </summary>
		public StreamingUnpacker()
		{
			this._unpackCollectionLength = this.UnpackCollectionLength;
			this._unpackHeader = this.UnpackHeader;
			this._unpackRawLength = this.UnpackRawLength;
			this._unpackRawBytes = this.UnpackRawBytes;
			this._unpackScalar = this.UnpackScalar;
			this._next = this._unpackHeader;
		}
Пример #13
0
        public void TestMap()
        {
            var emptyMap = new Dictionary <int, int>();
            {
                var output = new MemoryStream();
                Packer.Create(output).PackDictionary(emptyMap);
                Assert.AreEqual(0, Unpacking.UnpackDictionaryCount(new MemoryStream(output.ToArray())));
                Assert.AreEqual(0, Unpacking.UnpackDictionaryCount(output.ToArray()).Value);
            }

            var random = new Random();

            for (int i = 0; i < 100; i++)
            {
                var m   = new Dictionary <int, int>();
                int len = ( int )random.Next() % 1000 + 1;
                for (int j = 0; j < len; j++)
                {
                    m[j] = j;
                }
                var output = new MemoryStream();
                Packer.Create(output).PackDictionary(m);

                Stream streamInput = new MemoryStream(output.ToArray());
                Assert.AreEqual(len, Unpacking.UnpackDictionaryCount(streamInput));
                for (int j = 0; j < len; j++)
                {
                    int value;
                    Assert.IsTrue(m.TryGetValue(Unpacking.UnpackInt32(streamInput), out value));
                    Assert.AreEqual(value, Unpacking.UnpackInt32(streamInput));
                }

                byte[] byteArrayInput = output.ToArray();
                var    arrayLength    = Unpacking.UnpackDictionaryCount(byteArrayInput);
                Assert.AreEqual(len, arrayLength.Value);
                int offset = arrayLength.ReadCount;
                for (int j = 0; j < len; j++)
                {
                    var keyUar = Unpacking.UnpackInt32(byteArrayInput, offset);
                    Assert.AreNotEqual(0, keyUar.ReadCount);
                    int value;
                    Assert.IsTrue(m.TryGetValue(keyUar.Value, out value));
                    var valueUar = Unpacking.UnpackInt32(byteArrayInput, offset + keyUar.ReadCount);
                    Assert.AreEqual(value, valueUar.Value);
                    offset += keyUar.ReadCount + valueUar.ReadCount;
                }
            }

            for (int i = 0; i < 100; i++)
            {
                var m   = new Dictionary <string, int>();
                int len = ( int )random.Next() % 1000 + 1;
                for (int j = 0; j < len; j++)
                {
                    m[j.ToString()] = j;
                }
                var output = new MemoryStream();
                Packer.Create(output).PackDictionary(m);

                Stream streamInput = new MemoryStream(output.ToArray());
                Assert.AreEqual(len, Unpacking.UnpackDictionaryCount(streamInput));
                for (int j = 0; j < len; j++)
                {
                    int value;
                    Assert.IsTrue(m.TryGetValue(Unpacking.UnpackString(streamInput), out value));
                    Assert.AreEqual(value, Unpacking.UnpackInt32(streamInput));
                }

                byte[] byteArrayInput = output.ToArray();
                var    arrayLength    = Unpacking.UnpackDictionaryCount(byteArrayInput);
                Assert.AreEqual(len, arrayLength.Value);
                int offset = arrayLength.ReadCount;
                for (int j = 0; j < len; j++)
                {
                    var usr = Unpacking.UnpackString(byteArrayInput, offset);
                    Assert.AreNotEqual(0, usr.ReadCount);
                    int value;
                    Assert.IsTrue(m.TryGetValue(usr.Value, out value));
                    var uar = Unpacking.UnpackInt32(byteArrayInput, offset + usr.ReadCount);
                    Assert.AreEqual(value, uar.Value);
                    offset += usr.ReadCount + uar.ReadCount;
                }
            }
        }
Пример #14
0
        public void TestArray()
        {
            var emptyList = new List <int>();
            {
                var output = new MemoryStream();
                Packer.Create(output).PackCollection(emptyList);
                Assert.AreEqual(0, Unpacking.UnpackArrayLength(new MemoryStream(output.ToArray())));
                Assert.AreEqual(0, Unpacking.UnpackArrayLength(output.ToArray()).Value);
            }

            var random = new Random();

            for (int i = 0; i < 100; i++)
            {
                var l   = new List <int>();
                int len = ( int )random.Next() % 1000 + 1;
                for (int j = 0; j < len; j++)
                {
                    l.Add(j);
                }
                var output = new MemoryStream();
                Packer.Create(output).PackCollection(l);

                Stream streamInput = new MemoryStream(output.ToArray());
                Assert.AreEqual(len, Unpacking.UnpackArrayLength(streamInput));
                for (int j = 0; j < len; j++)
                {
                    Assert.AreEqual(l[j], Unpacking.UnpackInt32(streamInput));
                }

                byte[] byteArrayInput = output.ToArray();
                var    arrayLength    = Unpacking.UnpackArrayLength(byteArrayInput);
                Assert.AreEqual(len, arrayLength.Value);
                int offset = arrayLength.ReadCount;
                for (int j = 0; j < len; j++)
                {
                    var uar = Unpacking.UnpackInt32(byteArrayInput, offset);
                    Assert.AreNotEqual(0, uar.ReadCount);
                    Assert.AreEqual(l[j], uar.Value);
                    offset += uar.ReadCount;
                }
            }

            for (int i = 0; i < 100; i++)
            {
                var l   = new List <String>();
                int len = ( int )random.Next() % 1000 + 1;
                for (int j = 0; j < len; j++)
                {
                    l.Add(j.ToString());
                }
                var output = new MemoryStream();
                Packer.Create(output).PackCollection(l);

                Stream streamInput = new MemoryStream(output.ToArray());
                Assert.AreEqual(len, Unpacking.UnpackArrayLength(streamInput));
                for (int j = 0; j < len; j++)
                {
                    Assert.AreEqual(l[j], Unpacking.UnpackString(streamInput));
                }

                byte[] byteArrayInput = output.ToArray();
                var    arrayLength    = Unpacking.UnpackArrayLength(byteArrayInput);
                Assert.AreEqual(len, arrayLength.Value);
                int offset = arrayLength.ReadCount;
                for (int j = 0; j < len; j++)
                {
                    var usr = Unpacking.UnpackString(byteArrayInput, offset);
                    Assert.AreEqual(l[j], usr.Value);
                    offset += usr.ReadCount;
                }
            }
        }
Пример #15
0
 public void TestUnpackObject_Stream_Null()
 {
     Assert.Throws <ArgumentNullException>(() => Unpacking.UnpackObject(default(Stream)));
 }
 public void TestUnpackDictionaryCount_ByteArray_Offset_OffsetIsNegative()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => Unpacking.UnpackDictionaryCount(new byte[] { 0x1 }, -1));
 }
Пример #17
0
 public void TestUnpackDouble_ByteArray_NotDouble()
 {
     Assert.Throws <MessageTypeException>(() => Unpacking.UnpackDouble(new byte[] { 0xC3 }));
 }
 public void TestUnpackDictionaryCount_ByteArray_Offset_OffsetIsTooBig()
 {
     Assert.Throws <ArgumentException>(() => Unpacking.UnpackDictionaryCount(new byte[] { 0x1 }, 1));
 }
Пример #19
0
 public void TestUnpackString_ByteArray_Int32_Encoding_OffsetIsNegative()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => Unpacking.UnpackString(new byte[] { 0x1 }, -1, Encoding.UTF8));
 }
 public void TestUnpackDictionaryCount_ByteArray_Offset_Empty()
 {
     Assert.Throws <ArgumentException>(() => Unpacking.UnpackDictionaryCount(new byte[0], 0));
 }
Пример #21
0
 public void TestUnpackString_Stream_Encoding_StreamIsNull()
 {
     Assert.Throws <ArgumentNullException>(() => Unpacking.UnpackString(default(Stream), Encoding.UTF8));
 }
 public void TestUnpackDictionary_ByteArray_NotMap()
 {
     Assert.Throws <MessageTypeException>(() => Unpacking.UnpackDictionary(new byte[] { 0x1 }));
 }
Пример #23
0
 public void TestUnpackCharStream_Stream_Encoding_StreamIsNull()
 {
     Assert.Throws <ArgumentNullException>(() => Unpacking.UnpackCharStream(null, Encoding.UTF8));
 }
Пример #24
0
 public void TestUnpackString_ByteArray_1ByteNonUtf8String_ExceptionInReaderOperation()
 {
     Assert.Throws <MessageTypeException>(() => Unpacking.UnpackString(new byte[] { 0xA1, 0xFF }));
 }
Пример #25
0
 public void TestUnpackObject_ByteArray_ByteArrayIsNull()
 {
     Assert.Throws <ArgumentNullException>(() => Unpacking.UnpackObject(default(byte[])));
 }
Пример #26
0
 public void TestUnpackString_ByteArray_Null()
 {
     Assert.Throws <ArgumentNullException>(() => Unpacking.UnpackString(default(byte[])));
 }
Пример #27
0
 public void TestUnpackObject_ByteArray_Int32_ByteArrayIsNull()
 {
     Assert.Throws <ArgumentNullException>(() => Unpacking.UnpackObject(null, 0));
 }
Пример #28
0
 public void TestUnpackString_ByteArray_ByteArrayIsEmpty()
 {
     Assert.Throws <ArgumentException>(() => Unpacking.UnpackString(new byte[0]));
 }
Пример #29
0
 public void TestUnpackObject_ByteArray_Int32_OffsetIsTooBig()
 {
     Assert.Throws <ArgumentException>(() => Unpacking.UnpackObject(new byte[] { 0x1 }, 1));
 }
Пример #30
0
 public void TestUnpackString_ByteArray_Int32_Encoding_ByteArrayIsNull()
 {
     Assert.Throws <ArgumentNullException>(() => Unpacking.UnpackString(null, 0, Encoding.UTF8));
 }
Пример #31
0
 public void TestUnpackBinary_BinaryLengthIsGreaterThanInt32MaxValue()
 {
     Assert.Throws <MessageNotSupportedException>(() => Unpacking.UnpackBinary(new byte[] { 0xDB, 0x80, 0x00, 0x00, 0x00, 0xFF }));
 }
Пример #32
0
 public void TestUnpackString_ByteArray_Int32_Encoding_ByteArrayIsEmpty()
 {
     Assert.Throws <ArgumentException>(() => Unpacking.UnpackString(new byte[0], 0, Encoding.UTF8));
 }
Пример #33
0
 public void TestUnpackInt32_NotNumeric()
 {
     Assert.Throws <MessageTypeException>(() => Unpacking.UnpackInt32(new byte[] { 0x80 }));
 }
Пример #34
0
 public void TestUnpackDouble_ByteArray_Empty()
 {
     Assert.Throws <ArgumentException>(() => Unpacking.UnpackDouble(new byte[0]));
 }