]> git.saurik.com Git - wxWidgets.git/commitdiff
add simple (and disabled by default) wxSocket client test
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 12 Oct 2008 20:45:29 +0000 (20:45 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 12 Oct 2008 20:45:29 +0000 (20:45 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@56252 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

tests/Makefile.in
tests/makefile.bcc
tests/makefile.gcc
tests/makefile.vc
tests/makefile.wat
tests/net/socket.cpp [new file with mode: 0644]
tests/test.bkl
tests/test_test.dsp
tests/test_vc7_test.vcproj
tests/test_vc8_test.vcproj
tests/test_vc9_test.vcproj

index 3ee3a3939c25f13d593ccc1a1c36bc5912d6e4e5..b4e9d9bf027dd6a1e05562e3fd17dd8de47aa164 100644 (file)
@@ -72,6 +72,7 @@ TEST_OBJECTS =  \
        test_mbconvtest.o \
        test_misctests.o \
        test_ipc.o \
+       test_socket.o \
        test_regextest.o \
        test_wxregextest.o \
        test_scopeguardtest.o \
@@ -398,6 +399,9 @@ test_misctests.o: $(srcdir)/misc/misctests.cpp $(TEST_ODEP)
 test_ipc.o: $(srcdir)/net/ipc.cpp $(TEST_ODEP)
        $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/net/ipc.cpp
 
+test_socket.o: $(srcdir)/net/socket.cpp $(TEST_ODEP)
+       $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/net/socket.cpp
+
 test_regextest.o: $(srcdir)/regex/regextest.cpp $(TEST_ODEP)
        $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/regex/regextest.cpp
 
index 59b63dfb2c26ab92722b6d9476733fed5de593ab..0db0977053f2776cce5d29bb73ce67995fa47286 100644 (file)
@@ -59,6 +59,7 @@ TEST_OBJECTS =  \
        $(OBJS)\test_mbconvtest.obj \
        $(OBJS)\test_misctests.obj \
        $(OBJS)\test_ipc.obj \
+       $(OBJS)\test_socket.obj \
        $(OBJS)\test_regextest.obj \
        $(OBJS)\test_wxregextest.obj \
        $(OBJS)\test_scopeguardtest.obj \
@@ -435,6 +436,9 @@ $(OBJS)\test_misctests.obj: .\misc\misctests.cpp
 $(OBJS)\test_ipc.obj: .\net\ipc.cpp
        $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\net\ipc.cpp
 
+$(OBJS)\test_socket.obj: .\net\socket.cpp
+       $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\net\socket.cpp
+
 $(OBJS)\test_regextest.obj: .\regex\regextest.cpp
        $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\regex\regextest.cpp
 
index 134779dab0b282ece8fed6a42b7ba37baff3c681..bbcdd1c5a420b9a89e2f0c14e692897c869abd72 100644 (file)
@@ -51,6 +51,7 @@ TEST_OBJECTS =  \
        $(OBJS)\test_mbconvtest.o \
        $(OBJS)\test_misctests.o \
        $(OBJS)\test_ipc.o \
+       $(OBJS)\test_socket.o \
        $(OBJS)\test_regextest.o \
        $(OBJS)\test_wxregextest.o \
        $(OBJS)\test_scopeguardtest.o \
@@ -413,6 +414,9 @@ $(OBJS)\test_misctests.o: ./misc/misctests.cpp
 $(OBJS)\test_ipc.o: ./net/ipc.cpp
        $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $<
 
+$(OBJS)\test_socket.o: ./net/socket.cpp
+       $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $<
+
 $(OBJS)\test_regextest.o: ./regex/regextest.cpp
        $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $<
 
index 5cc6f074613d16ecbc1466b8d4f333521e7e90e0..dad6c2ee409b8a2e041a5c4f7c7a852db5cf1ab2 100644 (file)
@@ -52,6 +52,7 @@ TEST_OBJECTS =  \
        $(OBJS)\test_mbconvtest.obj \
        $(OBJS)\test_misctests.obj \
        $(OBJS)\test_ipc.obj \
+       $(OBJS)\test_socket.obj \
        $(OBJS)\test_regextest.obj \
        $(OBJS)\test_wxregextest.obj \
        $(OBJS)\test_scopeguardtest.obj \
@@ -520,6 +521,9 @@ $(OBJS)\test_misctests.obj: .\misc\misctests.cpp
 $(OBJS)\test_ipc.obj: .\net\ipc.cpp
        $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\net\ipc.cpp
 
+$(OBJS)\test_socket.obj: .\net\socket.cpp
+       $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\net\socket.cpp
+
 $(OBJS)\test_regextest.obj: .\regex\regextest.cpp
        $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\regex\regextest.cpp
 
index 950b2e96e575e66bd0e4794f2a106f3c7071691a..258fbc8e61d1a605146f4f2ed26ab68de3d8551e 100644 (file)
@@ -264,6 +264,7 @@ TEST_OBJECTS =  &
        $(OBJS)\test_mbconvtest.obj &
        $(OBJS)\test_misctests.obj &
        $(OBJS)\test_ipc.obj &
+       $(OBJS)\test_socket.obj &
        $(OBJS)\test_regextest.obj &
        $(OBJS)\test_wxregextest.obj &
        $(OBJS)\test_scopeguardtest.obj &
@@ -466,6 +467,9 @@ $(OBJS)\test_misctests.obj :  .AUTODEPEND .\misc\misctests.cpp
 $(OBJS)\test_ipc.obj :  .AUTODEPEND .\net\ipc.cpp
        $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $<
 
+$(OBJS)\test_socket.obj :  .AUTODEPEND .\net\socket.cpp
+       $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $<
+
 $(OBJS)\test_regextest.obj :  .AUTODEPEND .\regex\regextest.cpp
        $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $<
 
diff --git a/tests/net/socket.cpp b/tests/net/socket.cpp
new file mode 100644 (file)
index 0000000..9145c27
--- /dev/null
@@ -0,0 +1,178 @@
+///////////////////////////////////////////////////////////////////////////////
+// Name:        tests/net/socket.cpp
+// Purpose:     wxSocket unit tests
+// Author:      Vadim Zeitlin
+// RCS-ID:      $Id$
+// Copyright:   (c) 2008 Vadim Zeitlin
+// Licence:     wxWidgets licence
+///////////////////////////////////////////////////////////////////////////////
+
+/*
+    IMPORTANT NOTE: the environment variable WX_TEST_SERVER must be set to the
+    hostname of the server to use for the tests below, if it is not set all
+    tests are silently skipped (rationale: this makes it possible to run the
+    test in the restricted environments (e.g. sandboxes) without any network
+    connectivity).
+ */
+
+// For compilers that support precompilation, includes "wx/wx.h".
+// and "wx/cppunit.h"
+#include "testprec.h"
+
+#ifdef __BORLANDC__
+    #pragma hdrstop
+#endif
+
+#if wxUSE_SOCKETS
+
+#include "wx/socket.h"
+#include <memory>
+
+typedef std::auto_ptr<wxSockAddress> wxSockAddressPtr;
+typedef std::auto_ptr<wxSocketClient> wxSocketClientPtr;
+
+static wxString gs_serverHost(wxGetenv("WX_TEST_SERVER"));
+
+class SocketTestCase : public CppUnit::TestCase
+{
+public:
+    SocketTestCase() { }
+
+private:
+    CPPUNIT_TEST_SUITE( SocketTestCase );
+        CPPUNIT_TEST( BlockingConnect );
+        CPPUNIT_TEST( NonblockingConnect );
+        CPPUNIT_TEST( ReadNormal );
+        CPPUNIT_TEST( ReadNowait );
+        CPPUNIT_TEST( ReadWaitall );
+    CPPUNIT_TEST_SUITE_END();
+
+    // get the address to connect to, if NULL is returned it means that the
+    // test is disabled and shouldn't run at all
+    wxSockAddressPtr GetServer() const;
+
+    // get the socket to read HTTP reply from, returns NULL if the test is
+    // disabled
+    wxSocketClientPtr GetHTTPSocket(int flags = wxSOCKET_NONE) const;
+
+    void BlockingConnect();
+    void NonblockingConnect();
+    void ReadNormal();
+    void ReadNowait();
+    void ReadWaitall();
+
+    DECLARE_NO_COPY_CLASS(SocketTestCase)
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION( SocketTestCase );
+CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SocketTestCase, "SocketTestCase" );
+
+wxSockAddressPtr SocketTestCase::GetServer() const
+{
+    wxSockAddressPtr ptr;
+    if ( !gs_serverHost.empty() )
+    {
+        wxIPV4address *addr = new wxIPV4address;
+        addr->Hostname(gs_serverHost);
+        addr->Service("www");
+
+        ptr.reset(addr);
+    }
+
+    return ptr;
+}
+
+wxSocketClientPtr SocketTestCase::GetHTTPSocket(int flags) const
+{
+    wxSocketClientPtr ptr;
+
+    wxSockAddressPtr addr = GetServer();
+    if ( !addr.get() )
+        return ptr;
+
+    wxSocketClient *sock = new wxSocketClient(flags);
+    sock->SetTimeout(1);
+    CPPUNIT_ASSERT( sock->Connect(*addr) );
+
+    const wxString httpGetRoot =
+        "GET / HTTP/1.1\r\n"
+        "Host: " + gs_serverHost + "\r\n"
+        "\r\n";
+
+    sock->Write(httpGetRoot, httpGetRoot.length());
+
+    ptr.reset(sock);
+    return ptr;
+}
+
+void SocketTestCase::BlockingConnect()
+{
+    wxSockAddressPtr addr = GetServer();
+    if ( !addr.get() )
+        return;
+
+    wxSocketClient sock;
+    CPPUNIT_ASSERT( sock.Connect(*addr) );
+}
+
+void SocketTestCase::NonblockingConnect()
+{
+    wxSockAddressPtr addr = GetServer();
+    if ( !addr.get() )
+        return;
+
+    wxSocketClient sock;
+    sock.Connect(*addr, false);
+    sock.WaitOnConnect(10);
+
+    CPPUNIT_ASSERT( sock.IsConnected() );
+}
+
+void SocketTestCase::ReadNormal()
+{
+    wxSocketClientPtr sock(GetHTTPSocket());
+    if ( !sock.get() )
+        return;
+
+    char bufSmall[128];
+    sock->Read(bufSmall, WXSIZEOF(bufSmall));
+
+    CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
+    CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), sock->LastCount() );
+
+
+    char bufBig[1024*1024];
+    sock->Read(bufBig, WXSIZEOF(bufBig));
+
+    CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
+    CPPUNIT_ASSERT( WXSIZEOF(bufBig) >= sock->LastCount() );
+}
+
+void SocketTestCase::ReadNowait()
+{
+    wxSocketClientPtr sock(GetHTTPSocket(wxSOCKET_NOWAIT));
+    if ( !sock.get() )
+        return;
+
+    char buf[1024];
+    sock->Read(buf, WXSIZEOF(buf));
+    if ( sock->LastError() != wxSOCKET_WOULDBLOCK )
+    {
+        CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
+    }
+}
+
+void SocketTestCase::ReadWaitall()
+{
+    wxSocketClientPtr sock(GetHTTPSocket(wxSOCKET_WAITALL));
+    if ( !sock.get() )
+        return;
+
+    char buf[128];
+    sock->Read(buf, WXSIZEOF(buf));
+
+    CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
+    CPPUNIT_ASSERT_EQUAL( WXSIZEOF(buf), sock->LastCount() );
+}
+
+#endif // wxUSE_SOCKETS
index f6d3ec51ae654d2db111853ae183a060e6826c8f..8eea85127a16f0c2c5943b147540d68e614ba33f 100644 (file)
@@ -50,6 +50,7 @@
             mbconv/mbconvtest.cpp
             misc/misctests.cpp
             net/ipc.cpp
