summaryrefslogtreecommitdiff
path: root/auto_tests
diff options
context:
space:
mode:
Diffstat (limited to 'auto_tests')
-rw-r--r--auto_tests/encryptsave_test.c124
1 files changed, 66 insertions, 58 deletions
diff --git a/auto_tests/encryptsave_test.c b/auto_tests/encryptsave_test.c
index e2f41f43..9ab29a27 100644
--- a/auto_tests/encryptsave_test.c
+++ b/auto_tests/encryptsave_test.c
@@ -55,7 +55,6 @@ END_TEST
55 55
56START_TEST(test_save_friend) 56START_TEST(test_save_friend)
57{ 57{
58 TOX_ERR_ENCRYPTED_NEW err = TOX_ERR_ENCRYPTED_NEW_OK;
59 Tox *tox1 = tox_new(0, 0, 0, 0); 58 Tox *tox1 = tox_new(0, 0, 0, 0);
60 Tox *tox2 = tox_new(0, 0, 0, 0); 59 Tox *tox2 = tox_new(0, 0, 0, 0);
61 ck_assert_msg(tox1 || tox2, "Failed to create 2 tox instances"); 60 ck_assert_msg(tox1 || tox2, "Failed to create 2 tox instances");
@@ -63,99 +62,108 @@ START_TEST(test_save_friend)
63 tox_callback_friend_request(tox2, accept_friend_request, &to_compare); 62 tox_callback_friend_request(tox2, accept_friend_request, &to_compare);
64 uint8_t address[TOX_ADDRESS_SIZE]; 63 uint8_t address[TOX_ADDRESS_SIZE];
65 tox_self_get_address(tox2, address); 64 tox_self_get_address(tox2, address);
66 int test = tox_friend_add(tox1, address, (uint8_t *)"Gentoo", 7, 0); 65 uint32_t test = tox_friend_add(tox1, address, (uint8_t *)"Gentoo", 7, 0);
67 ck_assert_msg(test == 0, "Failed to add friend error code: %i", test); 66 ck_assert_msg(test != UINT32_MAX, "Failed to add friend");
68 67
69 uint32_t size = tox_encrypted_size(tox1); 68 size_t size = tox_get_savedata_size(tox1);
70 uint8_t data[size]; 69 uint8_t data[size];
71 test = tox_encrypted_save(tox1, data, "correcthorsebatterystaple", 25); 70 tox_get_savedata(tox1, data);
72 ck_assert_msg(test == 0, "failed to encrypted save"); 71 size_t size2 = size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
73 //ck_assert_msg(tox_is_save_encrypted(data) == 1, "magic number missing"); 72 uint8_t enc_data[size2];
74 73 TOX_ERR_ENCRYPTION error1;
75 Tox *tox3 = tox_encrypted_new(0, data, size, "correcthorsebatterystaple", 25, &err); 74 bool ret = tox_pass_encrypt(data, size, "correcthorsebatterystaple", 25, enc_data, &error1);
76 ck_assert_msg(err == TOX_ERR_ENCRYPTED_NEW_OK, "failed to encrypted new"); 75 ck_assert_msg(ret, "failed to encrypted save: %u", error1);
76 ck_assert_msg(tox_is_data_encrypted(enc_data), "magic number missing");
77
78 TOX_ERR_NEW err2;
79 Tox *tox3 = tox_new(0, enc_data, size2, &err2);
80 ck_assert_msg(err2 == TOX_ERR_NEW_LOAD_ENCRYPTED, "wrong error! %u. should fail with %u", err2, TOX_ERR_NEW_LOAD_ENCRYPTED);
81 uint8_t dec_data[size];
82 TOX_ERR_DECRYPTION err3;
83 ret = tox_pass_decrypt(enc_data, size2, "correcthorsebatterystaple", 25, dec_data, &err3);
84 ck_assert_msg(ret, "failed to decrypt save: %u", err3);
85 tox3 = tox_new(0, dec_data, size, &err2);
86 ck_assert_msg(err2 == TOX_ERR_NEW_OK, "failed to load from decrypted data: %u", err2);
77 uint8_t address2[TOX_PUBLIC_KEY_SIZE]; 87 uint8_t address2[TOX_PUBLIC_KEY_SIZE];
78 test = tox_friend_get_public_key(tox3, 0, address2, 0); 88 ret = tox_friend_get_public_key(tox3, 0, address2, 0);
79 ck_assert_msg(test == 1, "no friends!"); 89 ck_assert_msg(ret, "no friends!");
80 ck_assert_msg(memcmp(address, address2, TOX_PUBLIC_KEY_SIZE) == 0, "addresses don't match!"); 90 ck_assert_msg(memcmp(address, address2, TOX_PUBLIC_KEY_SIZE) == 0, "addresses don't match!");
81 91
82 size = tox_encrypted_size(tox3); 92 size = tox_get_savedata_size(tox3);
83 uint8_t data2[size]; 93 uint8_t data2[size];
94 tox_get_savedata(tox3, data2);
84 uint8_t key[32 + crypto_box_BEFORENMBYTES]; 95 uint8_t key[32 + crypto_box_BEFORENMBYTES];
85 memcpy(key, salt, 32); 96 memcpy(key, salt, 32);
86 memcpy(key + 32, known_key2, crypto_box_BEFORENMBYTES); 97 memcpy(key + 32, known_key2, crypto_box_BEFORENMBYTES);
87 test = tox_encrypted_key_save(tox3, data2, key); 98 size2 = size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
88 ck_assert_msg(test == 0, "failed to encrypted save the second"); 99 uint8_t encdata2[size2];
89 //ck_assert_msg(tox_is_save_encrypted(data2) == 1, "magic number the second missing"); 100 ret = tox_pass_key_encrypt(data2, size, key, encdata2, &error1);
90 101 ck_assert_msg(ret, "failed to key encrypt %u", error1);
91 // first test tox_encrypted_key_new 102 ck_assert_msg(tox_is_data_encrypted(encdata2), "magic number the second missing");
92 Tox *tox4 = tox_encrypted_key_new(0, data2, size, key, &err); 103
93 ck_assert_msg(err == TOX_ERR_ENCRYPTED_NEW_OK, "failed to encrypted new the second");
94 uint8_t address4[TOX_PUBLIC_KEY_SIZE];
95 test = tox_friend_get_public_key(tox4, 0, address4, 0);
96 ck_assert_msg(test == 1, "no friends! the second");
97 ck_assert_msg(memcmp(address, address2, TOX_PUBLIC_KEY_SIZE) == 0, "addresses don't match! the second");
98
99 // now test compaitibilty with tox_encrypted_new, first manually...
100 uint8_t out1[size], out2[size]; 104 uint8_t out1[size], out2[size];
101 //printf("Trying to decrypt from pw:\n"); 105 ret = tox_pass_decrypt(encdata2, size2, pw, pwlen, out1, &err3);
102 uint32_t sz1 = tox_pass_decrypt(data2, size, pw, pwlen, out1); 106 ck_assert_msg(ret, "failed to pw decrypt %u", err3);
103 uint32_t sz2 = tox_pass_key_decrypt(data2, size, key, out2); 107 ret = tox_pass_key_decrypt(encdata2, size2, key, out2, &err3);
104 ck_assert_msg(sz1 == sz2, "differing output sizes"); 108 ck_assert_msg(ret, "failed to key decrypt %u", err3);
105 ck_assert_msg(memcmp(out1, out2, sz1) == 0, "differing output data"); 109 ck_assert_msg(memcmp(out1, out2, size) == 0, "differing output data");
106 110
107 // and now with the code in use (I only bothered with manually to debug this, and it seems a waste 111 // and now with the code in use (I only bothered with manually to debug this, and it seems a waste
108 // to remove the manual check now that it's there) 112 // to remove the manual check now that it's there)
109 Tox *tox5 = tox_encrypted_new(0, data2, size, pw, pwlen, &err); 113 Tox *tox4 = tox_new(0, out1, size, &err2);
110 ck_assert_msg(err == TOX_ERR_ENCRYPTED_NEW_OK, "failed to encrypted new the third"); 114 ck_assert_msg(err2 == TOX_ERR_NEW_OK, "failed to new the third");
111 uint8_t address5[TOX_PUBLIC_KEY_SIZE]; 115 uint8_t address5[TOX_PUBLIC_KEY_SIZE];
112 test = tox_friend_get_public_key(tox4, 0, address5, 0); 116 ret = tox_friend_get_public_key(tox4, 0, address5, 0);
113 ck_assert_msg(test == 1, "no friends! the third"); 117 ck_assert_msg(ret, "no friends! the third");
114 ck_assert_msg(memcmp(address, address2, TOX_PUBLIC_KEY_SIZE) == 0, "addresses don't match! the third"); 118 ck_assert_msg(memcmp(address, address2, TOX_PUBLIC_KEY_SIZE) == 0, "addresses don't match! the third");
115 119
116 tox_kill(tox1); 120 tox_kill(tox1);
117 tox_kill(tox2); 121 tox_kill(tox2);
118 tox_kill(tox3); 122 tox_kill(tox3);
119 tox_kill(tox4); 123 tox_kill(tox4);
120 tox_kill(tox5);
121} 124}
122END_TEST 125END_TEST
123 126
124START_TEST(test_keys) 127START_TEST(test_keys)
125{ 128{
126 uint8_t key[tox_pass_key_length()]; 129 TOX_ERR_ENCRYPTION encerr;
127 tox_derive_key_from_pass("123qweasdzxc", 12, key); 130 TOX_ERR_DECRYPTION decerr;
131 TOX_ERR_KEY_DERIVATION keyerr;
132 uint8_t key[TOX_PASS_KEY_LENGTH];
133 bool ret = tox_derive_key_from_pass("123qweasdzxc", 12, key, &keyerr);
134 ck_assert_msg(ret, "generic failure 1: %u", keyerr);
128 uint8_t *string = "No Patrick, mayonnaise is not an instrument."; // 44 135 uint8_t *string = "No Patrick, mayonnaise is not an instrument."; // 44
129 136
130 uint8_t encrypted[44 + tox_pass_encryption_extra_length()]; 137 uint8_t encrypted[44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH];
131 int sz = tox_pass_key_encrypt(string, 44, key, encrypted); 138 ret = tox_pass_key_encrypt(string, 44, key, encrypted, &encerr);
132 139 ck_assert_msg(ret, "generic failure 2: %u", encerr);
133 uint8_t encrypted2[44 + tox_pass_encryption_extra_length()];
134 int sz2 = tox_pass_encrypt(string, 44, "123qweasdzxc", 12, encrypted2);
135 140
136 ck_assert_msg(sz == sz2, "an encryption failed"); 141 uint8_t encrypted2[44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH];
142 ret = tox_pass_encrypt(string, 44, "123qweasdzxc", 12, encrypted2, &encerr);
143 ck_assert_msg(ret, "generic failure 3: %u", encerr);
137 144
138 uint8_t out1[44 + tox_pass_encryption_extra_length()]; 145 uint8_t out1[44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH];
139 uint8_t out2[44 + tox_pass_encryption_extra_length()]; 146 uint8_t out2[44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH];
140 147
141 sz = tox_pass_key_decrypt(encrypted, 44 + tox_pass_encryption_extra_length(), key, out1); 148 ret = tox_pass_key_decrypt(encrypted, 44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH, key, out1, &decerr);
142 ck_assert_msg(sz == 44, "sz isn't right"); 149 ck_assert_msg(ret, "generic failure 4: %u", decerr);
143 ck_assert_msg(memcmp(out1, string, 44) == 0, "decryption 1 failed"); 150 ck_assert_msg(memcmp(out1, string, 44) == 0, "decryption 1 failed");
144 151
145 sz2 = tox_pass_decrypt(encrypted2, 44 + tox_pass_encryption_extra_length(), "123qweasdzxc", 12, out2); 152 ret = tox_pass_decrypt(encrypted2, 44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH, "123qweasdzxc", 12, out2, &decerr);
146 ck_assert_msg(sz2 == 44, "sz2 isn't right"); 153 ck_assert_msg(ret, "generic failure 5: %u", &decerr);
147 ck_assert_msg(memcmp(out2, string, 44) == 0, "decryption 2 failed"); 154 ck_assert_msg(memcmp(out2, string, 44) == 0, "decryption 2 failed");
148 155
149 // test that pass_decrypt can decrypt things from pass_key_encrypt 156 // test that pass_decrypt can decrypt things from pass_key_encrypt
150 sz = tox_pass_decrypt(encrypted, 44 + tox_pass_encryption_extra_length(), "123qweasdzxc", 12, out1); 157 ret = tox_pass_decrypt(encrypted, 44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH, "123qweasdzxc", 12, out1, &decerr);
151 ck_assert_msg(sz == 44, "sz isn't right"); 158 ck_assert_msg(ret, "generic failure 6: %u", decerr);
152 ck_assert_msg(memcmp(out1, string, 44) == 0, "decryption 3 failed"); 159 ck_assert_msg(memcmp(out1, string, 44) == 0, "decryption 3 failed");
153 160
154 uint8_t salt[tox_pass_salt_length()]; 161 uint8_t salt[TOX_PASS_SALT_LENGTH];
155 ck_assert_msg(0 == tox_get_salt(encrypted, salt), "couldn't get salt"); 162 ck_assert_msg(tox_get_salt(encrypted, salt), "couldn't get salt");
156 uint8_t key2[tox_pass_key_length()]; 163 uint8_t key2[TOX_PASS_KEY_LENGTH];
157 tox_derive_key_with_salt("123qweasdzxc", 12, salt, key2); 164 ret = tox_derive_key_with_salt("123qweasdzxc", 12, salt, key2, &keyerr);
158 ck_assert_msg(0 == memcmp(key, key2, tox_pass_key_length()), "salt comparison failed"); 165 ck_assert_msg(ret, "generic failure 7: %u", keyerr);
166 ck_assert_msg(0 == memcmp(key, key2, TOX_PASS_KEY_LENGTH), "salt comparison failed");
159} 167}
160END_TEST 168END_TEST
161 169
@@ -163,7 +171,7 @@ Suite *encryptsave_suite(void)
163{ 171{
164 Suite *s = suite_create("encryptsave"); 172 Suite *s = suite_create("encryptsave");
165 173
166 DEFTESTCASE_SLOW(known_kdf, 60); /* is 5-10 seconds on my computer, but is directly dependent on CPU */ 174 DEFTESTCASE_SLOW(known_kdf, 60);
167 DEFTESTCASE_SLOW(save_friend, 20); 175 DEFTESTCASE_SLOW(save_friend, 20);
168 DEFTESTCASE_SLOW(keys, 30); 176 DEFTESTCASE_SLOW(keys, 30);
169 177