Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a84f464 | 2017-08-23 16:14:51 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | d09d52b | 2023-02-15 14:15:06 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2023 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. |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/util/io.hpp" |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 25 | #include "tests/key-chain-fixture.hpp" |
Davide Pesavento | a84f464 | 2017-08-23 16:14:51 -0400 | [diff] [blame] | 26 | |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 27 | #include <boost/filesystem.hpp> |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 28 | #include <boost/mpl/vector.hpp> |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 29 | |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 30 | namespace ndn { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 31 | namespace tests { |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 32 | |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 33 | BOOST_AUTO_TEST_SUITE(Util) |
| 34 | BOOST_AUTO_TEST_SUITE(TestIo) |
| 35 | |
| 36 | struct NoEncoding |
| 37 | { |
| 38 | const io::IoEncoding encoding{io::NO_ENCODING}; |
| 39 | const std::vector<uint8_t> blob{0xd1, 0x0, 0xb0, 0x1a}; |
| 40 | std::istringstream stream{std::string("\xd1\x00\xb0\x1a", 4), std::ios_base::binary}; |
| 41 | }; |
| 42 | |
| 43 | struct Base64Encoding |
| 44 | { |
| 45 | const io::IoEncoding encoding = io::BASE64; |
| 46 | const std::vector<uint8_t> blob{0x42, 0x61, 0x73, 0x65, 0x36, 0x34, 0x45, 0x6e, 0x63}; |
| 47 | std::istringstream stream{"QmFzZTY0RW5j\n", std::ios_base::binary}; |
| 48 | }; |
| 49 | |
| 50 | struct HexEncoding |
| 51 | { |
| 52 | const io::IoEncoding encoding = io::HEX; |
| 53 | const std::vector<uint8_t> blob{0x48, 0x65, 0x78, 0x45, 0x6e, 0x63}; |
| 54 | std::istringstream stream{"486578456E63", std::ios_base::binary}; |
| 55 | }; |
| 56 | |
| 57 | using Encodings = boost::mpl::vector<NoEncoding, Base64Encoding, HexEncoding>; |
| 58 | |
| 59 | BOOST_AUTO_TEST_CASE_TEMPLATE(LoadBuffer, T, Encodings) |
| 60 | { |
| 61 | T t; |
| 62 | shared_ptr<Buffer> buf = io::loadBuffer(t.stream, t.encoding); |
| 63 | BOOST_CHECK_EQUAL_COLLECTIONS(buf->begin(), buf->end(), t.blob.begin(), t.blob.end()); |
| 64 | } |
| 65 | |
| 66 | BOOST_AUTO_TEST_CASE_TEMPLATE(SaveBuffer, T, Encodings) |
| 67 | { |
| 68 | T t; |
| 69 | std::ostringstream os(std::ios_base::binary); |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 70 | io::saveBuffer(t.blob, os, t.encoding); |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 71 | BOOST_CHECK_EQUAL(os.str(), t.stream.str()); |
| 72 | } |
| 73 | |
| 74 | BOOST_AUTO_TEST_CASE(LoadBufferException) |
| 75 | { |
| 76 | std::ifstream in("this-file-does-not-exist", std::ios_base::binary); |
| 77 | BOOST_CHECK_THROW(io::loadBuffer(in, io::NO_ENCODING), io::Error); |
| 78 | } |
| 79 | |
| 80 | BOOST_AUTO_TEST_CASE(SaveBufferException) |
| 81 | { |
| 82 | class NullStreambuf : public std::streambuf |
| 83 | { |
| 84 | }; |
| 85 | |
| 86 | NullStreambuf nullbuf; |
| 87 | std::ostream out(&nullbuf); |
| 88 | const Buffer buffer(1); |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 89 | BOOST_CHECK_THROW(io::saveBuffer(buffer, out, io::NO_ENCODING), io::Error); |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | BOOST_AUTO_TEST_CASE(UnknownIoEncoding) |
| 93 | { |
| 94 | std::stringstream ss; |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame] | 95 | BOOST_CHECK_THROW(io::loadTlv<Name>(ss, static_cast<io::IoEncoding>(5)), std::invalid_argument); |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 96 | BOOST_CHECK_THROW(io::loadBuffer(ss, static_cast<io::IoEncoding>(5)), std::invalid_argument); |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 97 | BOOST_CHECK_THROW(io::saveBuffer({}, ss, static_cast<io::IoEncoding>(5)), std::invalid_argument); |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | class FileIoFixture |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 101 | { |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 102 | protected: |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 103 | FileIoFixture() |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 104 | : filepath(boost::filesystem::path(UNIT_TESTS_TMPDIR) / "TestIo") |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 105 | , filename(filepath.string()) |
| 106 | { |
| 107 | boost::filesystem::create_directories(filepath.parent_path()); |
| 108 | } |
| 109 | |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 110 | ~FileIoFixture() |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 111 | { |
| 112 | boost::system::error_code ec; |
| 113 | boost::filesystem::remove(filepath, ec); // ignore error |
| 114 | } |
| 115 | |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame] | 116 | /** |
Davide Pesavento | c860bd1 | 2022-10-04 03:23:43 -0400 | [diff] [blame] | 117 | * \brief Create a directory at `filepath`, so that it's neither readable nor writable as a file. |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 118 | */ |
| 119 | void |
| 120 | mkdir() const |
| 121 | { |
| 122 | boost::filesystem::create_directory(filepath); |
| 123 | } |
| 124 | |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 125 | template<typename Container> |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 126 | Container |
| 127 | readFile() const |
| 128 | { |
| 129 | Container container; |
| 130 | std::ifstream fs(filename, std::ios_base::binary); |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 131 | BOOST_REQUIRE_MESSAGE(fs, "error opening file"); |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 132 | char ch; |
| 133 | while (fs.get(ch)) { |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 134 | container.push_back(static_cast<typename Container::value_type>(ch)); |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 135 | } |
| 136 | return container; |
| 137 | } |
| 138 | |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 139 | template<typename Container> |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 140 | void |
| 141 | writeFile(const Container& content) const |
| 142 | { |
| 143 | std::ofstream fs(filename, std::ios_base::binary); |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 144 | BOOST_REQUIRE_MESSAGE(fs, "error opening file"); |
| 145 | for (auto ch : content) { |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 146 | fs.put(static_cast<char>(ch)); |
| 147 | } |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 148 | BOOST_REQUIRE_MESSAGE(fs, "error writing file"); |
| 149 | } |
| 150 | |
| 151 | protected: |
| 152 | const boost::filesystem::path filepath; |
| 153 | const std::string filename; |
| 154 | }; |
| 155 | |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 156 | BOOST_FIXTURE_TEST_SUITE(FileIo, FileIoFixture) |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 157 | |
| 158 | class EncodableType |
| 159 | { |
| 160 | public: |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 161 | Block |
| 162 | wireEncode() const |
| 163 | { |
| 164 | if (shouldThrow) { |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 165 | NDN_THROW(tlv::Error("encode error")); |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // block will be 0xAA, 0x01, 0xDD |
| 169 | return makeNonNegativeIntegerBlock(0xAA, 0xDD); |
| 170 | } |
| 171 | |
| 172 | public: |
| 173 | bool shouldThrow = false; |
| 174 | }; |
| 175 | |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame] | 176 | class DecodableType |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 177 | { |
| 178 | public: |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame] | 179 | DecodableType() = default; |
Junxiao Shi | 435bb55 | 2016-09-04 03:14:47 +0000 | [diff] [blame] | 180 | |
| 181 | explicit |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame] | 182 | DecodableType(const Block& block) |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 183 | { |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame] | 184 | wireDecode(block); |
Junxiao Shi | 435bb55 | 2016-09-04 03:14:47 +0000 | [diff] [blame] | 185 | } |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 186 | |
| 187 | void |
Junxiao Shi | 435bb55 | 2016-09-04 03:14:47 +0000 | [diff] [blame] | 188 | wireDecode(const Block& block) |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 189 | { |
Davide Pesavento | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame] | 190 | BOOST_TEST(block == "BB01EE"_block); |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 191 | } |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 192 | }; |
| 193 | |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame] | 194 | class DecodableTypeThrow |
| 195 | { |
| 196 | public: |
| 197 | DecodableTypeThrow() = default; |
| 198 | |
| 199 | explicit |
| 200 | DecodableTypeThrow(const Block& block) |
| 201 | { |
| 202 | wireDecode(block); |
| 203 | } |
| 204 | |
| 205 | void |
| 206 | wireDecode(const Block&) |
| 207 | { |
| 208 | NDN_THROW(tlv::Error("decode error")); |
| 209 | } |
| 210 | }; |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 211 | |
| 212 | BOOST_AUTO_TEST_CASE(LoadNoEncoding) |
| 213 | { |
| 214 | this->writeFile<std::vector<uint8_t>>({0xBB, 0x01, 0xEE}); |
| 215 | shared_ptr<DecodableType> decoded = io::load<DecodableType>(filename, io::NO_ENCODING); |
| 216 | BOOST_CHECK(decoded != nullptr); |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame] | 217 | |
| 218 | std::ifstream ifs(filename); |
| 219 | BOOST_CHECK_NO_THROW(io::loadTlv<DecodableType>(ifs, io::NO_ENCODING)); |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | BOOST_AUTO_TEST_CASE(LoadBase64) |
| 223 | { |
| 224 | this->writeFile<std::string>("uwHu\n"); // printf '\xBB\x01\xEE' | base64 |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame] | 225 | shared_ptr<DecodableType> decoded = io::load<DecodableType>(filename, io::BASE64); |
| 226 | BOOST_CHECK(decoded != nullptr); |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame] | 227 | |
| 228 | std::ifstream ifs(filename); |
| 229 | BOOST_CHECK_NO_THROW(io::loadTlv<DecodableType>(ifs, io::BASE64)); |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | BOOST_AUTO_TEST_CASE(LoadBase64Newline64) |
| 233 | { |
| 234 | this->writeFile<std::string>( |
| 235 | "CEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" |
| 236 | "AAAAAAAAAAAA\n"); |
| 237 | // printf '\x08\x40\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 |
| 238 | // \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 |
| 239 | // \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 |
| 240 | // \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' | base64 |
| 241 | shared_ptr<name::Component> decoded = io::load<name::Component>(filename, io::BASE64); |
| 242 | BOOST_CHECK(decoded != nullptr); |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame] | 243 | |
| 244 | std::ifstream ifs(filename); |
| 245 | BOOST_CHECK_NO_THROW(io::loadTlv<name::Component>(ifs, io::BASE64)); |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | BOOST_AUTO_TEST_CASE(LoadBase64Newline32) |
| 249 | { |
| 250 | this->writeFile<std::string>( |
| 251 | "CEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" |
| 252 | "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" |
| 253 | "AAAAAAAAAAAA\n"); |
| 254 | shared_ptr<name::Component> decoded = io::load<name::Component>(filename, io::BASE64); |
| 255 | BOOST_CHECK(decoded != nullptr); |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame] | 256 | |
| 257 | std::ifstream ifs(filename); |
| 258 | BOOST_CHECK_NO_THROW(io::loadTlv<name::Component>(ifs, io::BASE64)); |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | BOOST_AUTO_TEST_CASE(LoadBase64NewlineEnd) |
| 262 | { |
| 263 | this->writeFile<std::string>( |
| 264 | "CEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"); |
| 265 | shared_ptr<name::Component> decoded = io::load<name::Component>(filename, io::BASE64); |
| 266 | BOOST_CHECK(decoded != nullptr); |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame] | 267 | |
| 268 | std::ifstream ifs(filename); |
| 269 | BOOST_CHECK_NO_THROW(io::loadTlv<name::Component>(ifs, io::BASE64)); |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | BOOST_AUTO_TEST_CASE(LoadBase64NoNewline) |
| 273 | { |
| 274 | this->writeFile<std::string>( |
| 275 | "CEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); |
| 276 | shared_ptr<name::Component> decoded = io::load<name::Component>(filename, io::BASE64); |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 277 | BOOST_CHECK(decoded != nullptr); |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame] | 278 | |
| 279 | std::ifstream ifs(filename); |
| 280 | BOOST_CHECK_NO_THROW(io::loadTlv<name::Component>(ifs, io::BASE64)); |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | BOOST_AUTO_TEST_CASE(LoadHex) |
| 284 | { |
| 285 | this->writeFile<std::string>("BB01EE"); |
| 286 | shared_ptr<DecodableType> decoded = io::load<DecodableType>(filename, io::HEX); |
| 287 | BOOST_CHECK(decoded != nullptr); |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame] | 288 | |
| 289 | std::ifstream ifs(filename); |
| 290 | BOOST_CHECK_NO_THROW(io::loadTlv<DecodableType>(ifs, io::HEX)); |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame] | 293 | BOOST_AUTO_TEST_CASE(LoadDecodeException) |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 294 | { |
| 295 | this->writeFile<std::vector<uint8_t>>({0xBB, 0x01, 0xEE}); |
| 296 | shared_ptr<DecodableTypeThrow> decoded; |
| 297 | BOOST_CHECK_NO_THROW(decoded = io::load<DecodableTypeThrow>(filename, io::NO_ENCODING)); |
| 298 | BOOST_CHECK(decoded == nullptr); |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame] | 299 | |
| 300 | std::ifstream ifs(filename); |
| 301 | BOOST_CHECK_THROW(io::loadTlv<DecodableTypeThrow>(ifs, io::NO_ENCODING), io::Error); |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | BOOST_AUTO_TEST_CASE(LoadNotHex) |
| 305 | { |
| 306 | this->writeFile<std::string>("not-hex"); |
| 307 | shared_ptr<DecodableType> decoded; |
| 308 | BOOST_CHECK_NO_THROW(decoded = io::load<DecodableType>(filename, io::HEX)); |
| 309 | BOOST_CHECK(decoded == nullptr); |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame] | 310 | |
| 311 | std::ifstream ifs(filename); |
| 312 | BOOST_CHECK_THROW(io::loadTlv<DecodableType>(ifs, io::HEX), io::Error); |
| 313 | } |
| 314 | |
| 315 | BOOST_AUTO_TEST_CASE(LoadEmpty) |
| 316 | { |
| 317 | this->writeFile<std::vector<uint8_t>>({}); |
| 318 | shared_ptr<DecodableType> decoded; |
| 319 | BOOST_CHECK_NO_THROW(decoded = io::load<DecodableType>(filename, io::NO_ENCODING)); |
| 320 | BOOST_CHECK(decoded == nullptr); |
| 321 | |
| 322 | std::ifstream ifs(filename); |
| 323 | BOOST_CHECK_THROW(io::loadTlv<DecodableType>(ifs, io::NO_ENCODING), io::Error); |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | BOOST_AUTO_TEST_CASE(LoadFileNotReadable) |
| 327 | { |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 328 | shared_ptr<DecodableType> decoded; |
| 329 | BOOST_CHECK_NO_THROW(decoded = io::load<DecodableType>(filename, io::NO_ENCODING)); |
| 330 | BOOST_CHECK(decoded == nullptr); |
| 331 | } |
| 332 | |
| 333 | BOOST_AUTO_TEST_CASE(SaveNoEncoding) |
| 334 | { |
| 335 | EncodableType encoded; |
| 336 | BOOST_CHECK_NO_THROW(io::save(encoded, filename, io::NO_ENCODING)); |
| 337 | auto content = this->readFile<std::vector<uint8_t>>(); |
| 338 | uint8_t expected[] = {0xAA, 0x01, 0xDD}; |
| 339 | BOOST_CHECK_EQUAL_COLLECTIONS(content.begin(), content.end(), |
| 340 | expected, expected + sizeof(expected)); |
| 341 | } |
| 342 | |
| 343 | BOOST_AUTO_TEST_CASE(SaveBase64) |
| 344 | { |
| 345 | EncodableType encoded; |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame] | 346 | BOOST_CHECK_NO_THROW(io::save(encoded, filename, io::BASE64)); |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 347 | auto content = this->readFile<std::string>(); |
| 348 | BOOST_CHECK_EQUAL(content, "qgHd\n"); // printf '\xAA\x01\xDD' | base64 |
| 349 | } |
| 350 | |
| 351 | BOOST_AUTO_TEST_CASE(SaveHex) |
| 352 | { |
| 353 | EncodableType encoded; |
| 354 | BOOST_CHECK_NO_THROW(io::save(encoded, filename, io::HEX)); |
| 355 | auto content = this->readFile<std::string>(); |
| 356 | BOOST_CHECK_EQUAL(content, "AA01DD"); |
| 357 | } |
| 358 | |
| 359 | BOOST_AUTO_TEST_CASE(SaveException) |
| 360 | { |
| 361 | EncodableType encoded; |
| 362 | encoded.shouldThrow = true; |
| 363 | BOOST_CHECK_THROW(io::save(encoded, filename, io::NO_ENCODING), io::Error); |
| 364 | } |
| 365 | |
| 366 | BOOST_AUTO_TEST_CASE(SaveFileNotWritable) |
| 367 | { |
| 368 | this->mkdir(); |
| 369 | EncodableType encoded; |
| 370 | encoded.shouldThrow = true; |
| 371 | BOOST_CHECK_THROW(io::save(encoded, filename, io::NO_ENCODING), io::Error); |
| 372 | } |
| 373 | |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 374 | BOOST_AUTO_TEST_SUITE_END() // FileIo |
| 375 | |
| 376 | class IdCertFixture : public FileIoFixture |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 377 | , public KeyChainFixture |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 378 | { |
| 379 | }; |
| 380 | |
| 381 | BOOST_FIXTURE_TEST_CASE(IdCert, IdCertFixture) |
| 382 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 383 | auto identity = m_keyChain.createIdentity("/TestIo/IdCert", RsaKeyParams()); |
Davide Pesavento | d09d52b | 2023-02-15 14:15:06 -0500 | [diff] [blame] | 384 | auto key = identity.getDefaultKey(); |
| 385 | const auto& cert = key.getDefaultCertificate(); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 386 | io::save(cert, filename); |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 387 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 388 | auto readCert = io::load<security::Certificate>(filename); |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 389 | |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame] | 390 | BOOST_REQUIRE(readCert != nullptr); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 391 | BOOST_CHECK_EQUAL(cert.getName(), readCert->getName()); |
Ashlesh Gawande | e84d1eb | 2018-01-04 20:46:44 -0600 | [diff] [blame] | 392 | |
| 393 | this->writeFile<std::string>(""); |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 394 | readCert = io::load<security::Certificate>(filename); |
Ashlesh Gawande | e84d1eb | 2018-01-04 20:46:44 -0600 | [diff] [blame] | 395 | BOOST_REQUIRE(readCert == nullptr); |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 396 | } |
| 397 | |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 398 | BOOST_AUTO_TEST_SUITE_END() // TestIo |
| 399 | BOOST_AUTO_TEST_SUITE_END() // Util |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 400 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 401 | } // namespace tests |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 402 | } // namespace ndn |