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