diff options
Diffstat (limited to 'auto_tests/tox_test.c')
-rw-r--r-- | auto_tests/tox_test.c | 196 |
1 files changed, 196 insertions, 0 deletions
diff --git a/auto_tests/tox_test.c b/auto_tests/tox_test.c index 0dab2e69..40022b0a 100644 --- a/auto_tests/tox_test.c +++ b/auto_tests/tox_test.c | |||
@@ -332,6 +332,7 @@ START_TEST(test_one) | |||
332 | tox_self_set_name(tox1, name, sizeof(name), 0); | 332 | tox_self_set_name(tox1, name, sizeof(name), 0); |
333 | ck_assert_msg(tox_self_get_name_size(tox1) == sizeof(name), "Can't set name of TOX_MAX_NAME_LENGTH"); | 333 | ck_assert_msg(tox_self_get_name_size(tox1) == sizeof(name), "Can't set name of TOX_MAX_NAME_LENGTH"); |
334 | 334 | ||
335 | tox_self_get_address(tox1, address); | ||
335 | size_t save_size = tox_get_savedata_size(tox1); | 336 | size_t save_size = tox_get_savedata_size(tox1); |
336 | uint8_t data[save_size]; | 337 | uint8_t data[save_size]; |
337 | tox_get_savedata(tox1, data); | 338 | tox_get_savedata(tox1, data); |
@@ -344,6 +345,9 @@ START_TEST(test_one) | |||
344 | 345 | ||
345 | ck_assert_msg(tox_self_get_name_size(tox2) == sizeof name, "Wrong name size."); | 346 | ck_assert_msg(tox_self_get_name_size(tox2) == sizeof name, "Wrong name size."); |
346 | 347 | ||
348 | uint8_t address2[TOX_ADDRESS_SIZE]; | ||
349 | tox_self_get_address(tox2, address2); | ||
350 | ck_assert_msg(memcmp(address2, address, TOX_ADDRESS_SIZE) == 0, "Wrong address."); | ||
347 | uint8_t new_name[TOX_MAX_NAME_LENGTH] = { 0 }; | 351 | uint8_t new_name[TOX_MAX_NAME_LENGTH] = { 0 }; |
348 | tox_self_get_name(tox2, new_name); | 352 | tox_self_get_name(tox2, new_name); |
349 | ck_assert_msg(memcmp(name, new_name, TOX_MAX_NAME_LENGTH) == 0, "Wrong name"); | 353 | ck_assert_msg(memcmp(name, new_name, TOX_MAX_NAME_LENGTH) == 0, "Wrong name"); |
@@ -809,6 +813,196 @@ loop_top: | |||
809 | } | 813 | } |
810 | END_TEST | 814 | END_TEST |
811 | 815 | ||
816 | #define TCP_RELAY_PORT 33448 | ||
817 | |||
818 | START_TEST(test_many_clients_tcp) | ||
819 | { | ||
820 | long long unsigned int cur_time = time(NULL); | ||
821 | Tox *toxes[NUM_TOXES]; | ||
822 | uint32_t i, j; | ||
823 | uint32_t to_comp = 974536; | ||
824 | |||
825 | for (i = 0; i < NUM_TOXES; ++i) { | ||
826 | struct Tox_Options opts; | ||
827 | tox_options_default(&opts); | ||
828 | |||
829 | if (i == 0) { | ||
830 | opts.tcp_port = TCP_RELAY_PORT; | ||
831 | } else { | ||
832 | opts.udp_enabled = 0; | ||
833 | } | ||
834 | |||
835 | toxes[i] = tox_new(&opts, 0, 0, 0); | ||
836 | ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i); | ||
837 | tox_callback_friend_request(toxes[i], accept_friend_request, &to_comp); | ||
838 | uint8_t dpk[TOX_PUBLIC_KEY_SIZE]; | ||
839 | tox_self_get_dht_id(toxes[0], dpk); | ||
840 | ck_assert_msg(tox_add_tcp_relay(toxes[i], "::1", TCP_RELAY_PORT, dpk, 0), "add relay error"); | ||
841 | ck_assert_msg(tox_bootstrap(toxes[i], "::1", 33445, dpk, 0), "Bootstrap error"); | ||
842 | } | ||
843 | |||
844 | { | ||
845 | TOX_ERR_GET_PORT error; | ||
846 | ck_assert_msg(tox_self_get_udp_port(toxes[0], &error) == 33445, "First Tox instance did not bind to udp port 33445.\n"); | ||
847 | ck_assert_msg(error == TOX_ERR_GET_PORT_OK, "wrong error"); | ||
848 | ck_assert_msg(tox_self_get_tcp_port(toxes[0], &error) == TCP_RELAY_PORT, | ||
849 | "First Tox instance did not bind to tcp port %u.\n", TCP_RELAY_PORT); | ||
850 | ck_assert_msg(error == TOX_ERR_GET_PORT_OK, "wrong error"); | ||
851 | } | ||
852 | |||
853 | struct { | ||
854 | uint16_t tox1; | ||
855 | uint16_t tox2; | ||
856 | } pairs[NUM_FRIENDS]; | ||
857 | |||
858 | uint8_t address[TOX_ADDRESS_SIZE]; | ||
859 | |||
860 | for (i = 0; i < NUM_FRIENDS; ++i) { | ||
861 | loop_top: | ||
862 | pairs[i].tox1 = rand() % NUM_TOXES; | ||
863 | pairs[i].tox2 = (pairs[i].tox1 + rand() % (NUM_TOXES - 1) + 1) % NUM_TOXES; | ||
864 | |||
865 | for (j = 0; j < i; ++j) { | ||
866 | if (pairs[j].tox2 == pairs[i].tox1 && pairs[j].tox1 == pairs[i].tox2) | ||
867 | goto loop_top; | ||
868 | } | ||
869 | |||
870 | tox_self_get_address(toxes[pairs[i].tox1], address); | ||
871 | |||
872 | TOX_ERR_FRIEND_ADD test; | ||
873 | uint32_t num = tox_friend_add(toxes[pairs[i].tox2], address, (uint8_t *)"Gentoo", 7, &test); | ||
874 | |||
875 | if (test == TOX_ERR_FRIEND_ADD_ALREADY_SENT) { | ||
876 | goto loop_top; | ||
877 | } | ||
878 | |||
879 | ck_assert_msg(num != UINT32_MAX && test == TOX_ERR_FRIEND_ADD_OK, "Failed to add friend error code: %i", test); | ||
880 | } | ||
881 | |||
882 | while (1) { | ||
883 | uint16_t counter = 0; | ||
884 | |||
885 | for (i = 0; i < NUM_TOXES; ++i) { | ||
886 | for (j = 0; j < tox_self_get_friend_list_size(toxes[i]); ++j) | ||
887 | if (tox_friend_get_connection_status(toxes[i], j, 0) == TOX_CONNECTION_TCP) | ||
888 | ++counter; | ||
889 | } | ||
890 | |||
891 | if (counter == NUM_FRIENDS * 2) { | ||
892 | break; | ||
893 | } | ||
894 | |||
895 | for (i = 0; i < NUM_TOXES; ++i) { | ||
896 | tox_iterate(toxes[i]); | ||
897 | } | ||
898 | |||
899 | c_sleep(50); | ||
900 | } | ||
901 | |||
902 | for (i = 0; i < NUM_TOXES; ++i) { | ||
903 | tox_kill(toxes[i]); | ||
904 | } | ||
905 | |||
906 | printf("test_many_clients_tcp succeeded, took %llu seconds\n", time(NULL) - cur_time); | ||
907 | } | ||
908 | END_TEST | ||
909 | |||
910 | #define NUM_TCP_RELAYS 3 | ||
911 | |||
912 | START_TEST(test_many_clients_tcp_b) | ||
913 | { | ||
914 | long long unsigned int cur_time = time(NULL); | ||
915 | Tox *toxes[NUM_TOXES]; | ||
916 | uint32_t i, j; | ||
917 | uint32_t to_comp = 974536; | ||
918 | |||
919 | for (i = 0; i < NUM_TOXES; ++i) { | ||
920 | struct Tox_Options opts; | ||
921 | tox_options_default(&opts); | ||
922 | |||
923 | if (i < NUM_TCP_RELAYS) { | ||
924 | opts.tcp_port = TCP_RELAY_PORT + i; | ||
925 | } else { | ||
926 | opts.udp_enabled = 0; | ||
927 | } | ||
928 | |||
929 | toxes[i] = tox_new(&opts, 0, 0, 0); | ||
930 | ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i); | ||
931 | tox_callback_friend_request(toxes[i], accept_friend_request, &to_comp); | ||
932 | uint8_t dpk[TOX_PUBLIC_KEY_SIZE]; | ||
933 | tox_self_get_dht_id(toxes[(i % NUM_TCP_RELAYS)], dpk); | ||
934 | ck_assert_msg(tox_add_tcp_relay(toxes[i], "::1", TCP_RELAY_PORT + (i % NUM_TCP_RELAYS), dpk, 0), "add relay error"); | ||
935 | tox_self_get_dht_id(toxes[0], dpk); | ||
936 | ck_assert_msg(tox_bootstrap(toxes[i], "::1", 33445, dpk, 0), "Bootstrap error"); | ||
937 | } | ||
938 | |||
939 | { | ||
940 | TOX_ERR_GET_PORT error; | ||
941 | ck_assert_msg(tox_self_get_udp_port(toxes[0], &error) == 33445, "First Tox instance did not bind to udp port 33445.\n"); | ||
942 | ck_assert_msg(error == TOX_ERR_GET_PORT_OK, "wrong error"); | ||
943 | ck_assert_msg(tox_self_get_tcp_port(toxes[0], &error) == TCP_RELAY_PORT, | ||
944 | "First Tox instance did not bind to tcp port %u.\n", TCP_RELAY_PORT); | ||
945 | ck_assert_msg(error == TOX_ERR_GET_PORT_OK, "wrong error"); | ||
946 | } | ||
947 | |||
948 | struct { | ||
949 | uint16_t tox1; | ||
950 | uint16_t tox2; | ||
951 | } pairs[NUM_FRIENDS]; | ||
952 | |||
953 | uint8_t address[TOX_ADDRESS_SIZE]; | ||
954 | |||
955 | for (i = 0; i < NUM_FRIENDS; ++i) { | ||
956 | loop_top: | ||
957 | pairs[i].tox1 = rand() % NUM_TOXES; | ||
958 | pairs[i].tox2 = (pairs[i].tox1 + rand() % (NUM_TOXES - 1) + 1) % NUM_TOXES; | ||
959 | |||
960 | for (j = 0; j < i; ++j) { | ||
961 | if (pairs[j].tox2 == pairs[i].tox1 && pairs[j].tox1 == pairs[i].tox2) | ||
962 | goto loop_top; | ||
963 | } | ||
964 | |||
965 | tox_self_get_address(toxes[pairs[i].tox1], address); | ||
966 | |||
967 | TOX_ERR_FRIEND_ADD test; | ||
968 | uint32_t num = tox_friend_add(toxes[pairs[i].tox2], address, (uint8_t *)"Gentoo", 7, &test); | ||
969 | |||
970 | if (test == TOX_ERR_FRIEND_ADD_ALREADY_SENT) { | ||
971 | goto loop_top; | ||
972 | } | ||
973 | |||
974 | ck_assert_msg(num != UINT32_MAX && test == TOX_ERR_FRIEND_ADD_OK, "Failed to add friend error code: %i", test); | ||
975 | } | ||
976 | |||
977 | while (1) { | ||
978 | uint16_t counter = 0; | ||
979 | |||
980 | for (i = 0; i < NUM_TOXES; ++i) { | ||
981 | for (j = 0; j < tox_self_get_friend_list_size(toxes[i]); ++j) | ||
982 | if (tox_friend_get_connection_status(toxes[i], j, 0) == TOX_CONNECTION_TCP) | ||
983 | ++counter; | ||
984 | } | ||
985 | |||
986 | if (counter == NUM_FRIENDS * 2) { | ||
987 | break; | ||
988 | } | ||
989 | |||
990 | for (i = 0; i < NUM_TOXES; ++i) { | ||
991 | tox_iterate(toxes[i]); | ||
992 | } | ||
993 | |||
994 | c_sleep(50); | ||
995 | } | ||
996 | |||
997 | for (i = 0; i < NUM_TOXES; ++i) { | ||
998 | tox_kill(toxes[i]); | ||
999 | } | ||
1000 | |||
1001 | printf("test_many_clients_tcp_b succeeded, took %llu seconds\n", time(NULL) - cur_time); | ||
1002 | } | ||
1003 | END_TEST | ||
1004 | |||
1005 | |||
812 | #define NUM_GROUP_TOX 32 | 1006 | #define NUM_GROUP_TOX 32 |
813 | 1007 | ||
814 | void g_accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata) | 1008 | void g_accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata) |
@@ -996,6 +1190,8 @@ Suite *tox_suite(void) | |||
996 | DEFTESTCASE(one); | 1190 | DEFTESTCASE(one); |
997 | DEFTESTCASE_SLOW(few_clients, 50); | 1191 | DEFTESTCASE_SLOW(few_clients, 50); |
998 | DEFTESTCASE_SLOW(many_clients, 150); | 1192 | DEFTESTCASE_SLOW(many_clients, 150); |
1193 | DEFTESTCASE_SLOW(many_clients_tcp, 20); | ||
1194 | DEFTESTCASE_SLOW(many_clients_tcp_b, 20); | ||
999 | DEFTESTCASE_SLOW(many_group, 100); | 1195 | DEFTESTCASE_SLOW(many_group, 100); |
1000 | return s; | 1196 | return s; |
1001 | } | 1197 | } |