예제 #1
0
 public void TestEncodeDecode()
 {
     var codec = new MessageCodec(manager, wcfCodec, 65536);
      // invalid encode
      try
      {
     codec.Encode(null);
     Assert.Fail("Expected: Exception");
      }
      catch (AssertFailedException) { throw; }
      catch { }
      try
      {
     codec.Encode(null, null);
     Assert.Fail("Expected: Exception");
      }
      catch (AssertFailedException) { throw; }
      catch { }
      try
      {
     codec.Encode(CreateMessage(1), null);
     Assert.Fail("Expected: Exception");
      }
      catch (AssertFailedException) { throw; }
      catch { }
      try
      {
     codec.Encode(null, new MemoryStream());
     Assert.Fail("Expected: Exception");
      }
      catch (AssertFailedException) { throw; }
      catch { }
      // invalid decode
      Assert.AreEqual(codec.Decode((Byte[])null), null);
      Assert.AreEqual(codec.Decode(new Byte[0]), null);
      try
      {
     codec.Decode(new Byte[1024]);
     Assert.Fail("Expected: Exception");
      }
      catch (AssertFailedException) { throw; }
      catch { }
      Assert.AreEqual(codec.Decode(default(ArraySegment<Byte>)), null);
      try
      {
     codec.Decode(new ArraySegment<Byte>(new Byte[1024]));
     Assert.Fail("Expected: Exception");
      }
      catch (AssertFailedException) { throw; }
      catch { }
      Assert.AreEqual(codec.Decode((Stream)null), null);
      Assert.AreEqual(codec.Decode(new MemoryStream()), null);
      try
      {
     var stream = new MemoryStream();
     stream.Write(new Byte[1024], 0, 1024);
     stream.Position = 0;
     codec.Decode(stream);
     Assert.Fail("Expected: Exception");
      }
      catch (AssertFailedException) { throw; }
      catch { }
      // valid buffered message
      AssertIsEqualMessage(
     CreateMessage(1),
     codec.Decode(codec.Encode(CreateMessage(1)))
      );
      var encoded = codec.Encode(CreateMessage(1));
      AssertIsEqualMessage(
     CreateMessage(1),
     codec.Decode(encoded.Raw.Skip(encoded.Offset).Take(encoded.Length).ToArray())
      );
      // valid streamed message
      using (var stream = new MemoryStream())
      {
     codec.Encode(CreateMessage(2), stream);
     stream.Position = 0;
     AssertIsEqualMessage(CreateMessage(2), codec.Decode(stream));
      }
      // using encoded
      codec.UsingEncoded(
     CreateMessage(3),
     buffer1 =>
     {
        var buffer2 = codec.Allocate();
        Array.Copy(buffer1.Raw, buffer1.Offset, buffer2.Raw, buffer2.Offset, buffer1.Length);
        AssertIsEqualMessage(
           CreateMessage(3),
           codec.Decode(new ArraySegment<Byte>(buffer2.Raw, buffer2.Offset, buffer1.Length))
        );
     }
      );
 }