Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, Regents of the University of California. |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 7 | * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 11 | * ChronoShare 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 General Public License for more details. |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 14 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 21 | #include "content-server.hpp" |
| 22 | #include "fetch-manager.hpp" |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 23 | #include "object-db.hpp" |
| 24 | #include "object-manager.hpp" |
Yukai Tu | 931546f | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 25 | #include "dummy-forwarder.hpp" |
| 26 | |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 27 | #include <boost/filesystem.hpp> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 28 | #include <boost/make_shared.hpp> |
| 29 | #include <boost/test/unit_test.hpp> |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 30 | #include <boost/thread/condition_variable.hpp> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 31 | #include <boost/thread/locks.hpp> |
| 32 | #include <boost/thread/mutex.hpp> |
| 33 | #include <boost/thread/thread_time.hpp> |
Zhenkai Zhu | 13d224d | 2013-01-23 19:40:25 -0800 | [diff] [blame] | 34 | #include <ctime> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 35 | #include <stdio.h> |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 36 | |
Yukai Tu | 931546f | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 37 | #include "test-common.hpp" |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 38 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 39 | |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 40 | using namespace std; |
| 41 | using namespace boost; |
| 42 | using namespace boost::filesystem; |
| 43 | |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 44 | |
Yukai Tu | 931546f | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 45 | namespace ndn { |
| 46 | namespace chronoshare { |
| 47 | namespace tests { |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 48 | |
Yukai Tu | 931546f | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 49 | _LOG_INIT(Test.ServerAndFetch); |
| 50 | |
| 51 | class TestServerAndFetchFixture : public IdentityManagementTimeFixture |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 52 | { |
Yukai Tu | 931546f | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 53 | public: |
| 54 | TestServerAndFetchFixture() |
| 55 | : forwarder(m_io, m_keyChain) |
| 56 | , root("test-server-and-fetch") |
| 57 | , filePath(root / "random-file") |
| 58 | , character('m') |
| 59 | , repeat(1024 * 40) |
| 60 | , finished(false) |
| 61 | , ack(0) |
| 62 | { |
| 63 | if (exists(root)) { |
| 64 | remove_all(root); |
| 65 | } |
| 66 | |
| 67 | create_directory(root); |
| 68 | |
| 69 | // create file |
| 70 | FILE* fp = fopen(filePath.string().c_str(), "w"); |
| 71 | for (int i = 0; i < repeat; i++) { |
| 72 | fwrite(&character, 1, sizeof(character), fp); |
| 73 | } |
| 74 | fclose(fp); |
| 75 | |
| 76 | ack = 0; |
| 77 | finished = false; |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Yukai Tu | 931546f | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 80 | ~TestServerAndFetchFixture() |
| 81 | { |
| 82 | if (exists(root)) { |
| 83 | remove_all(root); |
| 84 | } |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Yukai Tu | 931546f | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 87 | Name |
| 88 | simpleMap(const Name& deviceName) |
| 89 | { |
| 90 | return Name("/local"); |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 91 | } |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 92 | |
Yukai Tu | 931546f | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 93 | void |
| 94 | segmentCallback(const Name& deviceName, const Name& baseName, uint64_t seq, |
| 95 | ndn::shared_ptr<ndn::Data> data) |
| 96 | { |
| 97 | ack++; |
| 98 | ndn::Block block = Data(data->getContent().blockFromValue()).getContent(); |
| 99 | int size = block.value_size(); |
| 100 | const uint8_t* co = block.value(); |
| 101 | for (int i = 0; i < size; i++) { |
| 102 | BOOST_CHECK_EQUAL(co[i], character); |
| 103 | } |
| 104 | |
| 105 | _LOG_DEBUG("I'm called!!! segmentCallback!! size " << size << " ack times:" << ack); |
| 106 | } |
| 107 | |
| 108 | void |
| 109 | finishCallback(Name& deviceName, Name& baseName) |
| 110 | { |
| 111 | BOOST_CHECK_EQUAL(ack, repeat / 1024); |
| 112 | finished = true; |
| 113 | cond.notify_one(); |
| 114 | } |
| 115 | |
| 116 | public: |
| 117 | DummyForwarder forwarder; |
| 118 | path root; |
| 119 | path filePath; |
| 120 | |
| 121 | unsigned char character; |
| 122 | int repeat; |
| 123 | |
| 124 | boost::condition_variable cond; |
| 125 | bool finished; |
| 126 | int ack; |
| 127 | }; |
| 128 | |
| 129 | BOOST_FIXTURE_TEST_SUITE(TestServeAndFetch, TestServerAndFetchFixture) |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 130 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 131 | BOOST_AUTO_TEST_CASE(TestServeAndFetch) |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 132 | { |
Yukai Tu | 931546f | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 133 | Face& face_serve = forwarder.addFace(); |
| 134 | Face& face_fetch = forwarder.addFace(); |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 135 | |
Zhenkai Zhu | 13d224d | 2013-01-23 19:40:25 -0800 | [diff] [blame] | 136 | Name deviceName("/test/device"); |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 137 | Name localPrefix("/local"); |
Yukai Tu | 931546f | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 138 | Name broadcastPrefix("/multicast"); |
Zhenkai Zhu | 13d224d | 2013-01-23 19:40:25 -0800 | [diff] [blame] | 139 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 140 | const string APPNAME = "test-chronoshare"; |
| 141 | |
Yukai Tu | 931546f | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 142 | time_t start = std::time(NULL); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 143 | _LOG_DEBUG("At time " << start << ", publish local file to database, this is extremely slow ..."); |
Yukai Tu | 931546f | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 144 | |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 145 | // publish file to db |
Yukai Tu | 931546f | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 146 | ObjectManager om(face_serve, m_keyChain, root, APPNAME); |
| 147 | auto pub = om.localFileToObjects(filePath, deviceName); |
| 148 | |
| 149 | time_t end = std::time(NULL); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 150 | _LOG_DEBUG("At time " << end << ", publish finally finished, used " << end - start |
| 151 | << " seconds ..."); |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 152 | |
| 153 | ActionLogPtr dummyLog; |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 154 | ContentServer server(face_serve, dummyLog, root, deviceName, "pentagon's secrets", |
| 155 | name::Component(APPNAME), m_keyChain, time::seconds(5)); |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 156 | server.registerPrefix(localPrefix); |
| 157 | server.registerPrefix(broadcastPrefix); |
| 158 | |
Yukai Tu | 931546f | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 159 | FetchManager fm(face_fetch, bind(&TestServerAndFetchFixture::simpleMap, this, _1), Name("/")); |
| 160 | ConstBufferPtr hash = std::get<0>(pub); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 161 | |
Yukai Tu | 931546f | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 162 | Name baseName = Name(deviceName); |
| 163 | baseName.append(APPNAME).append("file").appendImplicitSha256Digest(hash); |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 164 | |
Yukai Tu | 931546f | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 165 | fm.Enqueue(deviceName, baseName, bind(&TestServerAndFetchFixture::segmentCallback, this, _1, _2, _3, _4), |
| 166 | bind(&TestServerAndFetchFixture::finishCallback, this, _1, _2), 0, std::get<1>(pub) - 1); |
| 167 | |
| 168 | advanceClocks(time::milliseconds(10), 1000); |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 169 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 170 | _LOG_DEBUG("Finish"); |
Zhenkai Zhu | 5f5dd9a | 2013-01-23 16:39:41 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | BOOST_AUTO_TEST_SUITE_END() |
Yukai Tu | 931546f | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 174 | |
| 175 | } // namespace tests |
| 176 | } // namespace chronoshare |
| 177 | } // namespace ndn |