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 | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 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 | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 22 | #include "encoding/encoding-buffer.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 23 | #include "encoding/buffer-stream.hpp" |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 24 | #include "encoding/block-helpers.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 | namespace ndn { |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 29 | namespace tests { |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 30 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 31 | BOOST_AUTO_TEST_SUITE(EncodingBlock) |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 33 | class BasicBlockFixture |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 34 | { |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 35 | public: |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 36 | EncodingBuffer buffer; |
| 37 | EncodingEstimator estimator; |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 38 | }; |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 39 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 40 | BOOST_FIXTURE_TEST_SUITE(Basic, BasicBlockFixture) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 41 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 42 | BOOST_AUTO_TEST_CASE(VarNumberOneByte1) |
| 43 | { |
| 44 | size_t s1 = buffer.prependVarNumber(252); |
| 45 | size_t s2 = estimator.prependVarNumber(252); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 46 | BOOST_CHECK_EQUAL(buffer.size(), 1); |
| 47 | BOOST_CHECK_EQUAL(s1, 1); |
| 48 | BOOST_CHECK_EQUAL(s2, 1); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 49 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 50 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 51 | BOOST_AUTO_TEST_CASE(VarNumberOneByte2) |
| 52 | { |
| 53 | size_t s1 = buffer.prependVarNumber(253); |
| 54 | size_t s2 = estimator.prependVarNumber(253); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 55 | BOOST_CHECK_EQUAL(buffer.size(), 3); |
| 56 | BOOST_CHECK_EQUAL(s1, 3); |
| 57 | BOOST_CHECK_EQUAL(s2, 3); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 58 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 59 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 60 | BOOST_AUTO_TEST_CASE(VarNumberThreeBytes1) |
| 61 | { |
| 62 | size_t s1 = buffer.prependVarNumber(255); |
| 63 | size_t s2 = estimator.prependVarNumber(255); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 64 | BOOST_CHECK_EQUAL(buffer.size(), 3); |
| 65 | BOOST_CHECK_EQUAL(s1, 3); |
| 66 | BOOST_CHECK_EQUAL(s2, 3); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 67 | } |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 68 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 69 | BOOST_AUTO_TEST_CASE(VarNumberThreeBytes2) |
| 70 | { |
| 71 | size_t s1 = buffer.prependVarNumber(65535); |
| 72 | size_t s2 = estimator.prependVarNumber(65535); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 73 | BOOST_CHECK_EQUAL(buffer.size(), 3); |
| 74 | BOOST_CHECK_EQUAL(s1, 3); |
| 75 | BOOST_CHECK_EQUAL(s2, 3); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 76 | } |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 77 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 78 | BOOST_AUTO_TEST_CASE(VarNumberFiveBytes1) |
| 79 | { |
| 80 | size_t s1 = buffer.prependVarNumber(65536); |
| 81 | size_t s2 = estimator.prependVarNumber(65536); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 82 | BOOST_CHECK_EQUAL(buffer.size(), 5); |
| 83 | BOOST_CHECK_EQUAL(s1, 5); |
| 84 | BOOST_CHECK_EQUAL(s2, 5); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 85 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 86 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 87 | BOOST_AUTO_TEST_CASE(VarNumberFiveBytes2) |
| 88 | { |
| 89 | size_t s1 = buffer.prependVarNumber(4294967295LL); |
| 90 | size_t s2 = estimator.prependVarNumber(4294967295LL); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 91 | BOOST_CHECK_EQUAL(buffer.size(), 5); |
| 92 | BOOST_CHECK_EQUAL(s1, 5); |
| 93 | BOOST_CHECK_EQUAL(s2, 5); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 94 | } |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 95 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 96 | BOOST_AUTO_TEST_CASE(VarNumberNineBytes) |
| 97 | { |
| 98 | size_t s1 = buffer.prependVarNumber(4294967296LL); |
| 99 | size_t s2 = estimator.prependVarNumber(4294967296LL); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 100 | BOOST_CHECK_EQUAL(buffer.size(), 9); |
| 101 | BOOST_CHECK_EQUAL(s1, 9); |
| 102 | BOOST_CHECK_EQUAL(s2, 9); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 103 | } |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 104 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 105 | BOOST_AUTO_TEST_CASE(NonNegativeNumberOneByte1) |
| 106 | { |
| 107 | size_t s1 = buffer.prependNonNegativeInteger(252); |
| 108 | size_t s2 = estimator.prependNonNegativeInteger(252); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 109 | BOOST_CHECK_EQUAL(buffer.size(), 1); |
| 110 | BOOST_CHECK_EQUAL(s1, 1); |
| 111 | BOOST_CHECK_EQUAL(s2, 1); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 112 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 113 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 114 | BOOST_AUTO_TEST_CASE(NonNegativeNumberOneByte2) |
| 115 | { |
| 116 | size_t s1 = buffer.prependNonNegativeInteger(255); |
| 117 | size_t s2 = estimator.prependNonNegativeInteger(255); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 118 | BOOST_CHECK_EQUAL(buffer.size(), 1); |
| 119 | BOOST_CHECK_EQUAL(s1, 1); |
| 120 | BOOST_CHECK_EQUAL(s2, 1); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 121 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 122 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 123 | BOOST_AUTO_TEST_CASE(NonNegativeNumberTwoBytes1) |
| 124 | { |
| 125 | size_t s1 = buffer.prependNonNegativeInteger(256); |
| 126 | size_t s2 = estimator.prependNonNegativeInteger(256); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 127 | BOOST_CHECK_EQUAL(buffer.size(), 2); |
| 128 | BOOST_CHECK_EQUAL(s1, 2); |
| 129 | BOOST_CHECK_EQUAL(s2, 2); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 130 | } |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 131 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 132 | BOOST_AUTO_TEST_CASE(NonNegativeNumberTwoBytes2) |
| 133 | { |
| 134 | size_t s1 = buffer.prependNonNegativeInteger(65535); |
| 135 | size_t s2 = estimator.prependNonNegativeInteger(65535); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 136 | BOOST_CHECK_EQUAL(buffer.size(), 2); |
| 137 | BOOST_CHECK_EQUAL(s1, 2); |
| 138 | BOOST_CHECK_EQUAL(s2, 2); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 139 | } |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 140 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 141 | BOOST_AUTO_TEST_CASE(NonNegativeNumberFourBytes1) |
| 142 | { |
| 143 | size_t s1 = buffer.prependNonNegativeInteger(65536); |
| 144 | size_t s2 = estimator.prependNonNegativeInteger(65536); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 145 | BOOST_CHECK_EQUAL(buffer.size(), 4); |
| 146 | BOOST_CHECK_EQUAL(s1, 4); |
| 147 | BOOST_CHECK_EQUAL(s2, 4); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 148 | } |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 149 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 150 | BOOST_AUTO_TEST_CASE(NonNegativeNumberFourBytes2) |
| 151 | { |
| 152 | size_t s1 = buffer.prependNonNegativeInteger(4294967295LL); |
| 153 | size_t s2 = estimator.prependNonNegativeInteger(4294967295LL); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 154 | BOOST_CHECK_EQUAL(buffer.size(), 4); |
| 155 | BOOST_CHECK_EQUAL(s1, 4); |
| 156 | BOOST_CHECK_EQUAL(s2, 4); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 157 | } |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 158 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 159 | BOOST_AUTO_TEST_CASE(NonNegativeNumberEightBytes) |
| 160 | { |
| 161 | size_t s1 = buffer.prependNonNegativeInteger(4294967296LL); |
| 162 | size_t s2 = estimator.prependNonNegativeInteger(4294967296LL); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 163 | BOOST_CHECK_EQUAL(buffer.size(), 8); |
| 164 | BOOST_CHECK_EQUAL(s1, 8); |
| 165 | BOOST_CHECK_EQUAL(s2, 8); |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 168 | BOOST_AUTO_TEST_SUITE_END() // Basic |
| 169 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 170 | BOOST_AUTO_TEST_CASE(EncodingBufferToBlock) |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 171 | { |
| 172 | uint8_t value[4]; |
| 173 | |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 174 | EncodingBuffer buffer; |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 175 | size_t length = buffer.prependByteArray(value, sizeof(value)); |
| 176 | buffer.prependVarNumber(length); |
| 177 | buffer.prependVarNumber(0xe0); |
| 178 | |
| 179 | Block block; |
| 180 | BOOST_REQUIRE_NO_THROW(block = buffer.block()); |
| 181 | BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| 182 | BOOST_CHECK_EQUAL(block.value_size(), sizeof(value)); |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 183 | |
| 184 | BOOST_REQUIRE_NO_THROW(block = Block(buffer)); |
| 185 | BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| 186 | BOOST_CHECK_EQUAL(block.value_size(), sizeof(value)); |
| 187 | } |
| 188 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 189 | BOOST_AUTO_TEST_CASE(BlockToBuffer) |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 190 | { |
| 191 | shared_ptr<Buffer> buf = make_shared<Buffer>(10); |
| 192 | for (int i = 0; i < 10; i++) (*buf)[i] = i; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 193 | |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 194 | Block block(0xab, buf); |
| 195 | block.encode(); |
| 196 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 197 | { |
| 198 | BOOST_REQUIRE_NO_THROW(EncodingBuffer(block)); |
| 199 | EncodingBuffer buffer(block); |
| 200 | BOOST_CHECK_EQUAL(buffer.size(), 12); |
| 201 | BOOST_CHECK_EQUAL(buffer.capacity(), 12); |
| 202 | } |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 203 | |
| 204 | (*buf)[1] = 0xe0; |
| 205 | (*buf)[2] = 2; |
| 206 | BOOST_REQUIRE_NO_THROW(block = Block(buf, buf->begin() + 1, buf->begin() + 5)); |
| 207 | BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| 208 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 209 | { |
| 210 | BOOST_REQUIRE_NO_THROW(EncodingBuffer(block)); |
| 211 | EncodingBuffer buffer(block); |
| 212 | BOOST_CHECK_EQUAL(buffer.size(), 4); |
| 213 | BOOST_CHECK_EQUAL(buffer.capacity(), 10); |
| 214 | } |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 217 | BOOST_AUTO_TEST_CASE(FromBuffer) |
| 218 | { |
| 219 | const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok |
| 220 | 0x01, 0x01, 0xfb, // ok |
| 221 | 0x03, 0x02, 0xff}; // not ok |
| 222 | BufferPtr buffer(new Buffer(TEST_BUFFER, sizeof(TEST_BUFFER))); |
| 223 | |
| 224 | // using BufferPtr (avoids memory copy) |
| 225 | size_t offset = 0; |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 226 | bool isOk = false; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 227 | Block testBlock; |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 228 | std::tie(isOk, testBlock) = Block::fromBuffer(buffer, offset); |
| 229 | BOOST_CHECK(isOk); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 230 | BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| 231 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 232 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 233 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| 234 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| 235 | offset += testBlock.size(); |
| 236 | |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 237 | std::tie(isOk, testBlock) = Block::fromBuffer(buffer, offset); |
| 238 | BOOST_CHECK(isOk); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 239 | BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| 240 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 241 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 242 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| 243 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| 244 | offset += testBlock.size(); |
| 245 | |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 246 | std::tie(isOk, testBlock) = Block::fromBuffer(buffer, offset); |
| 247 | BOOST_CHECK(!isOk); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 248 | |
| 249 | // just buffer, copies memory |
| 250 | offset = 0; |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 251 | std::tie(isOk, testBlock) = Block::fromBuffer(TEST_BUFFER + offset, |
| 252 | sizeof(TEST_BUFFER) - offset); |
| 253 | BOOST_CHECK(isOk); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 254 | BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| 255 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 256 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 257 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| 258 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| 259 | offset += testBlock.size(); |
| 260 | |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 261 | std::tie(isOk, testBlock) = Block::fromBuffer(TEST_BUFFER + offset, |
| 262 | sizeof(TEST_BUFFER) - offset); |
| 263 | BOOST_CHECK(isOk); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 264 | BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| 265 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 266 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 267 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| 268 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| 269 | offset += testBlock.size(); |
| 270 | |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame] | 271 | std::tie(isOk, testBlock) = Block::fromBuffer(TEST_BUFFER + offset, |
| 272 | sizeof(TEST_BUFFER) - offset); |
| 273 | BOOST_CHECK(!isOk); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 276 | BOOST_AUTO_TEST_CASE(FromStream) |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 277 | { |
| 278 | const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok |
| 279 | 0x01, 0x01, 0xfb, // ok |
| 280 | 0x03, 0x02, 0xff}; // not ok |
| 281 | |
| 282 | typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream; |
| 283 | ArrayStream stream(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER)); |
| 284 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 285 | |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 286 | Block testBlock; |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 287 | BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream)); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 288 | BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| 289 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 290 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 291 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| 292 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| 293 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 294 | BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream)); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 295 | BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| 296 | BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| 297 | BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| 298 | BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| 299 | BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| 300 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 301 | BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error); |
Alexander Afanasyev | 21ef239 | 2014-03-25 12:40:22 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Junxiao Shi | f0da789 | 2015-04-04 22:16:16 -0700 | [diff] [blame] | 304 | BOOST_AUTO_TEST_CASE(FromStreamWhitespace) // Bug 2728 |
| 305 | { |
| 306 | uint8_t PACKET[] = { |
| 307 | 0x06, 0x20, // Data |
| 308 | 0x07, 0x11, // Name |
| 309 | 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // NameComponent 'hello' |
| 310 | 0x08, 0x01, 0x31, // NameComponent '1' |
| 311 | 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64, // NameComponent 'world' |
| 312 | 0x14, 0x00, // MetaInfo empty |
| 313 | 0x15, 0x00, // Content empty |
| 314 | 0x16, 0x05, // SignatureInfo |
| 315 | 0x1b, 0x01, 0x01, // SignatureType RSA |
| 316 | 0x1c, 0x00, // KeyLocator empty |
| 317 | 0x17, 0x00 // SignatureValue empty |
| 318 | }; |
| 319 | // TLV-LENGTH of <Data> is 0x20 which happens to be ASCII whitespace |
| 320 | |
| 321 | std::stringstream stream; |
| 322 | stream.write(reinterpret_cast<const char*>(PACKET), sizeof(PACKET)); |
| 323 | stream.seekg(0); |
| 324 | |
| 325 | Block block = Block::fromStream(stream); |
| 326 | BOOST_CHECK_NO_THROW(block.parse()); |
| 327 | } |
| 328 | |
| 329 | BOOST_AUTO_TEST_CASE(FromStreamZeroLength) // Bug 2729 |
| 330 | { |
| 331 | uint8_t BUFFER[] = { 0x07, 0x00 }; // TLV-LENGTH is zero |
| 332 | |
| 333 | std::stringstream stream; |
| 334 | stream.write(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER)); |
| 335 | stream.seekg(0); |
| 336 | |
| 337 | Block block; |
| 338 | BOOST_CHECK_NO_THROW(block = Block::fromStream(stream)); |
| 339 | BOOST_CHECK_EQUAL(block.type(), 0x07); |
| 340 | BOOST_CHECK_EQUAL(block.value_size(), 0); |
| 341 | } |
| 342 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 343 | BOOST_AUTO_TEST_CASE(Equality) |
| 344 | { |
| 345 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>)); |
| 346 | |
| 347 | Block a("\x08\x00", 2); |
| 348 | Block b("\x08\x00", 2);; |
| 349 | BOOST_CHECK_EQUAL(a == b, true); |
| 350 | BOOST_CHECK_EQUAL(a != b, false); |
| 351 | |
| 352 | Block c("\x06\x00", 2); |
| 353 | Block d("\x08\x00", 2);; |
| 354 | BOOST_CHECK_EQUAL(c == d, false); |
| 355 | BOOST_CHECK_EQUAL(c != d, true); |
| 356 | |
| 357 | Block e("\x06\x00", 2); |
| 358 | Block f("\x06\x01\xcc", 3);; |
| 359 | BOOST_CHECK_EQUAL(e == f, false); |
| 360 | BOOST_CHECK_EQUAL(e != f, true); |
| 361 | } |
| 362 | |
Joao Pereira | 7476ebf | 2015-07-07 14:54:39 -0400 | [diff] [blame] | 363 | BOOST_AUTO_TEST_CASE(InsertBeginning) |
| 364 | { |
| 365 | Block masterBlock(tlv::Name); |
| 366 | Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName"); |
| 367 | Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName"); |
| 368 | Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName"); |
| 369 | |
| 370 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| 371 | masterBlock.push_back(secondBlock); |
| 372 | masterBlock.push_back(thirdBlock); |
| 373 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2); |
| 374 | Block::element_const_iterator it = masterBlock.find(tlv::NameComponent); |
| 375 | BOOST_CHECK_EQUAL(*it == secondBlock, true); |
| 376 | |
| 377 | it = masterBlock.insert(it, firstBlock); |
| 378 | |
| 379 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3); |
| 380 | BOOST_CHECK_EQUAL(*(it + 1) == secondBlock, true); |
| 381 | BOOST_CHECK_EQUAL(*(masterBlock.elements_begin()) == firstBlock, true); |
| 382 | } |
| 383 | |
| 384 | BOOST_AUTO_TEST_CASE(InsertEnd) |
| 385 | { |
| 386 | Block masterBlock(tlv::Name); |
| 387 | Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName"); |
| 388 | Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName"); |
| 389 | Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName"); |
| 390 | |
| 391 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| 392 | masterBlock.push_back(firstBlock); |
| 393 | masterBlock.push_back(secondBlock); |
| 394 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2); |
| 395 | Block::element_const_iterator it = masterBlock.elements_end(); |
| 396 | BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true); |
| 397 | |
| 398 | it = masterBlock.insert(it, thirdBlock); |
| 399 | |
| 400 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3); |
| 401 | BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true); |
| 402 | BOOST_CHECK_EQUAL(*(masterBlock.elements_end() - 1) == thirdBlock, true); |
| 403 | } |
| 404 | |
| 405 | BOOST_AUTO_TEST_CASE(InsertMiddle) |
| 406 | { |
| 407 | Block masterBlock(tlv::Name); |
| 408 | Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName"); |
| 409 | Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName"); |
| 410 | Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName"); |
| 411 | |
| 412 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| 413 | masterBlock.push_back(firstBlock); |
| 414 | masterBlock.push_back(thirdBlock); |
| 415 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2); |
| 416 | Block::element_const_iterator it = masterBlock.find(tlv::NameComponent); |
| 417 | BOOST_CHECK_EQUAL(*it == firstBlock, true); |
| 418 | |
| 419 | it = masterBlock.insert(it+1, secondBlock); |
| 420 | |
| 421 | BOOST_CHECK_EQUAL(*it == secondBlock, true); |
| 422 | BOOST_CHECK_EQUAL(*(it + 1) == thirdBlock, true); |
| 423 | BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true); |
| 424 | } |
| 425 | |
| 426 | BOOST_AUTO_TEST_CASE(EraseSingleElement) |
| 427 | { |
| 428 | Block masterBlock(tlv::Name); |
| 429 | Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName"); |
| 430 | Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName"); |
| 431 | Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName"); |
| 432 | |
| 433 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| 434 | masterBlock.push_back(firstBlock); |
| 435 | masterBlock.push_back(secondBlock); |
| 436 | masterBlock.push_back(thirdBlock); |
| 437 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3); |
| 438 | Block::element_const_iterator it = masterBlock.find(tlv::NameComponent); |
| 439 | it++; |
| 440 | BOOST_CHECK_EQUAL(*it == secondBlock, true); |
| 441 | |
| 442 | it = masterBlock.erase(it); |
| 443 | |
| 444 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2); |
| 445 | BOOST_CHECK_EQUAL(*(it) == thirdBlock, true); |
| 446 | BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true); |
| 447 | } |
| 448 | |
| 449 | BOOST_AUTO_TEST_CASE(EraseRange) |
| 450 | { |
| 451 | Block masterBlock(tlv::Name); |
| 452 | Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName"); |
| 453 | Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName"); |
| 454 | Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName"); |
| 455 | Block fourthBlock = makeStringBlock(tlv::NameComponent, "fourthName"); |
| 456 | Block fifthBlock = makeStringBlock(tlv::NameComponent, "fifthName"); |
| 457 | Block sixthBlock = makeStringBlock(tlv::NameComponent, "sixthName"); |
| 458 | |
| 459 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| 460 | masterBlock.push_back(firstBlock); |
| 461 | masterBlock.push_back(secondBlock); |
| 462 | masterBlock.push_back(thirdBlock); |
| 463 | masterBlock.push_back(fourthBlock); |
| 464 | masterBlock.push_back(fifthBlock); |
| 465 | masterBlock.push_back(sixthBlock); |
| 466 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 6); |
| 467 | Block::element_const_iterator itStart = masterBlock.find(tlv::NameComponent); |
| 468 | itStart++; |
| 469 | Block::element_const_iterator itEnd = itStart + 3; |
| 470 | BOOST_CHECK_EQUAL(*itStart == secondBlock, true); |
| 471 | BOOST_CHECK_EQUAL(*itEnd == fifthBlock, true); |
| 472 | |
| 473 | Block::element_const_iterator newIt = masterBlock.erase(itStart, itEnd); |
| 474 | |
| 475 | BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3); |
| 476 | BOOST_CHECK_EQUAL(*(newIt) == fifthBlock, true); |
| 477 | BOOST_CHECK_EQUAL(*(newIt - 1) == firstBlock, true); |
| 478 | } |
| 479 | |
Eric Newberry | a3d9fc0 | 2015-07-26 10:55:44 -0700 | [diff] [blame] | 480 | BOOST_AUTO_TEST_CASE(Remove) |
| 481 | { |
| 482 | Block block(tlv::Data); |
| 483 | block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 0)); |
| 484 | block.push_back(makeNonNegativeIntegerBlock(tlv::FreshnessPeriod, 123)); |
| 485 | block.push_back(makeStringBlock(tlv::Name, "ndn:/test-prefix")); |
| 486 | block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 2)); |
| 487 | block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 1)); |
| 488 | |
| 489 | BOOST_CHECK_EQUAL(5, block.elements_size()); |
| 490 | BOOST_REQUIRE_NO_THROW(block.remove(tlv::ContentType)); |
| 491 | BOOST_CHECK_EQUAL(2, block.elements_size()); |
| 492 | |
| 493 | Block::element_container elements = block.elements(); |
| 494 | |
| 495 | BOOST_CHECK_EQUAL(tlv::FreshnessPeriod, elements[0].type()); |
| 496 | BOOST_CHECK_EQUAL(123, readNonNegativeInteger(elements[0])); |
| 497 | BOOST_CHECK_EQUAL(tlv::Name, elements[1].type()); |
| 498 | BOOST_CHECK(readString(elements[1]).compare("ndn:/test-prefix") == 0); |
| 499 | } |
| 500 | |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 501 | BOOST_AUTO_TEST_SUITE_END() |
| 502 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 503 | } // namespace tests |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 504 | } // namespace ndn |