88#include "../toxcore/TCP_server.h"
99#include "../toxcore/crypto_core.h"
1010#include "../toxcore/mono_time.h"
11+ #include "../toxcore/os_random.h"
12+ #include "../toxcore/os_network.h"
13+ #include "../toxcore/os_memory.h"
1114#include "../toxcore/util.h"
1215#include "auto_test_support.h"
1316
@@ -45,15 +48,15 @@ static uint16_t ports[NUM_PORTS] = {13215, 33445, 25643};
4548
4649static void test_basic (void )
4750{
48- const Random * rng = system_random ();
51+ const Random * rng = os_random ();
4952 ck_assert (rng != nullptr );
50- const Network * ns = system_network ();
53+ const Network * ns = os_network ();
5154 ck_assert (ns != nullptr );
52- const Memory * mem = system_memory ();
55+ const Memory * mem = os_memory ();
5356 ck_assert (mem != nullptr );
5457
55- Mono_Time * mono_time = mono_time_new (mem , nullptr , nullptr );
56- Logger * logger = logger_new ();
58+ Mono_Time * mono_time = mono_time_new (mem , nullptr );
59+ Logger * logger = logger_new (mem );
5760 logger_callback_log (logger , print_debug_logger , nullptr , nullptr );
5861
5962 // Attempt to create a new TCP_Server instance.
@@ -103,7 +106,7 @@ static void test_basic(void)
103106
104107 // Encrypting handshake
105108 int ret = encrypt_data (self_public_key , f_secret_key , handshake + CRYPTO_PUBLIC_KEY_SIZE , handshake_plain ,
106- TCP_HANDSHAKE_PLAIN_SIZE , handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE );
109+ TCP_HANDSHAKE_PLAIN_SIZE , handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE , mem );
107110 ck_assert_msg (ret == TCP_CLIENT_HANDSHAKE_SIZE - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE ),
108111 "encrypt_data() call failed." );
109112
@@ -129,7 +132,7 @@ static void test_basic(void)
129132 ck_assert_msg (net_recv (ns , logger , sock , response , TCP_SERVER_HANDSHAKE_SIZE , & localhost ) == TCP_SERVER_HANDSHAKE_SIZE ,
130133 "Could/did not receive a server response to the initial handshake." );
131134 ret = decrypt_data (self_public_key , f_secret_key , response , response + CRYPTO_NONCE_SIZE ,
132- TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE , response_plain );
135+ TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE , response_plain , mem );
133136 ck_assert_msg (ret == TCP_HANDSHAKE_PLAIN_SIZE , "Failed to decrypt handshake response." );
134137 uint8_t f_nonce_r [CRYPTO_NONCE_SIZE ];
135138 uint8_t f_shared_key [CRYPTO_SHARED_KEY_SIZE ];
@@ -143,7 +146,7 @@ static void test_basic(void)
143146 uint8_t r_req [2 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE ];
144147 uint16_t size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE ;
145148 size = net_htons (size );
146- encrypt_data_symmetric (f_shared_key , f_nonce , r_req_p , 1 + CRYPTO_PUBLIC_KEY_SIZE , r_req + 2 );
149+ encrypt_data_symmetric (f_shared_key , f_nonce , r_req_p , 1 + CRYPTO_PUBLIC_KEY_SIZE , r_req + 2 , mem );
147150 increment_nonce (f_nonce );
148151 memcpy (r_req , & size , 2 );
149152
@@ -174,7 +177,7 @@ static void test_basic(void)
174177 "Wrong packet size for request response." );
175178
176179 uint8_t packet_resp_plain [4096 ];
177- ret = decrypt_data_symmetric (f_shared_key , f_nonce_r , packet_resp + 2 , recv_data_len - 2 , packet_resp_plain );
180+ ret = decrypt_data_symmetric (f_shared_key , f_nonce_r , packet_resp + 2 , recv_data_len - 2 , packet_resp_plain , mem );
178181 ck_assert_msg (ret != -1 , "Failed to decrypt the TCP server's response." );
179182 increment_nonce (f_nonce_r );
180183
@@ -229,7 +232,7 @@ static struct sec_TCP_con *new_TCP_con(const Logger *logger, const Memory *mem,
229232 random_nonce (rng , handshake + CRYPTO_PUBLIC_KEY_SIZE );
230233
231234 int ret = encrypt_data (tcp_server_public_key (tcp_s ), f_secret_key , handshake + CRYPTO_PUBLIC_KEY_SIZE , handshake_plain ,
232- TCP_HANDSHAKE_PLAIN_SIZE , handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE );
235+ TCP_HANDSHAKE_PLAIN_SIZE , handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE , mem );
233236 ck_assert_msg (ret == TCP_CLIENT_HANDSHAKE_SIZE - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE ),
234237 "Failed to encrypt the outgoing handshake." );
235238
@@ -249,7 +252,7 @@ static struct sec_TCP_con *new_TCP_con(const Logger *logger, const Memory *mem,
249252 ck_assert_msg (net_recv (sec_c -> ns , logger , sock , response , TCP_SERVER_HANDSHAKE_SIZE , & localhost ) == TCP_SERVER_HANDSHAKE_SIZE ,
250253 "Failed to receive server handshake response." );
251254 ret = decrypt_data (tcp_server_public_key (tcp_s ), f_secret_key , response , response + CRYPTO_NONCE_SIZE ,
252- TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE , response_plain );
255+ TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE , response_plain , mem );
253256 ck_assert_msg (ret == TCP_HANDSHAKE_PLAIN_SIZE , "Failed to decrypt server handshake response." );
254257 encrypt_precompute (response_plain , t_secret_key , sec_c -> shared_key );
255258 memcpy (sec_c -> recv_nonce , response_plain + CRYPTO_SHARED_KEY_SIZE , CRYPTO_NONCE_SIZE );
@@ -270,7 +273,7 @@ static int write_packet_TCP_test_connection(const Logger *logger, struct sec_TCP
270273
271274 uint16_t c_length = net_htons (length + CRYPTO_MAC_SIZE );
272275 memcpy (packet , & c_length , sizeof (uint16_t ));
273- int len = encrypt_data_symmetric (con -> shared_key , con -> sent_nonce , data , length , packet + sizeof (uint16_t ));
276+ int len = encrypt_data_symmetric (con -> shared_key , con -> sent_nonce , data , length , packet + sizeof (uint16_t ), con -> mem );
274277
275278 if ((unsigned int )len != (SIZEOF_VLA (packet ) - sizeof (uint16_t ))) {
276279 return -1 ;
@@ -295,23 +298,23 @@ static int read_packet_sec_TCP(const Logger *logger, struct sec_TCP_con *con, ui
295298
296299 int rlen = net_recv (con -> ns , logger , con -> sock , data , length , & localhost );
297300 ck_assert_msg (rlen == length , "Did not receive packet of correct length. Wanted %i, instead got %i" , length , rlen );
298- rlen = decrypt_data_symmetric (con -> shared_key , con -> recv_nonce , data + 2 , length - 2 , data );
301+ rlen = decrypt_data_symmetric (con -> shared_key , con -> recv_nonce , data + 2 , length - 2 , data , con -> mem );
299302 ck_assert_msg (rlen != -1 , "Failed to decrypt a received packet from the Relay server." );
300303 increment_nonce (con -> recv_nonce );
301304 return rlen ;
302305}
303306
304307static void test_some (void )
305308{
306- const Random * rng = system_random ();
309+ const Random * rng = os_random ();
307310 ck_assert (rng != nullptr );
308- const Network * ns = system_network ();
311+ const Network * ns = os_network ();
309312 ck_assert (ns != nullptr );
310- const Memory * mem = system_memory ();
313+ const Memory * mem = os_memory ();
311314 ck_assert (mem != nullptr );
312315
313- Mono_Time * mono_time = mono_time_new (mem , nullptr , nullptr );
314- Logger * logger = logger_new ();
316+ Mono_Time * mono_time = mono_time_new (mem , nullptr );
317+ Logger * logger = logger_new (mem );
315318
316319 uint8_t self_public_key [CRYPTO_PUBLIC_KEY_SIZE ];
317320 uint8_t self_secret_key [CRYPTO_SECRET_KEY_SIZE ];
@@ -498,15 +501,15 @@ static int oob_data_callback(void *object, const uint8_t *public_key, const uint
498501
499502static void test_client (void )
500503{
501- const Random * rng = system_random ();
504+ const Random * rng = os_random ();
502505 ck_assert (rng != nullptr );
503- const Network * ns = system_network ();
506+ const Network * ns = os_network ();
504507 ck_assert (ns != nullptr );
505- const Memory * mem = system_memory ();
508+ const Memory * mem = os_memory ();
506509 ck_assert (mem != nullptr );
507510
508- Logger * logger = logger_new ();
509- Mono_Time * mono_time = mono_time_new (mem , nullptr , nullptr );
511+ Logger * logger = logger_new (mem );
512+ Mono_Time * mono_time = mono_time_new (mem , nullptr );
510513
511514 uint8_t self_public_key [CRYPTO_PUBLIC_KEY_SIZE ];
512515 uint8_t self_secret_key [CRYPTO_SECRET_KEY_SIZE ];
@@ -632,15 +635,15 @@ static void test_client(void)
632635// Test how the client handles servers that don't respond.
633636static void test_client_invalid (void )
634637{
635- const Random * rng = system_random ();
638+ const Random * rng = os_random ();
636639 ck_assert (rng != nullptr );
637- const Network * ns = system_network ();
640+ const Network * ns = os_network ();
638641 ck_assert (ns != nullptr );
639- const Memory * mem = system_memory ();
642+ const Memory * mem = os_memory ();
640643 ck_assert (mem != nullptr );
641644
642- Mono_Time * mono_time = mono_time_new (mem , nullptr , nullptr );
643- Logger * logger = logger_new ();
645+ Mono_Time * mono_time = mono_time_new (mem , nullptr );
646+ Logger * logger = logger_new (mem );
644647
645648 uint8_t self_public_key [CRYPTO_PUBLIC_KEY_SIZE ];
646649 uint8_t self_secret_key [CRYPTO_SECRET_KEY_SIZE ];
@@ -711,15 +714,15 @@ static int tcp_data_callback(void *object, int id, const uint8_t *data, uint16_t
711714
712715static void test_tcp_connection (void )
713716{
714- const Random * rng = system_random ();
717+ const Random * rng = os_random ();
715718 ck_assert (rng != nullptr );
716- const Network * ns = system_network ();
719+ const Network * ns = os_network ();
717720 ck_assert (ns != nullptr );
718- const Memory * mem = system_memory ();
721+ const Memory * mem = os_memory ();
719722 ck_assert (mem != nullptr );
720723
721- Mono_Time * mono_time = mono_time_new (mem , nullptr , nullptr );
722- Logger * logger = logger_new ();
724+ Mono_Time * mono_time = mono_time_new (mem , nullptr );
725+ Logger * logger = logger_new (mem );
723726
724727 tcp_data_callback_called = 0 ;
725728 uint8_t self_public_key [CRYPTO_PUBLIC_KEY_SIZE ];
@@ -824,15 +827,15 @@ static int tcp_oobdata_callback(void *object, const uint8_t *public_key, unsigne
824827
825828static void test_tcp_connection2 (void )
826829{
827- const Random * rng = system_random ();
830+ const Random * rng = os_random ();
828831 ck_assert (rng != nullptr );
829- const Network * ns = system_network ();
832+ const Network * ns = os_network ();
830833 ck_assert (ns != nullptr );
831- const Memory * mem = system_memory ();
834+ const Memory * mem = os_memory ();
832835 ck_assert (mem != nullptr );
833836
834- Mono_Time * mono_time = mono_time_new (mem , nullptr , nullptr );
835- Logger * logger = logger_new ();
837+ Mono_Time * mono_time = mono_time_new (mem , nullptr );
838+ Logger * logger = logger_new (mem );
836839
837840 tcp_oobdata_callback_called = 0 ;
838841 tcp_data_callback_called = 0 ;
0 commit comments