aboutsummaryrefslogtreecommitdiff
path: root/tests/test_session.cpp
diff options
context:
space:
mode:
authorRichard van der Hoff <richard@matrix.org>2016-06-30 11:27:25 +0100
committerRichard van der Hoff <richard@matrix.org>2016-06-30 11:38:01 +0100
commit8dd3c182ee568b9121a336b106aaaceedd1cc470 (patch)
tree24560387e186d7235decfb251ce748401b585004 /tests/test_session.cpp
parent757c422578b417f5184fa9e17a32587de676b9d1 (diff)
Make space in the session pickle for chain index
Keeping track of the chain index is a useful thing to do, but is only required if we've enabled diagnostics. Extend the session pickle format to make a space for it, so that pickles can be transferred between the logging_enabled branch and the master branch without loss of information. Also add some tests for session pickling which explicitly check that we can unpickle both formats of pickle.
Diffstat (limited to 'tests/test_session.cpp')
-rw-r--r--tests/test_session.cpp144
1 files changed, 144 insertions, 0 deletions
diff --git a/tests/test_session.cpp b/tests/test_session.cpp
new file mode 100644
index 0000000..a0c1f6a
--- /dev/null
+++ b/tests/test_session.cpp
@@ -0,0 +1,144 @@
+#include "olm/session.hh"
+#include "olm/pickle_encoding.h"
+
+#include "unittest.hh"
+
+/* decode into a buffer, which is returned */
+std::uint8_t *decode_hex(
+ const char * input
+) {
+ static std::uint8_t buf[256];
+ std::uint8_t *p = buf;
+ while (*input != '\0') {
+ char high = *(input++);
+ char low = *(input++);
+ if (high >= 'a') high -= 'a' - ('9' + 1);
+ if (low >= 'a') low -= 'a' - ('9' + 1);
+ uint8_t value = ((high - '0') << 4) | (low - '0');
+ *p++ = value;
+ }
+ return buf;
+}
+
+void check_session(const olm::Session &session) {
+ assert_equals(
+ decode_hex("49d640dc96b80176694af69fc4b8ca9fac49aecbd697d01fd8bee1ed2693b6c9"),
+ session.ratchet.root_key, 32
+ );
+
+ assert_equals(
+ std::size_t(1),
+ session.ratchet.sender_chain.size()
+ );
+
+ assert_equals(
+ decode_hex("f77a03eaa9b301fa7d2a5aa6b50286906de12cc96044f526dbbcb12839ad7003"),
+ session.ratchet.sender_chain[0].ratchet_key.public_key, 32
+ );
+
+ assert_equals(
+ decode_hex("d945c6ed4c7c277117adf11fb133a7936d287afe97c0b3ac989644b4490d4f31"),
+ session.ratchet.sender_chain[0].ratchet_key.private_key, 32
+ );
+
+ assert_equals(
+ std::uint32_t(0),
+ session.ratchet.sender_chain[0].chain_key.index
+ );
+
+ assert_equals(
+ std::size_t(0),
+ session.ratchet.receiver_chains.size()
+ );
+
+ assert_equals(
+ std::size_t(0),
+ session.ratchet.skipped_message_keys.size()
+ );
+
+ assert_equals(OLM_SUCCESS, session.last_error);
+ assert_equals(false, session.received_message);
+
+ assert_equals(
+ decode_hex("7326b58623a3f7bd8da11a1bab51f432c02a7430241b326e9fc8916a21eb257e"),
+ session.alice_identity_key.public_key, 32
+ );
+
+ assert_equals(
+ decode_hex("0ab4b30bde20bd374ceccc72861660f0fd046f7516900796c3e5de41c598316c"),
+ session.alice_base_key.public_key, 32
+ );
+
+ assert_equals(
+ decode_hex("585dba930b10d90d81702c715f4085d07c42b0cd2d676010bb6086c86c4cc618"),
+ session.bob_one_time_key.public_key, 32
+ );
+}
+
+int main() {
+
+{
+ TestCase test_case("V1 session pickle");
+
+ const uint8_t *PICKLE_KEY=(uint8_t *)"secret_key";
+ uint8_t pickled[] =
+ "wkEpwMgiAqD7B1/Lw2cKYYDcUZVOd9QHes7ZroWxr/Rp/nWEAySgRsIu/a54YhO67rwitr"
+ "Lpos7tFxxK9IZ7pKB1qrR1coVWIt78V9lp9WgmBAvxHBSY+tu1lkL/JjLi963/yFdPancZ"
+ "+WHMVfaKlV3gWGpo7EfNK6qAOxI1Ea/eCsE2sYrsHEDvLLGlKAA9E56rmmoe2w6TKzsQjs"
+ "ZM2/XT2eJ82EgMO9pL02iLElXWmGNv72Ut7DouR0pQIT50HIEEKcFxYcoTb3WCfJD76Coe"
+ "sE4kx+TA6d45Xu1bwQNNkTGF+nCCu/GmKY+sECXbz9U6WhxG0YdF9Z4T8YkWYAgpKNS0FW"
+ "RV";
+ size_t pickle_len = _olm_enc_input(
+ PICKLE_KEY, strlen((char *)PICKLE_KEY),
+ pickled, strlen((char *)pickled), NULL
+ );
+
+ olm::Session session;
+ const uint8_t *unpickle_res = olm::unpickle(pickled, pickled+sizeof(pickled), session);
+ assert_equals(
+ pickle_len, (size_t)(unpickle_res - pickled)
+ );
+
+ check_session(session);
+
+#if 0
+ size_t rawlen = olm::pickle_length(session);
+ uint8_t *r1 = _olm_enc_output_pos(pickled, rawlen);
+ olm::pickle(r1, session);
+ _olm_enc_output(
+ PICKLE_KEY, strlen((char *)PICKLE_KEY),
+ pickled, rawlen);
+ printf("%s\n", pickled);
+#endif
+}
+
+{
+ TestCase test_case("V2 session pickle");
+
+ const uint8_t *PICKLE_KEY=(uint8_t *)"secret_key";
+ uint8_t pickled[1024] =
+ "XTc0/aHUZIVjq257h6sCI1f4/EFJ42QYSOp6SDn88FSXRkO9dfrxGk8gPvZiNCnXFYWggX"
+ "UkTkaiSd9MFuj6kB49COlLU1zKw3caJmEIgud7umbYrAXhaBMCTl8T3XFVJK5gXThOE3sI"
+ "QCRy4RP4zAVJMTwnvNTzobi3N0aCxEDC+RIol6cEv+bV04zs8wbivdBDVJ7WIDVBF42dzQ"
+ "qPhL4KC5TLB9FYn/eUvrn56tHa5B3udjm1dbPynkjzkRy7Mbp5zUe6XhHDesKo0VxaHhOl"
+ "t117bSOKdf72wK/bStaWTpNdAA9h0AFuj2Rgp7E42yJTHY0tv23lMH1Hd+IHdsMsgMcfvL"
+ "Si";
+
+ size_t pickle_len = _olm_enc_input(
+ PICKLE_KEY, strlen((char *)PICKLE_KEY),
+ pickled, strlen((char *)pickled), NULL
+ );
+
+ olm::Session session;
+ const uint8_t *unpickle_res = olm::unpickle(pickled, pickled+sizeof(pickled), session);
+ assert_equals(
+ pickle_len, (size_t)(unpickle_res - pickled)
+ );
+
+ check_session(session);
+}
+
+
+
+return 0;
+}