• R/O
  • SSH
  • HTTPS

eirsamples: Commit


Commit MetaInfo

Revision7 (tree)
Time2018-05-01 23:20:02
Authorquiret

Log Message

- added some (temporary) networking unit tests
- some cleanup

Change Summary

Incremental Difference

--- marketstatcollect/src/main.cpp (revision 6)
+++ marketstatcollect/src/main.cpp (revision 7)
@@ -5,8 +5,13 @@
55
66 #include <iostream>
77
8+// Temporary tests.
9+extern void DO_NETWORK_TEST( void );
10+
811 int main( int argc, char *argv )
912 {
13+ DO_NETWORK_TEST();
14+
1015 std::cout <<
1116 "Marketing Data Collector by (c)Martin Turski, turningtides@outlook.de" << std::endl <<
1217 "put config.ini and sites.txt files into the application folder and run this tool" << std::endl <<
--- marketstatcollect/src/network_tests.cpp (nonexistent)
+++ marketstatcollect/src/network_tests.cpp (revision 7)
@@ -0,0 +1,107 @@
1+// For now I am putting all the network tests here.
2+// Should be moved into it's own project eventually.
3+
4+#include <tidesnet/tidesnet.h>
5+#include <sdk/OSUtils.memheap.h>
6+
7+#include <stdio.h>
8+#include <assert.h>
9+
10+void DO_NETWORK_TEST( void )
11+{
12+ using namespace TidesNetwork;
13+
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+
38+ printf( "testing local hostname resolution..." );
39+ {
40+ // Test IPV4.
41+ {
42+ ipdescIPV4 resolved;
43+
44+ assert( netMan->ResolveHostnameIPV4( "127.0.0.1", resolved ) == true );
45+ assert( netMan->ResolveHostnameIPV4( "localhost", resolved ) == true );
46+ }
47+ // Test IPV6.
48+ {
49+ ipdescIPV6 resolved;
50+
51+ assert( netMan->ResolveHostnameIPV6( "localhost", resolved ) == true );
52+ assert( netMan->ResolveHostnameIPV6( "::1", resolved ) == true );
53+ }
54+ }
55+ printf( "ok.\n" );
56+
57+ printf( "testing remote hostname resolution..." );
58+ {
59+ // Test IPV4.
60+ {
61+ ipdescIPV4 resolved;
62+
63+ assert( netMan->ResolveHostnameIPV4( "microsoft.com", resolved ) == true );
64+ }
65+ // Test IPV6.
66+ {
67+ // Not many hosts actually have IPV6 even today (first of May 2018) so it was actually
68+ // difficult to find one.
69+ ipdescIPV6 resolved;
70+
71+ assert( netMan->ResolveHostnameIPV6( "mtasa.com", resolved ) == true );
72+ }
73+ }
74+ printf( "ok.\n" );
75+
76+ printf( "testing IPV6 TCP connect to local host..." );
77+ {
78+ // First get the IP.
79+ ipdescIPV6 resolved;
80+
81+ assert( netMan->ResolveHostnameIPV6( "localhost", resolved ) == true );
82+
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 );
107+}
\ No newline at end of file
Show on old repository browser