Commit ea13edca authored by Hubert Chathi's avatar Hubert Chathi
Browse files

don't use variable length or zero-length arrays in test files

as some compilers don't handle that
parent 157c0fa6
...@@ -14,7 +14,7 @@ std::size_t input_length = sizeof(input) - 1; ...@@ -14,7 +14,7 @@ std::size_t input_length = sizeof(input) - 1;
std::size_t output_length = olm::encode_base64_length(input_length); std::size_t output_length = olm::encode_base64_length(input_length);
assert_equals(std::size_t(15), output_length); assert_equals(std::size_t(15), output_length);
std::uint8_t output[output_length]; std::uint8_t output[15];
olm::encode_base64(input, input_length, output); olm::encode_base64(input, input_length, output);
assert_equals(expected_output, output, output_length); assert_equals(expected_output, output, output_length);
} }
...@@ -29,7 +29,7 @@ std::size_t input_length = sizeof(input) - 1; ...@@ -29,7 +29,7 @@ std::size_t input_length = sizeof(input) - 1;
std::size_t output_length = ::_olm_encode_base64_length(input_length); std::size_t output_length = ::_olm_encode_base64_length(input_length);
assert_equals(std::size_t(15), output_length); assert_equals(std::size_t(15), output_length);
std::uint8_t output[output_length]; std::uint8_t output[15];
output_length = ::_olm_encode_base64(input, input_length, output); output_length = ::_olm_encode_base64(input, input_length, output);
assert_equals(std::size_t(15), output_length); assert_equals(std::size_t(15), output_length);
assert_equals(expected_output, output, output_length); assert_equals(expected_output, output, output_length);
...@@ -45,7 +45,7 @@ std::size_t input_length = sizeof(input) - 1; ...@@ -45,7 +45,7 @@ std::size_t input_length = sizeof(input) - 1;
std::size_t output_length = olm::decode_base64_length(input_length); std::size_t output_length = olm::decode_base64_length(input_length);
assert_equals(std::size_t(11), output_length); assert_equals(std::size_t(11), output_length);
std::uint8_t output[output_length]; std::uint8_t output[11];
olm::decode_base64(input, input_length, output); olm::decode_base64(input, input_length, output);
assert_equals(expected_output, output, output_length); assert_equals(expected_output, output, output_length);
} }
...@@ -60,7 +60,7 @@ std::size_t input_length = sizeof(input) - 1; ...@@ -60,7 +60,7 @@ std::size_t input_length = sizeof(input) - 1;
std::size_t output_length = ::_olm_decode_base64_length(input_length); std::size_t output_length = ::_olm_decode_base64_length(input_length);
assert_equals(std::size_t(11), output_length); assert_equals(std::size_t(11), output_length);
std::uint8_t output[output_length]; std::uint8_t output[11];
output_length = ::_olm_decode_base64(input, input_length, output); output_length = ::_olm_decode_base64(input, input_length, output);
assert_equals(std::size_t(11), output_length); assert_equals(std::size_t(11), output_length);
assert_equals(expected_output, output, output_length); assert_equals(expected_output, output, output_length);
......
...@@ -146,7 +146,10 @@ assert_equals(input, actual, length); ...@@ -146,7 +146,10 @@ assert_equals(input, actual, length);
TestCase test_case("SHA 256 Test Case 1"); TestCase test_case("SHA 256 Test Case 1");
std::uint8_t input[0] = {}; // 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] = { std::uint8_t expected[32] = {
0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14, 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
...@@ -157,7 +160,7 @@ std::uint8_t expected[32] = { ...@@ -157,7 +160,7 @@ std::uint8_t expected[32] = {
std::uint8_t actual[32]; std::uint8_t actual[32];
_olm_crypto_sha256(input, sizeof(input), actual); _olm_crypto_sha256(input, 0, actual);
assert_equals(expected, actual, 32); assert_equals(expected, actual, 32);
...@@ -167,7 +170,10 @@ assert_equals(expected, actual, 32); ...@@ -167,7 +170,10 @@ assert_equals(expected, actual, 32);
TestCase test_case("HMAC Test Case 1"); TestCase test_case("HMAC Test Case 1");
std::uint8_t input[0] = {}; // 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] = { std::uint8_t expected[32] = {
0xb6, 0x13, 0x67, 0x9a, 0x08, 0x14, 0xd9, 0xec, 0xb6, 0x13, 0x67, 0x9a, 0x08, 0x14, 0xd9, 0xec,
...@@ -178,7 +184,7 @@ std::uint8_t expected[32] = { ...@@ -178,7 +184,7 @@ std::uint8_t expected[32] = {
std::uint8_t actual[32]; std::uint8_t actual[32];
_olm_crypto_hmac_sha256(input, sizeof(input), input, sizeof(input), actual); _olm_crypto_hmac_sha256(input, 0, input, 0, actual);
assert_equals(expected, actual, 32); assert_equals(expected, actual, 32);
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "olm/outbound_group_session.h" #include "olm/outbound_group_session.h"
#include "unittest.hh" #include "unittest.hh"
#include <vector>
int main() { int main() {
...@@ -23,33 +24,32 @@ int main() { ...@@ -23,33 +24,32 @@ int main() {
TestCase test_case("Pickle outbound group session"); TestCase test_case("Pickle outbound group session");
size_t size = olm_outbound_group_session_size(); size_t size = olm_outbound_group_session_size();
uint8_t memory[size]; std::vector<uint8_t> memory(size);
OlmOutboundGroupSession *session = olm_outbound_group_session(memory); OlmOutboundGroupSession *session = olm_outbound_group_session(memory.data());
size_t pickle_length = olm_pickle_outbound_group_session_length(session); size_t pickle_length = olm_pickle_outbound_group_session_length(session);
uint8_t pickle1[pickle_length]; std::vector<uint8_t> pickle1(pickle_length);
size_t res = olm_pickle_outbound_group_session( size_t res = olm_pickle_outbound_group_session(
session, "secret_key", 10, pickle1, pickle_length session, "secret_key", 10, pickle1.data(), pickle_length
); );
assert_equals(pickle_length, res); assert_equals(pickle_length, res);
uint8_t pickle2[pickle_length]; std::vector<uint8_t> pickle2(pickle1);
memcpy(pickle2, pickle1, pickle_length);
uint8_t buffer2[size]; std::vector<uint8_t> buffer2(size);
OlmOutboundGroupSession *session2 = olm_outbound_group_session(buffer2); OlmOutboundGroupSession *session2 = olm_outbound_group_session(buffer2.data());
res = olm_unpickle_outbound_group_session( res = olm_unpickle_outbound_group_session(
session2, "secret_key", 10, pickle2, pickle_length session2, "secret_key", 10, pickle2.data(), pickle_length
); );
assert_not_equals((size_t)-1, res); assert_not_equals((size_t)-1, res);
assert_equals(pickle_length, assert_equals(pickle_length,
olm_pickle_outbound_group_session_length(session2)); olm_pickle_outbound_group_session_length(session2));
res = olm_pickle_outbound_group_session( res = olm_pickle_outbound_group_session(
session2, "secret_key", 10, pickle2, pickle_length session2, "secret_key", 10, pickle2.data(), pickle_length
); );
assert_equals(pickle_length, res); assert_equals(pickle_length, res);
assert_equals(pickle1, pickle2, pickle_length); assert_equals(pickle1.data(), pickle2.data(), pickle_length);
} }
...@@ -57,32 +57,31 @@ int main() { ...@@ -57,32 +57,31 @@ int main() {
TestCase test_case("Pickle inbound group session"); TestCase test_case("Pickle inbound group session");
size_t size = olm_inbound_group_session_size(); size_t size = olm_inbound_group_session_size();
uint8_t memory[size]; std::vector<uint8_t> memory(size);
OlmInboundGroupSession *session = olm_inbound_group_session(memory); OlmInboundGroupSession *session = olm_inbound_group_session(memory.data());
size_t pickle_length = olm_pickle_inbound_group_session_length(session); size_t pickle_length = olm_pickle_inbound_group_session_length(session);
uint8_t pickle1[pickle_length]; std::vector<uint8_t> pickle1(pickle_length);
size_t res = olm_pickle_inbound_group_session( size_t res = olm_pickle_inbound_group_session(
session, "secret_key", 10, pickle1, pickle_length session, "secret_key", 10, pickle1.data(), pickle_length
); );
assert_equals(pickle_length, res); assert_equals(pickle_length, res);
uint8_t pickle2[pickle_length]; std::vector<uint8_t> pickle2(pickle1);
memcpy(pickle2, pickle1, pickle_length);
uint8_t buffer2[size]; std::vector<uint8_t> buffer2(size);
OlmInboundGroupSession *session2 = olm_inbound_group_session(buffer2); OlmInboundGroupSession *session2 = olm_inbound_group_session(buffer2.data());
res = olm_unpickle_inbound_group_session( res = olm_unpickle_inbound_group_session(
session2, "secret_key", 10, pickle2, pickle_length session2, "secret_key", 10, pickle2.data(), pickle_length
); );
assert_not_equals((size_t)-1, res); assert_not_equals((size_t)-1, res);
assert_equals(pickle_length, assert_equals(pickle_length,
olm_pickle_inbound_group_session_length(session2)); olm_pickle_inbound_group_session_length(session2));
res = olm_pickle_inbound_group_session( res = olm_pickle_inbound_group_session(
session2, "secret_key", 10, pickle2, pickle_length session2, "secret_key", 10, pickle2.data(), pickle_length
); );
assert_equals(pickle1, pickle2, pickle_length); assert_equals(pickle1.data(), pickle2.data(), pickle_length);
} }
{ {
...@@ -99,8 +98,8 @@ int main() { ...@@ -99,8 +98,8 @@ int main() {
/* build the outbound session */ /* build the outbound session */
size_t size = olm_outbound_group_session_size(); size_t size = olm_outbound_group_session_size();
uint8_t memory[size]; std::vector<uint8_t> memory(size);
OlmOutboundGroupSession *session = olm_outbound_group_session(memory); OlmOutboundGroupSession *session = olm_outbound_group_session(memory.data());
assert_equals((size_t)160, assert_equals((size_t)160,
olm_init_outbound_group_session_random_length(session)); olm_init_outbound_group_session_random_length(session));
...@@ -111,8 +110,8 @@ int main() { ...@@ -111,8 +110,8 @@ int main() {
assert_equals(0U, olm_outbound_group_session_message_index(session)); assert_equals(0U, olm_outbound_group_session_message_index(session));
size_t session_key_len = olm_outbound_group_session_key_length(session); size_t session_key_len = olm_outbound_group_session_key_length(session);
uint8_t session_key[session_key_len]; std::vector<uint8_t> session_key(session_key_len);
olm_outbound_group_session_key(session, session_key, session_key_len); olm_outbound_group_session_key(session, session_key.data(), session_key_len);
/* encode the message */ /* encode the message */
uint8_t plaintext[] = "Message"; uint8_t plaintext[] = "Message";
...@@ -121,57 +120,56 @@ int main() { ...@@ -121,57 +120,56 @@ int main() {
size_t msglen = olm_group_encrypt_message_length( size_t msglen = olm_group_encrypt_message_length(
session, plaintext_length); session, plaintext_length);
uint8_t msg[msglen]; std::vector<uint8_t> msg(msglen);
res = olm_group_encrypt(session, plaintext, plaintext_length, res = olm_group_encrypt(session, plaintext, plaintext_length,
msg, msglen); msg.data(), msglen);
assert_equals(msglen, res); assert_equals(msglen, res);
assert_equals(1U, olm_outbound_group_session_message_index(session)); assert_equals(1U, olm_outbound_group_session_message_index(session));
/* build the inbound session */ /* build the inbound session */
size = olm_inbound_group_session_size(); size = olm_inbound_group_session_size();
uint8_t inbound_session_memory[size]; std::vector<uint8_t> inbound_session_memory(size);
OlmInboundGroupSession *inbound_session = OlmInboundGroupSession *inbound_session =
olm_inbound_group_session(inbound_session_memory); olm_inbound_group_session(inbound_session_memory.data());
assert_equals(0, olm_inbound_group_session_is_verified(inbound_session)); assert_equals(0, olm_inbound_group_session_is_verified(inbound_session));
res = olm_init_inbound_group_session( res = olm_init_inbound_group_session(
inbound_session, session_key, session_key_len); inbound_session, session_key.data(), session_key_len);
assert_equals((size_t)0, res); assert_equals((size_t)0, res);
assert_equals(1, olm_inbound_group_session_is_verified(inbound_session)); assert_equals(1, olm_inbound_group_session_is_verified(inbound_session));
/* Check the session ids */ /* Check the session ids */
size_t out_session_id_len = olm_outbound_group_session_id_length(session); size_t out_session_id_len = olm_outbound_group_session_id_length(session);
uint8_t out_session_id[out_session_id_len]; std::vector<uint8_t> out_session_id(out_session_id_len);
assert_equals(out_session_id_len, olm_outbound_group_session_id( assert_equals(out_session_id_len, olm_outbound_group_session_id(
session, out_session_id, out_session_id_len session, out_session_id.data(), out_session_id_len
)); ));
size_t in_session_id_len = olm_inbound_group_session_id_length( size_t in_session_id_len = olm_inbound_group_session_id_length(
inbound_session inbound_session
); );
uint8_t in_session_id[in_session_id_len]; std::vector<uint8_t> in_session_id(in_session_id_len);
assert_equals(in_session_id_len, olm_inbound_group_session_id( assert_equals(in_session_id_len, olm_inbound_group_session_id(
inbound_session, in_session_id, in_session_id_len inbound_session, in_session_id.data(), in_session_id_len
)); ));
assert_equals(in_session_id_len, out_session_id_len); assert_equals(in_session_id_len, out_session_id_len);
assert_equals(out_session_id, in_session_id, in_session_id_len); assert_equals(out_session_id.data(), in_session_id.data(), in_session_id_len);
/* decode the message */ /* decode the message */
/* olm_group_decrypt_max_plaintext_length destroys the input so we have to /* olm_group_decrypt_max_plaintext_length destroys the input so we have to
copy it. */ copy it. */
uint8_t msgcopy[msglen]; std::vector<uint8_t> msgcopy(msg);
memcpy(msgcopy, msg, msglen); size = olm_group_decrypt_max_plaintext_length(inbound_session, msgcopy.data(), msglen);
size = olm_group_decrypt_max_plaintext_length(inbound_session, msgcopy, msglen); std::vector<uint8_t> plaintext_buf(size);
uint8_t plaintext_buf[size];
uint32_t message_index; uint32_t message_index;
res = olm_group_decrypt(inbound_session, msg, msglen, res = olm_group_decrypt(inbound_session, msg.data(), msglen,
plaintext_buf, size, &message_index); plaintext_buf.data(), size, &message_index);
assert_equals(plaintext_length, res); assert_equals(plaintext_length, res);
assert_equals(plaintext, plaintext_buf, res); assert_equals(plaintext, plaintext_buf.data(), res);
assert_equals(message_index, uint32_t(0)); assert_equals(message_index, uint32_t(0));
} }
...@@ -192,9 +190,9 @@ int main() { ...@@ -192,9 +190,9 @@ int main() {
/* init first inbound group session, and decrypt */ /* init first inbound group session, and decrypt */
std::size_t size = olm_inbound_group_session_size(); std::size_t size = olm_inbound_group_session_size();
uint8_t session_memory1[size]; std::vector<uint8_t> session_memory1(size);
OlmInboundGroupSession *session1 = OlmInboundGroupSession *session1 =
olm_inbound_group_session(session_memory1); olm_inbound_group_session(session_memory1.data());
assert_equals(0, olm_inbound_group_session_is_verified(session1)); assert_equals(0, olm_inbound_group_session_is_verified(session1));
std::size_t res = olm_init_inbound_group_session( std::size_t res = olm_init_inbound_group_session(
...@@ -205,24 +203,24 @@ int main() { ...@@ -205,24 +203,24 @@ int main() {
/* olm_group_decrypt_max_plaintext_length destroys the input so we have to /* olm_group_decrypt_max_plaintext_length destroys the input so we have to
copy it. */ copy it. */
uint8_t msgcopy[msglen]; std::vector<uint8_t> msgcopy(msglen);
memcpy(msgcopy, message, msglen); memcpy(msgcopy.data(), message, msglen);
size = olm_group_decrypt_max_plaintext_length(session1, msgcopy, msglen); size = olm_group_decrypt_max_plaintext_length(session1, msgcopy.data(), msglen);
uint8_t plaintext_buf[size]; std::vector<uint8_t> plaintext_buf(size);
uint32_t message_index; uint32_t message_index;
memcpy(msgcopy, message, msglen); memcpy(msgcopy.data(), message, msglen);
res = olm_group_decrypt( res = olm_group_decrypt(
session1, msgcopy, msglen, plaintext_buf, size, &message_index session1, msgcopy.data(), msglen, plaintext_buf.data(), size, &message_index
); );
assert_equals((std::size_t)7, res); assert_equals((std::size_t)7, res);
assert_equals((const uint8_t *)"Message", plaintext_buf, res); assert_equals((const uint8_t *)"Message", plaintext_buf.data(), res);
assert_equals(uint32_t(0), message_index); assert_equals(uint32_t(0), message_index);
/* export the keys */ /* export the keys */
size = olm_export_inbound_group_session_length(session1); size = olm_export_inbound_group_session_length(session1);
uint8_t export_memory[size]; std::vector<uint8_t> export_memory(size);
res = olm_export_inbound_group_session( res = olm_export_inbound_group_session(
session1, export_memory, size, 0 session1, export_memory.data(), size, 0
); );
assert_equals(size, res); assert_equals(size, res);
...@@ -231,25 +229,25 @@ int main() { ...@@ -231,25 +229,25 @@ int main() {
/* import the keys into another inbound group session */ /* import the keys into another inbound group session */
size = olm_inbound_group_session_size(); size = olm_inbound_group_session_size();
uint8_t session_memory2[size]; std::vector<uint8_t> session_memory2(size);
OlmInboundGroupSession *session2 = OlmInboundGroupSession *session2 =
olm_inbound_group_session(session_memory2); olm_inbound_group_session(session_memory2.data());
res = olm_import_inbound_group_session( res = olm_import_inbound_group_session(
session2, export_memory, sizeof(export_memory) session2, export_memory.data(), export_memory.size()
); );
assert_equals((size_t)0, res); assert_equals((size_t)0, res);
assert_equals(0, olm_inbound_group_session_is_verified(session2)); assert_equals(0, olm_inbound_group_session_is_verified(session2));
/* decrypt the message with the new session */ /* decrypt the message with the new session */
memcpy(msgcopy, message, msglen); memcpy(msgcopy.data(), message, msglen);
size = olm_group_decrypt_max_plaintext_length(session2, msgcopy, msglen); size = olm_group_decrypt_max_plaintext_length(session2, msgcopy.data(), msglen);
uint8_t plaintext_buf2[size]; std::vector<uint8_t> plaintext_buf2(size);
memcpy(msgcopy, message, msglen); memcpy(msgcopy.data(), message, msglen);
res = olm_group_decrypt( res = olm_group_decrypt(
session2, msgcopy, msglen, plaintext_buf2, size, &message_index session2, msgcopy.data(), msglen, plaintext_buf2.data(), size, &message_index
); );
assert_equals((std::size_t)7, res); assert_equals((std::size_t)7, res);
assert_equals((const uint8_t *)"Message", plaintext_buf2, res); assert_equals((const uint8_t *)"Message", plaintext_buf2.data(), res);
assert_equals(uint32_t(0), message_index); assert_equals(uint32_t(0), message_index);
assert_equals(1, olm_inbound_group_session_is_verified(session2)); assert_equals(1, olm_inbound_group_session_is_verified(session2));
} }
...@@ -274,9 +272,9 @@ int main() { ...@@ -274,9 +272,9 @@ int main() {
/* build the inbound session */ /* build the inbound session */
size_t size = olm_inbound_group_session_size(); size_t size = olm_inbound_group_session_size();
uint8_t inbound_session_memory[size]; std::vector<uint8_t> inbound_session_memory(size);
OlmInboundGroupSession *inbound_session = OlmInboundGroupSession *inbound_session =
olm_inbound_group_session(inbound_session_memory); olm_inbound_group_session(inbound_session_memory.data());
size_t res = olm_init_inbound_group_session( size_t res = olm_init_inbound_group_session(
inbound_session, session_key, sizeof(session_key)-1 inbound_session, session_key, sizeof(session_key)-1
...@@ -287,36 +285,36 @@ int main() { ...@@ -287,36 +285,36 @@ int main() {
/* olm_group_decrypt_max_plaintext_length destroys the input so we have to /* olm_group_decrypt_max_plaintext_length destroys the input so we have to
copy it. */ copy it. */
uint8_t msgcopy[msglen]; std::vector<uint8_t> msgcopy(msglen);
memcpy(msgcopy, message, msglen); memcpy(msgcopy.data(), message, msglen);
size = olm_group_decrypt_max_plaintext_length( size = olm_group_decrypt_max_plaintext_length(
inbound_session, msgcopy, msglen inbound_session, msgcopy.data(), msglen
); );
memcpy(msgcopy, message, msglen); memcpy(msgcopy.data(), message, msglen);
uint8_t plaintext_buf[size]; std::vector<uint8_t> plaintext_buf(size);
uint32_t message_index; uint32_t message_index;
res = olm_group_decrypt( res = olm_group_decrypt(
inbound_session, msgcopy, msglen, plaintext_buf, size, &message_index inbound_session, msgcopy.data(), msglen, plaintext_buf.data(), size, &message_index
); );
assert_equals(message_index, uint32_t(0)); assert_equals(message_index, uint32_t(0));
assert_equals(plaintext_length, res); assert_equals(plaintext_length, res);
assert_equals(plaintext, plaintext_buf, res); assert_equals(plaintext, plaintext_buf.data(), res);
/* now twiddle the signature */ /* now twiddle the signature */
message[msglen-1] = 'E'; message[msglen-1] = 'E';
memcpy(msgcopy, message, msglen); memcpy(msgcopy.data(), message, msglen);
assert_equals( assert_equals(
size, size,
olm_group_decrypt_max_plaintext_length( olm_group_decrypt_max_plaintext_length(
inbound_session, msgcopy, msglen inbound_session, msgcopy.data(), msglen
) )
); );
memcpy(msgcopy, message, msglen); memcpy(msgcopy.data(), message, msglen);
res = olm_group_decrypt( res = olm_group_decrypt(
inbound_session, msgcopy, msglen, inbound_session, msgcopy.data(), msglen,
plaintext_buf, size, &message_index plaintext_buf.data(), size, &message_index
); );
assert_equals((size_t)-1, res); assert_equals((size_t)-1, res);
assert_equals( assert_equals(
......
...@@ -49,7 +49,7 @@ TestCase test_case("Message encode test"); ...@@ -49,7 +49,7 @@ TestCase test_case("Message encode test");
std::size_t length = olm::encode_message_length(1, 10, 10, 8); std::size_t length = olm::encode_message_length(1, 10, 10, 8);
assert_equals(std::size_t(35), length); assert_equals(std::size_t(35), length);
std::uint8_t output[length]; std::uint8_t output[35];
olm::MessageWriter writer; olm::MessageWriter writer;
olm::encode_message(writer, 3, 1, 10, 10, output); olm::encode_message(writer, 3, 1, 10, 10, output);
......
This diff is collapsed.
#include "olm/olm.h" #include "olm/olm.h"
#include "unittest.hh" #include "unittest.hh"
#include <vector>
struct test_case { struct test_case {
const char *msghex; const char *msghex;
const char *expected_error; const char *expected_error;
...@@ -37,14 +39,14 @@ void decode_hex( ...@@ -37,14 +39,14 @@ void decode_hex(
} }
void decrypt_case(int message_type, const test_case * test_case) {