Commit 144defdb authored by Sébastien Blin's avatar Sébastien Blin Committed by Adrien Béraud

tests: fix fileTransfer test

Change-Id: Ibb1afba82e884913d1e8268fdd584dedef8ae496
parent b416ecf6
/*
* Copyright (C) 2020 Savoir-faire Linux Inc.
* Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*
* Copyright (C) 2020 Savoir-faire Linux Inc.
* Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cppunit/TestAssert.h>
#include <cppunit/TestFixture.h>
......@@ -34,19 +34,20 @@
using namespace DRing::Account;
namespace jami { namespace test {
namespace jami {
namespace test {
class FileTransferTest : public CppUnit::TestFixture {
class FileTransferTest : public CppUnit::TestFixture
{
public:
FileTransferTest() {
FileTransferTest()
{
// Init daemon
DRing::init(DRing::InitFlag(DRing::DRING_FLAG_DEBUG | DRing::DRING_FLAG_CONSOLE_LOG));
if (not Manager::instance().initialized)
CPPUNIT_ASSERT(DRing::start("dring-sample.yml"));
}
~FileTransferTest() {
DRing::fini();
}
~FileTransferTest() { DRing::fini(); }
static std::string name() { return "Call"; }
bool compare(const std::string& fileA, const std::string& fileB) const;
void setUp();
......@@ -68,19 +69,20 @@ private:
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FileTransferTest, FileTransferTest::name());
bool
FileTransferTest::compare(const std::string& fileA, const std::string& fileB) const {
std::ifstream f1(fileA, std::ifstream::binary|std::ifstream::ate);
std::ifstream f2(fileB, std::ifstream::binary|std::ifstream::ate);
if (f1.fail() || f2.fail() || f1.tellg() != f2.tellg()) {
return false;
}
f1.seekg(0, std::ifstream::beg);
f2.seekg(0, std::ifstream::beg);
return std::equal(std::istreambuf_iterator<char>(f1.rdbuf()),
std::istreambuf_iterator<char>(),
std::istreambuf_iterator<char>(f2.rdbuf()));
FileTransferTest::compare(const std::string& fileA, const std::string& fileB) const
{
std::ifstream f1(fileA, std::ifstream::binary | std::ifstream::ate);
std::ifstream f2(fileB, std::ifstream::binary | std::ifstream::ate);
if (f1.fail() || f2.fail() || f1.tellg() != f2.tellg()) {
return false;
}
f1.seekg(0, std::ifstream::beg);
f2.seekg(0, std::ifstream::beg);
return std::equal(std::istreambuf_iterator<char>(f1.rdbuf()),
std::istreambuf_iterator<char>(),
std::istreambuf_iterator<char>(f2.rdbuf()));
}
void
......@@ -111,18 +113,19 @@ FileTransferTest::setUp()
auto bobAccount = Manager::instance().getAccount<JamiAccount>(bobId);
std::map<std::string, std::shared_ptr<DRing::CallbackWrapperBase>> confHandlers;
std::mutex mtx;
std::unique_lock<std::mutex> lk{ mtx };
std::unique_lock<std::mutex> lk {mtx};
std::condition_variable cv;
confHandlers.insert(DRing::exportable_callback<DRing::ConfigurationSignal::VolatileDetailsChanged>(
[&](const std::string&, const std::map<std::string, std::string>&) {
bool ready = false;
auto details = aliceAccount->getVolatileAccountDetails();
auto daemonStatus = details[DRing::Account::ConfProperties::Registration::STATUS];
ready = (daemonStatus == "REGISTERED");
details = bobAccount->getVolatileAccountDetails();
daemonStatus = details[DRing::Account::ConfProperties::Registration::STATUS];
ready &= (daemonStatus == "REGISTERED");
}));
confHandlers.insert(
DRing::exportable_callback<DRing::ConfigurationSignal::VolatileDetailsChanged>(
[&](const std::string&, const std::map<std::string, std::string>&) {
bool ready = false;
auto details = aliceAccount->getVolatileAccountDetails();
auto daemonStatus = details[DRing::Account::ConfProperties::Registration::STATUS];
ready = (daemonStatus == "REGISTERED");
details = bobAccount->getVolatileAccountDetails();
daemonStatus = details[DRing::Account::ConfProperties::Registration::STATUS];
ready &= (daemonStatus == "REGISTERED");
}));
DRing::registerSignalHandlers(confHandlers);
cv.wait_for(lk, std::chrono::seconds(30));
DRing::unregisterSignalHandlers();
......@@ -135,15 +138,15 @@ FileTransferTest::tearDown()
std::map<std::string, std::shared_ptr<DRing::CallbackWrapperBase>> confHandlers;
std::mutex mtx;
std::unique_lock<std::mutex> lk{ mtx };
std::unique_lock<std::mutex> lk {mtx};
std::condition_variable cv;
auto currentAccSize = Manager::instance().getAccountList().size();
confHandlers.insert(DRing::exportable_callback<DRing::ConfigurationSignal::AccountsChanged>(
[&]() {
if (Manager::instance().getAccountList().size() <= currentAccSize - 2) {
cv.notify_one();
}
}));
confHandlers.insert(
DRing::exportable_callback<DRing::ConfigurationSignal::AccountsChanged>([&]() {
if (Manager::instance().getAccountList().size() <= currentAccSize - 2) {
cv.notify_one();
}
}));
DRing::registerSignalHandlers(confHandlers);
Manager::instance().removeAccount(aliceId, true);
......@@ -169,35 +172,27 @@ FileTransferTest::testCachedFileTransfer()
auto aliceUri = aliceAccount->getAccountDetails()[ConfProperties::USERNAME];
std::mutex mtx;
std::unique_lock<std::mutex> lk{ mtx };
std::unique_lock<std::mutex> lk {mtx};
std::condition_variable cv;
std::condition_variable cv2;
std::map<std::string, std::shared_ptr<DRing::CallbackWrapperBase>> confHandlers;
bool transferCreated = false, transferFinished = false;
bool transferWaiting = false, transferFinished = false;
DRing::DataTransferId finalId;
// Watch signals
confHandlers.insert(DRing::exportable_callback<DRing::DataTransferSignal::DataTransferEvent>(
[&](const long unsigned int& id, int code) {
if (code == static_cast<int>(DRing::DataTransferEventCode::created)) {
transferCreated = true;
finalId = id;
cv.notify_one();
} else if (code == static_cast<int>(DRing::DataTransferEventCode::finished)) {
transferFinished = true;
finalId = id;
cv.notify_one();
}
}));
[&](const long unsigned int& id, int code) {
if (code == static_cast<int>(DRing::DataTransferEventCode::wait_host_acceptance)) {
transferWaiting = true;
finalId = id;
cv.notify_one();
} else if (code == static_cast<int>(DRing::DataTransferEventCode::finished)) {
transferFinished = true;
finalId = id;
cv.notify_one();
}
}));
DRing::registerSignalHandlers(confHandlers);
bool successfullyReceive = false;
bobAccount->connectionManager().onChannelRequest(
[&successfullyReceive, &cv](const std::string&, const std::string& name) {
successfullyReceive = name.substr(0, 7) == "file://";
cv.notify_one();
return true;
});
// Create file to send
std::ofstream sendFile("SEND");
CPPUNIT_ASSERT(sendFile.is_open());
......@@ -215,13 +210,11 @@ FileTransferTest::testCachedFileTransfer()
CPPUNIT_ASSERT(DRing::sendFile(info, id) == DRing::DataTransferError::success);
cv.wait_for(lk, std::chrono::seconds(30));
CPPUNIT_ASSERT(successfullyReceive);
cv.wait_for(lk, std::chrono::seconds(30));
CPPUNIT_ASSERT(transferCreated);
CPPUNIT_ASSERT(transferWaiting);
auto rcv_path = "RECV";
CPPUNIT_ASSERT(DRing::acceptFileTransfer(finalId, rcv_path, 0) == DRing::DataTransferError::success);
CPPUNIT_ASSERT(DRing::acceptFileTransfer(finalId, rcv_path, 0)
== DRing::DataTransferError::success);
// Wait 2 times, both sides will got a finished status
cv.wait_for(lk, std::chrono::seconds(30));
......@@ -253,35 +246,27 @@ FileTransferTest::testMultipleFileTransfer()
auto aliceUri = aliceAccount->getAccountDetails()[ConfProperties::USERNAME];
std::mutex mtx;
std::unique_lock<std::mutex> lk{ mtx };
std::unique_lock<std::mutex> lk {mtx};
std::condition_variable cv;
std::condition_variable cv2;
std::map<std::string, std::shared_ptr<DRing::CallbackWrapperBase>> confHandlers;
bool transferCreated = false, transferFinished = false;
bool transferWaiting = false, transferFinished = false;
DRing::DataTransferId finalId;
// Watch signals
confHandlers.insert(DRing::exportable_callback<DRing::DataTransferSignal::DataTransferEvent>(
[&](const long unsigned int& id, int code) {
if (code == static_cast<int>(DRing::DataTransferEventCode::created)) {
transferCreated = true;
finalId = id;
cv.notify_one();
} else if (code == static_cast<int>(DRing::DataTransferEventCode::finished)) {
transferFinished = true;
finalId = id;
cv.notify_one();
}
}));
[&](const long unsigned int& id, int code) {
if (code == static_cast<int>(DRing::DataTransferEventCode::wait_host_acceptance)) {
transferWaiting = true;
finalId = id;
cv.notify_one();
} else if (code == static_cast<int>(DRing::DataTransferEventCode::finished)) {
transferFinished = true;
finalId = id;
cv.notify_one();
}
}));
DRing::registerSignalHandlers(confHandlers);
bool successfullyReceive = false;
bobAccount->connectionManager().onChannelRequest(
[&successfullyReceive, &cv](const std::string&, const std::string& name) {
successfullyReceive = name.substr(0, 7) == "file://";
cv.notify_one();
return true;
});
// Create file to send
std::ofstream sendFile("SEND");
CPPUNIT_ASSERT(sendFile.is_open());
......@@ -303,13 +288,12 @@ FileTransferTest::testMultipleFileTransfer()
CPPUNIT_ASSERT(DRing::sendFile(info, id) == DRing::DataTransferError::success);
cv.wait_for(lk, std::chrono::seconds(30));
CPPUNIT_ASSERT(successfullyReceive);
cv.wait_for(lk, std::chrono::seconds(30));
CPPUNIT_ASSERT(transferCreated);
CPPUNIT_ASSERT(transferWaiting);
transferWaiting = false;
auto rcv_path = "RECV";
CPPUNIT_ASSERT(DRing::acceptFileTransfer(finalId, rcv_path, 0) == DRing::DataTransferError::success);
CPPUNIT_ASSERT(DRing::acceptFileTransfer(finalId, rcv_path, 0)
== DRing::DataTransferError::success);
// Wait 2 times, both sides will got a finished status
cv.wait_for(lk, std::chrono::seconds(30));
......@@ -328,13 +312,11 @@ FileTransferTest::testMultipleFileTransfer()
CPPUNIT_ASSERT(DRing::sendFile(info2, id) == DRing::DataTransferError::success);
cv.wait_for(lk, std::chrono::seconds(30));
CPPUNIT_ASSERT(successfullyReceive);
cv.wait_for(lk, std::chrono::seconds(30));
CPPUNIT_ASSERT(transferCreated);
CPPUNIT_ASSERT(transferWaiting);
rcv_path = "RECV2";
CPPUNIT_ASSERT(DRing::acceptFileTransfer(finalId, rcv_path, 0) == DRing::DataTransferError::success);
CPPUNIT_ASSERT(DRing::acceptFileTransfer(finalId, rcv_path, 0)
== DRing::DataTransferError::success);
// Wait 2 times, both sides will got a finished status
cv.wait_for(lk, std::chrono::seconds(30));
......@@ -353,6 +335,7 @@ FileTransferTest::testMultipleFileTransfer()
std::this_thread::sleep_for(std::chrono::seconds(3));
}
}} // namespace test
} // namespace test
} // namespace jami
RING_TEST_RUNNER(jami::test::FileTransferTest::name())
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment