From 684dd4a5a68dc86a0e312366df6c7a83e798e51b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 23 Apr 2022 15:35:22 +0100 Subject: [PATCH 1/7] Use wxScopedPtr in wxImage unit test Don't manage memory manually, this resulted in error leaks if any checks failed. --- tests/image/image.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/image/image.cpp b/tests/image/image.cpp index 5eb923eb34..99a85fa66f 100644 --- a/tests/image/image.cpp +++ b/tests/image/image.cpp @@ -31,6 +31,7 @@ #include "wx/wfstream.h" #include "wx/clipbrd.h" #include "wx/dataobj.h" +#include "wx/scopedptr.h" #include "testimage.h" @@ -189,7 +190,7 @@ void ImageTestCase::LoadFromSocketStream() url.GetError() ); - wxInputStream *in_stream = url.GetInputStream(); + wxScopedPtr in_stream(url.GetInputStream()); WX_ASSERT_MESSAGE ( ("Opening URL \"%s\" failed.", testData[i].url), @@ -206,8 +207,6 @@ void ImageTestCase::LoadFromSocketStream() ("Loading image from \"%s\" failed.", testData[i].url), img.LoadFile(*in_stream, testData[i].type) ); - - delete in_stream; } } From 89a7a070eab0f01909b94e3a5d8f6bd733ad29c5 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 23 Apr 2022 15:36:40 +0100 Subject: [PATCH 2/7] Use WARN() instead of wxLogWarning() in the unit tests The latter isn't shown at all by default while the former one is. --- tests/image/image.cpp | 4 ++-- tests/uris/url.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/image/image.cpp b/tests/image/image.cpp index 99a85fa66f..e0acece78f 100644 --- a/tests/image/image.cpp +++ b/tests/image/image.cpp @@ -166,8 +166,8 @@ void ImageTestCase::LoadFromSocketStream() { if (!IsNetworkAvailable()) // implemented in test.cpp { - wxLogWarning("No network connectivity; skipping the " - "ImageTestCase::LoadFromSocketStream test unit."); + WARN("No network connectivity; skipping the " + "ImageTestCase::LoadFromSocketStream test unit."); return; } diff --git a/tests/uris/url.cpp b/tests/uris/url.cpp index b6100eee06..65c878aa1d 100644 --- a/tests/uris/url.cpp +++ b/tests/uris/url.cpp @@ -65,7 +65,7 @@ void URLTestCase::GetInputStream() { if (!IsNetworkAvailable()) // implemented in test.cpp { - wxLogWarning("No network connectivity; skipping the URLTestCase::GetInputStream test unit."); + WARN("No network connectivity; skipping the URLTestCase::GetInputStream test unit."); return; } From 668563f2b0867a67a9dfe1a5ff3c2bdb3d245196 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 23 Apr 2022 15:54:09 +0100 Subject: [PATCH 3/7] Rewrite ImageTestCase using CATCH macros Get rid of the ugly WX_ASSERT_MESSAGE() and also use REQUIRE/CATCH directly instead of CppUnit compatibility macros. Also use sections to execute the next loop iteration(s) even if the current one fails. This commit is best viewed ignoring whitespace-only changes. --- tests/image/image.cpp | 202 ++++++++++++++++++++---------------------- 1 file changed, 97 insertions(+), 105 deletions(-) diff --git a/tests/image/image.cpp b/tests/image/image.cpp index e0acece78f..8272beb6d5 100644 --- a/tests/image/image.cpp +++ b/tests/image/image.cpp @@ -159,7 +159,7 @@ void ImageTestCase::LoadFromFile() { wxImage img; for (unsigned int i=0; i in_stream(url.GetInputStream()); - WX_ASSERT_MESSAGE - ( - ("Opening URL \"%s\" failed.", testData[i].url), - in_stream && in_stream->IsOk() - ); + wxScopedPtr in_stream(url.GetInputStream()); + REQUIRE( in_stream ); + REQUIRE( in_stream->IsOk() ); - wxImage img; + wxImage img; - // NOTE: it's important to inform wxImage about the type of the image being - // loaded otherwise it will try to autodetect the format, but that - // requires a seekable stream! - WX_ASSERT_MESSAGE - ( - ("Loading image from \"%s\" failed.", testData[i].url), - img.LoadFile(*in_stream, testData[i].type) - ); + // NOTE: it's important to inform wxImage about the type of the image being + // loaded otherwise it will try to autodetect the format, but that + // requires a seekable stream! + CHECK( img.LoadFile(*in_stream, testData[i].type) ); + } } } @@ -214,47 +205,49 @@ void ImageTestCase::LoadFromZipStream() { for (unsigned int i=0; i Date: Sat, 23 Apr 2022 16:16:01 +0100 Subject: [PATCH 4/7] Check connection to www.wxwidgets.org and not www.google.com Our tests use the former and not the latter, so check connection to the site we're actually interested in. --- tests/test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test.cpp b/tests/test.cpp index b9e2f8c96e..e2b532e1a1 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -436,7 +436,7 @@ extern bool IsNetworkAvailable() wxSocketBase::Initialize(); wxIPV4address addr; - if (!addr.Hostname(wxASCII_STR("www.google.com")) || !addr.Service(wxASCII_STR("www"))) + if (!addr.Hostname(wxASCII_STR("www.wxwidgets.org")) || !addr.Service(wxASCII_STR("www"))) { wxSocketBase::Shutdown(); return false; From 239bbd6b82057ae768a9111f07d0195a029d4301 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 23 Apr 2022 16:17:40 +0100 Subject: [PATCH 5/7] Check that we can actually read from network and not just connect When using Cloudflare, as we do for www.wxwidgets.org, the connection succeeds as long as Cloudflare itself works, but reading later fails if the real server behind Cloudflare proxy does not, so check if we can read something from it. --- tests/test.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test.cpp b/tests/test.cpp index e2b532e1a1..944f5923c5 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -442,9 +442,13 @@ extern bool IsNetworkAvailable() return false; } + const char* const + HTTP_GET = "GET / HTTP /1.1\r\nHost: www.wxwidgets.org\r\n\r\n"; + wxSocketClient sock; sock.SetTimeout(10); // 10 secs - bool online = sock.Connect(addr); + bool online = sock.Connect(addr) && + (sock.Write(HTTP_GET, strlen(HTTP_GET)), sock.WaitForRead(1)); wxSocketBase::Shutdown(); From 9c4fe82242130472eacb470ddf0265203bdb319c Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 23 Apr 2022 16:20:04 +0100 Subject: [PATCH 6/7] Cache the results of network availability check It's unlikely to change while the tests are running, so don't redo it needlessly. --- tests/test.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/test.cpp b/tests/test.cpp index 944f5923c5..2a11bfb198 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -428,7 +428,7 @@ extern void SetProcessEventFunc(ProcessEventFunc func) wxGetApp().SetProcessEventFunc(func); } -extern bool IsNetworkAvailable() +static bool DoCheckConnection() { // NOTE: we could use wxDialUpManager here if it was in wxNet; since it's in // wxCore we use a simple rough test: @@ -455,6 +455,16 @@ extern bool IsNetworkAvailable() return online; } +extern bool IsNetworkAvailable() +{ + static int s_isNetworkAvailable = -1; + + if ( s_isNetworkAvailable == -1 ) + s_isNetworkAvailable = DoCheckConnection(); + + return s_isNetworkAvailable == 1; +} + extern bool IsAutomaticTest() { static int s_isAutomatic = -1; From da05a3770f65bb1404c80edda7ac0df46b0670f8 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 23 Apr 2022 18:03:25 +0100 Subject: [PATCH 7/7] Use address of www.wxwidgets.org instead of its name in HTTP URLs The name resolves to Cloudflare proxy which redirects all HTTP URLs to HTTPS, which breaks the existing tests using HTTP, so prevent this from happening by using the actual IP address instead. --- tests/image/image.cpp | 5 +++-- tests/test.cpp | 2 +- tests/uris/url.cpp | 3 ++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/image/image.cpp b/tests/image/image.cpp index 8272beb6d5..1f43fb810e 100644 --- a/tests/image/image.cpp +++ b/tests/image/image.cpp @@ -171,13 +171,14 @@ void ImageTestCase::LoadFromSocketStream() return; } + // These URLs use the real IP address of www.wxwidgets.org. struct { const char* url; wxBitmapType type; } testData[] = { - { "http://www.wxwidgets.org/assets/img/header-logo.png", wxBITMAP_TYPE_PNG }, - { "http://www.wxwidgets.org/assets/ico/favicon-1.ico", wxBITMAP_TYPE_ICO } + { "http://173.254.92.22/assets/img/header-logo.png", wxBITMAP_TYPE_PNG }, + { "http://173.254.92.22/assets/ico/favicon-1.ico", wxBITMAP_TYPE_ICO } }; for (unsigned int i=0; i in_stream(url.GetInputStream());