Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more Secure Session tests (#376)
* Reorder method declarations in SecureSessionTransport Move the required method get_public_key_for_id() first in the trait definition. This has no semantic meaning but it's important for IDE autocomplete or example, rustdoc also uses this order, and this is the order which is most likely to be used by users when defining transport implementations. * Add Debug, Clone, Copy impls to SecureSessionState Every public struct or enum should have a Debug implementation for logging whatnot, so add it. It is also sometimes useful to copy the state in the state reporting callback, so add Clone and Copy impls. This enum is likely to stay simple so copying is cheap. * Add MockTransport for testing Secure Session Rust does not have an established popular mocking framework, and mock objects are generally frowned upon in tests. However, sometimes they are useful, and the current practice is to roll out your own mocks. Introduce a simple implementation of SecureSessionTransport based on closures. This allows to reimplement and change behavior at run-time. * Replace DummyTransport with MockTransport Its functionality can be easily reimplemented with a simple utility that sets the get_public_key_for_id callback to recognize a single peer and return its public key. Drop the comments about RSA keys. It is now impossible to pass RSA keys instead of ECDSA to Secure Session. * Replace ChannelTransport with MockTransport Again, we can easily reimplement ad-hoc ChannelTransport with a similar ad-hoc function that sets up the same communication channel between two Secure Sessions. The test code is now a bit easier to follow with this change. * Test failures in get_public_key_for_id Both Secure Sessions should fail at connection negotiation stage with ErrorKind::SessionGetPublicKeyForIdError if any of them does not recognize its peer by an ID. * Test reporting connection state in state_changed callback Check that state changes are reported at correct moment. We use channels to transport state reports out of a closure. It's a bit tricky to do otherwise to satisfy the borrow checker. * Test failures in send/receive callbacks Check that errors reported from send_data and receive_data callbacks are correctly forwarded to the user (and that they actually cause connection failure). We use yet another tricky setup with channels to override the behavior of MockTransport. The channels are used to pass one-shot closures that describe the behavior of the next call to callback. The callbacks then revert to their previous behavior if there are no pending 'behavior overrides'. This can be used to test multiple failures in the future. * Test empty message handling Secure Session cannot send or receive empty messages for security reasons because empty message does not contain any authentication information. Verify that Secure Session reports errors when the user tries to send and empty message or when the transport layer returns an empty message.
- Loading branch information