diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/CMakeLists.txt | 47 | ||||
-rw-r--r-- | tests/include/unittest.hh | 107 | ||||
-rw-r--r-- | tests/test_base64.cpp | 70 | ||||
-rw-r--r-- | tests/test_crypto.cpp | 252 | ||||
-rw-r--r-- | tests/test_group_session.cpp | 327 | ||||
-rw-r--r-- | tests/test_list.cpp | 92 | ||||
-rw-r--r-- | tests/test_megolm.cpp | 134 | ||||
-rw-r--r-- | tests/test_message.cpp | 112 | ||||
-rw-r--r-- | tests/test_olm.cpp | 399 | ||||
-rw-r--r-- | tests/test_olm_decrypt.cpp | 92 | ||||
-rw-r--r-- | tests/test_olm_sha256.cpp | 22 | ||||
-rw-r--r-- | tests/test_olm_signature.cpp | 91 | ||||
-rw-r--r-- | tests/test_olm_using_malloc.cpp | 210 | ||||
-rw-r--r-- | tests/test_pk.cpp | 236 | ||||
-rw-r--r-- | tests/test_ratchet.cpp | 222 | ||||
-rw-r--r-- | tests/test_sas.cpp | 118 | ||||
-rw-r--r-- | tests/test_session.cpp | 144 |
17 files changed, 0 insertions, 2675 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt deleted file mode 100644 index 332da12..0000000 --- a/tests/CMakeLists.txt +++ /dev/null @@ -1,47 +0,0 @@ -enable_testing() - -set(TEST_LIST - test_base64 - test_crypto - test_group_session - test_list - test_megolm - test_message - test_olm - test_olm_decrypt - test_olm_sha256 - test_olm_signature - test_olm_using_malloc - test_session - test_pk - test_sas - ) - -if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Windows" AND BUILD_SHARED_LIBS)) - # test_ratchet doesn't work on Windows when building a DLL, because it tries - # to use internal symbols, so only enable it if we're not on Windows, or if - # we're building statically - set(TEST_LIST ${TEST_LIST} test_ratchet) - add_test(Ratchet test_ratchet) -endif() - -foreach(test IN ITEMS ${TEST_LIST}) -add_executable(${test} ${test}.cpp) -target_include_directories(${test} PRIVATE include) -target_link_libraries(${test} Olm::Olm) -endforeach(test) - -add_test(Base64 test_base64) -add_test(Crypto test_crypto) -add_test(GroupSession test_group_session) -add_test(List test_list) -add_test(Megolm test_megolm) -add_test(Message test_message) -add_test(Olm test_olm) -add_test(OlmDecrypt test_olm_decrypt) -add_test(OlmSha256 test_olm_sha256) -add_test(OlmSignature test_olm_signature) -add_test(OlmUsingMalloc test_olm_using_malloc) -add_test(Session test_session) -add_test(PublicKey test_session) -add_test(SAS test_sas) diff --git a/tests/include/unittest.hh b/tests/include/unittest.hh deleted file mode 100644 index 3ce4f38..0000000 --- a/tests/include/unittest.hh +++ /dev/null @@ -1,107 +0,0 @@ -/* Copyright 2015 OpenMarket Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include <cstring> -#include <iostream> -#include <iomanip> -#include <cstdlib> -#include <string> - -std::ostream & print_hex( - std::ostream & os, - std::uint8_t const * data, - std::size_t length -) { - for (std::size_t i = 0; i < length; i++) { - os << std::setw(2) << std::setfill('0') << std::right - << std::hex << (int) data[i]; - } - return os; -} - - -char const * TEST_CASE; - - -template<typename T> -void assert_equals( - const char *file, - unsigned line, - const char *expected_expr, - const char *actual_expr, - T const & expected, - T const & actual -) { - if (expected != actual) { - std::cout << "FAILED: " << TEST_CASE << std::endl; - std::cout << file << ":" << line << std::endl; - std::cout << expected_expr << " == " << actual_expr << std::endl; - std::cout << "Expected: " << expected << std::endl; - std::cout << "Actual: " << actual << std::endl; - std::exit(1); - } -} - -template<typename T> -void assert_not_equals( - const char *file, - unsigned line, - const char *expected_expr, - const char *actual_expr, - T const & expected, - T const & actual -) { - if (expected == actual) { - std::cout << "FAILED: " << TEST_CASE << std::endl; - std::cout << file << ":" << line << std::endl; - std::cout << expected_expr << " == " << actual_expr << std::endl; - std::cout << "Unexpected: " << expected << std::endl; - std::cout << "Actual: " << actual << std::endl; - std::exit(1); - } -} - - -void assert_equals( - const char *file, - unsigned line, - const char *expected_expr, - const char *actual_expr, - std::uint8_t const * expected, - std::uint8_t const * actual, - std::size_t length -) { - if (std::memcmp(expected, actual, length)) { - std::cout << "FAILED: " << TEST_CASE << std::endl; - std::cout << file << ":" << line << std::endl; - std::cout << expected_expr << " == " << actual_expr << std::endl; - print_hex(std::cout << "Expected: ", expected, length) << std::endl; - print_hex(std::cout << "Actual: ", actual, length) << std::endl; - std::exit(1); - } -} - -#define assert_equals(expected, actual, ...) assert_equals( \ - __FILE__, __LINE__, #expected, #actual, expected, actual, ##__VA_ARGS__ \ -) - -#define assert_not_equals(expected, actual, ...) assert_not_equals( \ - __FILE__, __LINE__, #expected, #actual, expected, actual, ##__VA_ARGS__ \ -) - -class TestCase { -public: - TestCase(const char *name) { TEST_CASE = name; } - ~TestCase() { std::cout << "PASSED: " << TEST_CASE << std::endl; } -}; diff --git a/tests/test_base64.cpp b/tests/test_base64.cpp deleted file mode 100644 index 6f80acf..0000000 --- a/tests/test_base64.cpp +++ /dev/null @@ -1,70 +0,0 @@ -#include "olm/base64.hh" -#include "olm/base64.h" -#include "unittest.hh" - -int main() { - -{ /* Base64 encode test */ -TestCase test_case("Base64 C++ binding encode test"); - -std::uint8_t input[] = "Hello World"; -std::uint8_t expected_output[] = "SGVsbG8gV29ybGQ"; -std::size_t input_length = sizeof(input) - 1; - -std::size_t output_length = olm::encode_base64_length(input_length); -assert_equals(std::size_t(15), output_length); - -std::uint8_t output[15]; -olm::encode_base64(input, input_length, output); -assert_equals(expected_output, output, output_length); -} - -{ -TestCase test_case("Base64 C binding encode test"); - -std::uint8_t input[] = "Hello World"; -std::uint8_t expected_output[] = "SGVsbG8gV29ybGQ"; -std::size_t input_length = sizeof(input) - 1; - -std::size_t output_length = ::_olm_encode_base64_length(input_length); -assert_equals(std::size_t(15), output_length); - -std::uint8_t output[15]; -output_length = ::_olm_encode_base64(input, input_length, output); -assert_equals(std::size_t(15), output_length); -assert_equals(expected_output, output, output_length); -} - -{ /* Base64 decode test */ -TestCase test_case("Base64 C++ binding decode test"); - -std::uint8_t input[] = "SGVsbG8gV29ybGQ"; -std::uint8_t expected_output[] = "Hello World"; -std::size_t input_length = sizeof(input) - 1; - -std::size_t output_length = olm::decode_base64_length(input_length); -assert_equals(std::size_t(11), output_length); - -std::uint8_t output[11]; -olm::decode_base64(input, input_length, output); -assert_equals(expected_output, output, output_length); -} - -{ -TestCase test_case("Base64 C binding decode test"); - -std::uint8_t input[] = "SGVsbG8gV29ybGQ"; -std::uint8_t expected_output[] = "Hello World"; -std::size_t input_length = sizeof(input) - 1; - -std::size_t output_length = ::_olm_decode_base64_length(input_length); -assert_equals(std::size_t(11), output_length); - -std::uint8_t output[11]; -output_length = ::_olm_decode_base64(input, input_length, output); -assert_equals(std::size_t(11), output_length); -assert_equals(expected_output, output, output_length); -} - - -} diff --git a/tests/test_crypto.cpp b/tests/test_crypto.cpp deleted file mode 100644 index 5da742c..0000000 --- a/tests/test_crypto.cpp +++ /dev/null @@ -1,252 +0,0 @@ -/* Copyright 2015 OpenMarket Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "olm/crypto.h" - -#include "unittest.hh" - -int main() { - - -{ /* Curve25529 Test Case 1 */ - -TestCase test_case("Curve25529 Test Case 1"); - -std::uint8_t alice_private[32] = { - 0x77, 0x07, 0x6D, 0x0A, 0x73, 0x18, 0xA5, 0x7D, - 0x3C, 0x16, 0xC1, 0x72, 0x51, 0xB2, 0x66, 0x45, - 0xDF, 0x4C, 0x2F, 0x87, 0xEB, 0xC0, 0x99, 0x2A, - 0xB1, 0x77, 0xFB, 0xA5, 0x1D, 0xB9, 0x2C, 0x2A -}; - -std::uint8_t alice_public[32] = { - 0x85, 0x20, 0xF0, 0x09, 0x89, 0x30, 0xA7, 0x54, - 0x74, 0x8B, 0x7D, 0xDC, 0xB4, 0x3E, 0xF7, 0x5A, - 0x0D, 0xBF, 0x3A, 0x0D, 0x26, 0x38, 0x1A, 0xF4, - 0xEB, 0xA4, 0xA9, 0x8E, 0xAA, 0x9B, 0x4E, 0x6A -}; - -std::uint8_t bob_private[32] = { - 0x5D, 0xAB, 0x08, 0x7E, 0x62, 0x4A, 0x8A, 0x4B, - 0x79, 0xE1, 0x7F, 0x8B, 0x83, 0x80, 0x0E, 0xE6, - 0x6F, 0x3B, 0xB1, 0x29, 0x26, 0x18, 0xB6, 0xFD, - 0x1C, 0x2F, 0x8B, 0x27, 0xFF, 0x88, 0xE0, 0xEB -}; - -std::uint8_t bob_public[32] = { - 0xDE, 0x9E, 0xDB, 0x7D, 0x7B, 0x7D, 0xC1, 0xB4, - 0xD3, 0x5B, 0x61, 0xC2, 0xEC, 0xE4, 0x35, 0x37, - 0x3F, 0x83, 0x43, 0xC8, 0x5B, 0x78, 0x67, 0x4D, - 0xAD, 0xFC, 0x7E, 0x14, 0x6F, 0x88, 0x2B, 0x4F -}; - -std::uint8_t expected_agreement[32] = { - 0x4A, 0x5D, 0x9D, 0x5B, 0xA4, 0xCE, 0x2D, 0xE1, - 0x72, 0x8E, 0x3B, 0xF4, 0x80, 0x35, 0x0F, 0x25, - 0xE0, 0x7E, 0x21, 0xC9, 0x47, 0xD1, 0x9E, 0x33, - 0x76, 0xF0, 0x9B, 0x3C, 0x1E, 0x16, 0x17, 0x42 -}; - -_olm_curve25519_key_pair alice_pair; -_olm_crypto_curve25519_generate_key(alice_private, &alice_pair); - -assert_equals(alice_private, alice_pair.private_key.private_key, 32); -assert_equals(alice_public, alice_pair.public_key.public_key, 32); - -_olm_curve25519_key_pair bob_pair; -_olm_crypto_curve25519_generate_key(bob_private, &bob_pair); - -assert_equals(bob_private, bob_pair.private_key.private_key, 32); -assert_equals(bob_public, bob_pair.public_key.public_key, 32); - -std::uint8_t actual_agreement[CURVE25519_SHARED_SECRET_LENGTH] = {}; - -_olm_crypto_curve25519_shared_secret(&alice_pair, &bob_pair.public_key, actual_agreement); - -assert_equals(expected_agreement, actual_agreement, 32); - -_olm_crypto_curve25519_shared_secret(&bob_pair, &alice_pair.public_key, actual_agreement); - -assert_equals(expected_agreement, actual_agreement, 32); - -} /* Curve25529 Test Case 1 */ - - -{ -TestCase test_case("Ed25519 Signature Test Case 1"); -std::uint8_t private_key[33] = "This key is a string of 32 bytes"; - -std::uint8_t message[] = "Hello, World"; -std::size_t message_length = sizeof(message) - 1; - -_olm_ed25519_key_pair key_pair; -_olm_crypto_ed25519_generate_key(private_key, &key_pair); - -std::uint8_t signature[64]; -_olm_crypto_ed25519_sign( - &key_pair, message, message_length, signature -); - -bool result = _olm_crypto_ed25519_verify( - &key_pair.public_key, message, message_length, signature -); -assert_equals(true, result); - -message[0] = 'n'; -result = _olm_crypto_ed25519_verify( - &key_pair.public_key, message, message_length, signature -); -assert_equals(false, result); -} - - -{ /* AES Test Case 1 */ - -TestCase test_case("AES Test Case 1"); - -_olm_aes256_key key = {}; -_olm_aes256_iv iv = {}; -std::uint8_t input[16] = {}; - -std::uint8_t expected[32] = { - 0xDC, 0x95, 0xC0, 0x78, 0xA2, 0x40, 0x89, 0x89, - 0xAD, 0x48, 0xA2, 0x14, 0x92, 0x84, 0x20, 0x87, - 0xF3, 0xC0, 0x03, 0xDD, 0xC4, 0xA7, 0xB8, 0xA9, - 0x4B, 0xAE, 0xDF, 0xFC, 0x3D, 0x21, 0x4C, 0x38 -}; - -std::size_t length = _olm_crypto_aes_encrypt_cbc_length(sizeof(input)); -assert_equals(std::size_t(32), length); - - -std::uint8_t actual[32] = {}; - -_olm_crypto_aes_encrypt_cbc(&key, &iv, input, sizeof(input), actual); -assert_equals(expected, actual, 32); - -length = _olm_crypto_aes_decrypt_cbc(&key, &iv, expected, sizeof(expected), actual); -assert_equals(std::size_t(16), length); -assert_equals(input, actual, length); - -} /* AES Test Case 1 */ - - -{ /* SHA 256 Test Case 1 */ - -TestCase test_case("SHA 256 Test Case 1"); - -// we want to take the hash of the empty string, but MSVC doesn't like -// allocating 0 bytes, so allocate one item, but pass a length of zero to -// sha256 -std::uint8_t input[1] = {0}; - -std::uint8_t expected[32] = { - 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14, - 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24, - 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C, - 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55 -}; - -std::uint8_t actual[32]; - -_olm_crypto_sha256(input, 0, actual); - -assert_equals(expected, actual, 32); - -} /* SHA 256 Test Case 1 */ - -{ /* HMAC Test Case 1 */ - -TestCase test_case("HMAC Test Case 1"); - -// we want to take the hash of the empty string, but MSVC doesn't like -// allocating 0 bytes, so allocate one item, but pass a length of zero to -// hmac_sha256 -std::uint8_t input[1] = {0}; - -std::uint8_t expected[32] = { - 0xb6, 0x13, 0x67, 0x9a, 0x08, 0x14, 0xd9, 0xec, - 0x77, 0x2f, 0x95, 0xd7, 0x78, 0xc3, 0x5f, 0xc5, - 0xff, 0x16, 0x97, 0xc4, 0x93, 0x71, 0x56, 0x53, - 0xc6, 0xc7, 0x12, 0x14, 0x42, 0x92, 0xc5, 0xad -}; - -std::uint8_t actual[32]; - -_olm_crypto_hmac_sha256(input, 0, input, 0, actual); - -assert_equals(expected, actual, 32); - -} /* HMAC Test Case 1 */ - -{ /* HDKF Test Case 1 */ - -TestCase test_case("HDKF Test Case 1"); - -std::uint8_t input[22] = { - 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, - 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, - 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b -}; - -std::uint8_t salt[13] = { - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0b, 0x0c -}; - -std::uint8_t info[10] = { - 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, - 0xf8, 0xf9 -}; - -std::uint8_t hmac_expected_output[32] = { - 0x07, 0x77, 0x09, 0x36, 0x2c, 0x2e, 0x32, 0xdf, - 0x0d, 0xdc, 0x3f, 0x0d, 0xc4, 0x7b, 0xba, 0x63, - 0x90, 0xb6, 0xc7, 0x3b, 0xb5, 0x0f, 0x9c, 0x31, - 0x22, 0xec, 0x84, 0x4a, 0xd7, 0xc2, 0xb3, 0xe5, -}; - -std::uint8_t hmac_actual_output[32] = {}; - -_olm_crypto_hmac_sha256( - salt, sizeof(salt), - input, sizeof(input), - hmac_actual_output -); - -assert_equals(hmac_expected_output, hmac_actual_output, 32); - -std::uint8_t hkdf_expected_output[42] = { - 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a, - 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a, - 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c, - 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf, - 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18, - 0x58, 0x65 -}; - -std::uint8_t hkdf_actual_output[42] = {}; - -_olm_crypto_hkdf_sha256( - input, sizeof(input), - salt, sizeof(salt), - info, sizeof(info), - hkdf_actual_output, sizeof(hkdf_actual_output) -); - -assert_equals(hkdf_expected_output, hkdf_actual_output, 42); - -} /* HDKF Test Case 1 */ - -} diff --git a/tests/test_group_session.cpp b/tests/test_group_session.cpp deleted file mode 100644 index 19b7761..0000000 --- a/tests/test_group_session.cpp +++ /dev/null @@ -1,327 +0,0 @@ -/* Copyright 2016 OpenMarket Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "olm/inbound_group_session.h" -#include "olm/outbound_group_session.h" -#include "unittest.hh" - -#include <vector> - -int main() { - -{ - TestCase test_case("Pickle outbound group session"); - - size_t size = olm_outbound_group_session_size(); - std::vector<uint8_t> memory(size); - OlmOutboundGroupSession *session = olm_outbound_group_session(memory.data()); - - size_t pickle_length = olm_pickle_outbound_group_session_length(session); - std::vector<uint8_t> pickle1(pickle_length); - size_t res = olm_pickle_outbound_group_session( - session, "secret_key", 10, pickle1.data(), pickle_length - ); - assert_equals(pickle_length, res); - - std::vector<uint8_t> pickle2(pickle1); - - std::vector<uint8_t> buffer2(size); - OlmOutboundGroupSession *session2 = olm_outbound_group_session(buffer2.data()); - res = olm_unpickle_outbound_group_session( - session2, "secret_key", 10, pickle2.data(), pickle_length - ); - assert_not_equals((size_t)-1, res); - assert_equals(pickle_length, - olm_pickle_outbound_group_session_length(session2)); - res = olm_pickle_outbound_group_session( - session2, "secret_key", 10, pickle2.data(), pickle_length - ); - assert_equals(pickle_length, res); - - assert_equals(pickle1.data(), pickle2.data(), pickle_length); -} - - -{ - TestCase test_case("Pickle inbound group session"); - - size_t size = olm_inbound_group_session_size(); - std::vector<uint8_t> memory(size); - OlmInboundGroupSession *session = olm_inbound_group_session(memory.data()); - - size_t pickle_length = olm_pickle_inbound_group_session_length(session); - std::vector<uint8_t> pickle1(pickle_length); - size_t res = olm_pickle_inbound_group_session( - session, "secret_key", 10, pickle1.data(), pickle_length - ); - assert_equals(pickle_length, res); - - std::vector<uint8_t> pickle2(pickle1); - - std::vector<uint8_t> buffer2(size); - OlmInboundGroupSession *session2 = olm_inbound_group_session(buffer2.data()); - res = olm_unpickle_inbound_group_session( - session2, "secret_key", 10, pickle2.data(), pickle_length - ); - assert_not_equals((size_t)-1, res); - assert_equals(pickle_length, - olm_pickle_inbound_group_session_length(session2)); - res = olm_pickle_inbound_group_session( - session2, "secret_key", 10, pickle2.data(), pickle_length - ); - - assert_equals(pickle1.data(), pickle2.data(), pickle_length); -} - -{ - TestCase test_case("Group message send/receive"); - - uint8_t random_bytes[] = - "0123456789ABDEF0123456789ABCDEF" - "0123456789ABDEF0123456789ABCDEF" - "0123456789ABDEF0123456789ABCDEF" - "0123456789ABDEF0123456789ABCDEF" - "0123456789ABDEF0123456789ABCDEF" - "0123456789ABDEF0123456789ABCDEF"; - - - /* build the outbound session */ - size_t size = olm_outbound_group_session_size(); - std::vector<uint8_t> memory(size); - OlmOutboundGroupSession *session = olm_outbound_group_session(memory.data()); - - assert_equals((size_t)160, - olm_init_outbound_group_session_random_length(session)); - - size_t res = olm_init_outbound_group_session( - session, random_bytes, sizeof(random_bytes)); - assert_equals((size_t)0, res); - - assert_equals(0U, olm_outbound_group_session_message_index(session)); - size_t session_key_len = olm_outbound_group_session_key_length(session); - std::vector<uint8_t> session_key(session_key_len); - olm_outbound_group_session_key(session, session_key.data(), session_key_len); - - /* encode the message */ - uint8_t plaintext[] = "Message"; - size_t plaintext_length = sizeof(plaintext) - 1; - - size_t msglen = olm_group_encrypt_message_length( - session, plaintext_length); - - std::vector<uint8_t> msg(msglen); - res = olm_group_encrypt(session, plaintext, plaintext_length, - msg.data(), msglen); - assert_equals(msglen, res); - assert_equals(1U, olm_outbound_group_session_message_index(session)); - - /* build the inbound session */ - size = olm_inbound_group_session_size(); - std::vector<uint8_t> inbound_session_memory(size); - OlmInboundGroupSession *inbound_session = - olm_inbound_group_session(inbound_session_memory.data()); - - assert_equals(0, olm_inbound_group_session_is_verified(inbound_session)); - - res = olm_init_inbound_group_session( - inbound_session, session_key.data(), session_key_len); - assert_equals((size_t)0, res); - assert_equals(1, olm_inbound_group_session_is_verified(inbound_session)); - - /* Check the session ids */ - - size_t out_session_id_len = olm_outbound_group_session_id_length(session); - std::vector<uint8_t> out_session_id(out_session_id_len); - assert_equals(out_session_id_len, olm_outbound_group_session_id( - session, out_session_id.data(), out_session_id_len - )); - - size_t in_session_id_len = olm_inbound_group_session_id_length( - inbound_session - ); - std::vector<uint8_t> in_session_id(in_session_id_len); - assert_equals(in_session_id_len, olm_inbound_group_session_id( - inbound_session, in_session_id.data(), in_session_id_len - )); - - assert_equals(in_session_id_len, out_session_id_len); - assert_equals(out_session_id.data(), in_session_id.data(), in_session_id_len); - - /* decode the message */ - - /* olm_group_decrypt_max_plaintext_length destroys the input so we have to - copy it. */ - std::vector<uint8_t> msgcopy(msg); - size = olm_group_decrypt_max_plaintext_length(inbound_session, msgcopy.data(), msglen); - std::vector<uint8_t> plaintext_buf(size); - uint32_t message_index; - res = olm_group_decrypt(inbound_session, msg.data(), msglen, - plaintext_buf.data(), size, &message_index); - assert_equals(plaintext_length, res); - assert_equals(plaintext, plaintext_buf.data(), res); - assert_equals(message_index, uint32_t(0)); -} - -{ - TestCase test_case("Inbound group session export/import"); - - uint8_t session_key[] = - "AgAAAAAwMTIzNDU2Nzg5QUJERUYwMTIzNDU2Nzg5QUJDREVGMDEyMzQ1Njc4OUFCREVGM" - "DEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkRFRjAxMjM0NTY3ODlBQkNERUYwMTIzND" - "U2Nzg5QUJERUYwMTIzNDU2Nzg5QUJDREVGMDEyMw0bdg1BDq4Px/slBow06q8n/B9WBfw" - "WYyNOB8DlUmXGGwrFmaSb9bR/eY8xgERrxmP07hFmD9uqA2p8PMHdnV5ysmgufE6oLZ5+" - "8/mWQOW3VVTnDIlnwd8oHUYRuk8TCQ"; - - const uint8_t message[] = - "AwgAEhAcbh6UpbByoyZxufQ+h2B+8XHMjhR69G8F4+qjMaFlnIXusJZX3r8LnRORG9T3D" - "XFdbVuvIWrLyRfm4i8QRbe8VPwGRFG57B1CtmxanuP8bHtnnYqlwPsD"; - const std::size_t msglen = sizeof(message)-1; - - /* init first inbound group session, and decrypt */ - std::size_t size = olm_inbound_group_session_size(); - std::vector<uint8_t> session_memory1(size); - OlmInboundGroupSession *session1 = - olm_inbound_group_session(session_memory1.data()); - assert_equals(0, olm_inbound_group_session_is_verified(session1)); - - std::size_t res = olm_init_inbound_group_session( - session1, session_key, sizeof(session_key)-1 - ); - assert_equals((size_t)0, res); - assert_equals(1, olm_inbound_group_session_is_verified(session1)); - - /* olm_group_decrypt_max_plaintext_length destroys the input so we have to - copy it. */ - std::vector<uint8_t> msgcopy(msglen); - memcpy(msgcopy.data(), message, msglen); - size = olm_group_decrypt_max_plaintext_length(session1, msgcopy.data(), msglen); - std::vector<uint8_t> plaintext_buf(size); - uint32_t message_index; - memcpy(msgcopy.data(), message, msglen); - res = olm_group_decrypt( - session1, msgcopy.data(), msglen, plaintext_buf.data(), size, &message_index - ); - assert_equals((std::size_t)7, res); - assert_equals((const uint8_t *)"Message", plaintext_buf.data(), res); - assert_equals(uint32_t(0), message_index); - - /* export the keys */ - size = olm_export_inbound_group_session_length(session1); - std::vector<uint8_t> export_memory(size); - res = olm_export_inbound_group_session( - session1, export_memory.data(), size, 0 - ); - assert_equals(size, res); - - /* free the old session to check there is no shared data */ - olm_clear_inbound_group_session(session1); - - /* import the keys into another inbound group session */ - size = olm_inbound_group_session_size(); - std::vector<uint8_t> session_memory2(size); - OlmInboundGroupSession *session2 = - olm_inbound_group_session(session_memory2.data()); - res = olm_import_inbound_group_session( - session2, export_memory.data(), export_memory.size() - ); - assert_equals((size_t)0, res); - assert_equals(0, olm_inbound_group_session_is_verified(session2)); - - /* decrypt the message with the new session */ - memcpy(msgcopy.data(), message, msglen); - size = olm_group_decrypt_max_plaintext_length(session2, msgcopy.data(), msglen); - std::vector<uint8_t> plaintext_buf2(size); - memcpy(msgcopy.data(), message, msglen); - res = olm_group_decrypt( - session2, msgcopy.data(), msglen, plaintext_buf2.data(), size, &message_index - ); - assert_equals((std::size_t)7, res); - assert_equals((const uint8_t *)"Message", plaintext_buf2.data(), res); - assert_equals(uint32_t(0), message_index); - assert_equals(1, olm_inbound_group_session_is_verified(session2)); -} - -{ - TestCase test_case("Invalid signature group message"); - - uint8_t plaintext[] = "Message"; - size_t plaintext_length = sizeof(plaintext) - 1; - - uint8_t session_key[] = - "AgAAAAAwMTIzNDU2Nzg5QUJERUYwMTIzNDU2Nzg5QUJDREVGMDEyMzQ1Njc4OUFCREVGM" - "DEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkRFRjAxMjM0NTY3ODlBQkNERUYwMTIzND" - "U2Nzg5QUJERUYwMTIzNDU2Nzg5QUJDREVGMDEyMztqJ7zOtqQtYqOo0CpvDXNlMhV3HeJ" - "DpjrASKGLWdop4lx1cSN3Xv1TgfLPW8rhGiW+hHiMxd36nRuxscNv9k4oJA/KP+o0mi1w" - "v44StrEJ1wwx9WZHBUIWkQbaBSuBDw"; - - uint8_t message[] = - "AwgAEhAcbh6UpbByoyZxufQ+h2B+8XHMjhR69G8nP4pNZGl/3QMgrzCZPmP+F2aPLyKPz" - "xRPBMUkeXRJ6Iqm5NeOdx2eERgTW7P20CM+lL3Xpk+ZUOOPvsSQNaAL"; - size_t msglen = sizeof(message)-1; - - /* build the inbound session */ - size_t size = olm_inbound_group_session_size(); - std::vector<uint8_t> inbound_session_memory(size); - OlmInboundGroupSession *inbound_session = - olm_inbound_group_session(inbound_session_memory.data()); - - size_t res = olm_init_inbound_group_session( - inbound_session, session_key, sizeof(session_key)-1 - ); - assert_equals((size_t)0, res); - - /* decode the message */ - - /* olm_group_decrypt_max_plaintext_length destroys the input so we have to - copy it. */ - std::vector<uint8_t> msgcopy(msglen); - memcpy(msgcopy.data(), message, msglen); - size = olm_group_decrypt_max_plaintext_length( - inbound_session, msgcopy.data(), msglen - ); - - memcpy(msgcopy.data(), message, msglen); - std::vector<uint8_t> plaintext_buf(size); - uint32_t message_index; - res = olm_group_decrypt( - inbound_session, msgcopy.data(), msglen, plaintext_buf.data(), size, &message_index - ); - assert_equals(message_index, uint32_t(0)); - assert_equals(plaintext_length, res); - assert_equals(plaintext, plaintext_buf.data(), res); - - /* now twiddle the signature */ - message[msglen-1] = 'E'; - memcpy(msgcopy.data(), message, msglen); - assert_equals( - size, - olm_group_decrypt_max_plaintext_length( - inbound_session, msgcopy.data(), msglen - ) - ); - - memcpy(msgcopy.data(), message, msglen); - res = olm_group_decrypt( - inbound_session, msgcopy.data(), msglen, - plaintext_buf.data(), size, &message_index - ); - assert_equals((size_t)-1, res); - assert_equals( - std::string("BAD_SIGNATURE"), - std::string(olm_inbound_group_session_last_error(inbound_session)) - ); -} - - -} diff --git a/tests/test_list.cpp b/tests/test_list.cpp deleted file mode 100644 index c054af6..0000000 --- a/tests/test_list.cpp +++ /dev/null @@ -1,92 +0,0 @@ -/* Copyright 2015 OpenMarket Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "olm/list.hh" -#include "unittest.hh" - -int main() { - -{ /** List insert test **/ - -TestCase test_case("List insert"); - -olm::List<int, 4> test_list; - -assert_equals(std::size_t(0), test_list.size()); - -for (int i = 0; i < 4; ++i) { - test_list.insert(test_list.end(), i); -} - -assert_equals(std::size_t(4), test_list.size()); - -int i = 0; -for (auto item : test_list) { - assert_equals(i++, item); -} - -assert_equals(4, i); - -test_list.insert(test_list.end(), 4); - -assert_equals(4, test_list[3]); - -} /** List insert test **/ - -{ /** List insert beginning test **/ - -TestCase test_case("List insert beginning"); - -olm::List<int, 4> test_list; - -assert_equals(std::size_t(0), test_list.size()); - -for (int i = 0; i < 4; ++i) { - test_list.insert(test_list.begin(), i); -} - -assert_equals(std::size_t(4), test_list.size()); - -int i = 4; -for (auto item : test_list) { - assert_equals(--i, item); -} - -} /** List insert test **/ - - -{ /** List erase test **/ -TestCase test_case("List erase"); - -olm::List<int, 4> test_list; -assert_equals(std::size_t(0), test_list.size()); - -for (int i = 0; i < 4; ++i) { - test_list.insert(test_list.end(), i); -} -assert_equals(std::size_t(4), test_list.size()); - -test_list.erase(test_list.begin()); -assert_equals(std::size_t(3), test_list.size()); - -int i = 0; -for (auto item : test_list) { - assert_equals(i + 1, item); - ++i; -} -assert_equals(3, i); - -} - -} diff --git a/tests/test_megolm.cpp b/tests/test_megolm.cpp deleted file mode 100644 index 3048fa3..0000000 --- a/tests/test_megolm.cpp +++ /dev/null @@ -1,134 +0,0 @@ -/* Copyright 2016 OpenMarket Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "olm/megolm.h" -#include "olm/memory.hh" - -#include "unittest.hh" - - -int main() { - -std::uint8_t random_bytes[] = - "0123456789ABCDEF0123456789ABCDEF" - "0123456789ABCDEF0123456789ABCDEF" - "0123456789ABCDEF0123456789ABCDEF" - "0123456789ABCDEF0123456789ABCDEF"; - -{ - TestCase test_case("Megolm::advance"); - - Megolm mr; - - megolm_init(&mr, random_bytes, 0); - // single-step advance - megolm_advance(&mr); - const std::uint8_t expected1[] = { - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, - 0xba, 0x9c, 0xd9, 0x55, 0x74, 0x1d, 0x1c, 0x16, 0x23, 0x23, 0xec, 0x82, 0x5e, 0x7c, 0x5c, 0xe8, - 0x89, 0xbb, 0xb4, 0x23, 0xa1, 0x8f, 0x23, 0x82, 0x8f, 0xb2, 0x09, 0x0d, 0x6e, 0x2a, 0xf8, 0x6a - }; - assert_equals(1U, mr.counter); - assert_equals(expected1, megolm_get_data(&mr), MEGOLM_RATCHET_LENGTH); - - // repeat with complex advance - megolm_init(&mr, random_bytes, 0); - megolm_advance_to(&mr, 1); - assert_equals(1U, mr.counter); - assert_equals(expected1, megolm_get_data(&mr), MEGOLM_RATCHET_LENGTH); - - megolm_advance_to(&mr, 0x1000000); - const std::uint8_t expected2[] = { - 0x54, 0x02, 0x2d, 0x7d, 0xc0, 0x29, 0x8e, 0x16, 0x37, 0xe2, 0x1c, 0x97, 0x15, 0x30, 0x92, 0xf9, - 0x33, 0xc0, 0x56, 0xff, 0x74, 0xfe, 0x1b, 0x92, 0x2d, 0x97, 0x1f, 0x24, 0x82, 0xc2, 0x85, 0x9c, - 0x70, 0x04, 0xc0, 0x1e, 0xe4, 0x9b, 0xd6, 0xef, 0xe0, 0x07, 0x35, 0x25, 0xaf, 0x9b, 0x16, 0x32, - 0xc5, 0xbe, 0x72, 0x6d, 0x12, 0x34, 0x9c, 0xc5, 0xbd, 0x47, 0x2b, 0xdc, 0x2d, 0xf6, 0x54, 0x0f, - 0x31, 0x12, 0x59, 0x11, 0x94, 0xfd, 0xa6, 0x17, 0xe5, 0x68, 0xc6, 0x83, 0x10, 0x1e, 0xae, 0xcd, - 0x7e, 0xdd, 0xd6, 0xde, 0x1f, 0xbc, 0x07, 0x67, 0xae, 0x34, 0xda, 0x1a, 0x09, 0xa5, 0x4e, 0xab, - 0xba, 0x9c, 0xd9, 0x55, 0x74, 0x1d, 0x1c, 0x16, 0x23, 0x23, 0xec, 0x82, 0x5e, 0x7c, 0x5c, 0xe8, - 0x89, 0xbb, 0xb4, 0x23, 0xa1, 0x8f, 0x23, 0x82, 0x8f, 0xb2, 0x09, 0x0d, 0x6e, 0x2a, 0xf8, 0x6a, - }; - assert_equals(0x1000000U, mr.counter); - assert_equals(expected2, megolm_get_data(&mr), MEGOLM_RATCHET_LENGTH); - - megolm_advance_to(&mr, 0x1041506); - const std::uint8_t expected3[] = { - 0x54, 0x02, 0x2d, 0x7d, 0xc0, 0x29, 0x8e, 0x16, 0x37, 0xe2, 0x1c, 0x97, 0x15, 0x30, 0x92, 0xf9, - 0x33, 0xc0, 0x56, 0xff, 0x74, 0xfe, 0x1b, 0x92, 0x2d, 0x97, 0x1f, 0x24, 0x82, 0xc2, 0x85, 0x9c, - 0x55, 0x58, 0x8d, 0xf5, 0xb7, 0xa4, 0x88, 0x78, 0x42, 0x89, 0x27, 0x86, 0x81, 0x64, 0x58, 0x9f, - 0x36, 0x63, 0x44, 0x7b, 0x51, 0xed, 0xc3, 0x59, 0x5b, 0x03, 0x6c, 0xa6, 0x04, 0xc4, 0x6d, 0xcd, - 0x5c, 0x54, 0x85, 0x0b, 0xfa, 0x98, 0xa1, 0xfd, 0x79, 0xa9, 0xdf, 0x1c, 0xbe, 0x8f, 0xc5, 0x68, - 0x19, 0x37, 0xd3, 0x0c, 0x85, 0xc8, 0xc3, 0x1f, 0x7b, 0xb8, 0x28, 0x81, 0x6c, 0xf9, 0xff, 0x3b, - 0x95, 0x6c, 0xbf, 0x80, 0x7e, 0x65, 0x12, 0x6a, 0x49, 0x55, 0x8d, 0x45, 0xc8, 0x4a, 0x2e, 0x4c, - 0xd5, 0x6f, 0x03, 0xe2, 0x44, 0x16, 0xb9, 0x8e, 0x1c, 0xfd, 0x97, 0xc2, 0x06, 0xaa, 0x90, 0x7a - }; - assert_equals(0x1041506U, mr.counter); - assert_equals(expected3, megolm_get_data(&mr), MEGOLM_RATCHET_LENGTH); -} - -{ - TestCase test_case("Megolm::advance wraparound"); - - Megolm mr1, mr2; - - megolm_init(&mr1, random_bytes, 0xffffffffUL); - megolm_advance_to(&mr1, 0x1000000); - assert_equals(0x1000000U, mr1.counter); - - megolm_init(&mr2, random_bytes, 0); - megolm_advance_to(&mr2, 0x2000000); - assert_equals(0x2000000U, mr2.counter); - - assert_equals(megolm_get_data(&mr2), megolm_get_data(&mr1), MEGOLM_RATCHET_LENGTH); -} - -{ - TestCase test_case("Megolm::advance overflow by one"); - - Megolm mr1, mr2; - - megolm_init(&mr1, random_bytes, 0xffffffffUL); - megolm_advance_to(&mr1, 0x0); - assert_equals(0x0U, mr1.counter); - - megolm_init(&mr2, random_bytes, 0xffffffffUL); - megolm_advance(&mr2); - assert_equals(0x0U, mr2.counter); - - assert_equals(megolm_get_data(&mr2), megolm_get_data(&mr1), MEGOLM_RATCHET_LENGTH); -} - -{ - TestCase test_case("Megolm::advance overflow"); - - Megolm mr1, mr2; - - megolm_init(&mr1, random_bytes, 0x1UL); - megolm_advance_to(&mr1, 0x80000000UL); - megolm_advance_to(&mr1, 0x0); - assert_equals(0x0U, mr1.counter); - - megolm_init(&mr2, random_bytes, 0x1UL); - megolm_advance_to(&mr2, 0x0UL); - assert_equals(0x0U, mr2.counter); - - assert_equals(megolm_get_data(&mr2), megolm_get_data(&mr1), MEGOLM_RATCHET_LENGTH); -} - -} diff --git a/tests/test_message.cpp b/tests/test_message.cpp deleted file mode 100644 index fa2d0cc..0000000 --- a/tests/test_message.cpp +++ /dev/null @@ -1,112 +0,0 @@ -/* Copyright 2015-2016 OpenMarket Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "olm/message.hh" -#include "unittest.hh" - -int main() { - -std::uint8_t message1[36] = "\x03\x10\x01\n\nratchetkey\"\nciphertexthmacsha2"; -std::uint8_t message2[36] = "\x03\n\nratchetkey\x10\x01\"\nciphertexthmacsha2"; -std::uint8_t ratchetkey[11] = "ratchetkey"; -std::uint8_t ciphertext[11] = "ciphertext"; -std::uint8_t hmacsha2[9] = "hmacsha2"; - -{ /* Message decode test */ - -TestCase test_case("Message decode test"); - -olm::MessageReader reader; -olm::decode_message(reader, message1, 35, 8); - -assert_equals(std::uint8_t(3), reader.version); -assert_equals(true, reader.has_counter); -assert_equals(std::uint32_t(1), reader.counter); -assert_equals(std::size_t(10), reader.ratchet_key_length); -assert_equals(std::size_t(10), reader.ciphertext_length); - -assert_equals(ratchetkey, reader.ratchet_key, 10); -assert_equals(ciphertext, reader.ciphertext, 10); - - -} /* Message decode test */ - -{ /* Message encode test */ - -TestCase test_case("Message encode test"); - -std::size_t length = olm::encode_message_length(1, 10, 10, 8); -assert_equals(std::size_t(35), length); - -std::uint8_t output[35]; - -olm::MessageWriter writer; -olm::encode_message(writer, 3, 1, 10, 10, output); - -std::memcpy(writer.ratchet_key, ratchetkey, 10); -std::memcpy(writer.ciphertext, ciphertext, 10); -std::memcpy(output + length - 8, hmacsha2, 8); - -assert_equals(message2, output, 35); - -} /* Message encode test */ - - -{ /* group message encode test */ - - TestCase test_case("Group message encode test"); - - size_t length = _olm_encode_group_message_length(200, 10, 8, 64); - size_t expected_length = 1 + (1+2) + (2+10) + 8 + 64; - assert_equals(expected_length, length); - - uint8_t output[50]; - uint8_t *ciphertext_ptr; - - _olm_encode_group_message( - 3, - 200, // counter - 10, // ciphertext length - output, - &ciphertext_ptr - ); - - uint8_t expected[] = - "\x03" - "\x08\xC8\x01" - "\x12\x0A"; - - assert_equals(expected, output, sizeof(expected)-1); - assert_equals(output+sizeof(expected)-1, ciphertext_ptr); -} /* group message encode test */ - -{ - TestCase test_case("Group message decode test"); - - struct _OlmDecodeGroupMessageResults results; - std::uint8_t message[] = - "\x03" - "\x08\xC8\x01" - "\x12\x0A" "ciphertext" - "hmacsha2" - "ed25519signature"; - - _olm_decode_group_message(message, sizeof(message)-1, 8, 16, &results); - assert_equals(std::uint8_t(3), results.version); - assert_equals(1, results.has_message_index); - assert_equals(std::uint32_t(200), results.message_index); - assert_equals(std::size_t(10), results.ciphertext_length); - assert_equals(ciphertext, results.ciphertext, 10); -} /* group message decode test */ -} diff --git a/tests/test_olm.cpp b/tests/test_olm.cpp deleted file mode 100644 index ab7d816..0000000 --- a/tests/test_olm.cpp +++ /dev/null @@ -1,399 +0,0 @@ -#include "olm/olm.h" -#include "unittest.hh" - -#include <cstddef> -#include <cstdint> -#include <cstring> -#include <vector> - -struct MockRandom { - MockRandom(std::uint8_t tag, std::uint8_t offset = 0) - : tag(tag), current(offset) {} - void operator()( - std::uint8_t * bytes, std::size_t length - ) { - while (length > 32) { - bytes[0] = tag; - std::memset(bytes + 1, current, 31); - length -= 32; - bytes += 32; - current += 1; - } - if (length) { - bytes[0] = tag; - std::memset(bytes + 1, current, length - 1); - current += 1; - } - } - std::uint8_t tag; - std::uint8_t current; -}; - -int main() { - -{ /** Pickle account test */ - -TestCase test_case("Pickle account test"); -MockRandom mock_random('P'); - - -std::vector<std::uint8_t> account_buffer(::olm_account_size()); -::OlmAccount *account = ::olm_account(account_buffer.data()); -std::vector<std::uint8_t> random(::olm_create_account_random_length(account)); -mock_random(random.data(), random.size()); -::olm_create_account(account, random.data(), random.size()); -std::vector<std::uint8_t> ot_random(::olm_account_generate_one_time_keys_random_length( - account, 42 - )); -mock_random(ot_random.data(), ot_random.size()); -::olm_account_generate_one_time_keys(account, 42, ot_random.data(), ot_random.size()); - -std::size_t pickle_length = ::olm_pickle_account_length(account); -std::vector<std::uint8_t> pickle1(pickle_length); -std::size_t res = ::olm_pickle_account(account, "secret_key", 10, pickle1.data(), pickle_length); -assert_equals(pickle_length, res); - -std::vector<std::uint8_t> pickle2(pickle1); - -std::vector<std::uint8_t> account_buffer2(::olm_account_size()); -::OlmAccount *account2 = ::olm_account(account_buffer2.data()); -assert_not_equals(std::size_t(-1), ::olm_unpickle_account( - account2, "secret_key", 10, pickle2.data(), pickle_length -)); -assert_equals(pickle_length, ::olm_pickle_account_length(account2)); -res = ::olm_pickle_account(account2, "secret_key", 10, pickle2.data(), pickle_length); -assert_equals(pickle_length, res); - -assert_equals(pickle1.data(), pickle2.data(), pickle_length); -} - - -{ - TestCase test_case("Old account unpickle test"); - - // this uses the old pickle format, which did not use enough space - // for the Ed25519 key. We should reject it. - std::uint8_t pickle[] = - "x3h9er86ygvq56pM1yesdAxZou4ResPQC9Rszk/fhEL9JY/umtZ2N/foL/SUgVXS" - "v0IxHHZTafYjDdzJU9xr8dQeBoOTGfV9E/lCqDGBnIlu7SZndqjEKXtzGyQr4sP4" - "K/A/8TOu9iK2hDFszy6xETiousHnHgh2ZGbRUh4pQx+YMm8ZdNZeRnwFGLnrWyf9" - "O5TmXua1FcU"; - - std::vector<std::uint8_t> account_buffer(::olm_account_size()); - ::OlmAccount *account = ::olm_account(account_buffer.data()); - assert_equals( - std::size_t(-1), - ::olm_unpickle_account( - account, "", 0, pickle, sizeof(pickle)-1 - ) - ); - assert_equals( - std::string("BAD_LEGACY_ACCOUNT_PICKLE"), - std::string(::olm_account_last_error(account)) - ); -} - - -{ /** Pickle session test */ - -TestCase test_case("Pickle session test"); -MockRandom mock_random('P'); - -std::vector<std::uint8_t> account_buffer(::olm_account_size()); -::OlmAccount *account = ::olm_account(account_buffer.data()); -std::vector<std::uint8_t> random(::olm_create_account_random_length(account)); -mock_random(random.data(), random.size()); -::olm_create_account(account, random.data(), random.size()); - -std::vector<std::uint8_t> session_buffer(::olm_session_size()); -::OlmSession *session = ::olm_session(session_buffer.data()); -std::uint8_t identity_key[32]; -std::uint8_t one_time_key[32]; -mock_random(identity_key, sizeof(identity_key)); -mock_random(one_time_key, sizeof(one_time_key)); -std::vector<std::uint8_t> random2(::olm_create_outbound_session_random_length(session)); -mock_random(random2.data(), random2.size()); - -::olm_create_outbound_session( - session, account, - identity_key, sizeof(identity_key), - one_time_key, sizeof(one_time_key), - random2.data(), random2.size() -); - - -std::size_t pickle_length = ::olm_pickle_session_length(session); -std::vector<std::uint8_t> pickle1(pickle_length); -std::size_t res = ::olm_pickle_session(session, "secret_key", 10, pickle1.data(), pickle_length); -assert_equals(pickle_length, res); - -std::vector<std::uint8_t> pickle2(pickle1); - -std::vector<std::uint8_t> session_buffer2(::olm_session_size()); -::OlmSession *session2 = ::olm_session(session_buffer2.data()); -assert_not_equals(std::size_t(-1), ::olm_unpickle_session( - session2, "secret_key", 10, pickle2.data(), pickle_length -)); -assert_equals(pickle_length, ::olm_pickle_session_length(session2)); -res = ::olm_pickle_session(session2, "secret_key", 10, pickle2.data(), pickle_length); -assert_equals(pickle_length, res); - -assert_equals(pickle1.data(), pickle2.data(), pickle_length); -} - -{ /** Loopback test */ - -TestCase test_case("Loopback test"); -MockRandom mock_random_a('A', 0x00); -MockRandom mock_random_b('B', 0x80); - -std::vector<std::uint8_t> a_account_buffer(::olm_account_size()); -::OlmAccount *a_account = ::olm_account(a_account_buffer.data()); -std::vector<std::uint8_t> a_random(::olm_create_account_random_length(a_account)); -mock_random_a(a_random.data(), a_random.size()); -::olm_create_account(a_account, a_random.data(), a_random.size()); - -std::vector<std::uint8_t> b_account_buffer(::olm_account_size()); -::OlmAccount *b_account = ::olm_account(b_account_buffer.data()); -std::vector<std::uint8_t> b_random(::olm_create_account_random_length(b_account)); -mock_random_b(b_random.data(), b_random.size()); -::olm_create_account(b_account, b_random.data(), b_random.size()); -std::vector<std::uint8_t> o_random(::olm_account_generate_one_time_keys_random_length( - b_account, 42 -)); -mock_random_b(o_random.data(), o_random.size()); -::olm_account_generate_one_time_keys(b_account, 42, o_random.data(), o_random.size()); - -std::vector<std::uint8_t> a_id_keys(::olm_account_identity_keys_length(a_account)); -::olm_account_identity_keys(a_account, a_id_keys.data(), a_id_keys.size()); - -std::vector<std::uint8_t> b_id_keys(::olm_account_identity_keys_length(b_account)); -std::vector<std::uint8_t> b_ot_keys(::olm_account_one_time_keys_length(b_account)); -::olm_account_identity_keys(b_account, b_id_keys.data(), b_id_keys.size()); -::olm_account_one_time_keys(b_account, b_ot_keys.data(), b_ot_keys.size()); - -std::vector<std::uint8_t> a_session_buffer(::olm_session_size()); -::OlmSession *a_session = ::olm_session(a_session_buffer.data()); -std::vector<std::uint8_t> a_rand(::olm_create_outbound_session_random_length(a_session)); -mock_random_a(a_rand.data(), a_rand.size()); -assert_not_equals(std::size_t(-1), ::olm_create_outbound_session( - a_session, a_account, - b_id_keys.data() + 15, 43, // B's curve25519 identity key - b_ot_keys.data() + 25, 43, // B's curve25519 one time key - a_rand.data(), a_rand.size() -)); - -std::uint8_t plaintext[] = "Hello, World"; -std::vector<std::uint8_t> message_1(::olm_encrypt_message_length(a_session, 12)); -std::vector<std::uint8_t> a_message_random(::olm_encrypt_random_length(a_session)); -mock_random_a(a_message_random.data(), a_message_random.size()); -assert_equals(std::size_t(0), ::olm_encrypt_message_type(a_session)); -assert_not_equals(std::size_t(-1), ::olm_encrypt( - a_session, - plaintext, 12, - a_message_random.data(), a_message_random.size(), - message_1.data(), message_1.size() -)); - - -std::vector<std::uint8_t> tmp_message_1(message_1); -std::vector<std::uint8_t> b_session_buffer(::olm_account_size()); -::OlmSession *b_session = ::olm_session(b_session_buffer.data()); -::olm_create_inbound_session( - b_session, b_account, tmp_message_1.data(), message_1.size() -); - -// Check that the inbound session matches the message it was created from. -std::memcpy(tmp_message_1.data(), message_1.data(), message_1.size()); -assert_equals(std::size_t(1), ::olm_matches_inbound_session( - b_session, - tmp_message_1.data(), message_1.size() -)); - -// Check that the inbound session matches the key this message is supposed -// to be from. -std::memcpy(tmp_message_1.data(), message_1.data(), message_1.size()); -assert_equals(std::size_t(1), ::olm_matches_inbound_session_from( - b_session, - a_id_keys.data() + 15, 43, // A's curve125519 identity key. - tmp_message_1.data(), message_1.size() -)); - -// Check that the inbound session isn't from a different user. -std::memcpy(tmp_message_1.data(), message_1.data(), message_1.size()); -assert_equals(std::size_t(0), ::olm_matches_inbound_session_from( - b_session, - b_id_keys.data() + 15, 43, // B's curve25519 identity key. - tmp_message_1.data(), message_1.size() -)); - -// Check that we can decrypt the message. -std::memcpy(tmp_message_1.data(), message_1.data(), message_1.size()); -std::vector<std::uint8_t> plaintext_1(::olm_decrypt_max_plaintext_length( - b_session, 0, tmp_message_1.data(), message_1.size() -)); -std::memcpy(tmp_message_1.data(), message_1.data(), message_1.size()); -assert_equals(std::size_t(12), ::olm_decrypt( - b_session, 0, - tmp_message_1.data(), message_1.size(), - plaintext_1.data(), plaintext_1.size() -)); - -assert_equals(plaintext, plaintext_1.data(), 12); - -std::vector<std::uint8_t> message_2(::olm_encrypt_message_length(b_session, 12)); -std::vector<std::uint8_t> b_message_random(::olm_encrypt_random_length(b_session)); -mock_random_b(b_message_random.data(), b_message_random.size()); -assert_equals(std::size_t(1), ::olm_encrypt_message_type(b_session)); -assert_not_equals(std::size_t(-1), ::olm_encrypt( - b_session, - plaintext, 12, - b_message_random.data(), b_message_random.size(), - message_2.data(), message_2.size() -)); - -std::vector<std::uint8_t> tmp_message_2(message_2); -std::vector<std::uint8_t> plaintext_2(::olm_decrypt_max_plaintext_length( - a_session, 1, tmp_message_2.data(), message_2.size() -)); -std::memcpy(tmp_message_2.data(), message_2.data(), message_2.size()); -assert_equals(std::size_t(12), ::olm_decrypt( - a_session, 1, - tmp_message_2.data(), message_2.size(), - plaintext_2.data(), plaintext_2.size() -)); - -assert_equals(plaintext, plaintext_2.data(), 12); - -std::memcpy(tmp_message_2.data(), message_2.data(), message_2.size()); -assert_equals(std::size_t(-1), ::olm_decrypt( - a_session, 1, - tmp_message_2.data(), message_2.size(), - plaintext_2.data(), plaintext_2.size() -)); - -std::vector<std::uint8_t> a_session_id(::olm_session_id_length(a_session)); -assert_not_equals(std::size_t(-1), ::olm_session_id( - a_session, a_session_id.data(), a_session_id.size() -)); - -std::vector<std::uint8_t> b_session_id(::olm_session_id_length(b_session)); -assert_not_equals(std::size_t(-1), ::olm_session_id( - b_session, b_session_id.data(), b_session_id.size() -)); - -assert_equals(a_session_id.size(), b_session_id.size()); -assert_equals(a_session_id.data(), b_session_id.data(), b_session_id.size()); - -} - -{ /** More messages test */ - -TestCase test_case("More messages test"); -MockRandom mock_random_a('A', 0x00); -MockRandom mock_random_b('B', 0x80); - -std::vector<std::uint8_t> a_account_buffer(::olm_account_size()); -::OlmAccount *a_account = ::olm_account(a_account_buffer.data()); -std::vector<std::uint8_t> a_random(::olm_create_account_random_length(a_account)); -mock_random_a(a_random.data(), a_random.size()); -::olm_create_account(a_account, a_random.data(), a_random.size()); - -std::vector<std::uint8_t> b_account_buffer(::olm_account_size()); -::OlmAccount *b_account = ::olm_account(b_account_buffer.data()); -std::vector<std::uint8_t> b_random(::olm_create_account_random_length(b_account)); -mock_random_b(b_random.data(), b_random.size()); -::olm_create_account(b_account, b_random.data(), b_random.size()); -std::vector<std::uint8_t> o_random(::olm_account_generate_one_time_keys_random_length( - b_account, 42 -)); -mock_random_b(o_random.data(), o_random.size()); -::olm_account_generate_one_time_keys(b_account, 42, o_random.data(), o_random.size()); - -std::vector<std::uint8_t> b_id_keys(::olm_account_identity_keys_length(b_account)); -std::vector<std::uint8_t> b_ot_keys(::olm_account_one_time_keys_length(b_account)); -::olm_account_identity_keys(b_account, b_id_keys.data(), b_id_keys.size()); -::olm_account_one_time_keys(b_account, b_ot_keys.data(), b_ot_keys.size()); - -std::vector<std::uint8_t> a_session_buffer(::olm_session_size()); -::OlmSession *a_session = ::olm_session(a_session_buffer.data()); -std::vector<std::uint8_t> a_rand(::olm_create_outbound_session_random_length(a_session)); -mock_random_a(a_rand.data(), a_rand.size()); -assert_not_equals(std::size_t(-1), ::olm_create_outbound_session( - a_session, a_account, - b_id_keys.data() + 15, 43, - b_ot_keys.data() + 25, 43, - a_rand.data(), a_rand.size() -)); - -std::uint8_t plaintext[] = "Hello, World"; -std::vector<std::uint8_t> message_1(::olm_encrypt_message_length(a_session, 12)); -std::vector<std::uint8_t> a_message_random(::olm_encrypt_random_length(a_session)); -mock_random_a(a_message_random.data(), a_message_random.size()); -assert_equals(std::size_t(0), ::olm_encrypt_message_type(a_session)); -assert_not_equals(std::size_t(-1), ::olm_encrypt( - a_session, - plaintext, 12, - a_message_random.data(), a_message_random.size(), - message_1.data(), message_1.size() -)); - -std::vector<std::uint8_t> tmp_message_1(message_1); -std::vector<std::uint8_t> b_session_buffer(::olm_account_size()); -::OlmSession *b_session = ::olm_session(b_session_buffer.data()); -::olm_create_inbound_session( - b_session, b_account, tmp_message_1.data(), message_1.size() -); - -std::memcpy(tmp_message_1.data(), message_1.data(), message_1.size()); -std::vector<std::uint8_t> plaintext_1(::olm_decrypt_max_plaintext_length( - b_session, 0, tmp_message_1.data(), message_1.size() -)); -std::memcpy(tmp_message_1.data(), message_1.data(), message_1.size()); -assert_equals(std::size_t(12), ::olm_decrypt( - b_session, 0, - tmp_message_1.data(), message_1.size(), - plaintext_1.data(), plaintext_1.size() -)); - -for (unsigned i = 0; i < 8; ++i) { - { - std::vector<std::uint8_t> msg_a(::olm_encrypt_message_length(a_session, 12)); - std::vector<std::uint8_t> rnd_a(::olm_encrypt_random_length(a_session)); - mock_random_a(rnd_a.data(), rnd_a.size()); - std::size_t type_a = ::olm_encrypt_message_type(a_session); - assert_not_equals(std::size_t(-1), ::olm_encrypt( - a_session, plaintext, 12, rnd_a.data(), rnd_a.size(), msg_a.data(), msg_a.size() - )); - - std::vector<std::uint8_t> tmp_a(msg_a); - std::vector<std::uint8_t> out_a(::olm_decrypt_max_plaintext_length( - b_session, type_a, tmp_a.data(), tmp_a.size() - )); - std::memcpy(tmp_a.data(), msg_a.data(), sizeof(msg_a)); - assert_equals(std::size_t(12), ::olm_decrypt( - b_session, type_a, msg_a.data(), msg_a.size(), out_a.data(), out_a.size() - )); - } - { - std::vector<std::uint8_t> msg_b(::olm_encrypt_message_length(b_session, 12)); - std::vector<std::uint8_t> rnd_b(::olm_encrypt_random_length(b_session)); - mock_random_b(rnd_b.data(), rnd_b.size()); - std::size_t type_b = ::olm_encrypt_message_type(b_session); - assert_not_equals(std::size_t(-1), ::olm_encrypt( - b_session, plaintext, 12, rnd_b.data(), rnd_b.size(), msg_b.data(), msg_b.size() - )); - - std::vector<std::uint8_t> tmp_b(msg_b); - std::vector<std::uint8_t> out_b(::olm_decrypt_max_plaintext_length( - a_session, type_b, tmp_b.data(), tmp_b.size() - )); - std::memcpy(tmp_b.data(), msg_b.data(), msg_b.size()); - assert_equals(std::size_t(12), ::olm_decrypt( - a_session, type_b, msg_b.data(), msg_b.size(), out_b.data(), out_b.size() - )); - } -} -} - -} diff --git a/tests/test_olm_decrypt.cpp b/tests/test_olm_decrypt.cpp deleted file mode 100644 index 0c8feb8..0000000 --- a/tests/test_olm_decrypt.cpp +++ /dev/null @@ -1,92 +0,0 @@ -#include "olm/olm.h" -#include "unittest.hh" - -#include <vector> - -struct test_case { - const char *msghex; - const char *expected_error; -}; - -const test_case test_cases[] = { - { "41776f", "BAD_MESSAGE_FORMAT" }, - { "7fff6f0101346d671201", "BAD_MESSAGE_FORMAT" }, - { "ee776f41496f674177804177778041776f6716670a677d6f670a67c2677d", "BAD_MESSAGE_FORMAT" }, - { "e9e9c9c1e9e9c9e9c9c1e9e9c9c1", "BAD_MESSAGE_FORMAT" }, -}; - - -const char * session_data = - "E0p44KO2y2pzp9FIjv0rud2wIvWDi2dx367kP4Fz/9JCMrH+aG369HGymkFtk0+PINTLB9lQRt" - "ohea5d7G/UXQx3r5y4IWuyh1xaRnojEZQ9a5HRZSNtvmZ9NY1f1gutYa4UtcZcbvczN8b/5Bqg" - "e16cPUH1v62JKLlhoAJwRkH1wU6fbyOudERg5gdXA971btR+Q2V8GKbVbO5fGKL5phmEPVXyMs" - "rfjLdzQrgjOTxN8Pf6iuP+WFPvfnR9lDmNCFxJUVAdLIMnLuAdxf1TGcS+zzCzEE8btIZ99mHF" - "dGvPXeH8qLeNZA"; - -void decode_hex( - const char * input, - std::uint8_t * output, std::size_t output_length -) { - std::uint8_t * end = output + output_length; - while (output != end) { - 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'); - *(output++) = value; - } -} - -void decrypt_case(int message_type, const test_case * test_case) { - std::vector<std::uint8_t> session_memory(olm_session_size()); - ::OlmSession * session = ::olm_session(session_memory.data()); - - std::vector<std::uint8_t> pickled(strlen(session_data)); - ::memcpy(pickled.data(), session_data, pickled.size()); - assert_not_equals( - ::olm_error(), - ::olm_unpickle_session(session, "", 0, pickled.data(), pickled.size()) - ); - - std::size_t message_length = strlen(test_case->msghex) / 2; - std::uint8_t * message = (std::uint8_t *) ::malloc(message_length); - decode_hex(test_case->msghex, message, message_length); - - size_t max_length = olm_decrypt_max_plaintext_length( - session, message_type, message, message_length - ); - - if (test_case->expected_error) { - assert_equals(::olm_error(), max_length); - assert_equals( - std::string(test_case->expected_error), - std::string(::olm_session_last_error(session)) - ); - free(message); - return; - } - - assert_not_equals(::olm_error(), max_length); - - std::vector<uint8_t> plaintext(max_length); - decode_hex(test_case->msghex, message, message_length); - olm_decrypt( - session, message_type, - message, message_length, - plaintext.data(), max_length - ); - free(message); -} - - -int main() { -{ -TestCase my_test("Olm decrypt test"); - -for (unsigned int i = 0; i < sizeof(test_cases)/ sizeof(test_cases[0]); ++i) { - decrypt_case(0, &test_cases[i]); -} - -} -} diff --git a/tests/test_olm_sha256.cpp b/tests/test_olm_sha256.cpp deleted file mode 100644 index d76e592..0000000 --- a/tests/test_olm_sha256.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "olm/olm.h" -#include "unittest.hh" - -#include <vector> - -int main() { -{ -TestCase("Olm sha256 test"); - - -std::vector<std::uint8_t> utility_buffer(::olm_utility_size()); -::OlmUtility * utility = ::olm_utility(utility_buffer.data()); - -assert_equals(std::size_t(43), ::olm_sha256_length(utility)); -std::uint8_t output[43]; -::olm_sha256(utility, "Hello, World", 12, output, 43); - -std::uint8_t expected_output[] = "A2daxT/5zRU1zMffzfosRYxSGDcfQY3BNvLRmsH76KU"; -assert_equals(output, expected_output, 43); - -} -} diff --git a/tests/test_olm_signature.cpp b/tests/test_olm_signature.cpp deleted file mode 100644 index f53bcec..0000000 --- a/tests/test_olm_signature.cpp +++ /dev/null @@ -1,91 +0,0 @@ -#include "olm/olm.h" -#include "unittest.hh" - -#include <cstddef> -#include <cstdint> -#include <cstring> - -struct MockRandom { - MockRandom(std::uint8_t tag, std::uint8_t offset = 0) - : tag(tag), current(offset) {} - void operator()( - void * buf, std::size_t length - ) { - std::uint8_t * bytes = (std::uint8_t *) buf; - while (length > 32) { - bytes[0] = tag; - std::memset(bytes + 1, current, 31); - length -= 32; - bytes += 32; - current += 1; - } - if (length) { - bytes[0] = tag; - std::memset(bytes + 1, current, length - 1); - current += 1; - } - } - std::uint8_t tag; - std::uint8_t current; -}; - -std::uint8_t * check_malloc(std::size_t size) { - if (size == std::size_t(-1)) { - assert_not_equals(std::size_t(-1), size); - } - return (std::uint8_t *)::malloc(size); -} - - -int main() { - -{ /** Signing Test */ -TestCase test_case("Signing test"); - -MockRandom mock_random_a('A', 0x00); - -void * account_buffer = check_malloc(::olm_account_size()); -::OlmAccount * account = ::olm_account(account_buffer); - -std::size_t random_size = ::olm_create_account_random_length(account); -void * random = check_malloc(random_size); -mock_random_a(random, random_size); -::olm_create_account(account, random, random_size); -::free(random); - -std::size_t message_size = 12; -void * message = check_malloc(message_size); -::memcpy(message, "Hello, World", message_size); - -std::size_t signature_size = ::olm_account_signature_length(account); -void * signature = check_malloc(signature_size); -assert_not_equals(std::size_t(-1), ::olm_account_sign( - account, message, message_size, signature, signature_size -)); - -std::size_t id_keys_size = ::olm_account_identity_keys_length(account); -std::uint8_t * id_keys = (std::uint8_t *) check_malloc(id_keys_size); -assert_not_equals(std::size_t(-1), ::olm_account_identity_keys( - account, id_keys, id_keys_size -)); - -olm_clear_account(account); -free(account_buffer); - -void * utility_buffer = check_malloc(::olm_utility_size()); -::OlmUtility * utility = ::olm_utility(utility_buffer); - -assert_not_equals(std::size_t(-1), ::olm_ed25519_verify( - utility, id_keys + 71, 43, message, message_size, signature, signature_size -)); - -olm_clear_utility(utility); -free(utility_buffer); - -free(id_keys); -free(signature); -free(message); - -} - -} diff --git a/tests/test_olm_using_malloc.cpp b/tests/test_olm_using_malloc.cpp deleted file mode 100644 index fff3ea2..0000000 --- a/tests/test_olm_using_malloc.cpp +++ /dev/null @@ -1,210 +0,0 @@ -#include "olm/olm.h" -#include "unittest.hh" - -#include <cstddef> -#include <cstdint> -#include <cstring> - -struct MockRandom { - MockRandom(std::uint8_t tag, std::uint8_t offset = 0) - : tag(tag), current(offset) {} - void operator()( - void * buf, std::size_t length - ) { - std::uint8_t * bytes = (std::uint8_t *) buf; - while (length > 32) { - bytes[0] = tag; - std::memset(bytes + 1, current, 31); - length -= 32; - bytes += 32; - current += 1; - } - if (length) { - bytes[0] = tag; - std::memset(bytes + 1, current, length - 1); - current += 1; - } - } - std::uint8_t tag; - std::uint8_t current; -}; - -std::uint8_t * check_malloc(std::size_t size) { - if (size == std::size_t(-1)) { - assert_not_equals(std::size_t(-1), size); - } - return (std::uint8_t *)::malloc(size); -} - -int main() { - -{ /** More messages test */ - -TestCase test_case("More messages test"); -MockRandom mock_random_a('A', 0x00); -MockRandom mock_random_b('B', 0x80); - -void * a_account_buffer = check_malloc(::olm_account_size()); -::OlmAccount *a_account = ::olm_account(a_account_buffer); - -std::size_t a_random_size = ::olm_create_account_random_length(a_account); -void * a_random = check_malloc(a_random_size); -mock_random_a(a_random, a_random_size); -::olm_create_account(a_account, a_random, a_random_size); -free(a_random); - -void * b_account_buffer = check_malloc(::olm_account_size()); -::OlmAccount *b_account = ::olm_account(b_account_buffer); - -std::size_t b_random_size = ::olm_create_account_random_length(b_account); -void * b_random = check_malloc(b_random_size); -mock_random_b(b_random, b_random_size); -::olm_create_account(b_account, b_random, b_random_size); -free(b_random); - -std::size_t o_random_size = ::olm_account_generate_one_time_keys_random_length( - b_account, 42 -); -void * o_random = check_malloc(o_random_size); -mock_random_b(o_random, o_random_size); -::olm_account_generate_one_time_keys(b_account, 42, o_random, o_random_size); -free(o_random); - - -std::size_t b_id_keys_size = ::olm_account_identity_keys_length(b_account); -std::size_t b_ot_keys_size = ::olm_account_one_time_keys_length(b_account); -std::uint8_t * b_id_keys = (std::uint8_t *) check_malloc(b_id_keys_size); -std::uint8_t * b_ot_keys = (std::uint8_t *) check_malloc(b_ot_keys_size); -::olm_account_identity_keys(b_account, b_id_keys, b_id_keys_size); -::olm_account_one_time_keys(b_account, b_ot_keys, b_ot_keys_size); - -void * a_session_buffer = check_malloc(::olm_session_size()); -::OlmSession *a_session = ::olm_session(a_session_buffer); - -std::size_t a_rand_size = ::olm_create_outbound_session_random_length(a_session); -void * a_rand = check_malloc(a_rand_size); -mock_random_a(a_rand, a_rand_size); -assert_not_equals(std::size_t(-1), ::olm_create_outbound_session( - a_session, a_account, - b_id_keys + 15, 43, - b_ot_keys + 25, 43, - a_rand, a_rand_size -)); -free(b_id_keys); -free(b_ot_keys); -free(a_rand); - -void * plaintext = malloc(12); -::memcpy(plaintext, "Hello, World", 12); - -std::size_t message_1_size = ::olm_encrypt_message_length(a_session, 12); -void * message_1 = check_malloc(message_1_size); -std::size_t a_message_random_size = ::olm_encrypt_random_length(a_session); -void * a_message_random = check_malloc(a_message_random_size); -mock_random_a(a_message_random, a_message_random_size); -assert_equals(std::size_t(0), ::olm_encrypt_message_type(a_session)); -assert_not_equals(std::size_t(-1), ::olm_encrypt( - a_session, - plaintext, 12, - a_message_random, a_message_random_size, - message_1, message_1_size -)); -free(a_message_random); - -void * tmp_message_1 = check_malloc(message_1_size); -std::memcpy(tmp_message_1, message_1, message_1_size); - -void * b_session_buffer = check_malloc(olm_account_size()); -::OlmSession *b_session = ::olm_session(b_session_buffer); -::olm_create_inbound_session( - b_session, b_account, tmp_message_1, message_1_size -); - -std::memcpy(tmp_message_1, message_1, message_1_size); - -std::size_t plaintext_1_size = ::olm_decrypt_max_plaintext_length( - b_session, 0, tmp_message_1, message_1_size -); -void * plaintext_1 = check_malloc(plaintext_1_size); -std::memcpy(tmp_message_1, message_1, message_1_size); -assert_equals(std::size_t(12), ::olm_decrypt( - b_session, 0, - tmp_message_1, message_1_size, - plaintext_1, plaintext_1_size -)); -free(tmp_message_1); -free(plaintext_1); -free(message_1); - -assert_not_equals( - std::size_t(-1), ::olm_remove_one_time_keys(b_account, b_session) -); - -for (unsigned i = 0; i < 8; ++i) { - { - std::size_t msg_a_size = ::olm_encrypt_message_length(a_session, 12); - std::size_t rnd_a_size = ::olm_encrypt_random_length(a_session); - void * msg_a = check_malloc(msg_a_size); - void * rnd_a = check_malloc(rnd_a_size); - mock_random_a(rnd_a, rnd_a_size); - std::size_t type_a = ::olm_encrypt_message_type(a_session); - assert_not_equals(std::size_t(-1), ::olm_encrypt( - a_session, plaintext, 12, rnd_a, rnd_a_size, msg_a, msg_a_size - )); - free(rnd_a); - - void * tmp_a = check_malloc(msg_a_size); - std::memcpy(tmp_a, msg_a, msg_a_size); - std::size_t out_a_size = ::olm_decrypt_max_plaintext_length( - b_session, type_a, tmp_a, msg_a_size - ); - void * out_a = check_malloc(out_a_size); - std::memcpy(tmp_a, msg_a, msg_a_size); - assert_equals(std::size_t(12), ::olm_decrypt( - b_session, type_a, tmp_a, msg_a_size, out_a, out_a_size - )); - free(tmp_a); - free(msg_a); - free(out_a); - } - { - std::size_t msg_b_size = ::olm_encrypt_message_length(b_session, 12); - std::size_t rnd_b_size = ::olm_encrypt_random_length(b_session); - void * msg_b = check_malloc(msg_b_size); - void * rnd_b = check_malloc(rnd_b_size); - mock_random_b(rnd_b, rnd_b_size); - std::size_t type_b = ::olm_encrypt_message_type(b_session); - assert_not_equals(std::size_t(-1), ::olm_encrypt( - b_session, plaintext, 12, rnd_b, rnd_b_size, msg_b, msg_b_size - )); - free(rnd_b); - - void * tmp_b = check_malloc(msg_b_size); - std::memcpy(tmp_b, msg_b, msg_b_size); - std::size_t out_b_size = ::olm_decrypt_max_plaintext_length( - a_session, type_b, tmp_b, msg_b_size - ); - void * out_b = check_malloc(out_b_size); - std::memcpy(tmp_b, msg_b, msg_b_size); - assert_equals(std::size_t(12), ::olm_decrypt( - a_session, type_b, msg_b, msg_b_size, out_b, out_b_size - )); - free(tmp_b); - free(msg_b); - free(out_b); - } -} -::olm_clear_account(a_account); -::olm_clear_account(b_account); -::olm_clear_session(a_session); -::olm_clear_session(b_session); - -free(a_account_buffer); -free(b_account_buffer); -free(a_session_buffer); -free(b_session_buffer); -free(plaintext); - -} - -} diff --git a/tests/test_pk.cpp b/tests/test_pk.cpp deleted file mode 100644 index b917a2e..0000000 --- a/tests/test_pk.cpp +++ /dev/null @@ -1,236 +0,0 @@ -#include "olm/pk.h" -#include "olm/crypto.h" -#include "olm/olm.h" - -#include "unittest.hh" - -#include <iostream> -#include <vector> - -int main() { - - -{ /* Encryption Test Case 1 */ - -TestCase test_case("Public Key Encryption/Decryption Test Case 1"); - -std::vector<std::uint8_t> decryption_buffer(olm_pk_decryption_size()); -OlmPkDecryption *decryption = olm_pk_decryption(decryption_buffer.data()); - -std::uint8_t alice_private[32] = { - 0x77, 0x07, 0x6D, 0x0A, 0x73, 0x18, 0xA5, 0x7D, - 0x3C, 0x16, 0xC1, 0x72, 0x51, 0xB2, 0x66, 0x45, - 0xDF, 0x4C, 0x2F, 0x87, 0xEB, 0xC0, 0x99, 0x2A, - 0xB1, 0x77, 0xFB, 0xA5, 0x1D, 0xB9, 0x2C, 0x2A -}; - -const std::uint8_t *alice_public = (std::uint8_t *) "hSDwCYkwp1R0i33ctD73Wg2/Og0mOBr066SpjqqbTmo"; - -std::uint8_t bob_private[32] = { - 0x5D, 0xAB, 0x08, 0x7E, 0x62, 0x4A, 0x8A, 0x4B, - 0x79, 0xE1, 0x7F, 0x8B, 0x83, 0x80, 0x0E, 0xE6, - 0x6F, 0x3B, 0xB1, 0x29, 0x26, 0x18, 0xB6, 0xFD, - 0x1C, 0x2F, 0x8B, 0x27, 0xFF, 0x88, 0xE0, 0xEB -}; - -const std::uint8_t *bob_public = (std::uint8_t *) "3p7bfXt9wbTTW2HC7OQ1Nz+DQ8hbeGdNrfx+FG+IK08"; - -std::vector<std::uint8_t> pubkey(::olm_pk_key_length()); - -olm_pk_key_from_private( - decryption, - pubkey.data(), pubkey.size(), - alice_private, sizeof(alice_private) -); - -assert_equals(alice_public, pubkey.data(), olm_pk_key_length()); - -uint8_t *alice_private_back_out = (uint8_t *)malloc(olm_pk_private_key_length()); -olm_pk_get_private_key(decryption, alice_private_back_out, olm_pk_private_key_length()); -assert_equals(alice_private, alice_private_back_out, olm_pk_private_key_length()); -free(alice_private_back_out); - -std::vector<std::uint8_t> encryption_buffer(olm_pk_encryption_size()); -OlmPkEncryption *encryption = olm_pk_encryption(encryption_buffer.data()); - -olm_pk_encryption_set_recipient_key(encryption, pubkey.data(), pubkey.size()); - -const size_t plaintext_length = 14; -const std::uint8_t *plaintext = (std::uint8_t *) "This is a test"; - -size_t ciphertext_length = olm_pk_ciphertext_length(encryption, plaintext_length); -std::uint8_t *ciphertext_buffer = (std::uint8_t *) malloc(ciphertext_length); - -std::vector<std::uint8_t> output_buffer(olm_pk_mac_length(encryption)); -std::vector<std::uint8_t> ephemeral_key(olm_pk_key_length()); - -olm_pk_encrypt( - encryption, - plaintext, plaintext_length, - ciphertext_buffer, ciphertext_length, - output_buffer.data(), output_buffer.size(), - ephemeral_key.data(), ephemeral_key.size(), - bob_private, sizeof(bob_private) -); - -assert_equals(bob_public, ephemeral_key.data(), olm_pk_key_length()); - -size_t max_plaintext_length = olm_pk_max_plaintext_length(decryption, ciphertext_length); -std::uint8_t *plaintext_buffer = (std::uint8_t *) malloc(max_plaintext_length); - -olm_pk_decrypt( - decryption, - ephemeral_key.data(), ephemeral_key.size(), - output_buffer.data(), output_buffer.size(), - ciphertext_buffer, ciphertext_length, - plaintext_buffer, max_plaintext_length -); - -assert_equals(plaintext, plaintext_buffer, plaintext_length); - -free(ciphertext_buffer); -free(plaintext_buffer); - -} - -{ /* Encryption Test Case 1 */ - -TestCase test_case("Public Key Decryption pickling"); - -std::vector<std::uint8_t> decryption_buffer(olm_pk_decryption_size()); -OlmPkDecryption *decryption = olm_pk_decryption(decryption_buffer.data()); - -std::uint8_t alice_private[32] = { - 0x77, 0x07, 0x6D, 0x0A, 0x73, 0x18, 0xA5, 0x7D, - 0x3C, 0x16, 0xC1, 0x72, 0x51, 0xB2, 0x66, 0x45, - 0xDF, 0x4C, 0x2F, 0x87, 0xEB, 0xC0, 0x99, 0x2A, - 0xB1, 0x77, 0xFB, 0xA5, 0x1D, 0xB9, 0x2C, 0x2A -}; - -const std::uint8_t *alice_public = (std::uint8_t *) "hSDwCYkwp1R0i33ctD73Wg2/Og0mOBr066SpjqqbTmoK"; - -std::vector<std::uint8_t> pubkey(olm_pk_key_length()); - -olm_pk_key_from_private( - decryption, - pubkey.data(), pubkey.size(), - alice_private, sizeof(alice_private) -); - -const uint8_t *PICKLE_KEY=(uint8_t *)"secret_key"; -std::vector<std::uint8_t> pickle_buffer(olm_pickle_pk_decryption_length(decryption)); -const uint8_t *expected_pickle = (uint8_t *) "qx37WTQrjZLz5tId/uBX9B3/okqAbV1ofl9UnHKno1eipByCpXleAAlAZoJgYnCDOQZDQWzo3luTSfkF9pU1mOILCbbouubs6TVeDyPfgGD9i86J8irHjA"; - -olm_pickle_pk_decryption( - decryption, - PICKLE_KEY, strlen((char *)PICKLE_KEY), - pickle_buffer.data(), pickle_buffer.size() -); -assert_equals(expected_pickle, pickle_buffer.data(), olm_pickle_pk_decryption_length(decryption)); - -olm_clear_pk_decryption(decryption); - -memset(pubkey.data(), 0, olm_pk_key_length()); - -olm_unpickle_pk_decryption( - decryption, - PICKLE_KEY, strlen((char *)PICKLE_KEY), - pickle_buffer.data(), pickle_buffer.size(), - pubkey.data(), pubkey.size() -); - -assert_equals(alice_public, pubkey.data(), olm_pk_key_length()); - -char *ciphertext = strdup("ntk49j/KozVFtSqJXhCejg"); -const char *mac = "zpzU6BkZcNI"; -const char *ephemeral_key = "3p7bfXt9wbTTW2HC7OQ1Nz+DQ8hbeGdNrfx+FG+IK08"; - -size_t max_plaintext_length = olm_pk_max_plaintext_length(decryption, strlen(ciphertext)); -std::uint8_t *plaintext_buffer = (std::uint8_t *) malloc(max_plaintext_length); - -olm_pk_decrypt( - decryption, - ephemeral_key, strlen(ephemeral_key), - mac, strlen(mac), - ciphertext, strlen(ciphertext), - plaintext_buffer, max_plaintext_length -); - -const std::uint8_t *plaintext = (std::uint8_t *) "This is a test"; - -assert_equals(plaintext, plaintext_buffer, strlen((const char *)plaintext)); - -free(ciphertext); -free(plaintext_buffer); - -} - -{ /* Signing Test Case 1 */ - -TestCase test_case("Public Key Signing"); - -std::vector<std::uint8_t> signing_buffer(olm_pk_signing_size()); -OlmPkSigning *signing = olm_pk_signing(signing_buffer.data()); - -std::uint8_t seed[32] = { - 0x77, 0x07, 0x6D, 0x0A, 0x73, 0x18, 0xA5, 0x7D, - 0x3C, 0x16, 0xC1, 0x72, 0x51, 0xB2, 0x66, 0x45, - 0xDF, 0x4C, 0x2F, 0x87, 0xEB, 0xC0, 0x99, 0x2A, - 0xB1, 0x77, 0xFB, 0xA5, 0x1D, 0xB9, 0x2C, 0x2A -}; - -//const std::uint8_t *pub_key = (std::uint8_t *) "hSDwCYkwp1R0i33ctD73Wg2/Og0mOBr066SpjqqbTmoK"; - -std::vector<char> pubkey(olm_pk_signing_public_key_length() + 1); - -olm_pk_signing_key_from_seed( - signing, - pubkey.data(), pubkey.size() - 1, - seed, sizeof(seed) -); - -char *message = strdup("We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness."); - -std::uint8_t *sig_buffer = (std::uint8_t *) malloc(olm_pk_signature_length() + 1); - -olm_pk_sign( - signing, - (const uint8_t *)message, strlen(message), - sig_buffer, olm_pk_signature_length() -); - -void * utility_buffer = malloc(::olm_utility_size()); -::OlmUtility * utility = ::olm_utility(utility_buffer); - -size_t result; - -result = ::olm_ed25519_verify( - utility, - pubkey.data(), olm_pk_signing_public_key_length(), - message, strlen(message), - sig_buffer, olm_pk_signature_length() -); - -assert_equals((size_t)0, result); - -sig_buffer[5] = 'm'; - -result = ::olm_ed25519_verify( - utility, - pubkey.data(), olm_pk_signing_public_key_length(), - message, strlen(message), - sig_buffer, olm_pk_signature_length() -); - -assert_equals((size_t)-1, result); - -olm_clear_utility(utility); -free(utility_buffer); - -free(message); -free(sig_buffer); - -olm_clear_pk_signing(signing); - -} -} diff --git a/tests/test_ratchet.cpp b/tests/test_ratchet.cpp deleted file mode 100644 index 0408429..0000000 --- a/tests/test_ratchet.cpp +++ /dev/null @@ -1,222 +0,0 @@ -/* Copyright 2015 OpenMarket Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "olm/ratchet.hh" -#include "olm/cipher.h" -#include "unittest.hh" - -#include <vector> - -int main() { - -std::uint8_t root_info[] = "Olm"; -std::uint8_t ratchet_info[] = "OlmRatchet"; -std::uint8_t message_info[] = "OlmMessageKeys"; - -olm::KdfInfo kdf_info = { - root_info, sizeof(root_info) - 1, - ratchet_info, sizeof(ratchet_info) - 1 -}; - -_olm_cipher_aes_sha_256 cipher0 = OLM_CIPHER_INIT_AES_SHA_256(message_info); -_olm_cipher *cipher = OLM_CIPHER_BASE(&cipher0); - -std::uint8_t random_bytes[] = "0123456789ABDEF0123456789ABCDEF"; -_olm_curve25519_key_pair alice_key; -_olm_crypto_curve25519_generate_key(random_bytes, &alice_key); - -std::uint8_t shared_secret[] = "A secret"; - -{ /* Send/Receive test case */ -TestCase test_case("Olm Send/Receive"); - -olm::Ratchet alice(kdf_info, cipher); -olm::Ratchet bob(kdf_info, cipher); - -alice.initialise_as_alice(shared_secret, sizeof(shared_secret) - 1, alice_key); -bob.initialise_as_bob(shared_secret, sizeof(shared_secret) - 1, alice_key.public_key); - -std::uint8_t plaintext[] = "Message"; -std::size_t plaintext_length = sizeof(plaintext) - 1; - -std::size_t message_length, random_length, output_length; -std::size_t encrypt_length, decrypt_length; -{ - /* Alice sends Bob a message */ - message_length = alice.encrypt_output_length(plaintext_length); - random_length = alice.encrypt_random_length(); - assert_equals(std::size_t(0), random_length); - - std::vector<std::uint8_t> message(message_length); - - encrypt_length = alice.encrypt( - plaintext, plaintext_length, - NULL, 0, - message.data(), message_length - ); - assert_equals(message_length, encrypt_length); - - output_length = bob.decrypt_max_plaintext_length(message.data(), message_length); - std::vector<std::uint8_t> output(output_length); - decrypt_length = bob.decrypt( - message.data(), message_length, - output.data(), output_length - ); - assert_equals(plaintext_length, decrypt_length); - assert_equals(plaintext, output.data(), decrypt_length); -} - - -{ - /* Bob sends Alice a message */ - message_length = bob.encrypt_output_length(plaintext_length); - random_length = bob.encrypt_random_length(); - assert_equals(std::size_t(32), random_length); - - std::vector<std::uint8_t> message(message_length); - std::uint8_t random[] = "This is a random 32 byte string."; - - encrypt_length = bob.encrypt( - plaintext, plaintext_length, - random, 32, - message.data(), message_length - ); - assert_equals(message_length, encrypt_length); - - output_length = alice.decrypt_max_plaintext_length(message.data(), message_length); - std::vector<std::uint8_t> output(output_length); - decrypt_length = alice.decrypt( - message.data(), message_length, - output.data(), output_length - ); - assert_equals(plaintext_length, decrypt_length); - assert_equals(plaintext, output.data(), decrypt_length); -} - -} /* Send/receive message test case */ - -{ /* Out of order test case */ - -TestCase test_case("Olm Out of Order"); - -olm::Ratchet alice(kdf_info, cipher); -olm::Ratchet bob(kdf_info, cipher); - -alice.initialise_as_alice(shared_secret, sizeof(shared_secret) - 1, alice_key); -bob.initialise_as_bob(shared_secret, sizeof(shared_secret) - 1, alice_key.public_key); - -std::uint8_t plaintext_1[] = "First Message"; -std::size_t plaintext_1_length = sizeof(plaintext_1) - 1; - -std::uint8_t plaintext_2[] = "Second Messsage. A bit longer than the first."; -std::size_t plaintext_2_length = sizeof(plaintext_2) - 1; - -std::size_t message_1_length, message_2_length, random_length, output_length; -std::size_t encrypt_length, decrypt_length; - -{ - /* Alice sends Bob two messages and they arrive out of order */ - message_1_length = alice.encrypt_output_length(plaintext_1_length); - random_length = alice.encrypt_random_length(); - assert_equals(std::size_t(0), random_length); - - std::vector<std::uint8_t> message_1(message_1_length); - std::uint8_t random[] = "This is a random 32 byte string."; - encrypt_length = alice.encrypt( - plaintext_1, plaintext_1_length, - random, 32, - message_1.data(), message_1_length - ); - assert_equals(message_1_length, encrypt_length); - - message_2_length = alice.encrypt_output_length(plaintext_2_length); - random_length = alice.encrypt_random_length(); - assert_equals(std::size_t(0), random_length); - - std::vector<std::uint8_t> message_2(message_2_length); - encrypt_length = alice.encrypt( - plaintext_2, plaintext_2_length, - NULL, 0, - message_2.data(), message_2_length - ); - assert_equals(message_2_length, encrypt_length); - - output_length = bob.decrypt_max_plaintext_length( - message_2.data(), message_2_length - ); - std::vector<std::uint8_t> output_1(output_length); - decrypt_length = bob.decrypt( - message_2.data(), message_2_length, - output_1.data(), output_length - ); - assert_equals(plaintext_2_length, decrypt_length); - assert_equals(plaintext_2, output_1.data(), decrypt_length); - - output_length = bob.decrypt_max_plaintext_length( - message_1.data(), message_1_length - ); - std::vector<std::uint8_t> output_2(output_length); - decrypt_length = bob.decrypt( - message_1.data(), message_1_length, - output_2.data(), output_length - ); - - assert_equals(plaintext_1_length, decrypt_length); - assert_equals(plaintext_1, output_2.data(), decrypt_length); -} - -} /* Out of order test case */ - -{ /* More messages */ - -TestCase test_case("Olm More Messages"); - -olm::Ratchet alice(kdf_info, cipher); -olm::Ratchet bob(kdf_info, cipher); - -alice.initialise_as_alice(shared_secret, sizeof(shared_secret) - 1, alice_key); -bob.initialise_as_bob(shared_secret, sizeof(shared_secret) - 1, alice_key.public_key); - -std::uint8_t plaintext[] = "These 15 bytes"; -assert_equals(std::size_t(15), sizeof(plaintext)); -std::uint8_t random[] = "This is a random 32 byte string"; - -for (unsigned i = 0; i < 8; ++i) { -{ - std::vector<std::uint8_t> msg(alice.encrypt_output_length(sizeof(plaintext))); - alice.encrypt( - plaintext, 15, random, 32, msg.data(), msg.size() - ); - std::vector<std::uint8_t> output(bob.decrypt_max_plaintext_length(msg.data(), msg.size())); - assert_equals( - std::size_t(15), bob.decrypt(msg.data(), msg.size(), output.data(), output.size()) - ); -} -random[31]++; -{ - std::vector<std::uint8_t> msg(bob.encrypt_output_length(sizeof(plaintext))); - bob.encrypt( - plaintext, 15, random, 32, msg.data(), msg.size() - ); - std::vector<std::uint8_t> output(alice.decrypt_max_plaintext_length(msg.data(), msg.size())); - assert_equals( - std::size_t(15), alice.decrypt(msg.data(), msg.size(), output.data(), output.size()) - ); -} -random[31]++; -} - -} - -} diff --git a/tests/test_sas.cpp b/tests/test_sas.cpp deleted file mode 100644 index 48b9ee6..0000000 --- a/tests/test_sas.cpp +++ /dev/null @@ -1,118 +0,0 @@ -#include "olm/sas.h" -#include "olm/crypto.h" -#include "olm/olm.h" - -#include "unittest.hh" - -#include <iostream> -#include <vector> - -int main() { - - -{ /* Generate bytes */ - -TestCase test_case("SAS generate bytes"); - -std::uint8_t alice_private[32] = { - 0x77, 0x07, 0x6D, 0x0A, 0x73, 0x18, 0xA5, 0x7D, - 0x3C, 0x16, 0xC1, 0x72, 0x51, 0xB2, 0x66, 0x45, - 0xDF, 0x4C, 0x2F, 0x87, 0xEB, 0xC0, 0x99, 0x2A, - 0xB1, 0x77, 0xFB, 0xA5, 0x1D, 0xB9, 0x2C, 0x2A -}; - -const std::uint8_t *alice_public = (std::uint8_t *) "hSDwCYkwp1R0i33ctD73Wg2/Og0mOBr066SpjqqbTmo"; - -std::uint8_t bob_private[32] = { - 0x5D, 0xAB, 0x08, 0x7E, 0x62, 0x4A, 0x8A, 0x4B, - 0x79, 0xE1, 0x7F, 0x8B, 0x83, 0x80, 0x0E, 0xE6, - 0x6F, 0x3B, 0xB1, 0x29, 0x26, 0x18, 0xB6, 0xFD, - 0x1C, 0x2F, 0x8B, 0x27, 0xFF, 0x88, 0xE0, 0xEB -}; - -const std::uint8_t *bob_public = (std::uint8_t *) "3p7bfXt9wbTTW2HC7OQ1Nz+DQ8hbeGdNrfx+FG+IK08"; - -std::vector<std::uint8_t> alice_sas_buffer(olm_sas_size()); -OlmSAS *alice_sas = olm_sas(alice_sas_buffer.data()); -olm_create_sas(alice_sas, alice_private, sizeof(alice_private)); -std::vector<std::uint8_t> bob_sas_buffer(olm_sas_size()); -OlmSAS *bob_sas = olm_sas(bob_sas_buffer.data()); -olm_create_sas(bob_sas, bob_private, sizeof(bob_private)); - -std::vector<std::uint8_t> pubkey(::olm_sas_pubkey_length(alice_sas)); - -olm_sas_get_pubkey(alice_sas, pubkey.data(), pubkey.size()); - -assert_equals(alice_public, pubkey.data(), olm_sas_pubkey_length(alice_sas)); - -olm_sas_set_their_key(bob_sas, pubkey.data(), olm_sas_pubkey_length(bob_sas)); - -olm_sas_get_pubkey(bob_sas, pubkey.data(), pubkey.size()); - -assert_equals(bob_public, pubkey.data(), olm_sas_pubkey_length(bob_sas)); - -olm_sas_set_their_key(alice_sas, pubkey.data(), olm_sas_pubkey_length(alice_sas)); - -std::uint8_t alice_bytes[6]; -std::uint8_t bob_bytes[6]; - -olm_sas_generate_bytes(alice_sas, "SAS", 3, alice_bytes, 6); -olm_sas_generate_bytes(bob_sas, "SAS", 3, bob_bytes, 6); - -assert_equals(alice_bytes, bob_bytes, 6); - -} - -{ /* Calculate MAC */ - -TestCase test_case("SAS calculate MAC"); - -std::uint8_t alice_private[32] = { - 0x77, 0x07, 0x6D, 0x0A, 0x73, 0x18, 0xA5, 0x7D, - 0x3C, 0x16, 0xC1, 0x72, 0x51, 0xB2, 0x66, 0x45, - 0xDF, 0x4C, 0x2F, 0x87, 0xEB, 0xC0, 0x99, 0x2A, - 0xB1, 0x77, 0xFB, 0xA5, 0x1D, 0xB9, 0x2C, 0x2A -}; - -const std::uint8_t *alice_public = (std::uint8_t *) "hSDwCYkwp1R0i33ctD73Wg2/Og0mOBr066SpjqqbTmo"; - -std::uint8_t bob_private[32] = { - 0x5D, 0xAB, 0x08, 0x7E, 0x62, 0x4A, 0x8A, 0x4B, - 0x79, 0xE1, 0x7F, 0x8B, 0x83, 0x80, 0x0E, 0xE6, - 0x6F, 0x3B, 0xB1, 0x29, 0x26, 0x18, 0xB6, 0xFD, - 0x1C, 0x2F, 0x8B, 0x27, 0xFF, 0x88, 0xE0, 0xEB -}; - -const std::uint8_t *bob_public = (std::uint8_t *) "3p7bfXt9wbTTW2HC7OQ1Nz+DQ8hbeGdNrfx+FG+IK08"; - -std::vector<std::uint8_t> alice_sas_buffer(olm_sas_size()); -OlmSAS *alice_sas = olm_sas(alice_sas_buffer.data()); -olm_create_sas(alice_sas, alice_private, sizeof(alice_private)); -std::vector<std::uint8_t> bob_sas_buffer(olm_sas_size()); -OlmSAS *bob_sas = olm_sas(bob_sas_buffer.data()); -olm_create_sas(bob_sas, bob_private, sizeof(bob_private)); - -std::vector<std::uint8_t> pubkey(::olm_sas_pubkey_length(alice_sas)); - -olm_sas_get_pubkey(alice_sas, pubkey.data(), pubkey.size()); - -assert_equals(alice_public, pubkey.data(), olm_sas_pubkey_length(alice_sas)); - -olm_sas_set_their_key(bob_sas, pubkey.data(), olm_sas_pubkey_length(bob_sas)); - -olm_sas_get_pubkey(bob_sas, pubkey.data(), pubkey.size()); - -assert_equals(bob_public, pubkey.data(), olm_sas_pubkey_length(bob_sas)); - -olm_sas_set_their_key(alice_sas, pubkey.data(), olm_sas_pubkey_length(alice_sas)); - -std::vector<std::uint8_t> alice_mac(olm_sas_mac_length(alice_sas)); -std::vector<std::uint8_t> bob_mac(olm_sas_mac_length(bob_sas)); - -olm_sas_calculate_mac(alice_sas, (void *) "Hello world!", 12, "MAC", 3, alice_mac.data(), olm_sas_mac_length(alice_sas)); -olm_sas_calculate_mac(bob_sas, (void *) "Hello world!", 12, "MAC", 3, bob_mac.data(), olm_sas_mac_length(bob_sas)); - -assert_equals(alice_mac.data(), bob_mac.data(), olm_sas_mac_length(alice_sas)); - -} -} diff --git a/tests/test_session.cpp b/tests/test_session.cpp deleted file mode 100644 index e2c3199..0000000 --- a/tests/test_session.cpp +++ /dev/null @@ -1,144 +0,0 @@ -#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.public_key, 32 - ); - - assert_equals( - decode_hex("d945c6ed4c7c277117adf11fb133a7936d287afe97c0b3ac989644b4490d4f31"), - session.ratchet.sender_chain[0].ratchet_key.private_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[] = - "m+DS/q34MXpw2xp50ZD0B7val1mlMpQXo0mx+VPje0weFYRRuuZQBdJgcFPEpi2MVSpA4c" - "qgqHyj2/bU7/lz+BXkEBrCFVx0BJidxXfOLDW4TNtRhLS1YHJNGP8GvTg1+dCytBTLsCdm" - "5f945Eq1U/pY3Cg96YTUufFP6EYrfRoDbAsRHc+h+wKKftQv+W44yUmRhcCemGHtpxk3UQ" - "AMCI7EBv9BvveyZMy3p9qZ3xvFK34Hef+R7gjtFycz7Nk/4UF46sT3cTmUlXz9iFW4uz2F" - "rTI1Wjym+l0DadsbSpHSUjmp9zt4qRP2UjwfZ5QNLv+cdObIfqFsiThGu/PlKigdF4SLHr" - "nG"; - - 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; -} |