• R/O
  • SSH
  • HTTPS

eirsamples: Commit


Commit MetaInfo

Revision8 (tree)
Time2018-05-02 22:43:44
Authorquiret

Log Message

- added some more network unit tests (it is pretty stable now)

Change Summary

Incremental Difference

--- marketstatcollect/src/network_tests.cpp (revision 7)
+++ marketstatcollect/src/network_tests.cpp (revision 8)
@@ -7,34 +7,10 @@
77 #include <stdio.h>
88 #include <assert.h>
99
10-void DO_NETWORK_TEST( void )
10+static void LOCAL_NETWORK_TEST( TidesNetwork::TidesNet *netMan )
1111 {
1212 using namespace TidesNetwork;
1313
14- struct MemoryManager : public MemoryManagerInterface
15- {
16- void* Allocate( size_t memSize )
17- {
18- return heapMem.Allocate( memSize );
19- }
20-
21- void Free( void *memPtr )
22- {
23- return heapMem.Free( memPtr );
24- }
25-
26- NativeHeapAllocator heapMem;
27- };
28- MemoryManager memMan;
29-
30- TidesNet::constr_params cparams;
31- cparams.memMan = &memMan;
32-
33- // Create the main networking manager for testing.
34- TidesNet *netMan = TidesNet::Construct( cparams );
35-
36- assert( netMan != nullptr );
37-
3814 printf( "testing local hostname resolution..." );
3915 {
4016 // Test IPV4.
@@ -54,6 +30,162 @@
5430 }
5531 printf( "ok.\n" );
5632
33+ printf( "testing IPV6 TCP connect to localhost..." );
34+ {
35+ // First get the IP.
36+ ipdescIPV6 resolved;
37+
38+ assert( netMan->ResolveHostnameIPV6( "localhost", resolved ) == true );
39+
40+ // Next open a listener socket.
41+ connRecvInst listener = netMan->CreateNetListener( 7733, eConnFamily::IPV6, eConnProtocol::TCP );
42+
43+ assert( listener.is_good() );
44+
45+ // Then connect to it with a new socket.
46+ netSocketInst localConn = netMan->ConnectRemoteIPV6( resolved, 7733, eConnProtocol::TCP );
47+
48+ assert( localConn.is_good() );
49+
50+ // Accept with our listener.
51+ netSocketInst remoteConn = listener->Accept();
52+
53+ assert( remoteConn != nullptr );
54+ }
55+ printf( "ok.\n" );
56+
57+ printf( "testing IPV4 TCP connect to localhost..." );
58+ {
59+ // First get the IP.
60+ ipdescIPV4 resolved;
61+
62+ assert( netMan->ResolveHostnameIPV4( "localhost", resolved ) == true );
63+
64+ // Next open a listener socket.
65+ connRecvInst listener = netMan->CreateNetListener( 4422, eConnFamily::IPV4, eConnProtocol::TCP );
66+
67+ assert( listener.is_good() );
68+
69+ // Then connect to it with a new socket.
70+ netSocketInst localConn = netMan->ConnectRemoteIPV4( resolved, 4422, eConnProtocol::TCP );
71+
72+ assert( localConn.is_good() );
73+
74+ // Accept with our listener.
75+ netSocketInst remoteConn = listener->Accept();
76+
77+ assert( remoteConn.is_good() );
78+ }
79+ printf( "ok.\n" );
80+
81+ printf( "testing local TCP IPV6 small send-and-receive..." );
82+ {
83+ static const char sample_data[] = "my meow is a great one.";
84+
85+ ipdescIPV6 localhost;
86+
87+ bool gotToResolve = netMan->ResolveHostnameIPV6( "localhost", localhost );
88+
89+ assert( gotToResolve == true );
90+
91+ connRecvInst serverSocket = netMan->CreateNetListener( 6336, eConnFamily::IPV6, eConnProtocol::TCP );
92+
93+ assert( serverSocket.is_good() );
94+
95+ netSocketInst clientSocket = netMan->ConnectRemoteIPV6( localhost, 6336, eConnProtocol::TCP );
96+
97+ assert( clientSocket.is_good() );
98+
99+ netSocketInst serverSendSocket = serverSocket->Accept();
100+
101+ assert( serverSendSocket.is_good() );
102+
103+ // Helpers.
104+ auto send_and_receive = [&]( NetSocket *sender, NetSocket *receiver )
105+ {
106+ // We assume that the small size that we sent fits 100% into the buffer.
107+ {
108+ size_t writeCount = sender->Write( sample_data, sizeof(sample_data) );
109+
110+ assert( writeCount == sizeof(sample_data) );
111+ }
112+
113+ // Receive and validate.
114+ {
115+ char wanted_data[ sizeof(sample_data) * 2 ];
116+
117+ size_t readCount = receiver->Read( wanted_data, sizeof(wanted_data) );
118+
119+ assert( readCount == sizeof(sample_data) );
120+
121+ for ( size_t n = 0; n < sizeof(sample_data); n++ )
122+ {
123+ assert( wanted_data[n] == sample_data[n] );
124+ }
125+ }
126+ };
127+
128+ // Send from client to server.
129+ send_and_receive( clientSocket, serverSendSocket );
130+
131+ // Also test the other way round.
132+ send_and_receive( serverSendSocket, clientSocket );
133+ }
134+ printf( "ok.\n" );
135+
136+ printf( "testing local IPV6 TCP big data transfer..." );
137+ {
138+ // We want to use an incremental row of (d/q)words and transfer it to the client, as one big chunk.
139+ static constexpr size_t NUM_COUNT = 1000;
140+
141+ size_t *number_array = new size_t[ NUM_COUNT ];
142+
143+ for ( size_t n = 0; n < NUM_COUNT; n++ )
144+ {
145+ number_array[ n ] = n;
146+ }
147+
148+ ipdescIPV6 localhost;
149+
150+ bool gotToResolve = netMan->ResolveHostnameIPV6( "localhost", localhost );
151+
152+ // Create the network architecture.
153+ connRecvInst listener = netMan->CreateNetListener( 1298, eConnFamily::IPV6, eConnProtocol::TCP );
154+
155+ assert( listener.is_good() );
156+
157+ netSocketInst senderSocket = netMan->ConnectRemoteIPV6( localhost, 1298, eConnProtocol::TCP );
158+
159+ assert( senderSocket.is_good() );
160+
161+ netSocketInst receiverSocket = listener->Accept();
162+
163+ assert( receiverSocket.is_good() );
164+
165+ // Send the big data.
166+ size_t big_tx_size = ( sizeof(size_t) * NUM_COUNT );
167+
168+ netMan->SocketTransferBuffer( senderSocket, number_array, big_tx_size );
169+
170+ // Clear the buffer.
171+ memset( number_array, 0, big_tx_size );
172+
173+ // Receive the big data again.
174+ netMan->SocketReceiveBuffer( receiverSocket, number_array, big_tx_size );
175+
176+ // Verify the received data.
177+ for ( size_t n = 0; n < NUM_COUNT; n++ )
178+ {
179+ assert( number_array[n] == n );
180+ }
181+ }
182+ printf( "ok.\n" );
183+}
184+
185+static void REMOTE_NETWORK_TEST( TidesNetwork::TidesNet *netMan )
186+{
187+ using namespace TidesNetwork;
188+
57189 printf( "testing remote hostname resolution..." );
58190 {
59191 // Test IPV4.
@@ -66,6 +198,7 @@
66198 {
67199 // Not many hosts actually have IPV6 even today (first of May 2018) so it was actually
68200 // difficult to find one.
201+ // WARNING: not all networks seem to support IPV6 anyway.
69202 ipdescIPV6 resolved;
70203
71204 assert( netMan->ResolveHostnameIPV6( "mtasa.com", resolved ) == true );
@@ -72,36 +205,15 @@
72205 }
73206 }
74207 printf( "ok.\n" );
208+}
75209
76- printf( "testing IPV6 TCP connect to local host..." );
77- {
78- // First get the IP.
79- ipdescIPV6 resolved;
210+void DO_NETWORK_TEST( void )
211+{
212+ using namespace TidesNetwork;
80213
81- assert( netMan->ResolveHostnameIPV6( "localhost", resolved ) == true );
214+ // Smart capsules take care of the annoying stuff.
215+ netManInst netMan;
82216
83- // Next open a listener socket.
84- ConnReceiver *listener = netMan->CreateNetListener( 5050, eConnFamily::IPV6, eConnProtocol::TCP );
85-
86- assert( listener != nullptr );
87-
88- // Then connect to it with a new socket.
89- NetSocket *localConn = netMan->ConnectRemoteIPV6( resolved, 5050, eConnProtocol::TCP );
90-
91- assert( localConn != nullptr );
92-
93- // Accept with our listener.
94- NetSocket *remoteConn = listener->Accept();
95-
96- assert( remoteConn != nullptr );
97-
98- // Quit everything we created.
99- netMan->DestroyNetSocket( remoteConn );
100- netMan->DestroyNetListener( listener );
101- netMan->DestroyNetSocket( localConn );
102- }
103- printf( "ok.\n" );
104-
105- // Destroy the networking manager again.
106- TidesNet::Destruct( netMan );
217+ LOCAL_NETWORK_TEST( netMan );
218+ REMOTE_NETWORK_TEST( netMan );
107219 }
\ No newline at end of file
Show on old repository browser