+            net/socket.cpp
             regex/regextest.cpp
             regex/wxregextest.cpp
             scopeguard/scopeguardtest.cpp
index def8ffb07f182ef4a5662289bbf7bbb53a7c0bc7..be63a023b1452a0f16697339d8ea218fcfcd0986 100644 (file)
@@ -377,6 +377,10 @@ SOURCE=.\scopeguard\scopeguardtest.cpp
 # End Source File\r
 # Begin Source File\r
 \r
+SOURCE=.\net\socket.cpp\r
+# End Source File\r
+# Begin Source File\r
+\r
 SOURCE=.\streams\socketstream.cpp\r
 # End Source File\r
 # Begin Source File\r
index 2e85983f67660ee69ae8bc69fd93e4b0a2656d80..136604537a0cbb269e970754feed76d148b16ef8 100644 (file)
                        <File\r
                                RelativePath=".\scopeguard\scopeguardtest.cpp">\r
                        </File>\r
+                       <File\r
+                               RelativePath=".\net\socket.cpp">\r
+                       </File>\r
                        <File\r
                                RelativePath=".\streams\socketstream.cpp">\r
                        </File>\r
index 2e1955050911948b89c5be33e94e7eb250d83bdd..23676eefab2be94105a47055cc0b3cd17b6cce14 100644 (file)
                                RelativePath=".\scopeguard\scopeguardtest.cpp"\r
                                >\r
                        </File>\r
+                       <File\r
+                               RelativePath=".\net\socket.cpp"\r
+                               >\r
+                       </File>\r
                        <File\r
                                RelativePath=".\streams\socketstream.cpp"\r
                                >\r
index 2ff9434f3540b42f9c6bb1f1958bf92674c46557..df77ba663848f19858dc41511538da502e1d91aa 100644 (file)
                                RelativePath=".\scopeguard\scopeguardtest.cpp"\r
                                >\r
                        </File>\r
+                       <File\r
+                               RelativePath=".\net\socket.cpp"\r
+                               >\r
+                       </File>\r
                        <File\r
                                RelativePath=".\streams\socketstream.cpp"\r
                                >\r