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