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 |
| 165 | shared_ptr<DecodableType> decoded = io::load<DecodableType>(filename, io::BASE_64); |
| 166 | BOOST_CHECK(decoded != nullptr); |
| 167 | } |
| 168 | |
| 169 | BOOST_AUTO_TEST_CASE(LoadHex) |
| 170 | { |
| 171 | this->writeFile<std::string>("BB01EE"); |
| 172 | shared_ptr<DecodableType> decoded = io::load<DecodableType>(filename, io::HEX); |
| 173 | BOOST_CHECK(decoded != nullptr); |
| 174 | } |
| 175 | |
| 176 | BOOST_AUTO_TEST_CASE(LoadException) |
| 177 | { |
| 178 | this->writeFile<std::vector<uint8_t>>({0xBB, 0x01, 0xEE}); |
| 179 | shared_ptr<DecodableTypeThrow> decoded; |
| 180 | BOOST_CHECK_NO_THROW(decoded = io::load<DecodableTypeThrow>(filename, io::NO_ENCODING)); |
| 181 | BOOST_CHECK(decoded == nullptr); |
| 182 | } |
| 183 | |
| 184 | BOOST_AUTO_TEST_CASE(LoadNotHex) |
| 185 | { |
| 186 | this->writeFile<std::string>("not-hex"); |
| 187 | shared_ptr<DecodableType> decoded; |
| 188 | BOOST_CHECK_NO_THROW(decoded = io::load<DecodableType>(filename, io::HEX)); |
| 189 | BOOST_CHECK(decoded == nullptr); |
| 190 | } |
| 191 | |
| 192 | BOOST_AUTO_TEST_CASE(LoadFileNotReadable) |
| 193 | { |
| 194 | this->mkdir(); |
| 195 | shared_ptr<DecodableType> decoded; |
| 196 | BOOST_CHECK_NO_THROW(decoded = io::load<DecodableType>(filename, io::NO_ENCODING)); |
| 197 | BOOST_CHECK(decoded == nullptr); |
| 198 | } |
| 199 | |
| 200 | BOOST_AUTO_TEST_CASE(SaveNoEncoding) |
| 201 | { |
| 202 | EncodableType encoded; |
| 203 | BOOST_CHECK_NO_THROW(io::save(encoded, filename, io::NO_ENCODING)); |
| 204 | auto content = this->readFile<std::vector<uint8_t>>(); |
| 205 | uint8_t expected[] = {0xAA, 0x01, 0xDD}; |
| 206 | BOOST_CHECK_EQUAL_COLLECTIONS(content.begin(), content.end(), |
| 207 | expected, expected + sizeof(expected)); |
| 208 | } |
| 209 | |
| 210 | BOOST_AUTO_TEST_CASE(SaveBase64) |
| 211 | { |
| 212 | EncodableType encoded; |
| 213 | BOOST_CHECK_NO_THROW(io::save(encoded, filename, io::BASE_64)); |
| 214 | auto content = this->readFile<std::string>(); |
| 215 | BOOST_CHECK_EQUAL(content, "qgHd\n"); // printf '\xAA\x01\xDD' | base64 |
| 216 | } |
| 217 | |
| 218 | BOOST_AUTO_TEST_CASE(SaveHex) |
| 219 | { |
| 220 | EncodableType encoded; |
| 221 | BOOST_CHECK_NO_THROW(io::save(encoded, filename, io::HEX)); |
| 222 | auto content = this->readFile<std::string>(); |
| 223 | BOOST_CHECK_EQUAL(content, "AA01DD"); |
| 224 | } |
| 225 | |
| 226 | BOOST_AUTO_TEST_CASE(SaveException) |
| 227 | { |
| 228 | EncodableType encoded; |
| 229 | encoded.shouldThrow = true; |
| 230 | BOOST_CHECK_THROW(io::save(encoded, filename, io::NO_ENCODING), io::Error); |
| 231 | } |
| 232 | |
| 233 | BOOST_AUTO_TEST_CASE(SaveFileNotWritable) |
| 234 | { |
| 235 | this->mkdir(); |
| 236 | EncodableType encoded; |
| 237 | encoded.shouldThrow = true; |
| 238 | BOOST_CHECK_THROW(io::save(encoded, filename, io::NO_ENCODING), io::Error); |
| 239 | } |
| 240 | |
| 241 | class IdCertFixture : public IoFixture |
| 242 | , public IdentityManagementFixture |
| 243 | { |
| 244 | }; |
| 245 | |
| 246 | BOOST_FIXTURE_TEST_CASE(IdCert, IdCertFixture) |
| 247 | { |
| 248 | Name identity("/TestIo/IdCert"); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 249 | identity.appendVersion(); |
Yingdi Yu | 3ed09d0 | 2014-10-13 16:24:08 -0700 | [diff] [blame] | 250 | BOOST_REQUIRE(addIdentity(identity, RsaKeyParams())); |
| 251 | Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity); |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 252 | shared_ptr<IdentityCertificate> idCert; |
Yingdi Yu | 3ed09d0 | 2014-10-13 16:24:08 -0700 | [diff] [blame] | 253 | BOOST_REQUIRE_NO_THROW(idCert = m_keyChain.getCertificate(certName)); |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 254 | |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame^] | 255 | io::save(*idCert, filename); |
| 256 | shared_ptr<IdentityCertificate> readCert = io::load<IdentityCertificate>(filename); |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 257 | |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame^] | 258 | BOOST_CHECK(readCert != nullptr); |
| 259 | BOOST_CHECK_EQUAL(idCert->getName(), readCert->getName()); |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Junxiao Shi | c177988 | 2016-08-17 01:59:23 +0000 | [diff] [blame^] | 262 | BOOST_AUTO_TEST_SUITE_END() // TestIo |
| 263 | BOOST_AUTO_TEST_SUITE_END() // Util |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 264 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 265 | } // namespace tests |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 266 | } // namespace ndn |