aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2019-06-28 11:32:10 -0400
committerDeterminant <[email protected]>2019-06-28 11:32:10 -0400
commitd28bf1b0c8baec3c5ab40cfb988ff974f98da439 (patch)
treec5887855dfe221e1f3aa61f49ebdaf4611b4985d /test
parentf72f83d97356f592f2dee2024676aab30829c8e7 (diff)
update test programs
Diffstat (limited to 'test')
-rw-r--r--test/test_msgnet.cpp11
-rw-r--r--test/test_msgnet_tls.cpp27
-rw-r--r--test/test_p2p.cpp12
-rw-r--r--test/test_p2p_tls.cpp32
4 files changed, 52 insertions, 30 deletions
diff --git a/test/test_msgnet.cpp b/test/test_msgnet.cpp
index f6dbe1b..5578031 100644
--- a/test/test_msgnet.cpp
+++ b/test/test_msgnet.cpp
@@ -101,7 +101,7 @@ struct MyNet: public MsgNetworkByteOp {
salticidae::static_pointer_cast<Conn>(conn));
}
else
- printf("[%s] Accepted, waiting for greetings.\n",
+ printf("[%s] accepted, waiting for greetings.\n",
this->name.c_str());
}
else
@@ -115,15 +115,12 @@ struct MyNet: public MsgNetworkByteOp {
}
void on_receive_hello(MsgHello &&msg, const MyNet::conn_t &conn) {
- printf("[%s] %s says %s\n",
- name.c_str(),
- msg.name.c_str(), msg.text.c_str());
+ printf("[%s] %s says %s\n", name.c_str(), msg.name.c_str(), msg.text.c_str());
/* send acknowledgement */
send_msg(MsgAck(), conn);
}
};
-
void on_receive_ack(MsgAck &&msg, const MyNet::conn_t &conn) {
auto net = static_cast<MyNet *>(conn->get_net());
printf("[%s] the peer knows\n", net->name.c_str());
@@ -135,8 +132,8 @@ int main() {
NetAddr bob_addr("127.0.0.1:12346");
/* test two nodes in the same main loop */
- MyNet alice(ec, "Alice", bob_addr);
- MyNet bob(ec, "Bob", alice_addr);
+ MyNet alice(ec, "alice", bob_addr);
+ MyNet bob(ec, "bob", alice_addr);
/* message handler could be a normal function */
alice.reg_handler(on_receive_ack);
diff --git a/test/test_msgnet_tls.cpp b/test/test_msgnet_tls.cpp
index 9c82234..b839497 100644
--- a/test/test_msgnet_tls.cpp
+++ b/test/test_msgnet_tls.cpp
@@ -77,12 +77,12 @@ using MsgNetworkByteOp = MsgNetwork<uint8_t>;
struct MyNet: public MsgNetworkByteOp {
const std::string name;
- const salticidae::uint256_t peer_footprint;
+ const salticidae::uint256_t peer_fingerprint;
const NetAddr peer;
MyNet(const salticidae::EventContext &ec,
const std::string &name,
- const std::string &peer_footprint_hex,
+ const std::string &peer_fingerprint_hex,
const NetAddr &peer):
MsgNetwork<uint8_t>(ec, MsgNetwork::Config(
ConnPool::Config()
@@ -91,7 +91,7 @@ struct MyNet: public MsgNetworkByteOp {
.tls_key_file(name + ".pem")
)),
name(name),
- peer_footprint(salticidae::from_hex(peer_footprint_hex)),
+ peer_fingerprint(salticidae::from_hex(peer_fingerprint_hex)),
peer(peer) {
/* message handler could be a bound method */
reg_handler(
@@ -101,8 +101,8 @@ struct MyNet: public MsgNetworkByteOp {
bool res = true;
if (connected)
{
- auto cert_der = salticidae::get_hash(conn->get_peer_cert()->get_der());
- res = peer_footprint == cert_der;
+ auto cert_hash = salticidae::get_hash(conn->get_peer_cert()->get_der());
+ res = peer_fingerprint == cert_hash;
if (conn->get_mode() == ConnPool::Conn::ACTIVE)
{
printf("[%s] Connected, sending hello.\n",
@@ -112,12 +112,10 @@ struct MyNet: public MsgNetworkByteOp {
salticidae::static_pointer_cast<Conn>(conn));
}
else
- {
- printf("[%s] Accepted, waiting for greetings.\n"
- "The peer certificate footprint is %s (%s).\n",
- this->name.c_str(), salticidae::get_hex(cert_der).c_str(),
+ printf("[%s] accepted, waiting for greetings.\n"
+ "The peer certificate fingerprint is %s (%s).\n",
+ this->name.c_str(), salticidae::get_hex(cert_hash).c_str(),
res ? "ok" : "fail");
- }
}
else
{
@@ -130,15 +128,12 @@ struct MyNet: public MsgNetworkByteOp {
}
void on_receive_hello(MsgHello &&msg, const MyNet::conn_t &conn) {
- printf("[%s] %s says %s\n",
- name.c_str(),
- msg.name.c_str(), msg.text.c_str());
+ printf("[%s] %s says %s\n", name.c_str(), msg.name.c_str(), msg.text.c_str());
/* send acknowledgement */
send_msg(MsgAck(), conn);
}
};
-
void on_receive_ack(MsgAck &&msg, const MyNet::conn_t &conn) {
auto net = static_cast<MyNet *>(conn->get_net());
printf("[%s] the peer knows\n", net->name.c_str());
@@ -166,8 +161,8 @@ int main() {
bob.listen(bob_addr);
/* try to connect once */
- alice.connect(bob_addr);
- bob.connect(alice_addr);
+ alice.connect(bob_addr, false);
+ bob.connect(alice_addr, false);
/* the main loop can be shutdown by ctrl-c or kill */
auto shutdown = [&](int) {ec.stop();};
diff --git a/test/test_p2p.cpp b/test/test_p2p.cpp
index 7f80f85..5fc930a 100644
--- a/test/test_p2p.cpp
+++ b/test/test_p2p.cpp
@@ -27,6 +27,7 @@
#include <string>
#include <functional>
#include <unordered_map>
+#include <unistd.h>
#include "salticidae/msg.h"
#include "salticidae/event.h"
@@ -260,6 +261,12 @@ int main(int argc, char **argv) {
it->second->net->send_msg(MsgText(id, buff), it2->second->listen_addr);
};
+ auto cmd_sleep = [](char *buff) {
+ int sec = read_int(buff);
+ if (sec < 0) return;
+ sleep(sec);
+ };
+
auto cmd_help = [](char *) {
fprintf(stdout,
"add <node-id> <port> -- start a node (create a PeerNetwork instance)\n"
@@ -268,6 +275,7 @@ int main(int argc, char **argv) {
"del <node-id> -- remove a node (destroy a PeerNetwork instance)\n"
"msg <node-id> <peer-id> <msg> -- send a text message to a node\n"
"ls -- list all node ids\n"
+ "sleep <sec> -- wait for some seconds\n"
"exit -- quit the program\n"
"help -- show this info\n"
);
@@ -279,12 +287,14 @@ int main(int argc, char **argv) {
cmd_map.insert(std::make_pair("delpeer", cmd_delpeer));
cmd_map.insert(std::make_pair("msg", cmd_msg));
cmd_map.insert(std::make_pair("ls", cmd_ls));
+ cmd_map.insert(std::make_pair("sleep", cmd_sleep));
cmd_map.insert(std::make_pair("exit", cmd_exit));
cmd_map.insert(std::make_pair("help", cmd_help));
+ bool is_tty = isatty(0);
for (;;)
{
- fprintf(stdout, "> ");
+ if (is_tty) fprintf(stdout, "> ");
char buff[128];
if (scanf("%64s", buff) == EOF) break;
auto it = cmd_map.find(buff);
diff --git a/test/test_p2p_tls.cpp b/test/test_p2p_tls.cpp
index 9fe0aec..fa0487d 100644
--- a/test/test_p2p_tls.cpp
+++ b/test/test_p2p_tls.cpp
@@ -27,6 +27,8 @@
#include <string>
#include <functional>
#include <unordered_map>
+#include <unordered_set>
+#include <unistd.h>
#include "salticidae/msg.h"
#include "salticidae/event.h"
@@ -67,6 +69,8 @@ struct MsgText {
const uint8_t MsgText::opcode;
+std::unordered_set<uint256_t> valid_certs;
+
struct Net {
uint64_t id;
EventContext ec;
@@ -74,12 +78,15 @@ struct Net {
std::thread th;
PeerNetwork *net;
const std::string listen_addr;
+ uint256_t cert;
Net(uint64_t id, uint16_t port): id(id), tc(ec), listen_addr("127.0.0.1:"+ std::to_string(port)) {
auto tls_key = new PKey(PKey::create_privkey_rsa(2048));
auto tls_cert = new salticidae::X509(salticidae::X509::create_self_signed_from_pubkey(*tls_key));
tls_key->save_privkey_to_file(std::to_string(port) + "_pkey.pem");
tls_cert->save_to_file(std::to_string(port) + ".pem");
+ cert = salticidae::get_hash(tls_cert->get_der());
+ valid_certs.insert(cert);
net = new PeerNetwork(ec, PeerNetwork::Config(salticidae::ConnPool::Config()
.enable_tls(true)
.tls_key(tls_key)
@@ -93,8 +100,8 @@ struct Net {
net->reg_conn_handler([this](const salticidae::ConnPool::conn_t &conn, bool connected) {
if (connected)
{
- fprintf(stdout, "net %lu: peer's cert is %s\n", this->id,
- salticidae::get_hash(conn->get_peer_cert()->get_der()).to_hex().c_str());
+ auto cert_hash = salticidae::get_hash(conn->get_peer_cert()->get_der());
+ return valid_certs.count(cert_hash) > 0;
}
return true;
});
@@ -107,9 +114,10 @@ struct Net {
}
});
net->reg_peer_handler([this](const PeerNetwork::conn_t &conn, bool connected) {
- fprintf(stdout, "net %lu: %s peer %s\n", this->id,
+ fprintf(stdout, "net %lu: %s peer %s (cert: %s)\n", this->id,
connected ? "connected to" : "disconnected from",
- std::string(conn->get_peer_addr()).c_str());
+ std::string(conn->get_peer_addr()).c_str(),
+ salticidae::get_hash(conn->get_peer_cert()->get_der()).to_hex().c_str());
});
net->reg_unknown_peer_handler([this](const NetAddr &claimed_addr) {
fprintf(stdout, "net %lu: unknown peer %s attempts to connnect\n",
@@ -149,7 +157,10 @@ struct Net {
th.join();
}
- ~Net() { delete net; }
+ ~Net() {
+ valid_certs.erase(cert);
+ delete net;
+ }
};
std::unordered_map<uint64_t, Net *> nets;
@@ -279,6 +290,12 @@ int main(int argc, char **argv) {
it->second->net->send_msg(MsgText(id, buff), it2->second->listen_addr);
};
+ auto cmd_sleep = [](char *buff) {
+ int sec = read_int(buff);
+ if (sec < 0) return;
+ sleep(sec);
+ };
+
auto cmd_help = [](char *) {
fprintf(stdout,
"add <node-id> <port> -- start a node (create a PeerNetwork instance)\n"
@@ -287,6 +304,7 @@ int main(int argc, char **argv) {
"del <node-id> -- remove a node (destroy a PeerNetwork instance)\n"
"msg <node-id> <peer-id> <msg> -- send a text message to a node\n"
"ls -- list all node ids\n"
+ "sleep <sec> -- wait for some seconds\n"
"exit -- quit the program\n"
"help -- show this info\n"
);
@@ -298,12 +316,14 @@ int main(int argc, char **argv) {
cmd_map.insert(std::make_pair("delpeer", cmd_delpeer));
cmd_map.insert(std::make_pair("msg", cmd_msg));
cmd_map.insert(std::make_pair("ls", cmd_ls));
+ cmd_map.insert(std::make_pair("sleep", cmd_sleep));
cmd_map.insert(std::make_pair("exit", cmd_exit));
cmd_map.insert(std::make_pair("help", cmd_help));
+ bool is_tty = isatty(0);
for (;;)
{
- fprintf(stdout, "> ");
+ if (is_tty) fprintf(stdout, "> ");
char buff[128];
if (scanf("%64s", buff) == EOF) break;
auto it = cmd_map.find(buff);