void Core::requestFriendship(const ToxId& friendId, const QString& message) { ToxPk friendPk = friendId.getPublicKey(); QString errorMessage = getFriendRequestErrorMessage(friendId, message); if (!errorMessage.isNull()) { emit failedToAddFriend(friendPk, errorMessage); profile.saveToxSave(); } ToxString cMessage(message); uint32_t friendNumber = tox_friend_add(tox, friendId.getBytes(), cMessage.data(), cMessage.size(), nullptr); if (friendNumber == std::numeric_limits<uint32_t>::max()) { qDebug() << "Failed to request friendship"; emit failedToAddFriend(friendPk); } else { qDebug() << "Requested friendship of " << friendNumber; Settings::getInstance().updateFriendAddress(friendId.toString()); emit friendAdded(friendNumber, friendPk); emit requestSent(friendPk, message); } profile.saveToxSave(); }
void cmd_add_helper(ToxWindow *self, Tox *m, const char *id_bin, const char *msg) { const char *errmsg; TOX_ERR_FRIEND_ADD err; uint32_t f_num = tox_friend_add(m, (uint8_t *) id_bin, (uint8_t *) msg, strlen(msg), &err); switch (err) { case TOX_ERR_FRIEND_ADD_TOO_LONG: errmsg = "Message is too long."; break; case TOX_ERR_FRIEND_ADD_NO_MESSAGE: errmsg = "Please add a message to your request."; break; case TOX_ERR_FRIEND_ADD_OWN_KEY: errmsg = "That appears to be your own ID."; break; case TOX_ERR_FRIEND_ADD_ALREADY_SENT: errmsg = "Friend request has already been sent."; break; case TOX_ERR_FRIEND_ADD_BAD_CHECKSUM: errmsg = "Bad checksum in address."; break; case TOX_ERR_FRIEND_ADD_SET_NEW_NOSPAM: errmsg = "Nospam was different."; break; case TOX_ERR_FRIEND_ADD_MALLOC: errmsg = "Core memory allocation failed."; break; case TOX_ERR_FRIEND_ADD_OK: errmsg = "Friend request sent."; on_friendadded(m, f_num, true); break; case TOX_ERR_FRIEND_ADD_NULL: /* fallthrough */ default: errmsg = "Faile to add friend: Unknown error."; break; } line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); }
static void r2tox_addfriend(const char *addr) { if (!tox) { eprintf ("[tox] Not connected\n"); return; } char *a = strdup (addr); if (!a) { return; } char *b = strchr (a, ' '); uint8_t fa[128] = {0}; if (b) { *b++ = 0; } b = NULL; int len = r_hex_str2bin (a, &fa); printf ("LEN %d (%s)\n", len, a); // int friendid = tox_friend_add_norequest(tox, fa, NULL); int friendid = b ? tox_friend_add (tox, fa, b, strlen(b), NULL) : tox_friend_add (tox, fa, "hi there", 8, NULL); printf ("friendid %d\n", friendid); free (a); }
void Core::requestFriendship(const QString& friendAddress, const QString& message) { const QString userId = friendAddress.mid(0, TOX_PUBLIC_KEY_SIZE * 2); if (message.isEmpty()) { emit failedToAddFriend(userId, tr("You need to write a message with your request")); } else if (message.size() > TOX_MAX_FRIEND_REQUEST_LENGTH) { emit failedToAddFriend(userId, tr("Your message is too long!")); } else if (hasFriendWithAddress(friendAddress)) { emit failedToAddFriend(userId, tr("Friend is already added")); } else { CString cMessage(message); uint32_t friendId = tox_friend_add(tox, CFriendAddress(friendAddress).data(), cMessage.data(), cMessage.size(), nullptr); if (friendId == std::numeric_limits<uint32_t>::max()) { qDebug() << "Failed to request friendship"; emit failedToAddFriend(userId); } else { qDebug() << "Requested friendship of "<<friendId; // Update our friendAddresses Settings::getInstance().updateFriendAddress(friendAddress); QString inviteStr = tr("/me offers friendship."); if (message.length()) inviteStr = tr("/me offers friendship, \"%1\"").arg(message); Profile* profile = Nexus::getProfile(); if (profile->isHistoryEnabled()) profile->getHistory()->addNewMessage(userId, inviteStr, getSelfId().publicKey, QDateTime::currentDateTime(), true, QString()); emit friendAdded(friendId, userId); emit friendshipChanged(friendId); } } profile.saveToxSave(); }
void Core::requestFriendship(const QString& friendAddress, const QString& message) { const QString userId = friendAddress.mid(0, TOX_PUBLIC_KEY_SIZE * 2); if (message.isEmpty()) { emit failedToAddFriend(userId, tr("You need to write a message with your request")); } else if (message.size() > TOX_MAX_FRIEND_REQUEST_LENGTH) { emit failedToAddFriend(userId, tr("Your message is too long!")); } else if (hasFriendWithAddress(friendAddress)) { emit failedToAddFriend(userId, tr("Friend is already added")); } else { qDebug() << "Core: requesting friendship of "+friendAddress; CString cMessage(message); uint32_t friendId = tox_friend_add(tox, CFriendAddress(friendAddress).data(), cMessage.data(), cMessage.size(), nullptr); if (friendId == std::numeric_limits<uint32_t>::max()) { emit failedToAddFriend(userId); } else { // Update our friendAddresses Settings::getInstance().updateFriendAdress(friendAddress); QString inviteStr = tr("/me offers friendship."); if (message.length()) inviteStr = tr("/me offers friendship, \"%1\"").arg(message); HistoryKeeper::getInstance()->addChatEntry(userId, inviteStr, getSelfId().publicKey, QDateTime::currentDateTime(), true); emit friendAdded(friendId, userId); } } saveConfiguration(); }
static void test_one(void) { uint8_t name[TOX_MAX_NAME_LENGTH]; uint8_t status_message[TOX_MAX_STATUS_MESSAGE_LENGTH]; uint8_t name2[TOX_MAX_NAME_LENGTH]; uint8_t status_message2[TOX_MAX_STATUS_MESSAGE_LENGTH]; uint32_t index[] = { 1, 2 }; Tox *tox1 = tox_new_log(nullptr, nullptr, &index[0]); set_random_name_and_status_message(tox1, name, status_message); Tox *tox2 = tox_new_log(nullptr, nullptr, &index[1]); set_random_name_and_status_message(tox2, name2, status_message2); uint8_t address[TOX_ADDRESS_SIZE]; tox_self_get_address(tox1, address); Tox_Err_Friend_Add error; uint32_t ret = tox_friend_add(tox1, address, (const uint8_t *)"m", 1, &error); ck_assert_msg(ret == UINT32_MAX && error == TOX_ERR_FRIEND_ADD_OWN_KEY, "Adding own address worked."); tox_self_get_address(tox2, address); uint8_t message[TOX_MAX_FRIEND_REQUEST_LENGTH + 1]; ret = tox_friend_add(tox1, address, nullptr, 0, &error); ck_assert_msg(ret == UINT32_MAX && error == TOX_ERR_FRIEND_ADD_NULL, "Sending request with no message worked."); ret = tox_friend_add(tox1, address, message, 0, &error); ck_assert_msg(ret == UINT32_MAX && error == TOX_ERR_FRIEND_ADD_NO_MESSAGE, "Sending request with no message worked."); ret = tox_friend_add(tox1, address, message, sizeof(message), &error); ck_assert_msg(ret == UINT32_MAX && error == TOX_ERR_FRIEND_ADD_TOO_LONG, "TOX_MAX_FRIEND_REQUEST_LENGTH is too big."); address[0]++; ret = tox_friend_add(tox1, address, (const uint8_t *)"m", 1, &error); ck_assert_msg(ret == UINT32_MAX && error == TOX_ERR_FRIEND_ADD_BAD_CHECKSUM, "Adding address with bad checksum worked."); tox_self_get_address(tox2, address); ret = tox_friend_add(tox1, address, message, TOX_MAX_FRIEND_REQUEST_LENGTH, &error); ck_assert_msg(ret == 0 && error == TOX_ERR_FRIEND_ADD_OK, "Failed to add friend."); ret = tox_friend_add(tox1, address, message, TOX_MAX_FRIEND_REQUEST_LENGTH, &error); ck_assert_msg(ret == UINT32_MAX && error == TOX_ERR_FRIEND_ADD_ALREADY_SENT, "Adding friend twice worked."); tox_self_set_name(tox1, name, sizeof(name), nullptr); ck_assert_msg(tox_self_get_name_size(tox1) == sizeof(name), "Can't set name of TOX_MAX_NAME_LENGTH"); tox_self_set_status_message(tox1, status_message, sizeof(status_message), nullptr); ck_assert_msg(tox_self_get_status_message_size(tox1) == sizeof(status_message), "Can't set status message of TOX_MAX_STATUS_MESSAGE_LENGTH"); tox_self_get_address(tox1, address); size_t save_size = tox_get_savedata_size(tox1); VLA(uint8_t, data, save_size); tox_get_savedata(tox1, data); tox_kill(tox2); Tox_Err_New err_n; struct Tox_Options *options = tox_options_new(nullptr); tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); tox_options_set_savedata_data(options, data, save_size); tox2 = tox_new_log(options, &err_n, &index[1]); ck_assert_msg(err_n == TOX_ERR_NEW_OK, "Load failed"); ck_assert_msg(tox_self_get_name_size(tox2) == sizeof name, "Wrong name size."); ck_assert_msg(tox_self_get_status_message_size(tox2) == sizeof status_message, "Wrong status message size"); uint8_t name_loaded[TOX_MAX_NAME_LENGTH] = { 0 }; tox_self_get_name(tox2, name_loaded); ck_assert_msg(!memcmp(name, name_loaded, sizeof name), "Wrong name."); uint8_t status_message_loaded[TOX_MAX_STATUS_MESSAGE_LENGTH] = { 0 }; tox_self_get_status_message(tox2, status_message_loaded); ck_assert_msg(!memcmp(status_message, status_message_loaded, sizeof status_message_loaded), "Wrong status message."); uint8_t address2[TOX_ADDRESS_SIZE] = { 0 }; tox_self_get_address(tox2, address2); ck_assert_msg(memcmp(address2, address, TOX_ADDRESS_SIZE) == 0, "Wrong address."); uint8_t new_name[TOX_MAX_NAME_LENGTH] = { 0 }; tox_self_get_name(tox2, new_name); ck_assert_msg(memcmp(name, new_name, TOX_MAX_NAME_LENGTH) == 0, "Wrong name"); uint8_t sk[TOX_SECRET_KEY_SIZE]; tox_self_get_secret_key(tox2, sk); tox_kill(tox2); tox_options_default(options); tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_SECRET_KEY); tox_options_set_savedata_data(options, sk, sizeof(sk)); tox2 = tox_new_log(options, &err_n, &index[1]); ck_assert_msg(err_n == TOX_ERR_NEW_OK, "Load failed"); uint8_t address3[TOX_ADDRESS_SIZE]; tox_self_get_address(tox2, address3); ck_assert_msg(memcmp(address3, address, TOX_PUBLIC_KEY_SIZE) == 0, "Wrong public key."); uint8_t pk[TOX_PUBLIC_KEY_SIZE]; tox_self_get_public_key(tox2, pk); ck_assert_msg(memcmp(pk, address, TOX_PUBLIC_KEY_SIZE) == 0, "Wrong public key."); tox_options_free(options); tox_kill(tox1); tox_kill(tox2); }
END_TEST #define NUM_TCP_RELAYS 3 START_TEST(test_many_clients_tcp_b) { long long unsigned int cur_time = time(nullptr); Tox *toxes[NUM_TOXES_TCP]; uint32_t index[NUM_TOXES_TCP]; uint32_t i, j; uint32_t to_comp = 974536; for (i = 0; i < NUM_TOXES_TCP; ++i) { struct Tox_Options *opts = tox_options_new(nullptr); if (i < NUM_TCP_RELAYS) { tox_options_set_tcp_port(opts, TCP_RELAY_PORT + i); } else { tox_options_set_udp_enabled(opts, 0); } index[i] = i + 1; toxes[i] = tox_new_log(opts, nullptr, &index[i]); ck_assert_msg(toxes[i] != nullptr, "Failed to create tox instances %u", i); tox_callback_friend_request(toxes[i], accept_friend_request); uint8_t dpk[TOX_PUBLIC_KEY_SIZE]; tox_self_get_dht_id(toxes[(i % NUM_TCP_RELAYS)], dpk); ck_assert_msg(tox_add_tcp_relay(toxes[i], TOX_LOCALHOST, TCP_RELAY_PORT + (i % NUM_TCP_RELAYS), dpk, nullptr), "add relay error"); tox_self_get_dht_id(toxes[0], dpk); uint16_t first_port = tox_self_get_udp_port(toxes[0], nullptr); ck_assert_msg(tox_bootstrap(toxes[i], TOX_LOCALHOST, first_port, dpk, nullptr), "Bootstrap error"); tox_options_free(opts); } struct { uint16_t tox1; uint16_t tox2; } pairs[NUM_FRIENDS]; uint8_t address[TOX_ADDRESS_SIZE]; for (i = 0; i < NUM_FRIENDS; ++i) { loop_top: pairs[i].tox1 = random_u32() % NUM_TOXES_TCP; pairs[i].tox2 = (pairs[i].tox1 + random_u32() % (NUM_TOXES_TCP - 1) + 1) % NUM_TOXES_TCP; for (j = 0; j < i; ++j) { if (pairs[j].tox2 == pairs[i].tox1 && pairs[j].tox1 == pairs[i].tox2) { goto loop_top; } } tox_self_get_address(toxes[pairs[i].tox1], address); TOX_ERR_FRIEND_ADD test; uint32_t num = tox_friend_add(toxes[pairs[i].tox2], address, (const uint8_t *)"Gentoo", 7, &test); if (test == TOX_ERR_FRIEND_ADD_ALREADY_SENT) { goto loop_top; } ck_assert_msg(num != UINT32_MAX && test == TOX_ERR_FRIEND_ADD_OK, "Failed to add friend error code: %i", test); } uint16_t last_count = 0; while (1) { uint16_t counter = 0; for (i = 0; i < NUM_TOXES_TCP; ++i) { for (j = 0; j < tox_self_get_friend_list_size(toxes[i]); ++j) { if (tox_friend_get_connection_status(toxes[i], j, nullptr) == TOX_CONNECTION_TCP) { ++counter; } } } if (counter != last_count) { printf("many_clients_tcp_b got to %u\n", counter); last_count = counter; } if (counter == NUM_FRIENDS * 2) { break; } for (i = 0; i < NUM_TOXES_TCP; ++i) { tox_iterate(toxes[i], &to_comp); } c_sleep(30); } for (i = 0; i < NUM_TOXES_TCP; ++i) { tox_kill(toxes[i]); } printf("test_many_clients_tcp_b succeeded, took %llu seconds\n", time(nullptr) - cur_time); }
static void file_transfer_test(void) { printf("Starting test: few_clients\n"); uint32_t index[] = { 1, 2, 3 }; long long unsigned int cur_time = time(nullptr); TOX_ERR_NEW t_n_error; Tox *tox1 = tox_new_log(nullptr, &t_n_error, &index[0]); ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error"); Tox *tox2 = tox_new_log(nullptr, &t_n_error, &index[1]); ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error"); Tox *tox3 = tox_new_log(nullptr, &t_n_error, &index[2]); ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error"); ck_assert_msg(tox1 && tox2 && tox3, "Failed to create 3 tox instances"); tox_callback_friend_request(tox2, accept_friend_request); uint8_t address[TOX_ADDRESS_SIZE]; tox_self_get_address(tox2, address); uint32_t test = tox_friend_add(tox3, address, (const uint8_t *)"Gentoo", 7, nullptr); ck_assert_msg(test == 0, "Failed to add friend error code: %i", test); uint8_t dhtKey[TOX_PUBLIC_KEY_SIZE]; tox_self_get_dht_id(tox1, dhtKey); uint16_t dhtPort = tox_self_get_udp_port(tox1, nullptr); tox_bootstrap(tox2, TOX_LOCALHOST, dhtPort, dhtKey, nullptr); tox_bootstrap(tox3, TOX_LOCALHOST, dhtPort, dhtKey, nullptr); printf("Waiting for toxes to come online\n"); while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE || tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE || tox_self_get_connection_status(tox3) == TOX_CONNECTION_NONE || tox_friend_get_connection_status(tox2, 0, nullptr) == TOX_CONNECTION_NONE || tox_friend_get_connection_status(tox3, 0, nullptr) == TOX_CONNECTION_NONE) { tox_iterate(tox1, nullptr); tox_iterate(tox2, nullptr); tox_iterate(tox3, nullptr); printf("Connections: self (%d, %d, %d), friends (%d, %d)\n", tox_self_get_connection_status(tox1), tox_self_get_connection_status(tox2), tox_self_get_connection_status(tox3), tox_friend_get_connection_status(tox2, 0, nullptr), tox_friend_get_connection_status(tox3, 0, nullptr)); c_sleep(ITERATION_INTERVAL); } printf("Starting file transfer test: 100MiB file.\n"); file_accepted = file_size = sendf_ok = size_recv = 0; file_recv = 0; max_sending = UINT64_MAX; long long unsigned int f_time = time(nullptr); tox_callback_file_recv_chunk(tox3, write_file); tox_callback_file_recv_control(tox2, file_print_control); tox_callback_file_chunk_request(tox2, tox_file_chunk_request); tox_callback_file_recv_control(tox3, file_print_control); tox_callback_file_recv(tox3, tox_file_receive); uint64_t totalf_size = 100 * 1024 * 1024; uint32_t fnum = tox_file_send(tox2, 0, TOX_FILE_KIND_DATA, totalf_size, nullptr, (const uint8_t *)"Gentoo.exe", sizeof("Gentoo.exe"), nullptr); ck_assert_msg(fnum != UINT32_MAX, "tox_new_file_sender fail"); TOX_ERR_FILE_GET gfierr; ck_assert_msg(!tox_file_get_file_id(tox2, 1, fnum, file_cmp_id, &gfierr), "tox_file_get_file_id didn't fail"); ck_assert_msg(gfierr == TOX_ERR_FILE_GET_FRIEND_NOT_FOUND, "wrong error"); ck_assert_msg(!tox_file_get_file_id(tox2, 0, fnum + 1, file_cmp_id, &gfierr), "tox_file_get_file_id didn't fail"); ck_assert_msg(gfierr == TOX_ERR_FILE_GET_NOT_FOUND, "wrong error"); ck_assert_msg(tox_file_get_file_id(tox2, 0, fnum, file_cmp_id, &gfierr), "tox_file_get_file_id failed"); ck_assert_msg(gfierr == TOX_ERR_FILE_GET_OK, "wrong error"); const size_t max_iterations = INT16_MAX; for (size_t i = 0; i < max_iterations; i++) { tox_iterate(tox1, nullptr); tox_iterate(tox2, nullptr); tox_iterate(tox3, nullptr); if (file_sending_done) { if (sendf_ok && file_recv && totalf_size == file_size && size_recv == file_size && sending_pos == size_recv && file_accepted == 1) { break; } ck_abort_msg("Something went wrong in file transfer %u %u %u %u %u %u %lu %lu %lu", sendf_ok, file_recv, totalf_size == file_size, size_recv == file_size, sending_pos == size_recv, file_accepted == 1, (unsigned long)totalf_size, (unsigned long)size_recv, (unsigned long)sending_pos); } uint32_t tox1_interval = tox_iteration_interval(tox1); uint32_t tox2_interval = tox_iteration_interval(tox2); uint32_t tox3_interval = tox_iteration_interval(tox3); if ((i + 1) % 500 == 0) { printf("after %u iterations: %.2fMiB done\n", (unsigned int)i + 1, (double)size_recv / 1024 / 1024); } c_sleep(MIN(tox1_interval, MIN(tox2_interval, tox3_interval))); } ck_assert_msg(file_sending_done, "file sending did not complete after %u iterations: sendf_ok:%u file_recv:%u " "totalf_size==file_size:%u size_recv==file_size:%u sending_pos==size_recv:%u file_accepted:%u " "totalf_size:%lu size_recv:%lu sending_pos:%lu", (unsigned int)max_iterations, sendf_ok, file_recv, totalf_size == file_size, size_recv == file_size, sending_pos == size_recv, file_accepted == 1, (unsigned long)totalf_size, (unsigned long)size_recv, (unsigned long)sending_pos); printf("100MiB file sent in %llu seconds\n", time(nullptr) - f_time); printf("Starting file streaming transfer test.\n"); file_sending_done = 0; file_accepted = 0; file_size = 0; sendf_ok = 0; size_recv = 0; file_recv = 0; tox_callback_file_recv_chunk(tox3, write_file); tox_callback_file_recv_control(tox2, file_print_control); tox_callback_file_chunk_request(tox2, tox_file_chunk_request); tox_callback_file_recv_control(tox3, file_print_control); tox_callback_file_recv(tox3, tox_file_receive); totalf_size = UINT64_MAX; fnum = tox_file_send(tox2, 0, TOX_FILE_KIND_DATA, totalf_size, nullptr, (const uint8_t *)"Gentoo.exe", sizeof("Gentoo.exe"), nullptr); ck_assert_msg(fnum != UINT32_MAX, "tox_new_file_sender fail"); ck_assert_msg(!tox_file_get_file_id(tox2, 1, fnum, file_cmp_id, &gfierr), "tox_file_get_file_id didn't fail"); ck_assert_msg(gfierr == TOX_ERR_FILE_GET_FRIEND_NOT_FOUND, "wrong error"); ck_assert_msg(!tox_file_get_file_id(tox2, 0, fnum + 1, file_cmp_id, &gfierr), "tox_file_get_file_id didn't fail"); ck_assert_msg(gfierr == TOX_ERR_FILE_GET_NOT_FOUND, "wrong error"); ck_assert_msg(tox_file_get_file_id(tox2, 0, fnum, file_cmp_id, &gfierr), "tox_file_get_file_id failed"); ck_assert_msg(gfierr == TOX_ERR_FILE_GET_OK, "wrong error"); max_sending = 100 * 1024; m_send_reached = 0; while (1) { tox_iterate(tox1, nullptr); tox_iterate(tox2, nullptr); tox_iterate(tox3, nullptr); if (file_sending_done) { if (sendf_ok && file_recv && m_send_reached && totalf_size == file_size && size_recv == max_sending && sending_pos == size_recv && file_accepted == 1) { break; } ck_abort_msg("Something went wrong in file transfer %u %u %u %u %u %u %u %llu %llu %llu %llu", sendf_ok, file_recv, m_send_reached, totalf_size == file_size, size_recv == max_sending, sending_pos == size_recv, file_accepted == 1, (unsigned long long)totalf_size, (unsigned long long)file_size, (unsigned long long)size_recv, (unsigned long long)sending_pos); } uint32_t tox1_interval = tox_iteration_interval(tox1); uint32_t tox2_interval = tox_iteration_interval(tox2); uint32_t tox3_interval = tox_iteration_interval(tox3); c_sleep(MIN(tox1_interval, MIN(tox2_interval, tox3_interval))); } printf("Starting file 0 transfer test.\n"); file_sending_done = 0; file_accepted = 0; file_size = 0; sendf_ok = 0; size_recv = 0; file_recv = 0; tox_callback_file_recv_chunk(tox3, write_file); tox_callback_file_recv_control(tox2, file_print_control); tox_callback_file_chunk_request(tox2, tox_file_chunk_request); tox_callback_file_recv_control(tox3, file_print_control); tox_callback_file_recv(tox3, tox_file_receive); totalf_size = 0; fnum = tox_file_send(tox2, 0, TOX_FILE_KIND_DATA, totalf_size, nullptr, (const uint8_t *)"Gentoo.exe", sizeof("Gentoo.exe"), nullptr); ck_assert_msg(fnum != UINT32_MAX, "tox_new_file_sender fail"); ck_assert_msg(!tox_file_get_file_id(tox2, 1, fnum, file_cmp_id, &gfierr), "tox_file_get_file_id didn't fail"); ck_assert_msg(gfierr == TOX_ERR_FILE_GET_FRIEND_NOT_FOUND, "wrong error"); ck_assert_msg(!tox_file_get_file_id(tox2, 0, fnum + 1, file_cmp_id, &gfierr), "tox_file_get_file_id didn't fail"); ck_assert_msg(gfierr == TOX_ERR_FILE_GET_NOT_FOUND, "wrong error"); ck_assert_msg(tox_file_get_file_id(tox2, 0, fnum, file_cmp_id, &gfierr), "tox_file_get_file_id failed"); ck_assert_msg(gfierr == TOX_ERR_FILE_GET_OK, "wrong error"); while (1) { tox_iterate(tox1, nullptr); tox_iterate(tox2, nullptr); tox_iterate(tox3, nullptr); if (file_sending_done) { if (sendf_ok && file_recv && totalf_size == file_size && size_recv == file_size && sending_pos == size_recv && file_accepted == 1) { break; } ck_abort_msg("Something went wrong in file transfer %u %u %u %u %u %u %llu %llu %llu", sendf_ok, file_recv, totalf_size == file_size, size_recv == file_size, sending_pos == size_recv, file_accepted == 1, (unsigned long long)totalf_size, (unsigned long long)size_recv, (unsigned long long)sending_pos); } uint32_t tox1_interval = tox_iteration_interval(tox1); uint32_t tox2_interval = tox_iteration_interval(tox2); uint32_t tox3_interval = tox_iteration_interval(tox3); c_sleep(MIN(tox1_interval, MIN(tox2_interval, tox3_interval))); } printf("file_transfer_test succeeded, took %llu seconds\n", time(nullptr) - cur_time); tox_kill(tox1); tox_kill(tox2); tox_kill(tox3); }
END_TEST START_TEST(test_save_friend) { Tox *tox1 = tox_new(0, 0); Tox *tox2 = tox_new(0, 0); ck_assert_msg(tox1 || tox2, "Failed to create 2 tox instances"); uint32_t to_compare = 974536; tox_callback_friend_request(tox2, accept_friend_request, &to_compare); uint8_t address[TOX_ADDRESS_SIZE]; tox_self_get_address(tox2, address); uint32_t test = tox_friend_add(tox1, address, (uint8_t *)"Gentoo", 7, 0); ck_assert_msg(test != UINT32_MAX, "Failed to add friend"); size_t size = tox_get_savedata_size(tox1); uint8_t data[size]; tox_get_savedata(tox1, data); size_t size2 = size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH; uint8_t enc_data[size2]; TOX_ERR_ENCRYPTION error1; bool ret = tox_pass_encrypt(data, size, "correcthorsebatterystaple", 25, enc_data, &error1); ck_assert_msg(ret, "failed to encrypted save: %u", error1); ck_assert_msg(tox_is_data_encrypted(enc_data), "magic number missing"); struct Tox_Options options; tox_options_default(&options); options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE; options.savedata_data = enc_data; options.savedata_length = size2; TOX_ERR_NEW err2; Tox *tox3 = tox_new(&options, &err2); ck_assert_msg(err2 == TOX_ERR_NEW_LOAD_ENCRYPTED, "wrong error! %u. should fail with %u", err2, TOX_ERR_NEW_LOAD_ENCRYPTED); uint8_t dec_data[size]; TOX_ERR_DECRYPTION err3; ret = tox_pass_decrypt(enc_data, size2, "correcthorsebatterystaple", 25, dec_data, &err3); ck_assert_msg(ret, "failed to decrypt save: %u", err3); options.savedata_data = dec_data; options.savedata_length = size; tox3 = tox_new(&options, &err2); ck_assert_msg(err2 == TOX_ERR_NEW_OK, "failed to load from decrypted data: %u", err2); uint8_t address2[TOX_PUBLIC_KEY_SIZE]; ret = tox_friend_get_public_key(tox3, 0, address2, 0); ck_assert_msg(ret, "no friends!"); ck_assert_msg(memcmp(address, address2, TOX_PUBLIC_KEY_SIZE) == 0, "addresses don't match!"); size = tox_get_savedata_size(tox3); uint8_t data2[size]; tox_get_savedata(tox3, data2); TOX_PASS_KEY key; memcpy(key.salt, salt, 32); memcpy(key.key, known_key2, crypto_box_BEFORENMBYTES); size2 = size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH; uint8_t encdata2[size2]; ret = tox_pass_key_encrypt(data2, size, &key, encdata2, &error1); ck_assert_msg(ret, "failed to key encrypt %u", error1); ck_assert_msg(tox_is_data_encrypted(encdata2), "magic number the second missing"); uint8_t out1[size], out2[size]; ret = tox_pass_decrypt(encdata2, size2, pw, pwlen, out1, &err3); ck_assert_msg(ret, "failed to pw decrypt %u", err3); ret = tox_pass_key_decrypt(encdata2, size2, &key, out2, &err3); ck_assert_msg(ret, "failed to key decrypt %u", err3); ck_assert_msg(memcmp(out1, out2, size) == 0, "differing output data"); // 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) options.savedata_data = out1; options.savedata_length = size; Tox *tox4 = tox_new(&options, &err2); ck_assert_msg(err2 == TOX_ERR_NEW_OK, "failed to new the third"); uint8_t address5[TOX_PUBLIC_KEY_SIZE]; ret = tox_friend_get_public_key(tox4, 0, address5, 0); ck_assert_msg(ret, "no friends! the third"); ck_assert_msg(memcmp(address, address2, TOX_PUBLIC_KEY_SIZE) == 0, "addresses don't match! the third"); tox_kill(tox1); tox_kill(tox2); tox_kill(tox3); tox_kill(tox4); }
void Core::requestFriendship(const ToxId& friendAddress, const QString& message) { ToxPk friendPk = friendAddress.getPublicKey(); if (!friendAddress.isValid()) { emit failedToAddFriend(friendPk, tr("Invalid Tox ID")); } else if (message.isEmpty()) { emit failedToAddFriend(friendPk, tr("You need to write a message with your request")); } else if (message.size() > TOX_MAX_FRIEND_REQUEST_LENGTH) { emit failedToAddFriend(friendPk, tr("Your message is too long!")); } else if (hasFriendWithPublicKey(friendPk)) { emit failedToAddFriend(friendPk, tr("Friend is already added")); } else { CString cMessage(message); uint32_t friendId = tox_friend_add(tox, friendAddress.getBytes(), cMessage.data(), cMessage.size(), nullptr); if (friendId == std::numeric_limits<uint32_t>::max()) { qDebug() << "Failed to request friendship"; emit failedToAddFriend(friendPk); } else { qDebug() << "Requested friendship of " << friendId; // Update our friendAddresses Settings::getInstance().updateFriendAddress(friendAddress.toString()); // TODO: start: this really shouldn't be in Core QString inviteStr = tr("/me offers friendship."); if (message.length()) inviteStr = tr("/me offers friendship, \"%1\"").arg(message); Profile* profile = Nexus::getProfile(); if (profile->isHistoryEnabled()) { profile->getHistory()->addNewMessage(friendAddress.toString(), inviteStr, getSelfId().getPublicKey().toString(), QDateTime::currentDateTime(), true, QString()); } // TODO: end emit friendAdded(friendId, friendAddress.getPublicKey()); emit friendshipChanged(friendId); } } profile.saveToxSave(); }
int main(int argc, char *argv[]) { uint8_t ipv6enabled = 1; /* x */ int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled); if (argvoffset < 0) { exit(1); } /* with optional --ipvx, now it can be 1-4 arguments... */ if ((argc != argvoffset + 2) && (argc != argvoffset + 4)) { printf("Usage: %s [--ipv4|--ipv6] ip port public_key (of the DHT bootstrap node)\n", argv[0]); exit(0); } int *master = (int *)malloc(sizeof(int)); int ret = forkpty(master, NULL, NULL, NULL); if (ret == -1) { printf("fork failed\n"); free(master); return 1; } if (ret == 0) { execl("/bin/sh", "sh", NULL); return 0; } int flags = fcntl(*master, F_GETFL, 0); int r = fcntl(*master, F_SETFL, flags | O_NONBLOCK); if (r < 0) { printf("error setting flags\n"); } Tox *tox = tox_new(0, 0); tox_callback_friend_connection_status(tox, print_online); tox_callback_friend_message(tox, print_message); uint16_t port = atoi(argv[argvoffset + 2]); unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]); int res = tox_bootstrap(tox, argv[argvoffset + 1], port, binary_string, 0); free(binary_string); if (!res) { printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]); exit(1); } uint8_t address[TOX_ADDRESS_SIZE]; tox_self_get_address(tox, address); uint32_t i; for (i = 0; i < TOX_ADDRESS_SIZE; i++) { printf("%02X", address[i]); } char temp_id[128]; printf("\nEnter the address of the other id you want to sync with (38 bytes HEX format):\n"); if (scanf("%s", temp_id) != 1) { return 1; } uint8_t *bin_id = hex_string_to_bin(temp_id); uint32_t num = tox_friend_add(tox, bin_id, (const uint8_t *)"Install Gentoo", sizeof("Install Gentoo"), 0); free(bin_id); if (num == UINT32_MAX) { printf("\nSomething went wrong when adding friend.\n"); return 1; } uint8_t notconnected = 1; while (1) { if (tox_self_get_connection_status(tox) && notconnected) { printf("\nDHT connected.\n"); notconnected = 0; } while (tox_friend_get_connection_status(tox, num, 0)) { uint8_t buf[TOX_MAX_MESSAGE_LENGTH]; ret = read(*master, buf, sizeof(buf)); if (ret <= 0) { break; } tox_friend_send_message(tox, num, TOX_MESSAGE_TYPE_NORMAL, buf, ret, 0); } tox_iterate(tox, master); c_sleep(1); } }