diff options
-rw-r--r-- | include/erebos/pairing.h | 14 | ||||
-rw-r--r-- | src/main.cpp | 18 | ||||
-rw-r--r-- | src/pairing.cpp | 20 | ||||
-rw-r--r-- | test/attach.test | 8 |
4 files changed, 40 insertions, 20 deletions
diff --git a/include/erebos/pairing.h b/include/erebos/pairing.h index b8b680a..936b3ac 100644 --- a/include/erebos/pairing.h +++ b/include/erebos/pairing.h @@ -34,7 +34,17 @@ public: typedef function<void(const Peer &)> RequestInitHook; void onRequestInit(RequestInitHook); - typedef function<future<bool>(const Peer &, string, future<bool> &&)> ConfirmHook; + enum class Outcome + { + Success, + PeerRejected, + UserRejected, + UnexpectedMessage, + NonceMismatch, + Stale, + }; + + typedef function<future<bool>(const Peer &, string, future<Outcome> &&)> ConfirmHook; void onResponse(ConfirmHook); void onRequest(ConfirmHook); @@ -75,7 +85,7 @@ private: StatePhase phase; vector<uint8_t> nonce; vector<uint8_t> peerCheck; - promise<bool> success; + promise<Outcome> outcome; }; map<Peer, shared_ptr<State>> peerStates; diff --git a/src/main.cpp b/src/main.cpp index 84dfe2a..cb9002e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -128,18 +128,28 @@ void createIdentity(const vector<string> & args) } } -void printAttachResult(string prefix, Peer peer, future<bool> && success) +void printPairingResult(string prefix, Peer peer, future<PairingServiceBase::Outcome> && future) { + auto outcome = future.get(); ostringstream ss; ss << prefix << - (success.get() ? "-done " : "-failed ") << + (outcome == PairingServiceBase::Outcome::Success ? "-done " : "-failed ") << getPeer(peer).id; + switch (outcome) + { + case PairingServiceBase::Outcome::Success: break; + case PairingServiceBase::Outcome::PeerRejected: ss << " rejected"; break; + case PairingServiceBase::Outcome::UserRejected: ss << " user"; break; + case PairingServiceBase::Outcome::UnexpectedMessage: ss << " unexpected"; break; + case PairingServiceBase::Outcome::NonceMismatch: ss << " nonce"; break; + case PairingServiceBase::Outcome::Stale: ss << " stale"; break; + } printLine(ss.str()); } -future<bool> confirmPairing(string prefix, const Peer & peer, string confirm, future<bool> && success) +future<bool> confirmPairing(string prefix, const Peer & peer, string confirm, future<PairingServiceBase::Outcome> && outcome) { - thread(printAttachResult, prefix, peer, move(success)).detach(); + thread(printPairingResult, prefix, peer, move(outcome)).detach(); promise<bool> promise; auto input = promise.get_future(); diff --git a/src/pairing.cpp b/src/pairing.cpp index 91e0b0f..babe886 100644 --- a/src/pairing.cpp +++ b/src/pairing.cpp @@ -29,7 +29,7 @@ PairingServiceBase::~PairingServiceBase() scoped_lock lock(state->lock); if (state->phase != StatePhase::PairingDone && state->phase != StatePhase::PairingFailed) { - state->success.set_value(false); + state->outcome.set_value(Outcome::Stale); state->phase = StatePhase::PairingFailed; } } @@ -124,7 +124,7 @@ void PairingServiceBase::handle(Context & ctx) requestNonceFailedHook(ctx.peer()); if (state->phase < StatePhase::PairingDone) { state->phase = StatePhase::PairingFailed; - state->success.set_value(false); + state->outcome.set_value(Outcome::NonceMismatch); } return; } @@ -143,7 +143,7 @@ void PairingServiceBase::handle(Context & ctx) else if (auto reject = rec->item("reject").asText()) { if (state->phase < StatePhase::PairingDone) { state->phase = StatePhase::PairingFailed; - state->success.set_value(false); + state->outcome.set_value(Outcome::PeerRejected); } } @@ -151,7 +151,7 @@ void PairingServiceBase::handle(Context & ctx) if (state->phase == StatePhase::OurRequestReady) { handlePairingResult(ctx); state->phase = StatePhase::PairingDone; - state->success.set_value(true); + state->outcome.set_value(Outcome::Success); } else { result = ctx.ref(); } @@ -211,16 +211,16 @@ string PairingServiceBase::confirmationNumber(const vector<uint8_t> & digest) void PairingServiceBase::waitForConfirmation(Peer peer, weak_ptr<State> wstate, string confirm, ConfirmHook hook) { - future<bool> success; + future<Outcome> outcome; if (auto state = wstate.lock()) { - success = state->success.get_future(); + outcome = state->outcome.get_future(); } else { return; } bool ok; try { - ok = hook(peer, confirm, std::move(success)).get(); + ok = hook(peer, confirm, std::move(outcome)).get(); } catch (const std::future_error & e) { if (e.code() == std::future_errc::broken_promise) @@ -249,20 +249,20 @@ void PairingServiceBase::waitForConfirmation(Peer peer, weak_ptr<State> wstate, return ctx.local(); }); state->phase = StatePhase::PairingDone; - state->success.set_value(true); + state->outcome.set_value(Outcome::Success); } else { state->phase = StatePhase::OurRequestReady; } } else if (state->phase == StatePhase::PeerRequestConfirm) { peer.send(uuid(), handlePairingCompleteRef(peer)); state->phase = StatePhase::PairingDone; - state->success.set_value(true); + state->outcome.set_value(Outcome::Success); } } else { if (state->phase != StatePhase::PairingFailed) { peer.send(uuid(), Object(Record({{ "reject", string() }}))); state->phase = StatePhase::PairingFailed; - state->success.set_value(false); + state->outcome.set_value(Outcome::UserRejected); } } } diff --git a/test/attach.test b/test/attach.test index bfd8ba1..3b202e6 100644 --- a/test/attach.test +++ b/test/attach.test @@ -20,15 +20,15 @@ test: expect /attach-request 1 .*/ from p1 expect /attach-response 1 .*/ from p2 send "attach-reject 1" to p1 - expect /attach-request-failed 1/ from p1 - expect /attach-response-failed 1/ from p2 + expect /attach-request-failed 1 user/ from p1 + expect /attach-response-failed 1 rejected/ from p2 send "attach-to 1" to p2 expect /attach-request 1 .*/ from p1 expect /attach-response 1 .*/ from p2 send "attach-reject 1" to p2 - expect /attach-request-failed 1/ from p1 - expect /attach-response-failed 1/ from p2 + expect /attach-request-failed 1 rejected/ from p1 + expect /attach-response-failed 1 user/ from p2 send "attach-to 1" to p2 expect /attach-request 1 ([0-9]*)/ from p1 capture code1 |