]> git.saurik.com Git - wxWidgets.git/blobdiff - tests/net/socket.cpp
Fix warnings in the MSW IE backend.
[wxWidgets.git] / tests / net / socket.cpp
index 7929b5ae3450b4291525f54c342697ae9c9631d9..aece666280da65bf2d44826d8c852870c40ecd19 100644 (file)
@@ -4,7 +4,7 @@
 // Author:      Vadim Zeitlin
 // RCS-ID:      $Id$
 // Copyright:   (c) 2008 Vadim Zeitlin
 // Author:      Vadim Zeitlin
 // RCS-ID:      $Id$
 // Copyright:   (c) 2008 Vadim Zeitlin
-// Licence:     wxWidgets licence
+// Licence:     wxWindows licence
 ///////////////////////////////////////////////////////////////////////////////
 
 /*
 ///////////////////////////////////////////////////////////////////////////////
 
 /*
@@ -26,6 +26,9 @@
 #if wxUSE_SOCKETS
 
 #include "wx/socket.h"
 #if wxUSE_SOCKETS
 
 #include "wx/socket.h"
+#include "wx/url.h"
+#include "wx/sstream.h"
+#include "wx/evtloop.h"
 #include <memory>
 
 typedef std::auto_ptr<wxSockAddress> wxSockAddressPtr;
 #include <memory>
 
 typedef std::auto_ptr<wxSockAddress> wxSockAddressPtr;
@@ -39,14 +42,51 @@ public:
     SocketTestCase() { }
 
 private:
     SocketTestCase() { }
 
 private:
+    // we need to repeat the tests twice as the sockets behave differently when
+    // there is an active event loop and without it
+    #define ALL_SOCKET_TESTS() \
+        CPPUNIT_TEST( BlockingConnect ); \
+        CPPUNIT_TEST( NonblockingConnect ); \
+        CPPUNIT_TEST( ReadNormal ); \
+        CPPUNIT_TEST( ReadBlock ); \
+        CPPUNIT_TEST( ReadNowait ); \
+        CPPUNIT_TEST( ReadWaitall ); \
+        CPPUNIT_TEST( UrlTest )
+
     CPPUNIT_TEST_SUITE( SocketTestCase );
     CPPUNIT_TEST_SUITE( SocketTestCase );
-        CPPUNIT_TEST( BlockingConnect );
-        CPPUNIT_TEST( NonblockingConnect );
-        CPPUNIT_TEST( ReadNormal );
-        CPPUNIT_TEST( ReadNowait );
-        CPPUNIT_TEST( ReadWaitall );
+        ALL_SOCKET_TESTS();
+        CPPUNIT_TEST( PseudoTest_SetUseEventLoop );
+        ALL_SOCKET_TESTS();
     CPPUNIT_TEST_SUITE_END();
 
     CPPUNIT_TEST_SUITE_END();
 
+    // helper event loop class which sets itself as active only if we pass it
+    // true in ctor
+    class SocketTestEventLoop : public wxEventLoop
+    {
+    public:
+        SocketTestEventLoop(bool useLoop)
+        {
+            m_useLoop = useLoop;
+            if ( useLoop )
+            {
+                m_evtLoopOld = wxEventLoopBase::GetActive();
+                SetActive(this);
+            }
+        }
+
+        virtual ~SocketTestEventLoop()
+        {
+            if ( m_useLoop )
+            {
+                wxEventLoopBase::SetActive(m_evtLoopOld);
+            }
+        }
+
+    private:
+        bool m_useLoop;
+        wxEventLoopBase *m_evtLoopOld;
+    };
+
     // 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 address to connect to, if NULL is returned it means that the
     // test is disabled and shouldn't run at all
     wxSockAddressPtr GetServer() const;
@@ -55,40 +95,44 @@ private:
     // disabled
     wxSocketClientPtr GetHTTPSocket(int flags = wxSOCKET_NONE) const;
 
     // disabled
     wxSocketClientPtr GetHTTPSocket(int flags = wxSOCKET_NONE) const;
 
+    void PseudoTest_SetUseEventLoop() { ms_useLoop = true; }
+
     void BlockingConnect();
     void NonblockingConnect();
     void ReadNormal();
     void BlockingConnect();
     void NonblockingConnect();
     void ReadNormal();
+    void ReadBlock();
     void ReadNowait();
     void ReadWaitall();
 
     void ReadNowait();
     void ReadWaitall();
 
+    void UrlTest();
+
+    static bool ms_useLoop;
+
     DECLARE_NO_COPY_CLASS(SocketTestCase)
 };
 
     DECLARE_NO_COPY_CLASS(SocketTestCase)
 };
 
+bool SocketTestCase::ms_useLoop = false;
+
 CPPUNIT_TEST_SUITE_REGISTRATION( SocketTestCase );
 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SocketTestCase, "SocketTestCase" );
 
 wxSockAddressPtr SocketTestCase::GetServer() const
 {
 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");
+    if ( gs_serverHost.empty() )
+        return wxSockAddressPtr();
 
 
-        ptr.reset(addr);
-    }
+    wxIPV4address *addr = new wxIPV4address;
+    addr->Hostname(gs_serverHost);
+    addr->Service("www");
 
 
-    return ptr;
+    return wxSockAddressPtr(addr);
 }
 
 wxSocketClientPtr SocketTestCase::GetHTTPSocket(int flags) const
 {
 }
 
 wxSocketClientPtr SocketTestCase::GetHTTPSocket(int flags) const
 {
-    wxSocketClientPtr ptr;
-
     wxSockAddressPtr addr = GetServer();
     if ( !addr.get() )
     wxSockAddressPtr addr = GetServer();
     if ( !addr.get() )
-        return ptr;
+        return wxSocketClientPtr();
 
     wxSocketClient *sock = new wxSocketClient(flags);
     sock->SetTimeout(1);
 
     wxSocketClient *sock = new wxSocketClient(flags);
     sock->SetTimeout(1);
@@ -101,8 +145,7 @@ wxSocketClientPtr SocketTestCase::GetHTTPSocket(int flags) const
 
     sock->Write(httpGetRoot.ToAscii(), httpGetRoot.length());
 
 
     sock->Write(httpGetRoot.ToAscii(), httpGetRoot.length());
 
-    ptr.reset(sock);
-    return ptr;
+    return wxSocketClientPtr(sock);
 }
 
 void SocketTestCase::BlockingConnect()
 }
 
 void SocketTestCase::BlockingConnect()
@@ -121,6 +164,8 @@ void SocketTestCase::NonblockingConnect()
     if ( !addr.get() )
         return;
 
     if ( !addr.get() )
         return;
 
+    SocketTestEventLoop loop(ms_useLoop);
+
     wxSocketClient sock;
     sock.Connect(*addr, false);
     sock.WaitOnConnect(10);
     wxSocketClient sock;
     sock.Connect(*addr, false);
     sock.WaitOnConnect(10);
@@ -130,6 +175,8 @@ void SocketTestCase::NonblockingConnect()
 
 void SocketTestCase::ReadNormal()
 {
 
 void SocketTestCase::ReadNormal()
 {
+    SocketTestEventLoop loop(ms_useLoop);
+
     wxSocketClientPtr sock(GetHTTPSocket());
     if ( !sock.get() )
         return;
     wxSocketClientPtr sock(GetHTTPSocket());
     if ( !sock.get() )
         return;
@@ -138,7 +185,27 @@ void SocketTestCase::ReadNormal()
     sock->Read(bufSmall, WXSIZEOF(bufSmall));
 
     CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
     sock->Read(bufSmall, WXSIZEOF(bufSmall));
 
     CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
-    CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), sock->LastCount() );
+    CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), (size_t)sock->LastCount() );
+
+
+    char bufBig[102400];
+    sock->Read(bufBig, WXSIZEOF(bufBig));
+
+    CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
+    CPPUNIT_ASSERT( WXSIZEOF(bufBig) >= sock->LastCount() );
+}
+
+void SocketTestCase::ReadBlock()
+{
+    wxSocketClientPtr sock(GetHTTPSocket(wxSOCKET_BLOCK));
+    if ( !sock.get() )
+        return;
+
+    char bufSmall[128];
+    sock->Read(bufSmall, WXSIZEOF(bufSmall));
+
+    CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
+    CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), (size_t)sock->LastCount() );
 
 
     char bufBig[102400];
 
 
     char bufBig[102400];
@@ -164,6 +231,8 @@ void SocketTestCase::ReadNowait()
 
 void SocketTestCase::ReadWaitall()
 {
 
 void SocketTestCase::ReadWaitall()
 {
+    SocketTestEventLoop loop(ms_useLoop);
+
     wxSocketClientPtr sock(GetHTTPSocket(wxSOCKET_WAITALL));
     if ( !sock.get() )
         return;
     wxSocketClientPtr sock(GetHTTPSocket(wxSOCKET_WAITALL));
     if ( !sock.get() )
         return;
@@ -172,7 +241,23 @@ void SocketTestCase::ReadWaitall()
     sock->Read(buf, WXSIZEOF(buf));
 
     CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
     sock->Read(buf, WXSIZEOF(buf));
 
     CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
-    CPPUNIT_ASSERT_EQUAL( WXSIZEOF(buf), sock->LastCount() );
+    CPPUNIT_ASSERT_EQUAL( WXSIZEOF(buf), (size_t)sock->LastCount() );
+}
+
+void SocketTestCase::UrlTest()
+{
+    if ( gs_serverHost.empty() )
+        return;
+
+    SocketTestEventLoop loop(ms_useLoop);
+
+    wxURL url("http://" + gs_serverHost);
+
+    const std::auto_ptr<wxInputStream> in(url.GetInputStream());
+    CPPUNIT_ASSERT( in.get() );
+
+    wxStringOutputStream out;
+    CPPUNIT_ASSERT_EQUAL( wxSTREAM_EOF, in->Read(out).GetLastError() );
 }
 
 #endif // wxUSE_SOCKETS
 }
 
 #endif // wxUSE_SOCKETS