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