mirror of
https://github.com/savoirfairelinux/opendht.git
synced 2026-11-08 04:21:51 +00:00
106 lines
3.0 KiB
C++
106 lines
3.0 KiB
C++
/*
|
|
* Copyright (C) 2019 Savoir-faire Linux Inc.
|
|
*
|
|
* Author: Mingrui Zhang <mingrui.zhang@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 "peerdiscoverytester.h"
|
|
#include "opendht/value.h"
|
|
|
|
namespace test {
|
|
|
|
class NodeInsertion{
|
|
public:
|
|
dht::InfoHash nodeid_;
|
|
in_port_t node_port_;
|
|
dht::NetId nid_;
|
|
MSGPACK_DEFINE(nodeid_, node_port_, nid_)
|
|
};
|
|
|
|
class TestPack{
|
|
public:
|
|
int num;
|
|
char cha;
|
|
std::string str;
|
|
MSGPACK_DEFINE(num, cha, str)
|
|
};
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(PeerDiscoveryTester);
|
|
|
|
void PeerDiscoveryTester::setUp(){}
|
|
|
|
void PeerDiscoveryTester::testTransmission(){
|
|
|
|
// Node for getnode id
|
|
const std::string type {"dht"};
|
|
const std::string test_type {"pdd"};
|
|
dht::InfoHash data_n = dht::InfoHash::get("applepin");
|
|
int port = 2222;
|
|
in_port_t port_n = 50000;
|
|
|
|
msgpack::sbuffer sbuf;
|
|
NodeInsertion adc;
|
|
adc.nid_ = 10;
|
|
adc.node_port_ = port_n;
|
|
adc.nodeid_ = data_n;
|
|
msgpack::pack(sbuf,adc);
|
|
|
|
msgpack::sbuffer pbuf;
|
|
TestPack pdd;
|
|
pdd.num = 100;
|
|
pdd.cha = 'a';
|
|
pdd.str = "apple";
|
|
msgpack::pack(pbuf,pdd);
|
|
|
|
try{
|
|
dht::PeerDiscovery test_n(port);
|
|
dht::PeerDiscovery test_s(port);
|
|
try{
|
|
test_s.startDiscovery(type,[&](msgpack::object&& obj, dht::SockAddr&& add){
|
|
auto v = obj.as<NodeInsertion>();
|
|
CPPUNIT_ASSERT_EQUAL(v.node_port_, port_n);
|
|
CPPUNIT_ASSERT_EQUAL(v.nodeid_, data_n);
|
|
});
|
|
|
|
test_s.startDiscovery(test_type,[&](msgpack::object&& obj, dht::SockAddr&& add){
|
|
auto v = obj.as<TestPack>();
|
|
CPPUNIT_ASSERT_EQUAL(v.num, 100);
|
|
CPPUNIT_ASSERT_EQUAL(v.cha, 'a');
|
|
});
|
|
|
|
test_n.startPublish(type, sbuf);
|
|
std::this_thread::sleep_for(std::chrono::seconds(5));
|
|
test_n.startPublish(test_type, pbuf);
|
|
std::this_thread::sleep_for(std::chrono::seconds(5));
|
|
test_n.stopPublish(test_type);
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(10));
|
|
test_n.stop();
|
|
test_s.stop();
|
|
test_n.join();
|
|
test_s.join();
|
|
} catch(std::exception &exception){
|
|
perror(exception.what());
|
|
CPPUNIT_ASSERT(false);
|
|
}
|
|
} catch(std::exception &exception){
|
|
perror(exception.what());
|
|
}
|
|
}
|
|
|
|
void PeerDiscoveryTester::tearDown(){}
|
|
|
|
} // namespace test
|