Chengyu Fan | c7b87ad | 2015-07-09 16:44:37 -0600 | [diff] [blame] | 1 | /** NDN-Atmos: Cataloging Service for distributed data originally developed |
| 2 | * for atmospheric science data |
| 3 | * Copyright (C) 2015 Colorado State University |
| 4 | * |
| 5 | * NDN-Atmos is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * NDN-Atmos is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with NDN-Atmos. If not, see <http://www.gnu.org/licenses/>. |
| 17 | **/ |
| 18 | |
| 19 | #include "publish/publish-adapter.hpp" |
| 20 | #include "boost-test.hpp" |
| 21 | #include "../../unit-test-time-fixture.hpp" |
| 22 | #include "util/config-file.hpp" |
| 23 | |
| 24 | #include <boost/mpl/list.hpp> |
| 25 | #include <boost/thread.hpp> |
| 26 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 27 | #include <boost/property_tree/info_parser.hpp> |
| 28 | |
| 29 | namespace atmos{ |
| 30 | namespace tests{ |
| 31 | using ndn::util::DummyClientFace; |
| 32 | using ndn::util::makeDummyClientFace; |
| 33 | |
| 34 | class PublishAdapterTest : public publish::PublishAdapter<std::string> |
| 35 | { |
| 36 | public: |
| 37 | PublishAdapterTest(const std::shared_ptr<ndn::util::DummyClientFace>& face, |
| 38 | const std::shared_ptr<ndn::KeyChain>& keyChain) |
| 39 | : publish::PublishAdapter<std::string>(face, keyChain) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | virtual |
| 44 | ~PublishAdapterTest() |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | const ndn::Name |
| 49 | getPrefix() |
| 50 | { |
| 51 | return m_prefix; |
| 52 | } |
| 53 | |
| 54 | const ndn::Name |
| 55 | getSigningId() |
| 56 | { |
| 57 | return m_signingId; |
| 58 | } |
| 59 | |
| 60 | const ndn::Name |
| 61 | getSyncPrefix() |
| 62 | { |
| 63 | return m_syncPrefix; |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | configAdapter(const util::ConfigSection& section, |
| 68 | const ndn::Name& prefix) |
| 69 | { |
| 70 | onConfig(section, false, std::string("test.txt"), prefix); |
| 71 | } |
| 72 | |
| 73 | bool |
| 74 | testValidatePublicationChanges(const std::shared_ptr<const ndn::Data>& data) |
| 75 | { |
| 76 | return validatePublicationChanges(data); |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | class PublishAdapterFixture : public UnitTestTimeFixture |
| 81 | { |
| 82 | public: |
| 83 | PublishAdapterFixture() |
| 84 | : face(makeDummyClientFace(io)) |
| 85 | , keyChain(new ndn::KeyChain()) |
| 86 | , publishAdapterTest1(face, keyChain) |
| 87 | , publishAdapterTest2(face, keyChain) |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | virtual |
| 92 | ~PublishAdapterFixture() |
| 93 | { |
| 94 | } |
| 95 | |
| 96 | protected: |
| 97 | void |
| 98 | initializePublishAdapterTest1() |
| 99 | { |
| 100 | util::ConfigSection section; |
| 101 | try { |
| 102 | std::stringstream ss; |
| 103 | ss << "database \ |
| 104 | { \ |
| 105 | dbServer localhost \ |
| 106 | dbName testdb \ |
| 107 | dbUser testuser \ |
| 108 | dbPasswd testpwd \ |
| 109 | } \ |
| 110 | sync \ |
| 111 | { \ |
| 112 | prefix ndn:/ndn/broadcast1 \ |
| 113 | }"; |
| 114 | boost::property_tree::read_info(ss, section); |
| 115 | } |
| 116 | catch (boost::property_tree::info_parser_error &e) { |
| 117 | std::cout << "Failed to read config file " << e.what() << std::endl; |
| 118 | } |
| 119 | publishAdapterTest1.configAdapter(section, ndn::Name("/test")); |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | initializePublishAdapterTest2() |
| 124 | { |
| 125 | util::ConfigSection section; |
| 126 | try { |
| 127 | std::stringstream ss; |
| 128 | ss << "\ |
| 129 | signingId /prefix/signingId \ |
| 130 | database \ |
| 131 | { \ |
| 132 | dbServer localhost \ |
| 133 | dbName testdb \ |
| 134 | dbUser testuser \ |
| 135 | dbPasswd testpwd \ |
| 136 | } \ |
| 137 | sync \ |
| 138 | { \ |
| 139 | }"; |
| 140 | boost::property_tree::read_info(ss, section); |
| 141 | } |
| 142 | catch (boost::property_tree::info_parser_error &e) { |
| 143 | std::cout << "Failed to read config file " << e.what() << std::endl;; |
| 144 | } |
| 145 | publishAdapterTest2.configAdapter(section, ndn::Name("/test")); |
| 146 | } |
| 147 | |
| 148 | protected: |
| 149 | std::shared_ptr<DummyClientFace> face; |
| 150 | std::shared_ptr<ndn::KeyChain> keyChain; |
| 151 | PublishAdapterTest publishAdapterTest1; |
| 152 | PublishAdapterTest publishAdapterTest2; |
| 153 | }; |
| 154 | |
| 155 | BOOST_FIXTURE_TEST_SUITE(PublishAdapterTestSuite, PublishAdapterFixture) |
| 156 | |
| 157 | BOOST_AUTO_TEST_CASE(BasicPublishAdapterTest1) |
| 158 | { |
| 159 | BOOST_CHECK(publishAdapterTest1.getPrefix() == ndn::Name()); |
| 160 | BOOST_CHECK(publishAdapterTest1.getSigningId() == ndn::Name()); |
| 161 | BOOST_CHECK(publishAdapterTest1.getSyncPrefix() == ndn::Name()); |
| 162 | } |
| 163 | |
| 164 | BOOST_AUTO_TEST_CASE(BasicPublishAdapterTest2) |
| 165 | { |
| 166 | initializePublishAdapterTest1(); |
| 167 | BOOST_CHECK(publishAdapterTest1.getPrefix() == ndn::Name("/test")); |
| 168 | BOOST_CHECK(publishAdapterTest1.getSigningId() == ndn::Name()); |
| 169 | BOOST_CHECK(publishAdapterTest1.getSyncPrefix() == ndn::Name("ndn:/ndn/broadcast1")); |
| 170 | |
| 171 | initializePublishAdapterTest2(); |
| 172 | BOOST_CHECK(publishAdapterTest2.getPrefix() == ndn::Name("/test")); |
| 173 | BOOST_CHECK(publishAdapterTest2.getSigningId() == ndn::Name("/prefix/signingId")); |
| 174 | BOOST_CHECK(publishAdapterTest2.getSyncPrefix() == |
| 175 | ndn::Name("ndn:/ndn-atmos/broadcast/chronosync")); |
| 176 | } |
| 177 | |
| 178 | BOOST_AUTO_TEST_CASE(PublishAdapterValidateDataTestSuccess) |
| 179 | { |
| 180 | ndn::Name dataName("/test/publisher/12345"); // data name must be prefix+nonce |
| 181 | Json::Value testJson; |
| 182 | testJson["add"][0] = "/test/publisher/1"; |
| 183 | testJson["add"][1] = "/test/publisher/2"; |
| 184 | testJson["remove"][0] = "/test/publisher/5"; |
| 185 | |
| 186 | Json::FastWriter fastWriter; |
| 187 | const std::string jsonMessage = fastWriter.write(testJson); |
| 188 | const char* payload = jsonMessage.c_str(); |
| 189 | size_t payLoadLength = jsonMessage.size() + 1; |
| 190 | |
| 191 | std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(dataName); |
| 192 | data->setContent(reinterpret_cast<const uint8_t*>(payload), payLoadLength); |
| 193 | data->setFreshnessPeriod(ndn::time::milliseconds(10000)); |
| 194 | |
| 195 | BOOST_CHECK_EQUAL(true, publishAdapterTest1.testValidatePublicationChanges(data)); |
| 196 | |
| 197 | ndn::Name dataName2("/"); // short data name |
| 198 | Json::Value testJson2; |
| 199 | testJson2["add"][0] = "/test/publisher2/1"; |
| 200 | testJson2["remove"][0] = "/test/publisher/1/2/3"; |
| 201 | |
| 202 | const std::string jsonMessage2 = fastWriter.write(testJson2); |
| 203 | const char* payload2 = jsonMessage2.c_str(); |
| 204 | size_t payLoadLength2 = jsonMessage2.size() + 1; |
| 205 | |
| 206 | std::shared_ptr<ndn::Data> data2 = std::make_shared<ndn::Data>(dataName2); |
| 207 | data2->setContent(reinterpret_cast<const uint8_t*>(payload2), payLoadLength2); |
| 208 | data2->setFreshnessPeriod(ndn::time::milliseconds(10000)); |
| 209 | BOOST_CHECK_EQUAL(true, publishAdapterTest1.testValidatePublicationChanges(data2)); |
| 210 | } |
| 211 | |
| 212 | BOOST_AUTO_TEST_CASE(PublishAdapterValidateDataTestFail) |
| 213 | { |
| 214 | ndn::Name dataName1("test/publisher2/12345"); // data name must be prefix+nonce |
| 215 | Json::Value testJson1; |
| 216 | testJson1["add"][0] = "/test/publisher2/1"; |
| 217 | testJson1["remove"][0] = "/test/publisher/1/2/3"; |
| 218 | testJson1["remove"][1] = "/test/publisher2/4"; |
| 219 | |
| 220 | Json::FastWriter fastWriter; |
| 221 | const std::string jsonMessage1 = fastWriter.write(testJson1); |
| 222 | const char* payload1 = jsonMessage1.c_str(); |
| 223 | size_t payLoadLength1 = jsonMessage1.size() + 1; |
| 224 | |
| 225 | std::shared_ptr<ndn::Data> data1 = std::make_shared<ndn::Data>(dataName1); |
| 226 | data1->setContent(reinterpret_cast<const uint8_t*>(payload1), payLoadLength1); |
| 227 | data1->setFreshnessPeriod(ndn::time::milliseconds(10000)); |
| 228 | |
| 229 | BOOST_CHECK_EQUAL(false, publishAdapterTest1.testValidatePublicationChanges(data1)); |
| 230 | } |
| 231 | |
| 232 | BOOST_AUTO_TEST_SUITE_END() |
| 233 | |
| 234 | }//tests |
| 235 | }//atmos |