Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012 University of California, Los Angeles |
| 4 | */ |
| 5 | |
| 6 | #include <boost/test/unit_test.hpp> |
| 7 | #include "sync-validator.h" |
| 8 | |
| 9 | BOOST_AUTO_TEST_SUITE(TestSyncValidator) |
| 10 | |
| 11 | void onValidated(const ndn::shared_ptr<const ndn::Data>& data) |
| 12 | { |
| 13 | BOOST_CHECK(true); |
| 14 | } |
| 15 | |
| 16 | void onValidationFailed(const ndn::shared_ptr<const ndn::Data>& data, |
| 17 | const std::string& failureInfo) |
| 18 | { |
| 19 | BOOST_CHECK(false); |
| 20 | } |
| 21 | |
| 22 | void onValidated2(const ndn::shared_ptr<const ndn::Data>& data) |
| 23 | { |
| 24 | BOOST_CHECK(false); |
| 25 | } |
| 26 | |
| 27 | void onValidationFailed2(const ndn::shared_ptr<const ndn::Data>& data, |
| 28 | const std::string& failureInfo) |
| 29 | { |
| 30 | BOOST_CHECK(true); |
| 31 | } |
| 32 | |
| 33 | BOOST_AUTO_TEST_CASE (Graph) |
| 34 | { |
| 35 | using namespace Sync; |
| 36 | using namespace ndn; |
| 37 | |
| 38 | Name prefix("/Sync/TestSyncValidator/AddEdge"); |
| 39 | KeyChain keychain; |
| 40 | |
| 41 | Name identity1("/TestSyncValidator/AddEdge-1/" + boost::lexical_cast<std::string>(time::now())); |
| 42 | Name certName1 = keychain.createIdentity(identity1); |
| 43 | shared_ptr<IdentityCertificate> anchor = keychain.getCertificate(certName1); |
| 44 | |
| 45 | Name identity2("/TestSyncValidator/AddEdge-2/" + boost::lexical_cast<std::string>(time::now())); |
| 46 | Name certName2 = keychain.createIdentity(identity2); |
| 47 | shared_ptr<IdentityCertificate> introducer = keychain.getCertificate(certName2); |
| 48 | |
| 49 | Name identity3("/TestSyncValidator/AddEdge-3/" + boost::lexical_cast<std::string>(time::now())); |
| 50 | Name certName3 = keychain.createIdentity(identity3); |
| 51 | shared_ptr<IdentityCertificate> introducee = keychain.getCertificate(certName3); |
| 52 | |
| 53 | Name identity4("/TestSyncValidator/AddEdge-4/" + boost::lexical_cast<std::string>(time::now())); |
| 54 | Name certName4 = keychain.createIdentity(identity4); |
| 55 | shared_ptr<IdentityCertificate> introducer2 = keychain.getCertificate(certName4); |
| 56 | |
| 57 | Name identity5("/TestSyncValidator/AddEdge-5/" + boost::lexical_cast<std::string>(time::now())); |
| 58 | Name certName5 = keychain.createIdentity(identity5); |
| 59 | shared_ptr<IdentityCertificate> introducee2 = keychain.getCertificate(certName5); |
| 60 | |
| 61 | shared_ptr<boost::asio::io_service> ioService = make_shared<boost::asio::io_service>(); |
| 62 | shared_ptr<Face> face = make_shared<Face>(ioService); |
| 63 | SyncValidator validator(prefix, *anchor, face); |
| 64 | |
| 65 | validator.addParticipant(*introducer); |
| 66 | BOOST_CHECK(validator.canTrust(certName2)); |
| 67 | |
| 68 | IntroCertificate introCert(prefix, *introducee, certName2.getPrefix(-1)); |
| 69 | keychain.sign(introCert, certName2); |
| 70 | validator.addParticipant(introCert); |
| 71 | BOOST_CHECK(validator.canTrust(certName3)); |
| 72 | |
| 73 | IntroCertificate introCert1(prefix, *anchor, certName3.getPrefix(-1)); |
| 74 | keychain.sign(introCert1, certName3); |
| 75 | validator.addParticipant(introCert1); |
| 76 | validator.setAnchor(*introducer); |
| 77 | BOOST_CHECK(validator.canTrust(certName2)); |
| 78 | BOOST_CHECK(validator.canTrust(certName3)); |
| 79 | BOOST_CHECK(validator.canTrust(certName1)); |
| 80 | |
| 81 | IntroCertificate introCert2(prefix, *introducee2, certName4.getPrefix(-1)); |
| 82 | keychain.sign(introCert2, certName4); |
| 83 | validator.addParticipant(introCert2); |
| 84 | BOOST_CHECK(validator.canTrust(certName5) == false); |
| 85 | BOOST_CHECK(validator.canTrust(certName4) == false); |
| 86 | |
| 87 | IntroCertificate introCert3(prefix, *introducee, certName5.getPrefix(-1)); |
| 88 | keychain.sign(introCert3, certName5); |
| 89 | validator.addParticipant(introCert3); |
| 90 | BOOST_CHECK(validator.canTrust(certName5) == false); |
| 91 | BOOST_CHECK(validator.canTrust(certName4) == false); |
| 92 | |
| 93 | validator.setAnchor(*introducee2); |
| 94 | BOOST_CHECK(validator.canTrust(certName1)); |
| 95 | BOOST_CHECK(validator.canTrust(certName2)); |
| 96 | BOOST_CHECK(validator.canTrust(certName3)); |
| 97 | BOOST_CHECK(validator.canTrust(certName4) == false); |
| 98 | BOOST_CHECK(validator.canTrust(certName5)); |
| 99 | |
| 100 | |
| 101 | keychain.deleteIdentity(identity1); |
| 102 | keychain.deleteIdentity(identity2); |
| 103 | keychain.deleteIdentity(identity3); |
| 104 | keychain.deleteIdentity(identity4); |
| 105 | keychain.deleteIdentity(identity5); |
| 106 | } |
| 107 | |
| 108 | BOOST_AUTO_TEST_CASE (OfflineValidate) |
| 109 | { |
| 110 | using namespace Sync; |
| 111 | using namespace ndn; |
| 112 | |
| 113 | Name prefix("/Sync/TestSyncValidator/OfflineValidate"); |
| 114 | KeyChain keychain; |
| 115 | |
| 116 | Name identity1("/TestSyncValidator/OfflineValidate-1/" + boost::lexical_cast<std::string>(time::now())); |
| 117 | Name certName1 = keychain.createIdentity(identity1); |
| 118 | shared_ptr<IdentityCertificate> anchor = keychain.getCertificate(certName1); |
| 119 | |
| 120 | Name identity2("/TestSyncValidator/OfflineValidate-2/" + boost::lexical_cast<std::string>(time::now())); |
| 121 | Name certName2 = keychain.createIdentity(identity2); |
| 122 | shared_ptr<IdentityCertificate> introducer = keychain.getCertificate(certName2); |
| 123 | |
| 124 | Name identity3("/TestSyncValidator/OfflineValidate-3/" + boost::lexical_cast<std::string>(time::now())); |
| 125 | Name certName3 = keychain.createIdentity(identity3); |
| 126 | shared_ptr<IdentityCertificate> introducee = keychain.getCertificate(certName3); |
| 127 | |
| 128 | Name identity4("/TestSyncValidator/OfflineValidate-4/" + boost::lexical_cast<std::string>(time::now())); |
| 129 | Name certName4 = keychain.createIdentity(identity4); |
| 130 | shared_ptr<IdentityCertificate> introducer2 = keychain.getCertificate(certName4); |
| 131 | |
| 132 | Name identity5("/TestSyncValidator/OfflineValidate-5/" + boost::lexical_cast<std::string>(time::now())); |
| 133 | Name certName5 = keychain.createIdentity(identity5); |
| 134 | shared_ptr<IdentityCertificate> introducee2 = keychain.getCertificate(certName5); |
| 135 | |
| 136 | shared_ptr<boost::asio::io_service> ioService = make_shared<boost::asio::io_service>(); |
| 137 | shared_ptr<Face> face = make_shared<Face>(ioService); |
| 138 | SyncValidator validator(prefix, *anchor, face); |
| 139 | |
| 140 | validator.addParticipant(*introducer); |
| 141 | BOOST_CHECK(validator.canTrust(certName2)); |
| 142 | |
| 143 | IntroCertificate introCert(prefix, *introducee, certName2.getPrefix(-1)); |
| 144 | keychain.sign(introCert, certName2); |
| 145 | validator.addParticipant(introCert); |
| 146 | BOOST_CHECK(validator.canTrust(certName3)); |
| 147 | |
| 148 | IntroCertificate introCert2(prefix, *introducee2, certName4.getPrefix(-1)); |
| 149 | keychain.sign(introCert2, certName4); |
| 150 | validator.addParticipant(introCert2); |
| 151 | BOOST_CHECK(validator.canTrust(certName5) == false); |
| 152 | BOOST_CHECK(validator.canTrust(certName4) == false); |
| 153 | |
| 154 | validator.setAnchor(*introducer2); |
| 155 | BOOST_CHECK(validator.canTrust(certName1) == false); |
| 156 | BOOST_CHECK(validator.canTrust(certName2) == false); |
| 157 | BOOST_CHECK(validator.canTrust(certName3) == false); |
| 158 | BOOST_CHECK(validator.canTrust(certName4)); |
| 159 | BOOST_CHECK(validator.canTrust(certName5)); |
| 160 | |
| 161 | Name dataName1 = prefix; |
| 162 | dataName1.append("data-1"); |
| 163 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
| 164 | keychain.sign(*data1, certName5); |
| 165 | |
| 166 | validator.validate(*data1, |
| 167 | bind(&onValidated, _1), |
| 168 | bind(&onValidationFailed, _1, _2)); |
| 169 | |
| 170 | Name dataName2 = prefix; |
| 171 | dataName2.append("data-2"); |
| 172 | shared_ptr<Data> data2 = make_shared<Data>(dataName2); |
| 173 | keychain.sign(*data2, certName1); |
| 174 | |
| 175 | validator.validate(*data2, |
| 176 | bind(&onValidated2, _1), |
| 177 | bind(&onValidationFailed2, _1, _2)); |
| 178 | |
| 179 | ioService->run(); |
| 180 | |
| 181 | keychain.deleteIdentity(identity1); |
| 182 | keychain.deleteIdentity(identity2); |
| 183 | keychain.deleteIdentity(identity3); |
| 184 | keychain.deleteIdentity(identity4); |
| 185 | keychain.deleteIdentity(identity5); |
| 186 | } |
| 187 | |
| 188 | BOOST_AUTO_TEST_CASE (OnlineValidate) |
| 189 | { |
| 190 | using namespace Sync; |
| 191 | using namespace ndn; |
| 192 | |
| 193 | Name prefix("/Sync/TestSyncValidator/OnlineValidate"); |
| 194 | KeyChain keychain; |
| 195 | |
| 196 | Name identity1("/TestSyncValidator/OnlineValidate-1/" + boost::lexical_cast<std::string>(time::now())); |
| 197 | Name certName1 = keychain.createIdentity(identity1); |
| 198 | shared_ptr<IdentityCertificate> anchor = keychain.getCertificate(certName1); |
| 199 | |
| 200 | Name identity2("/TestSyncValidator/OnlineValidate-2/" + boost::lexical_cast<std::string>(time::now())); |
| 201 | Name certName2 = keychain.createIdentity(identity2); |
| 202 | shared_ptr<IdentityCertificate> introducer = keychain.getCertificate(certName2); |
| 203 | |
| 204 | Name identity3("/TestSyncValidator/OnlineValidate-3/" + boost::lexical_cast<std::string>(time::now())); |
| 205 | Name certName3 = keychain.createIdentity(identity3); |
| 206 | shared_ptr<IdentityCertificate> introducee = keychain.getCertificate(certName3); |
| 207 | |
| 208 | Name identity4("/TestSyncValidator/OfflineValidate-4/" + boost::lexical_cast<std::string>(time::now())); |
| 209 | Name certName4 = keychain.createIdentity(identity4); |
| 210 | shared_ptr<IdentityCertificate> introducee2 = keychain.getCertificate(certName4); |
| 211 | |
| 212 | shared_ptr<boost::asio::io_service> ioService = make_shared<boost::asio::io_service>(); |
| 213 | shared_ptr<Face> face = make_shared<Face>(ioService); |
| 214 | SyncValidator validator(prefix, *anchor, face); |
| 215 | |
| 216 | validator.addParticipant(*introducer); |
| 217 | BOOST_CHECK(validator.canTrust(certName2)); |
| 218 | |
| 219 | IntroCertificate introCert(prefix, *introducee, certName2.getPrefix(-1)); |
| 220 | keychain.sign(introCert, certName2); |
| 221 | face->put(introCert); |
| 222 | BOOST_CHECK(validator.canTrust(certName3) == false); |
| 223 | |
| 224 | IntroCertificate introCert2(prefix, *introducee2, certName3.getPrefix(-1)); |
| 225 | keychain.sign(introCert2, certName3); |
| 226 | face->put(introCert2); |
| 227 | BOOST_CHECK(validator.canTrust(certName4) == false); |
| 228 | |
| 229 | Name dataName1 = prefix; |
| 230 | dataName1.append("data-1"); |
| 231 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
| 232 | keychain.sign(*data1, certName4); |
| 233 | |
| 234 | validator.validate(*data1, |
| 235 | bind(&onValidated, _1), |
| 236 | bind(&onValidationFailed, _1, _2)); |
| 237 | |
| 238 | ioService->run(); |
| 239 | |
| 240 | BOOST_CHECK(validator.canTrust(certName3)); |
| 241 | BOOST_CHECK(validator.canTrust(certName4)); |
| 242 | |
| 243 | keychain.deleteIdentity(identity1); |
| 244 | keychain.deleteIdentity(identity2); |
| 245 | keychain.deleteIdentity(identity3); |
| 246 | keychain.deleteIdentity(identity4); |
| 247 | } |
| 248 | |
| 249 | BOOST_AUTO_TEST_SUITE_END() |