1 /* Copyright 2015-2016 OpenMarket Ltd
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "olm/message.hh"
16 #include "unittest.hh"
17 
main()18 int main() {
19 
20 std::uint8_t message1[36] = "\x03\x10\x01\n\nratchetkey\"\nciphertexthmacsha2";
21 std::uint8_t message2[36] = "\x03\n\nratchetkey\x10\x01\"\nciphertexthmacsha2";
22 std::uint8_t ratchetkey[11] = "ratchetkey";
23 std::uint8_t ciphertext[11] = "ciphertext";
24 std::uint8_t hmacsha2[9] = "hmacsha2";
25 
26 { /* Message decode test */
27 
28 TestCase test_case("Message decode test");
29 
30 olm::MessageReader reader;
31 olm::decode_message(reader, message1, 35, 8);
32 
33 assert_equals(std::uint8_t(3), reader.version);
34 assert_equals(true, reader.has_counter);
35 assert_equals(std::uint32_t(1), reader.counter);
36 assert_equals(std::size_t(10), reader.ratchet_key_length);
37 assert_equals(std::size_t(10), reader.ciphertext_length);
38 
39 assert_equals(ratchetkey, reader.ratchet_key, 10);
40 assert_equals(ciphertext, reader.ciphertext, 10);
41 
42 
43 } /* Message decode test */
44 
45 { /* Message encode test */
46 
47 TestCase test_case("Message encode test");
48 
49 std::size_t length = olm::encode_message_length(1, 10, 10, 8);
50 assert_equals(std::size_t(35), length);
51 
52 std::uint8_t output[35];
53 
54 olm::MessageWriter writer;
55 olm::encode_message(writer, 3, 1, 10, 10, output);
56 
57 std::memcpy(writer.ratchet_key, ratchetkey, 10);
58 std::memcpy(writer.ciphertext, ciphertext, 10);
59 std::memcpy(output + length - 8, hmacsha2, 8);
60 
61 assert_equals(message2, output, 35);
62 
63 } /* Message encode test */
64 
65 
66 { /* group message encode test */
67 
68     TestCase test_case("Group message encode test");
69 
70     size_t length = _olm_encode_group_message_length(200, 10, 8, 64);
71     size_t expected_length = 1 + (1+2) + (2+10) + 8 + 64;
72     assert_equals(expected_length, length);
73 
74     uint8_t output[50];
75     uint8_t *ciphertext_ptr;
76 
77     _olm_encode_group_message(
78         3,
79         200, // counter
80         10,  // ciphertext length
81         output,
82         &ciphertext_ptr
83     );
84 
85     uint8_t expected[] =
86         "\x03"
87         "\x08\xC8\x01"
88         "\x12\x0A";
89 
90     assert_equals(expected, output, sizeof(expected)-1);
91     assert_equals(output+sizeof(expected)-1, ciphertext_ptr);
92 } /* group message encode test */
93 
94 {
95     TestCase test_case("Group message decode test");
96 
97     struct _OlmDecodeGroupMessageResults results;
98     std::uint8_t message[] =
99         "\x03"
100         "\x08\xC8\x01"
101         "\x12\x0A" "ciphertext"
102         "hmacsha2"
103         "ed25519signature";
104 
105     _olm_decode_group_message(message, sizeof(message)-1, 8, 16, &results);
106     assert_equals(std::uint8_t(3), results.version);
107     assert_equals(1, results.has_message_index);
108     assert_equals(std::uint32_t(200), results.message_index);
109     assert_equals(std::size_t(10), results.ciphertext_length);
110     assert_equals(ciphertext, results.ciphertext, 10);
111 } /* group message decode test */
112 }
113