encoding: Extending Block and EncodingBuffer interfaces

Now it is possible to use constructors to create Block from
EncodingBuffer and EncodingBuffer from Block.

Block->EncodingBuffer conversion is potentially dangerous and should be
used only in exceptional cases, such as Data packet encoding: to encode
the signed part first and then extend signed part with signature and
other related fields.

Change-Id: I5a13bf0c196ecd0d45dfa14c4cb6f4a9f612420c
diff --git a/tests/test-block.cpp b/tests/test-block.cpp
index c2fab26..7ed80fa 100644
--- a/tests/test-block.cpp
+++ b/tests/test-block.cpp
@@ -12,11 +12,11 @@
 
 BOOST_AUTO_TEST_SUITE(TestBlock)
 
-BOOST_AUTO_TEST_CASE (Decode)
+BOOST_AUTO_TEST_CASE (EncodingBufferToBlock)
 {
   uint8_t value[4];
 
-  ndn::EncodingBuffer buffer;
+  EncodingBuffer buffer;
   size_t length = buffer.prependByteArray(value, sizeof(value));
   buffer.prependVarNumber(length);
   buffer.prependVarNumber(0xe0);
@@ -25,6 +25,33 @@
   BOOST_REQUIRE_NO_THROW(block = buffer.block());
   BOOST_CHECK_EQUAL(block.type(), 0xe0);
   BOOST_CHECK_EQUAL(block.value_size(), sizeof(value));
+
+  BOOST_REQUIRE_NO_THROW(block = Block(buffer));
+  BOOST_CHECK_EQUAL(block.type(), 0xe0);
+  BOOST_CHECK_EQUAL(block.value_size(), sizeof(value));
+}
+
+BOOST_AUTO_TEST_CASE (BlockToBuffer)
+{
+  shared_ptr<Buffer> buf = make_shared<Buffer>(10);
+  for (int i = 0; i < 10; i++) (*buf)[i] = i;
+  
+  Block block(0xab, buf);
+  block.encode();
+
+  EncodingBuffer buffer(0,0);
+  BOOST_REQUIRE_NO_THROW(buffer = EncodingBuffer(block));
+  BOOST_CHECK_EQUAL(buffer.size(), 12);
+  BOOST_CHECK_EQUAL(buffer.capacity(), 12);
+
+  (*buf)[1] = 0xe0;
+  (*buf)[2] = 2;
+  BOOST_REQUIRE_NO_THROW(block = Block(buf, buf->begin() + 1, buf->begin() + 5));
+  BOOST_CHECK_EQUAL(block.type(), 0xe0);
+
+  BOOST_REQUIRE_NO_THROW(buffer = EncodingBuffer(block));
+  BOOST_CHECK_EQUAL(buffer.size(), 4);
+  BOOST_CHECK_EQUAL(buffer.capacity(), 10);
 }
 
 BOOST_AUTO_TEST_SUITE_END()