PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/source/library/Interlace.Tests/Amf/CodecTests.cs

https://bitbucket.org/VahidN/interlace
C# | 172 lines | 114 code | 32 blank | 26 comment | 8 complexity | 3f6eb7f646236c7c5b84748aa7fd4218 MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2007-2010, Computer Consultancy Pty Ltd
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright
  10. // notice, this list of conditions and the following disclaimer in the
  11. // documentation and/or other materials provided with the distribution.
  12. // * Neither the name of the Computer Consultancy Pty Ltd nor the
  13. // names of its contributors may be used to endorse or promote products
  14. // derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. using System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Text;
  31. using MbUnit.Framework;
  32. using Interlace.Amf;
  33. #endregion
  34. namespace Interlace.Tests.Amf
  35. {
  36. [TestFixture]
  37. public class CodecTests
  38. {
  39. public static void AssertListsAreEqual<T>(IList<T> lhs, IList<T> rhs)
  40. {
  41. Assert.AreEqual(lhs.Count, rhs.Count);
  42. for (int i = 0; i < lhs.Count; i++)
  43. {
  44. AssertEquality(lhs[i], rhs[i]);
  45. }
  46. }
  47. public static void AssertDictionariesAreEqual<K, V>(IDictionary<K, V> lhs, IDictionary<K, V> rhs)
  48. {
  49. Assert.AreEqual(lhs.Count, rhs.Count);
  50. foreach (KeyValuePair<K, V> pair in lhs)
  51. {
  52. Assert.IsTrue(rhs.ContainsKey(pair.Key));
  53. Assert.AreEqual(pair.Value, rhs[pair.Key]);
  54. }
  55. }
  56. public static void AssertEquality(object expectedValue, object actualValue)
  57. {
  58. if (expectedValue is byte[])
  59. {
  60. ArrayAssert.AreEqual(expectedValue as byte[], actualValue as byte[]);
  61. }
  62. else if (expectedValue is AmfArray)
  63. {
  64. AmfArray lhs = expectedValue as AmfArray;
  65. AmfArray rhs = actualValue as AmfArray;
  66. AssertListsAreEqual(lhs.DenseElements, rhs.DenseElements);
  67. AssertDictionariesAreEqual(lhs.AssociativeElements, rhs.AssociativeElements);
  68. }
  69. else if (expectedValue != null && expectedValue.GetType().Equals(typeof(AmfObject)))
  70. {
  71. AmfObject lhs = expectedValue as AmfObject;
  72. AmfObject rhs = actualValue as AmfObject;
  73. AssertDictionariesAreEqual(lhs.Properties, rhs.Properties);
  74. }
  75. else
  76. {
  77. Assert.AreEqual(expectedValue, actualValue);
  78. }
  79. }
  80. public static void AssertRoundTrip(object expectedValue, byte[] encodedBytes)
  81. {
  82. AmfRegistry registry = new AmfRegistry();
  83. registry.RegisterClassAlias(typeof(BitPlantationSinglePropertyTest));
  84. object actualValue = AmfReader.Read(registry, encodedBytes);
  85. AssertEquality(actualValue, expectedValue);
  86. // Test the other way:
  87. byte[] firstTrip = AmfWriter.Write(registry, expectedValue);
  88. object secondTrip = AmfReader.Read(registry, firstTrip);
  89. AssertEquality(expectedValue, secondTrip);
  90. }
  91. [Test]
  92. public void Test()
  93. {
  94. AssertRoundTrip(null, new byte[] {1});
  95. AssertRoundTrip(false, new byte[] {2});
  96. AssertRoundTrip(true, new byte[] {3});
  97. AssertRoundTrip(0, new byte[] {4, 0x00});
  98. AssertRoundTrip(1, new byte[] {4, 0x01});
  99. AssertRoundTrip(0x7f, new byte[] {4, 0x7f});
  100. AssertRoundTrip(0x80, new byte[] {4, 0x81, 0x00});
  101. AssertRoundTrip(0x3fff, new byte[] {4, 0xff, 0x7f});
  102. AssertRoundTrip(0x4000, new byte[] {4, 0x81, 0x80, 0x00});
  103. AssertRoundTrip(0x001fffff, new byte[] {4, 0xff, 0xff, 0x7f});
  104. AssertRoundTrip(0.5, new byte[] {0x05, 0x3f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
  105. AssertRoundTrip(2535301200456458802993406410752.0, new byte[] {0x05, 0x46, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
  106. AssertRoundTrip("", new byte[] {0x06, 0x01});
  107. AssertRoundTrip("Hello", new byte[] {0x06, 0x0b, 0x48, 0x65, 0x6c, 0x6c, 0x6f});
  108. AssertRoundTrip(new DateTime(1970, 1, 1, 0, 0, 0), new byte[] {0x08, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
  109. AssertRoundTrip(new DateTime(2001, 10, 10, 21, 22, 0), new byte[] {0x08, 0x01, 0x42, 0x6d, 0x2f, 0x0f, 0xc8, 0x18, 0x00, 0x00});
  110. AssertRoundTrip(new AmfArray(), new byte[] {0x09, 0x01, 0x01});
  111. AssertRoundTrip(AmfArray.Dense(1, "test"), new byte[] {0x09, 0x05, 0x01, 0x04, 0x01, 0x06, 0x09, 0x74, 0x65, 0x73, 0x74});
  112. AmfArray array = AmfArray.Dense(1, 2);
  113. array["test"] = "case";
  114. array[6] = "six";
  115. AssertRoundTrip(array,
  116. new byte[] {0x09, 0x05, 0x03, 0x36, 0x06, 0x07, 0x73, 0x69,
  117. 0x78, 0x09, 0x74, 0x65, 0x73, 0x74, 0x06, 0x09,
  118. 0x63, 0x61, 0x73, 0x65, 0x01, 0x04, 0x01, 0x04,
  119. 0x02});
  120. AssertRoundTrip(new byte[] {}, new byte[] {0x0c, 0x01});
  121. AssertRoundTrip(new byte[] {0x00, 0x80, 0xff}, new byte[] {0x0c, 0x07, 0x00, 0x80, 0xff});
  122. }
  123. [Test]
  124. public void TestDynamicObjects()
  125. {
  126. AssertRoundTrip(new AmfObject(),
  127. new byte[] {0x0a, 0x0b, 0x01, 0x01});
  128. AmfObject attributedObject = new AmfObject();
  129. attributedObject.Properties["DynamicAttribute"] = "test";
  130. attributedObject.Properties["OtherAttribute"] = 1;
  131. AssertRoundTrip(attributedObject,
  132. new byte[] {0x0a, 0x0b, 0x01, 0x1d, 0x4f, 0x74, 0x68, 0x65,
  133. 0x72, 0x41, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75,
  134. 0x74, 0x65, 0x04, 0x01, 0x21, 0x44, 0x79, 0x6e,
  135. 0x61, 0x6d, 0x69, 0x63, 0x41, 0x74, 0x74, 0x72,
  136. 0x69, 0x62, 0x75, 0x74, 0x65, 0x06, 0x09, 0x74,
  137. 0x65, 0x73, 0x74, 0x01});
  138. }
  139. }
  140. }