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: |
| 93 | class Error : public tlv::Error |
| 94 | { |
| 95 | public: |
| 96 | Error() |
| 97 | : tlv::Error("encode error") |
| 98 | { |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | Block |
| 103 | wireEncode() const |
| 104 | { |
| 105 | if (shouldThrow) { |
| 106 | BOOST_THROW_EXCEPTION(Error()); |
| 107 | } |
| 108 | |
| 109 | // block will be 0xAA, 0x01, 0xDD |
| 110 | return makeNonNegativeIntegerBlock(0xAA, 0xDD); |
| 111 | } |
| 112 | |
| 113 | public: |
| 114 | bool shouldThrow = false; |
| 115 | }; |
| 116 | |
| 117 | class DecodableType |
| 118 | { |
| 119 | public: |
| 120 | class Error : public tlv::Error |
| 121 | { |
| 122 | public: |
| 123 | Error() |
| 124 | : tlv::Error("decode error") |
| 125 | { |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | void |
| 130 | wireDecode(const Block& block) const |
| 131 | { |
| 132 | if (shouldThrow) { |
| 133 | BOOST_THROW_EXCEPTION(Error()); |
| 134 | } |
| 135 | |
| 136 | // block must be 0xBB, 0x01, 0xEE |
| 137 | BOOST_CHECK_EQUAL(block.type(), 0xBB); |
| 138 | BOOST_REQUIRE_EQUAL(block.value_size(), 1); |
| 139 | BOOST_CHECK_EQUAL(block.value()[0], 0xEE); |
| 140 | } |
| 141 | |
| 142 | public: |
| 143 | bool shouldThrow = false; |
| 144 | }; |
| 145 | |
| 146 | class DecodableTypeThrow : public DecodableType |
| 147 | { |
| 148 | public: |
| 149 | DecodableTypeThrow() |
| 150 | { |
| 151 | this->shouldThrow = true; |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | BOOST_AUTO_TEST_CASE(LoadNoEncoding) |
| 156 | { |
| 157 | this->writeFile<std::vector<uint8_t>>({0xBB, 0x01, 0xEE}); |
| 158 | shared_ptr<DecodableType> decoded = io::load<DecodableType>(filename, io::NO_ENCODING); |
| 159 | BOOST_CHECK(decoded != nullptr); |
| 160 | } |
| 161 | |
| 162 | BOOST_AUTO_TEST_CASE(LoadBase64) |
| 163 | { |
| 164 | this->writeFile<std::string>("uwHu\n"); // printf '\xBB\x01\xEE' | base64 |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame^] | 165 | shared_ptr<DecodableType> decoded = io::load<DecodableType>(filename, io::BASE64); |
| 166 | BOOST_CHECK(decoded != nullptr); |
| 167 | } |
| 168 | |
| 169 | BOOST_AUTO_TEST_CASE(LoadBase64Newline64) |
| 170 | { |
| 171 | this->writeFile<std::string>( |
| 172 | "CEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" |
| 173 | "AAAAAAAAAAAA\n"); |
| 174 | // printf '\x08\x40\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 |
| 175 | // \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 |
| 176 | // \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 |
| 177 | // \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' | base64 |
| 178 | shared_ptr<name::Component> decoded = io::load<name::Component>(filename, io::BASE64); |
| 179 | BOOST_CHECK(decoded != nullptr); |
| 180 | } |
| 181 | |
| 182 | BOOST_AUTO_TEST_CASE(LoadBase64Newline32) |
| 183 | { |
| 184 | this->writeFile<std::string>( |
| 185 | "CEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" |
| 186 | "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" |
| 187 | "AAAAAAAAAAAA\n"); |
| 188 | shared_ptr<name::Component> decoded = io::load<name::Component>(filename, io::BASE64); |
| 189 | BOOST_CHECK(decoded != nullptr); |
| 190 | } |
| 191 | |
| 192 | BOOST_AUTO_TEST_CASE(LoadBase64NewlineEnd) |
| 193 | { |
| 194 | this->writeFile<std::string>( |
| 195 | "CEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"); |
| 196 | shared_ptr<name::Component> decoded = io::load<name::Component>(filename, io::BASE64); |
| 197 | BOOST_CHECK(decoded != nullptr); |
| 198 | } |
| 199 | |
| 200 | BOOST_AUTO_TEST_CASE(LoadBase64NoNewline) |
| 201 | { |
| 202 | this->writeFile<std::string>( |
| 203 | "CEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); |
| 204 | shared_ptr<name::Component> decoded = io::load<name::Component>(filename, io::BASE64); |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 205 | BOOST_CHECK(decoded != nullptr); |
| 206 | } |
| 207 | |
| 208 | BOOST_AUTO_TEST_CASE(LoadHex) |
| 209 | { |
| 210 | this->writeFile<std::string>("BB01EE"); |
| 211 | shared_ptr<DecodableType> decoded = io::load<DecodableType>(filename, io::HEX); |
| 212 | BOOST_CHECK(decoded != nullptr); |
| 213 | } |
| 214 | |
| 215 | BOOST_AUTO_TEST_CASE(LoadException) |
| 216 | { |
| 217 | this->writeFile<std::vector<uint8_t>>({0xBB, 0x01, 0xEE}); |
| 218 | shared_ptr<DecodableTypeThrow> decoded; |
| 219 | BOOST_CHECK_NO_THROW(decoded = io::load<DecodableTypeThrow>(filename, io::NO_ENCODING)); |
| 220 | BOOST_CHECK(decoded == nullptr); |
| 221 | } |
| 222 | |
| 223 | BOOST_AUTO_TEST_CASE(LoadNotHex) |
| 224 | { |
| 225 | this->writeFile<std::string>("not-hex"); |
| 226 | shared_ptr<DecodableType> decoded; |
| 227 | BOOST_CHECK_NO_THROW(decoded = io::load<DecodableType>(filename, io::HEX)); |
| 228 | BOOST_CHECK(decoded == nullptr); |
| 229 | } |
| 230 | |
| 231 | BOOST_AUTO_TEST_CASE(LoadFileNotReadable) |
| 232 | { |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 233 | shared_ptr<DecodableType> decoded; |
| 234 | BOOST_CHECK_NO_THROW(decoded = io::load<DecodableType>(filename, io::NO_ENCODING)); |
| 235 | BOOST_CHECK(decoded == nullptr); |
| 236 | } |
| 237 | |
| 238 | BOOST_AUTO_TEST_CASE(SaveNoEncoding) |
| 239 | { |
| 240 | EncodableType encoded; |
| 241 | BOOST_CHECK_NO_THROW(io::save(encoded, filename, io::NO_ENCODING)); |
| 242 | auto content = this->readFile<std::vector<uint8_t>>(); |
| 243 | uint8_t expected[] = {0xAA, 0x01, 0xDD}; |
| 244 | BOOST_CHECK_EQUAL_COLLECTIONS(content.begin(), content.end(), |
| 245 | expected, expected + sizeof(expected)); |
| 246 | } |
| 247 | |
| 248 | BOOST_AUTO_TEST_CASE(SaveBase64) |
| 249 | { |
| 250 | EncodableType encoded; |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame^] | 251 | BOOST_CHECK_NO_THROW(io::save(encoded, filename, io::BASE64)); |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 252 | auto content = this->readFile<std::string>(); |
| 253 | BOOST_CHECK_EQUAL(content, "qgHd\n"); // printf '\xAA\x01\xDD' | base64 |
| 254 | } |
| 255 | |
| 256 | BOOST_AUTO_TEST_CASE(SaveHex) |
| 257 | { |
| 258 | EncodableType encoded; |
| 259 | BOOST_CHECK_NO_THROW(io::save(encoded, filename, io::HEX)); |
| 260 | auto content = this->readFile<std::string>(); |
| 261 | BOOST_CHECK_EQUAL(content, "AA01DD"); |
| 262 | } |
| 263 | |
| 264 | BOOST_AUTO_TEST_CASE(SaveException) |
| 265 | { |
| 266 | EncodableType encoded; |
| 267 | encoded.shouldThrow = true; |
| 268 | BOOST_CHECK_THROW(io::save(encoded, filename, io::NO_ENCODING), io::Error); |
| 269 | } |
| 270 | |
| 271 | BOOST_AUTO_TEST_CASE(SaveFileNotWritable) |
| 272 | { |
| 273 | this->mkdir(); |
| 274 | EncodableType encoded; |
| 275 | encoded.shouldThrow = true; |
| 276 | BOOST_CHECK_THROW(io::save(encoded, filename, io::NO_ENCODING), io::Error); |
| 277 | } |
| 278 | |
| 279 | class IdCertFixture : public IoFixture |
| 280 | , public IdentityManagementFixture |
| 281 | { |
| 282 | }; |
| 283 | |
| 284 | BOOST_FIXTURE_TEST_CASE(IdCert, IdCertFixture) |
| 285 | { |
| 286 | Name identity("/TestIo/IdCert"); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 287 | identity.appendVersion(); |
Yingdi Yu | 3ed09d0 | 2014-10-13 16:24:08 -0700 | [diff] [blame] | 288 | BOOST_REQUIRE(addIdentity(identity, RsaKeyParams())); |
| 289 | Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 290 | shared_ptr<security::v1::IdentityCertificate> idCert; |
Yingdi Yu | 3ed09d0 | 2014-10-13 16:24:08 -0700 | [diff] [blame] | 291 | BOOST_REQUIRE_NO_THROW(idCert = m_keyChain.getCertificate(certName)); |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 292 | |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 293 | io::save(*idCert, filename); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 294 | shared_ptr<security::v1::IdentityCertificate> readCert = io::load<security::v1::IdentityCertificate>(filename); |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 295 | |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame^] | 296 | BOOST_REQUIRE(readCert != nullptr); |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 297 | BOOST_CHECK_EQUAL(idCert->getName(), readCert->getName()); |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame] | 300 | BOOST_AUTO_TEST_SUITE_END() // TestIo |
| 301 | BOOST_AUTO_TEST_SUITE_END() // Util |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 302 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 303 | } // namespace tests |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 304 | } // namespace ndn |