tests[content-server]: Rewrite all tests cases
Change-Id: Id3daa7c1ba757f60abe9a460dcaa6772fd92aea8
diff --git a/tests/unit-tests/serve-and-fetch.t.cpp b/tests/unit-tests/serve-and-fetch.t.cpp
index da6aa1a..c5ea473 100644
--- a/tests/unit-tests/serve-and-fetch.t.cpp
+++ b/tests/unit-tests/serve-and-fetch.t.cpp
@@ -18,13 +18,12 @@
* See AUTHORS.md for complete list of ChronoShare authors and contributors.
*/
-#include "ccnx-common.hpp"
-#include "ccnx-wrapper.hpp"
#include "content-server.hpp"
#include "fetch-manager.hpp"
#include "object-db.hpp"
#include "object-manager.hpp"
-#include "scheduler.hpp"
+#include "dummy-forwarder.hpp"
+
#include <boost/filesystem.hpp>
#include <boost/make_shared.hpp>
#include <boost/test/unit_test.hpp>
@@ -35,134 +34,143 @@
#include <ctime>
#include <stdio.h>
-#include "logging.hpp"
+#include "test-common.hpp"
-_LOG_INIT(Test.ServerAndFetch);
-using namespace Ndnx;
using namespace std;
using namespace boost;
using namespace boost::filesystem;
-BOOST_AUTO_TEST_SUITE(TestServeAndFetch)
-path root("test-server-and-fetch");
-path filePath = root / "random-file";
-unsigned char magic = 'm';
-int repeat = 1024 * 400;
-mutex mut;
-condition_variable cond;
-bool finished;
-int ack;
+namespace ndn {
+namespace chronoshare {
+namespace tests {
-void
-setup()
+_LOG_INIT(Test.ServerAndFetch);
+
+class TestServerAndFetchFixture : public IdentityManagementTimeFixture
{
- if (exists(root)) {
- remove_all(root);
+public:
+ TestServerAndFetchFixture()
+ : forwarder(m_io, m_keyChain)
+ , root("test-server-and-fetch")
+ , filePath(root / "random-file")
+ , character('m')
+ , repeat(1024 * 40)
+ , finished(false)
+ , ack(0)
+ {
+ if (exists(root)) {
+ remove_all(root);
+ }
+
+ create_directory(root);
+
+ // create file
+ FILE* fp = fopen(filePath.string().c_str(), "w");
+ for (int i = 0; i < repeat; i++) {
+ fwrite(&character, 1, sizeof(character), fp);
+ }
+ fclose(fp);
+
+ ack = 0;
+ finished = false;
}
- create_directory(root);
-
- // create file
- FILE* fp = fopen(filePath.string().c_str(), "w");
- for (int i = 0; i < repeat; i++) {
- fwrite(&magic, 1, sizeof(magic), fp);
- }
- fclose(fp);
-
- ack = 0;
- finished = false;
-}
-
-void
-teardown()
-{
- if (exists(root)) {
- remove_all(root);
+ ~TestServerAndFetchFixture()
+ {
+ if (exists(root)) {
+ remove_all(root);
+ }
}
- ack = 0;
- finished = false;
-}
-
-Name
-simpleMap(const Name& deviceName)
-{
- return Name("/local");
-}
-
-void
-segmentCallback(const Name& deviceName, const Name& baseName, uint64_t seq, PcoPtr pco)
-{
- ack++;
- Bytes co = pco->content();
- int size = co.size();
- for (int i = 0; i < size; i++) {
- BOOST_CHECK_EQUAL(co[i], magic);
+ Name
+ simpleMap(const Name& deviceName)
+ {
+ return Name("/local");
}
-}
-void
-finishCallback(Name& deviceName, Name& baseName)
-{
- BOOST_CHECK_EQUAL(ack, repeat / 1024);
- unique_lock<mutex> lock(mut);
- finished = true;
- cond.notify_one();
-}
+ void
+ segmentCallback(const Name& deviceName, const Name& baseName, uint64_t seq,
+ ndn::shared_ptr<ndn::Data> data)
+ {
+ ack++;
+ ndn::Block block = Data(data->getContent().blockFromValue()).getContent();
+ int size = block.value_size();
+ const uint8_t* co = block.value();
+ for (int i = 0; i < size; i++) {
+ BOOST_CHECK_EQUAL(co[i], character);
+ }
+
+ _LOG_DEBUG("I'm called!!! segmentCallback!! size " << size << " ack times:" << ack);
+ }
+
+ void
+ finishCallback(Name& deviceName, Name& baseName)
+ {
+ BOOST_CHECK_EQUAL(ack, repeat / 1024);
+ finished = true;
+ cond.notify_one();
+ }
+
+public:
+ DummyForwarder forwarder;
+ path root;
+ path filePath;
+
+ unsigned char character;
+ int repeat;
+
+ boost::condition_variable cond;
+ bool finished;
+ int ack;
+};
+
+BOOST_FIXTURE_TEST_SUITE(TestServeAndFetch, TestServerAndFetchFixture)
BOOST_AUTO_TEST_CASE(TestServeAndFetch)
{
- _LOG_DEBUG("Setting up test environment ...");
- setup();
-
- NdnxWrapperPtr ndnx_serve = make_shared<NdnxWrapper>();
- usleep(1000);
- NdnxWrapperPtr ndnx_fetch = make_shared<NdnxWrapper>();
+ Face& face_serve = forwarder.addFace();
+ Face& face_fetch = forwarder.addFace();
Name deviceName("/test/device");
Name localPrefix("/local");
- Name broadcastPrefix("/broadcast");
+ Name broadcastPrefix("/multicast");
const string APPNAME = "test-chronoshare";
- time_t start = time(NULL);
+ time_t start = std::time(NULL);
_LOG_DEBUG("At time " << start << ", publish local file to database, this is extremely slow ...");
+
// publish file to db
- ObjectManager om(ndnx_serve, root, APPNAME);
- tuple<HashPtr, size_t> pub = om.localFileToObjects(filePath, deviceName);
- time_t end = time(NULL);
+ ObjectManager om(face_serve, m_keyChain, root, APPNAME);
+ auto pub = om.localFileToObjects(filePath, deviceName);
+
+ time_t end = std::time(NULL);
_LOG_DEBUG("At time " << end << ", publish finally finished, used " << end - start
<< " seconds ...");
ActionLogPtr dummyLog;
- ContentServer server(ndnx_serve, dummyLog, root, deviceName, "pentagon's secrets", APPNAME, 5);
+ ContentServer server(face_serve, dummyLog, root, deviceName, "pentagon's secrets", APPNAME, m_keyChain, 5);
server.registerPrefix(localPrefix);
server.registerPrefix(broadcastPrefix);
- FetchManager fm(ccnx_fetch, bind(simpleMap, _1), Name("/local/broadcast"));
- HashPtr hash = pub.get<0>();
- Name baseName = Name("/")(deviceName)(APPNAME)("file")(hash->GetHash(), hash->GetHashBytes());
+ FetchManager fm(face_fetch, bind(&TestServerAndFetchFixture::simpleMap, this, _1), Name("/"));
+ ConstBufferPtr hash = std::get<0>(pub);
- fm.Enqueue(deviceName, baseName, bind(segmentCallback, _1, _2, _3, _4),
- bind(finishCallback, _1, _2), 0, pub.get<1>() - 1);
+ Name baseName = Name(deviceName);
+ baseName.append(APPNAME).append("file").appendImplicitSha256Digest(hash);
- unique_lock<mutex> lock(mut);
- system_time timeout = get_system_time() + posix_time::milliseconds(5000);
- while (!finished) {
- if (!cond.timed_wait(lock, timeout)) {
- BOOST_FAIL("Fetching has not finished after 5 seconds");
- break;
- }
- }
- ccnx_fetch->shutdown();
- ccnx_serve->shutdown();
+ fm.Enqueue(deviceName, baseName, bind(&TestServerAndFetchFixture::segmentCallback, this, _1, _2, _3, _4),
+ bind(&TestServerAndFetchFixture::finishCallback, this, _1, _2), 0, std::get<1>(pub) - 1);
+
+ advanceClocks(time::milliseconds(10), 1000);
_LOG_DEBUG("Finish");
- usleep(100000);
-
- teardown();
}
BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace chronoshare
+} // namespace ndn