Making tox_encrypted_new() use the same error codes as tox_new was a bad idea.

They now have their own error codes.
This commit is contained in:
irungentoo 2015-03-26 12:39:09 -04:00
parent e888b89ac9
commit 06b389ea5e
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
4 changed files with 63 additions and 19 deletions

View File

@ -55,7 +55,7 @@ END_TEST
START_TEST(test_save_friend) START_TEST(test_save_friend)
{ {
TOX_ERR_NEW err = TOX_ERR_NEW_OK; TOX_ERR_ENCRYPTED_NEW err = TOX_ERR_ENCRYPTED_NEW_OK;
Tox *tox1 = tox_new(0, 0, 0, 0); Tox *tox1 = tox_new(0, 0, 0, 0);
Tox *tox2 = tox_new(0, 0, 0, 0); Tox *tox2 = tox_new(0, 0, 0, 0);
ck_assert_msg(tox1 || tox2, "Failed to create 2 tox instances"); ck_assert_msg(tox1 || tox2, "Failed to create 2 tox instances");
@ -73,7 +73,7 @@ START_TEST(test_save_friend)
//ck_assert_msg(tox_is_save_encrypted(data) == 1, "magic number missing"); //ck_assert_msg(tox_is_save_encrypted(data) == 1, "magic number missing");
Tox *tox3 = tox_encrypted_new(0, data, size, "correcthorsebatterystaple", 25, &err); Tox *tox3 = tox_encrypted_new(0, data, size, "correcthorsebatterystaple", 25, &err);
ck_assert_msg(err == TOX_ERR_NEW_OK, "failed to encrypted new"); ck_assert_msg(err == TOX_ERR_ENCRYPTED_NEW_OK, "failed to encrypted new");
uint8_t address2[TOX_PUBLIC_KEY_SIZE]; uint8_t address2[TOX_PUBLIC_KEY_SIZE];
test = tox_friend_get_public_key(tox3, 0, address2, 0); test = tox_friend_get_public_key(tox3, 0, address2, 0);
ck_assert_msg(test == 1, "no friends!"); ck_assert_msg(test == 1, "no friends!");
@ -90,7 +90,7 @@ START_TEST(test_save_friend)
// first test tox_encrypted_key_new // first test tox_encrypted_key_new
Tox *tox4 = tox_encrypted_key_new(0, data2, size, key, &err); Tox *tox4 = tox_encrypted_key_new(0, data2, size, key, &err);
ck_assert_msg(err == TOX_ERR_NEW_OK, "failed to encrypted new the second"); ck_assert_msg(err == TOX_ERR_ENCRYPTED_NEW_OK, "failed to encrypted new the second");
uint8_t address4[TOX_PUBLIC_KEY_SIZE]; uint8_t address4[TOX_PUBLIC_KEY_SIZE];
test = tox_friend_get_public_key(tox4, 0, address4, 0); test = tox_friend_get_public_key(tox4, 0, address4, 0);
ck_assert_msg(test == 1, "no friends! the second"); ck_assert_msg(test == 1, "no friends! the second");
@ -107,7 +107,7 @@ START_TEST(test_save_friend)
// and now with the code in use (I only bothered with manually to debug this, and it seems a waste // and now with the code in use (I only bothered with manually to debug this, and it seems a waste
// to remove the manual check now that it's there) // to remove the manual check now that it's there)
Tox *tox5 = tox_encrypted_new(0, data2, size, pw, pwlen, &err); Tox *tox5 = tox_encrypted_new(0, data2, size, pw, pwlen, &err);
ck_assert_msg(err == TOX_ERR_NEW_OK, "failed to encrypted new the third"); ck_assert_msg(err == TOX_ERR_ENCRYPTED_NEW_OK, "failed to encrypted new the third");
uint8_t address5[TOX_PUBLIC_KEY_SIZE]; uint8_t address5[TOX_PUBLIC_KEY_SIZE];
test = tox_friend_get_public_key(tox4, 0, address5, 0); test = tox_friend_get_public_key(tox4, 0, address5, 0);
ck_assert_msg(test == 1, "no friends! the third"); ck_assert_msg(test == 1, "no friends! the third");

View File

@ -487,14 +487,6 @@ typedef enum TOX_ERR_NEW {
* The byte array to be loaded contained an encrypted save. * The byte array to be loaded contained an encrypted save.
*/ */
TOX_ERR_NEW_LOAD_ENCRYPTED, TOX_ERR_NEW_LOAD_ENCRYPTED,
/**
* The encrypted byte array could not be decrypted. Either the data was
* corrupt or the password/key was incorrect.
*
* NOTE: This error code is only set by tox_encrypted_new() and
* tox_encrypted_key_new(), in the toxencryptsave module.
*/
TOX_ERR_NEW_LOAD_DECRYPTION_FAILED,
/** /**
* The data format was invalid. This can happen when loading data that was * The data format was invalid. This can happen when loading data that was
* saved by an older version of Tox, or when the data has been corrupted. * saved by an older version of Tox, or when the data has been corrupted.

View File

@ -300,14 +300,14 @@ int tox_pass_decrypt(const uint8_t *data, uint32_t length, uint8_t *passphrase,
* returns NULL on failure; see the documentation in toxcore/tox.h. * returns NULL on failure; see the documentation in toxcore/tox.h.
*/ */
Tox *tox_encrypted_new(const struct Tox_Options *options, const uint8_t *data, size_t length, uint8_t *passphrase, Tox *tox_encrypted_new(const struct Tox_Options *options, const uint8_t *data, size_t length, uint8_t *passphrase,
size_t pplength, TOX_ERR_NEW *error) size_t pplength, TOX_ERR_ENCRYPTED_NEW *error)
{ {
uint32_t decrypt_length = length - TOX_PASS_ENCRYPTION_EXTRA_LENGTH; uint32_t decrypt_length = length - TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
uint8_t temp_data[decrypt_length]; uint8_t temp_data[decrypt_length];
if (tox_pass_decrypt(data, length, passphrase, pplength, temp_data) if (tox_pass_decrypt(data, length, passphrase, pplength, temp_data)
!= decrypt_length) { != decrypt_length) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_DECRYPTION_FAILED); SET_ERROR_PARAMETER(error, TOX_ERR_ENCRYPTED_NEW_LOAD_DECRYPTION_FAILED);
return NULL; return NULL;
} }
@ -320,14 +320,14 @@ Tox *tox_encrypted_new(const struct Tox_Options *options, const uint8_t *data, s
* returns NULL on failure; see the documentation in toxcore/tox.h. * returns NULL on failure; see the documentation in toxcore/tox.h.
*/ */
Tox *tox_encrypted_key_new(const struct Tox_Options *options, const uint8_t *data, size_t length, uint8_t *key, Tox *tox_encrypted_key_new(const struct Tox_Options *options, const uint8_t *data, size_t length, uint8_t *key,
TOX_ERR_NEW *error) TOX_ERR_ENCRYPTED_NEW *error)
{ {
uint32_t decrypt_length = length - TOX_PASS_ENCRYPTION_EXTRA_LENGTH; uint32_t decrypt_length = length - TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
uint8_t temp_data[decrypt_length]; uint8_t temp_data[decrypt_length];
if (tox_pass_key_decrypt(data, length, key, temp_data) if (tox_pass_key_decrypt(data, length, key, temp_data)
!= decrypt_length) { != decrypt_length) {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_DECRYPTION_FAILED); SET_ERROR_PARAMETER(error, TOX_ERR_ENCRYPTED_NEW_LOAD_DECRYPTION_FAILED);
return NULL; return NULL;
} }

View File

@ -35,7 +35,6 @@ extern "C" {
#define TOX_DEFINED #define TOX_DEFINED
typedef struct Tox Tox; typedef struct Tox Tox;
struct Tox_Options; struct Tox_Options;
typedef enum TOX_ERR_NEW TOX_ERR_NEW;
#endif #endif
// these functions provide access to these defines in toxencryptsave.c, which // these functions provide access to these defines in toxencryptsave.c, which
@ -110,13 +109,66 @@ int tox_encrypted_save(const Tox *tox, uint8_t *data, uint8_t *passphrase, uint3
*/ */
int tox_pass_decrypt(const uint8_t *data, uint32_t length, uint8_t *passphrase, uint32_t pplength, uint8_t *out); int tox_pass_decrypt(const uint8_t *data, uint32_t length, uint8_t *passphrase, uint32_t pplength, uint8_t *out);
typedef enum TOX_ERR_ENCRYPTED_NEW {
TOX_ERR_ENCRYPTED_NEW_OK,
TOX_ERR_ENCRYPTED_NEW_NULL,
/**
* The function was unable to allocate enough memory to store the internal
* structures for the Tox object.
*/
TOX_ERR_ENCRYPTED_NEW_MALLOC,
/**
* The function was unable to bind to a port. This may mean that all ports
* have already been bound, e.g. by other Tox instances, or it may mean
* a permission error. You may be able to gather more information from errno.
*/
TOX_ERR_ENCRYPTED_NEW_PORT_ALLOC,
/**
* proxy_type was invalid.
*/
TOX_ERR_ENCRYPTED_NEW_PROXY_BAD_TYPE,
/**
* proxy_type was valid but the proxy_host passed had an invalid format
* or was NULL.
*/
TOX_ERR_ENCRYPTED_NEW_PROXY_BAD_HOST,
/**
* proxy_type was valid, but the proxy_port was invalid.
*/
TOX_ERR_ENCRYPTED_NEW_PROXY_BAD_PORT,
/**
* The proxy host passed could not be resolved.
*/
TOX_ERR_ENCRYPTED_NEW_PROXY_NOT_FOUND,
/**
* The byte array to be loaded contained an encrypted save.
*/
TOX_ERR_ENCRYPTED_NEW_LOAD_ENCRYPTED,
/**
* The data format was invalid. This can happen when loading data that was
* saved by an older version of Tox, or when the data has been corrupted.
* When loading from badly formatted data, some data may have been loaded,
* and the rest is discarded. Passing an invalid length parameter also
* causes this error.
*/
TOX_ERR_ENCRYPTED_NEW_LOAD_BAD_FORMAT,
/**
* The encrypted byte array could not be decrypted. Either the data was
* corrupt or the password/key was incorrect.
*
* NOTE: This error code is only set by tox_encrypted_new() and
* tox_encrypted_key_new(), in the toxencryptsave module.
*/
TOX_ERR_ENCRYPTED_NEW_LOAD_DECRYPTION_FAILED
} TOX_ERR_ENCRYPTED_NEW;
/* Load the new messenger from encrypted data of size length. /* Load the new messenger from encrypted data of size length.
* All other arguments are like toxcore/tox_new(). * All other arguments are like toxcore/tox_new().
* *
* returns NULL on failure; see the documentation in toxcore/tox.h. * returns NULL on failure; see the documentation in toxcore/tox.h.
*/ */
Tox *tox_encrypted_new(const struct Tox_Options *options, const uint8_t *data, size_t length, uint8_t *passphrase, Tox *tox_encrypted_new(const struct Tox_Options *options, const uint8_t *data, size_t length, uint8_t *passphrase,
size_t pplength, TOX_ERR_NEW *error); size_t pplength, TOX_ERR_ENCRYPTED_NEW *error);
/******************************* BEGIN PART 1 ******************************* /******************************* BEGIN PART 1 *******************************
@ -190,7 +242,7 @@ int tox_pass_key_decrypt(const uint8_t *data, uint32_t length, const uint8_t *ke
* returns NULL on failure; see the documentation in toxcore/tox.h. * returns NULL on failure; see the documentation in toxcore/tox.h.
*/ */
Tox *tox_encrypted_key_new(const struct Tox_Options *options, const uint8_t *data, size_t length, uint8_t *key, Tox *tox_encrypted_key_new(const struct Tox_Options *options, const uint8_t *data, size_t length, uint8_t *key,
TOX_ERR_NEW *error); TOX_ERR_ENCRYPTED_NEW *error);
/* Determines whether or not the given data is encrypted (by checking the magic number) /* Determines whether or not the given data is encrypted (by checking the magic number)