Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 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 | |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 22 | #include "util/io.hpp" |
| 23 | #include "security/key-chain.hpp" |
| 24 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 25 | #include "boost-test.hpp" |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 26 | #include "identity-management-fixture.hpp" |
| 27 | #include <boost/filesystem.hpp> |
| 28 | #include <fstream> |
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 | |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 33 | class IoFixture |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 34 | { |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 35 | protected: |
| 36 | IoFixture() |
| 37 | : filepath(boost::filesystem::path(UNIT_TEST_CONFIG_PATH) /= "TestIo") |
| 38 | , filename(filepath.string()) |
| 39 | { |
| 40 | boost::filesystem::create_directories(filepath.parent_path()); |
| 41 | } |
| 42 | |
| 43 | ~IoFixture() |
| 44 | { |
| 45 | boost::system::error_code ec; |
| 46 | boost::filesystem::remove(filepath, ec); // ignore error |
| 47 | } |
| 48 | |
| 49 | /** \brief create a directory at filename, so that it's neither readable nor writable as a file |
| 50 | */ |
| 51 | void |
| 52 | mkdir() const |
| 53 | { |
| 54 | boost::filesystem::create_directory(filepath); |
| 55 | } |
| 56 | |
| 57 | template<typename Container, typename CharT = typename Container::value_type> |
| 58 | Container |
| 59 | readFile() const |
| 60 | { |
| 61 | Container container; |
| 62 | std::ifstream fs(filename, std::ios_base::binary); |
| 63 | char ch; |
| 64 | while (fs.get(ch)) { |
| 65 | container.push_back(static_cast<CharT>(ch)); |
| 66 | } |
| 67 | return container; |
| 68 | } |
| 69 | |
| 70 | template<typename Container, typename CharT = typename Container::value_type> |
| 71 | void |
| 72 | writeFile(const Container& content) const |
| 73 | { |
| 74 | std::ofstream fs(filename, std::ios_base::binary); |
| 75 | for (CharT ch : content) { |
| 76 | fs.put(static_cast<char>(ch)); |
| 77 | } |
| 78 | fs.close(); |
| 79 | BOOST_REQUIRE_MESSAGE(fs, "error writing file"); |
| 80 | } |
| 81 | |
| 82 | protected: |
| 83 | const boost::filesystem::path filepath; |
| 84 | const std::string filename; |
| 85 | }; |
| 86 | |
| 87 | BOOST_AUTO_TEST_SUITE(Util) |
| 88 | BOOST_FIXTURE_TEST_SUITE(TestIo, IoFixture) |
| 89 | |
| 90 | class EncodableType |
| 91 | { |
| 92 | public: |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 93 | Block |
| 94 | wireEncode() const |
| 95 | { |
| 96 | if (shouldThrow) { |
Junxiao Shi | 435bb55 | 2016-09-04 03:14:47 +0000 | [diff] [blame^] | 97 | BOOST_THROW_EXCEPTION(tlv::Error("encode error")); |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // block will be 0xAA, 0x01, 0xDD |
| 101 | return makeNonNegativeIntegerBlock(0xAA, 0xDD); |
| 102 | } |
| 103 | |
| 104 | public: |
| 105 | bool shouldThrow = false; |
| 106 | }; |
| 107 | |
Junxiao Shi | 435bb55 | 2016-09-04 03:14:47 +0000 | [diff] [blame^] | 108 | template<bool SHOULD_THROW = false> |
| 109 | class DecodableTypeTpl |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 110 | { |
| 111 | public: |
Junxiao Shi | 435bb55 | 2016-09-04 03:14:47 +0000 | [diff] [blame^] | 112 | DecodableTypeTpl() = default; |
| 113 | |
| 114 | explicit |
| 115 | DecodableTypeTpl(const Block& block) |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 116 | { |
Junxiao Shi | 435bb55 | 2016-09-04 03:14:47 +0000 | [diff] [blame^] | 117 | this->wireDecode(block); |
| 118 | } |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 119 | |
| 120 | void |
Junxiao Shi | 435bb55 | 2016-09-04 03:14:47 +0000 | [diff] [blame^] | 121 | wireDecode(const Block& block) |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 122 | { |
Junxiao Shi | 435bb55 | 2016-09-04 03:14:47 +0000 | [diff] [blame^] | 123 | if (m_shouldThrow) { |
| 124 | BOOST_THROW_EXCEPTION(tlv::Error("decode error")); |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // block must be 0xBB, 0x01, 0xEE |
| 128 | BOOST_CHECK_EQUAL(block.type(), 0xBB); |
| 129 | BOOST_REQUIRE_EQUAL(block.value_size(), 1); |
| 130 | BOOST_CHECK_EQUAL(block.value()[0], 0xEE); |
| 131 | } |
| 132 | |
Junxiao Shi | 435bb55 | 2016-09-04 03:14:47 +0000 | [diff] [blame^] | 133 | private: |
| 134 | bool m_shouldThrow = SHOULD_THROW; |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
Junxiao Shi | 435bb55 | 2016-09-04 03:14:47 +0000 | [diff] [blame^] | 137 | typedef DecodableTypeTpl<false> DecodableType; |
| 138 | typedef DecodableTypeTpl<true> DecodableTypeThrow; |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 139 | |
| 140 | BOOST_AUTO_TEST_CASE(LoadNoEncoding) |
| 141 | { |
| 142 | this->writeFile<std::vector<uint8_t>>({0xBB, 0x01, 0xEE}); |
| 143 | shared_ptr<DecodableType> decoded = io::load<DecodableType>(filename, io::NO_ENCODING); |
| 144 | BOOST_CHECK(decoded != nullptr); |
| 145 | } |
| 146 | |
| 147 | BOOST_AUTO_TEST_CASE(LoadBase64) |
| 148 | { |
| 149 | this->writeFile<std::string>("uwHu\n"); // printf '\xBB\x01\xEE' | base64 |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame] | 150 | shared_ptr<DecodableType> decoded = io::load<DecodableType>(filename, io::BASE64); |
| 151 | BOOST_CHECK(decoded != nullptr); |
| 152 | } |
| 153 | |
| 154 | BOOST_AUTO_TEST_CASE(LoadBase64Newline64) |
| 155 | { |
| 156 | this->writeFile<std::string>( |
| 157 | "CEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" |
| 158 | "AAAAAAAAAAAA\n"); |
| 159 | // printf '\x08\x40\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 |
| 160 | // \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 |
| 161 | // \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 |
| 162 | // \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' | base64 |
| 163 | shared_ptr<name::Component> decoded = io::load<name::Component>(filename, io::BASE64); |
| 164 | BOOST_CHECK(decoded != nullptr); |
| 165 | } |
| 166 | |
| 167 | BOOST_AUTO_TEST_CASE(LoadBase64Newline32) |
| 168 | { |
| 169 | this->writeFile<std::string>( |
| 170 | "CEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" |
| 171 | "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" |
| 172 | "AAAAAAAAAAAA\n"); |
| 173 | shared_ptr<name::Component> decoded = io::load<name::Component>(filename, io::BASE64); |
| 174 | BOOST_CHECK(decoded != nullptr); |
| 175 | } |
| 176 | |
| 177 | BOOST_AUTO_TEST_CASE(LoadBase64NewlineEnd) |
| 178 | { |
| 179 | this->writeFile<std::string>( |
| 180 | "CEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"); |
| 181 | shared_ptr<name::Component> decoded = io::load<name::Component>(filename, io::BASE64); |
| 182 | BOOST_CHECK(decoded != nullptr); |
| 183 | } |
| 184 | |
| 185 | BOOST_AUTO_TEST_CASE(LoadBase64NoNewline) |
| 186 | { |
| 187 | this->writeFile<std::string>( |
| 188 | "CEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); |
| 189 | shared_ptr<name::Component> decoded = io::load<name::Component>(filename, io::BASE64); |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 190 | BOOST_CHECK(decoded != nullptr); |
| 191 | } |
| 192 | |
| 193 | BOOST_AUTO_TEST_CASE(LoadHex) |
| 194 | { |
| 195 | this->writeFile<std::string>("BB01EE"); |
| 196 | shared_ptr<DecodableType> decoded = io::load<DecodableType>(filename, io::HEX); |
| 197 | BOOST_CHECK(decoded != nullptr); |
| 198 | } |
| 199 | |
| 200 | BOOST_AUTO_TEST_CASE(LoadException) |
| 201 | { |
| 202 | this->writeFile<std::vector<uint8_t>>({0xBB, 0x01, 0xEE}); |
| 203 | shared_ptr<DecodableTypeThrow> decoded; |
| 204 | BOOST_CHECK_NO_THROW(decoded = io::load<DecodableTypeThrow>(filename, io::NO_ENCODING)); |
| 205 | BOOST_CHECK(decoded == nullptr); |
| 206 | } |
| 207 | |
| 208 | BOOST_AUTO_TEST_CASE(LoadNotHex) |
| 209 | { |
| 210 | this->writeFile<std::string>("not-hex"); |
| 211 | shared_ptr<DecodableType> decoded; |
| 212 | BOOST_CHECK_NO_THROW(decoded = io::load<DecodableType>(filename, io::HEX)); |
| 213 | BOOST_CHECK(decoded == nullptr); |
| 214 | } |
| 215 | |
| 216 | BOOST_AUTO_TEST_CASE(LoadFileNotReadable) |
| 217 | { |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 218 | shared_ptr<DecodableType> decoded; |
| 219 | BOOST_CHECK_NO_THROW(decoded = io::load<DecodableType>(filename, io::NO_ENCODING)); |
| 220 | BOOST_CHECK(decoded == nullptr); |
| 221 | } |
| 222 | |
| 223 | BOOST_AUTO_TEST_CASE(SaveNoEncoding) |
| 224 | { |
| 225 | EncodableType encoded; |
| 226 | BOOST_CHECK_NO_THROW(io::save(encoded, filename, io::NO_ENCODING)); |
| 227 | auto content = this->readFile<std::vector<uint8_t>>(); |
| 228 | uint8_t expected[] = {0xAA, 0x01, 0xDD}; |
| 229 | BOOST_CHECK_EQUAL_COLLECTIONS(content.begin(), content.end(), |
| 230 | expected, expected + sizeof(expected)); |
| 231 | } |
| 232 | |
| 233 | BOOST_AUTO_TEST_CASE(SaveBase64) |
| 234 | { |
| 235 | EncodableType encoded; |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame] | 236 | BOOST_CHECK_NO_THROW(io::save(encoded, filename, io::BASE64)); |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 237 | auto content = this->readFile<std::string>(); |
| 238 | BOOST_CHECK_EQUAL(content, "qgHd\n"); // printf '\xAA\x01\xDD' | base64 |
| 239 | } |
| 240 | |
| 241 | BOOST_AUTO_TEST_CASE(SaveHex) |
| 242 | { |
| 243 | EncodableType encoded; |
| 244 | BOOST_CHECK_NO_THROW(io::save(encoded, filename, io::HEX)); |
| 245 | auto content = this->readFile<std::string>(); |
| 246 | BOOST_CHECK_EQUAL(content, "AA01DD"); |
| 247 | } |
| 248 | |
| 249 | BOOST_AUTO_TEST_CASE(SaveException) |
| 250 | { |
| 251 | EncodableType encoded; |
| 252 | encoded.shouldThrow = true; |
| 253 | BOOST_CHECK_THROW(io::save(encoded, filename, io::NO_ENCODING), io::Error); |
| 254 | } |
| 255 | |
| 256 | BOOST_AUTO_TEST_CASE(SaveFileNotWritable) |
| 257 | { |
| 258 | this->mkdir(); |
| 259 | EncodableType encoded; |
| 260 | encoded.shouldThrow = true; |
| 261 | BOOST_CHECK_THROW(io::save(encoded, filename, io::NO_ENCODING), io::Error); |
| 262 | } |
| 263 | |
| 264 | class IdCertFixture : public IoFixture |
| 265 | , public IdentityManagementFixture |
| 266 | { |
| 267 | }; |
| 268 | |
| 269 | BOOST_FIXTURE_TEST_CASE(IdCert, IdCertFixture) |
| 270 | { |
| 271 | Name identity("/TestIo/IdCert"); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 272 | identity.appendVersion(); |
Yingdi Yu | 3ed09d0 | 2014-10-13 16:24:08 -0700 | [diff] [blame] | 273 | BOOST_REQUIRE(addIdentity(identity, RsaKeyParams())); |
| 274 | Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 275 | shared_ptr<security::v1::IdentityCertificate> idCert; |
Yingdi Yu | 3ed09d0 | 2014-10-13 16:24:08 -0700 | [diff] [blame] | 276 | BOOST_REQUIRE_NO_THROW(idCert = m_keyChain.getCertificate(certName)); |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 277 | |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 278 | io::save(*idCert, filename); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 279 | shared_ptr<security::v1::IdentityCertificate> readCert = io::load<security::v1::IdentityCertificate>(filename); |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 280 | |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame] | 281 | BOOST_REQUIRE(readCert != nullptr); |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 282 | BOOST_CHECK_EQUAL(idCert->getName(), readCert->getName()); |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 285 | BOOST_AUTO_TEST_SUITE_END() // TestIo |
| 286 | BOOST_AUTO_TEST_SUITE_END() // Util |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 287 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 288 | } // namespace tests |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 289 | } // namespace ndn |