Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 8b1674a | 2014-05-15 00:58:43 -0700 | [diff] [blame] | 22 | |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 23 | #include "encoding/encoding-buffer.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 24 | #include "encoding/buffer-stream.hpp" |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 25 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 26 | #include "boost-test.hpp" |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 27 | |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 28 | using namespace std; |
| 29 | namespace ndn { |
| 30 | |
| 31 | BOOST_AUTO_TEST_SUITE(TestBlock) |
| 32 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 33 | BOOST_AUTO_TEST_CASE(BlockBasic) |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 34 | { |
| 35 | EncodingBuffer buffer; |
| 36 | EncodingEstimator estimator; |
| 37 | size_t s1, s2; |
| 38 | |
| 39 | // VarNumber checks |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 40 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 41 | s1 = buffer.prependVarNumber(252); |
| 42 | s2 = estimator.prependVarNumber(252); |
| 43 | BOOST_CHECK_EQUAL(buffer.size(), 1); |
| 44 | BOOST_CHECK_EQUAL(s1, 1); |
| 45 | BOOST_CHECK_EQUAL(s2, 1); |
| 46 | buffer = EncodingBuffer(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 47 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 48 | s1 = buffer.prependVarNumber(253); |
| 49 | s2 = estimator.prependVarNumber(253); |
| 50 | BOOST_CHECK_EQUAL(buffer.size(), 3); |
| 51 | BOOST_CHECK_EQUAL(s1, 3); |
| 52 | BOOST_CHECK_EQUAL(s2, 3); |
| 53 | buffer = EncodingBuffer(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 54 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 55 | s1 = buffer.prependVarNumber(255); |
| 56 | s2 = estimator.prependVarNumber(255); |
| 57 | BOOST_CHECK_EQUAL(buffer.size(), 3); |
| 58 | BOOST_CHECK_EQUAL(s1, 3); |
| 59 | BOOST_CHECK_EQUAL(s2, 3); |
| 60 | buffer = EncodingBuffer(); |
| 61 | |
| 62 | s1 = buffer.prependVarNumber(65535); |
| 63 | s2 = estimator.prependVarNumber(65535); |
| 64 | BOOST_CHECK_EQUAL(buffer.size(), 3); |
| 65 | BOOST_CHECK_EQUAL(s1, 3); |
| 66 | BOOST_CHECK_EQUAL(s2, 3); |
| 67 | buffer = EncodingBuffer(); |
| 68 | |
| 69 | s1 = buffer.prependVarNumber(65536); |
| 70 | s2 = estimator.prependVarNumber(65536); |
| 71 | BOOST_CHECK_EQUAL(buffer.size(), 5); |
| 72 | BOOST_CHECK_EQUAL(s1, 5); |
| 73 | BOOST_CHECK_EQUAL(s2, 5); |
| 74 | buffer = EncodingBuffer(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 75 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 76 | s1 = buffer.prependVarNumber(4294967295LL); |
| 77 | s2 = estimator.prependVarNumber(4294967295LL); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 78 | BOOST_CHECK_EQUAL(buffer.size(), 5); |
| 79 | BOOST_CHECK_EQUAL(s1, 5); |
| 80 | BOOST_CHECK_EQUAL(s2, 5); |
| 81 | buffer = EncodingBuffer(); |
| 82 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 83 | s1 = buffer.prependVarNumber(4294967296LL); |
| 84 | s2 = estimator.prependVarNumber(4294967296LL); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 85 | BOOST_CHECK_EQUAL(buffer.size(), 9); |
| 86 | BOOST_CHECK_EQUAL(s1, 9); |
| 87 | BOOST_CHECK_EQUAL(s2, 9); |
| 88 | buffer = EncodingBuffer(); |
| 89 | |
| 90 | // nonNegativeInteger checks |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 91 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 92 | s1 = buffer.prependNonNegativeInteger(252); |
| 93 | s2 = estimator.prependNonNegativeInteger(252); |
| 94 | BOOST_CHECK_EQUAL(buffer.size(), 1); |
| 95 | BOOST_CHECK_EQUAL(s1, 1); |
| 96 | BOOST_CHECK_EQUAL(s2, 1); |
| 97 | buffer = EncodingBuffer(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 98 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 99 | s1 = buffer.prependNonNegativeInteger(255); |
| 100 | s2 = estimator.prependNonNegativeInteger(255); |
| 101 | BOOST_CHECK_EQUAL(buffer.size(), 1); |
| 102 | BOOST_CHECK_EQUAL(s1, 1); |
| 103 | BOOST_CHECK_EQUAL(s2, 1); |
| 104 | buffer = EncodingBuffer(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 105 | |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 106 | s1 = buffer.prependNonNegativeInteger(256); |
| 107 | s2 = estimator.prependNonNegativeInteger(256); |
| 108 | BOOST_CHECK_EQUAL(buffer.size(), 2); |
| 109 | BOOST_CHECK_EQUAL(s1, 2); |
| 110 | BOOST_CHECK_EQUAL(s2, 2); |
| 111 | buffer = EncodingBuffer(); |
| 112 | |
| 113 | s1 = buffer.prependNonNegativeInteger(65535); |
| 114 | s2 = estimator.prependNonNegativeInteger(65535); |
| 115 | BOOST_CHECK_EQUAL(buffer.size(), 2); |
| 116 | BOOST_CHECK_EQUAL(s1, 2); |
| 117 | BOOST_CHECK_EQUAL(s2, 2); |
| 118 | buffer = EncodingBuffer(); |
| 119 | |
| 120 | s1 = buffer.prependNonNegativeInteger(65536); |
| 121 | s2 = estimator.prependNonNegativeInteger(65536); |
| 122 | BOOST_CHECK_EQUAL(buffer.size(), 4); |
| 123 | BOOST_CHECK_EQUAL(s1, 4); |
| 124 | BOOST_CHECK_EQUAL(s2, 4); |
| 125 | buffer = EncodingBuffer(); |
| 126 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 127 | s1 = buffer.prependNonNegativeInteger(4294967295LL); |
| 128 | s2 = estimator.prependNonNegativeInteger(4294967295LL); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 129 | BOOST_CHECK_EQUAL(buffer.size(), 4); |
| 130 | BOOST_CHECK_EQUAL(s1, 4); |
| 131 | BOOST_CHECK_EQUAL(s2, 4); |
| 132 | buffer = EncodingBuffer(); |
| 133 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 134 | s1 = buffer.prependNonNegativeInteger(4294967296LL); |
| 135 | s2 = estimator.prependNonNegativeInteger(4294967296LL); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 136 | BOOST_CHECK_EQUAL(buffer.size(), 8); |
| 137 | BOOST_CHECK_EQUAL(s1, 8); |
| 138 | BOOST_CHECK_EQUAL(s2, 8); |
| 139 | buffer = EncodingBuffer(); |
| 140 | } |
| 141 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 142 | BOOST_AUTO_TEST_CASE(EncodingBufferToBlock) |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 143 | { |
| 144 | uint8_t value[4]; |
| 145 | |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 146 | EncodingBuffer buffer; |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 147 | size_t length = buffer.prependByteArray(value, sizeof(value)); |
| 148 | buffer.prependVarNumber(length); |
| 149 | buffer.prependVarNumber(0xe0); |
| 150 | |
| 151 | Block block; |
| 152 | BOOST_REQUIRE_NO_THROW(block = buffer.block()); |
| 153 | BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| 154 | BOOST_CHECK_EQUAL(block.value_size(), sizeof(value)); |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 155 | |
| 156 | BOOST_REQUIRE_NO_THROW(block = Block(buffer)); |
| 157 | BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| 158 | BOOST_CHECK_EQUAL(block.value_size(), sizeof(value)); |
| 159 | } |
| 160 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 161 | BOOST_AUTO_TEST_CASE(BlockToBuffer) |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 162 | { |
| 163 | shared_ptr<Buffer> buf = make_shared<Buffer>(10); |
| 164 | for (int i = 0; i < 10; i++) (*buf)[i] = i; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 165 | |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 166 | Block block(0xab, buf); |
| 167 | block.encode(); |
| 168 | |
| 169 | EncodingBuffer buffer(0,0); |
| 170 | BOOST_REQUIRE_NO_THROW(buffer = EncodingBuffer(block)); |
| 171 | BOOST_CHECK_EQUAL(buffer.size(), 12); |
| 172 | BOOST_CHECK_EQUAL(buffer.capacity(), 12); |
| 173 | |
| 174 | (*buf)[1] = 0xe0; |
| 175 | (*buf)[2] = 2; |
| 176 | BOOST_REQUIRE_NO_THROW(block = Block(buf, buf->begin() + 1, buf->begin() + 5)); |
| 177 | BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| 178 | |
| 179 | BOOST_REQUIRE_NO_THROW(buffer = EncodingBuffer(block)); |
| 180 | BOOST_CHECK_EQUAL(buffer.size(), 4); |
| 181 | BOOST_CHECK_EQUAL(buffer.capacity(), 10); |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 184 | BOOST_AUTO_TEST_CASE(FromBuffer) |
| 185 | { |
| 186 | const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok |
| 187 | 0x01, 0x01, 0xfb, // ok |
| 188 | 0x03, 0x02, 0xff}; // not ok |
| 189 | BufferPtr buffer(new Buffer(TEST_BUFFER, sizeof(TEST_BUFFER))); |
| 190 | |
| 191 | // using BufferPtr (avoids memory copy) |
| 192 | size_t offset = 0; |
| 193 | Block testBlock; |
| 194 | BOOST_CHECK(Block::fromBuffer(buffer, offset, testBlock)); |
| 195 | BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| 196 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 197 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 198 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| 199 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| 200 | offset += testBlock.size(); |
| 201 | |
| 202 | BOOST_CHECK(Block::fromBuffer(buffer, offset, testBlock)); |
| 203 | BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| 204 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 205 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 206 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| 207 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| 208 | offset += testBlock.size(); |
| 209 | |
| 210 | BOOST_CHECK(!Block::fromBuffer(buffer, offset, testBlock)); |
| 211 | |
| 212 | // just buffer, copies memory |
| 213 | offset = 0; |
| 214 | BOOST_CHECK(Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock)); |
| 215 | BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| 216 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 217 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 218 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| 219 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| 220 | offset += testBlock.size(); |
| 221 | |
| 222 | BOOST_CHECK(Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock)); |
| 223 | BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| 224 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 225 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 226 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| 227 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| 228 | offset += testBlock.size(); |
| 229 | |
| 230 | BOOST_CHECK(!Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset, testBlock)); |
| 231 | } |
| 232 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 233 | BOOST_AUTO_TEST_CASE(BlockFromStream) |
| 234 | { |
| 235 | const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok |
| 236 | 0x01, 0x01, 0xfb, // ok |
| 237 | 0x03, 0x02, 0xff}; // not ok |
| 238 | |
| 239 | typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream; |
| 240 | ArrayStream stream(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER)); |
| 241 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 242 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 243 | Block testBlock; |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 244 | BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream)); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 245 | BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| 246 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 247 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 248 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| 249 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| 250 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 251 | BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream)); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 252 | BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| 253 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 254 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 255 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| 256 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| 257 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 258 | BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 261 | BOOST_AUTO_TEST_CASE(Equality) |
| 262 | { |
| 263 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>)); |
| 264 | |
| 265 | Block a("\x08\x00", 2); |
| 266 | Block b("\x08\x00", 2);; |
| 267 | BOOST_CHECK_EQUAL(a == b, true); |
| 268 | BOOST_CHECK_EQUAL(a != b, false); |
| 269 | |
| 270 | Block c("\x06\x00", 2); |
| 271 | Block d("\x08\x00", 2);; |
| 272 | BOOST_CHECK_EQUAL(c == d, false); |
| 273 | BOOST_CHECK_EQUAL(c != d, true); |
| 274 | |
| 275 | Block e("\x06\x00", 2); |
| 276 | Block f("\x06\x01\xcc", 3);; |
| 277 | BOOST_CHECK_EQUAL(e == f, false); |
| 278 | BOOST_CHECK_EQUAL(e != f, true); |
| 279 | } |
| 280 | |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 281 | BOOST_AUTO_TEST_SUITE_END() |
| 282 | |
| 283 | } // namespace ndn |