office-gobmx/framework/qa/cppunit/dispatchtest.cxx
Caolán McNamara 30c582a720 make CppunitTest_framework_dispatch reliable
the appearance of 2 interceptions is reproducible for me on linux with

make CppunitTest_framework_dispatch VALGRIND=memcheck

1st time
 #1 (anonymous namespace)::MyInterceptor::queryDispatch(com::sun::util::URL const&, rtl::OUString const&, int) at core/framework/qa/cppunit/dispatchtest.cxx:139
 #2 non-virtual thunk to (anonymous namespace)::MyInterceptor::queryDispatch(com::sun::util::URL const&, rtl::OUString const&, int) at core/framework/qa/cppunit/dispatchtest.cxx:0
 #3 framework::InterceptionHelper::queryDispatch(com::sun::util::URL const&, rtl::OUString const&, int) at core/framework/source/dispatch/interceptionhelper.cxx:87
 #4 non-virtual thunk to framework::InterceptionHelper::queryDispatch(com::sun::util::URL const&, rtl::OUString const&, int) at core/framework/source/dispatch/interceptionhelper.cxx:0
 #5 (anonymous namespace)::XFrameImpl::queryDispatch(com::sun::util::URL const&, rtl::OUString const&, int) at core/framework/source/services/frame.cxx:2329
 #6 non-virtual thunk to (anonymous namespace)::XFrameImpl::queryDispatch(com::sun::util::URL const&, rtl::OUString const&, int) at core/framework/source/services/frame.cxx:0
 #7 framework::DispatchHelper::executeDispatch(com::sun::uno::Reference<com::sun::frame::XDispatchProvider> const&, rtl::OUString const&, rtl::OUString const&, int, com::sun::uno::Sequence<com::sun:🫘:PropertyValue> const&) at core/framework/source/services/dispatchhelper.cxx:110
 #8 non-virtual thunk to framework::DispatchHelper::executeDispatch(com::sun::uno::Reference<com::sun::frame::XDispatchProvider> const&, rtl::OUString const&, rtl::OUString const&, int, com::sun::uno::Sequence<com::sun:🫘:PropertyValue> const&) at core/framework/source/services/dispatchhelper.cxx:0
 #9 unotest::MacrosTest::dispatchCommand(com::sun::uno::Reference<com::sun::lang::XComponent> const&, rtl::OUString const&, com::sun::uno::Sequence<com::sun:🫘:PropertyValue> const&) at core/unotest/source/cpp/macros_test.cxx:94
 #10 (anonymous namespace)::testInterception::TestBody() at core/framework/qa/cppunit/dispatchtest.cxx:172

2nd time
 #1 (anonymous namespace)::MyInterceptor::queryDispatch(com::sun::util::URL const&, rtl::OUString const&, int) at core/framework/qa/cppunit/dispatchtest.cxx:139
 #2 non-virtual thunk to (anonymous namespace)::MyInterceptor::queryDispatch(com::sun::util::URL const&, rtl::OUString const&, int) at core/framework/qa/cppunit/dispatchtest.cxx:0
 #3 framework::InterceptionHelper::queryDispatch(com::sun::util::URL const&, rtl::OUString const&, int) at core/framework/source/dispatch/interceptionhelper.cxx:87
 #4 non-virtual thunk to framework::InterceptionHelper::queryDispatch(com::sun::util::URL const&, rtl::OUString const&, int) at core/framework/source/dispatch/interceptionhelper.cxx:0
 #5 (anonymous namespace)::XFrameImpl::queryDispatch(com::sun::util::URL const&, rtl::OUString const&, int) at core/framework/source/services/frame.cxx:2329
 #6 non-virtual thunk to (anonymous namespace)::XFrameImpl::queryDispatch(com::sun::util::URL const&, rtl::OUString const&, int) at core/framework/source/services/frame.cxx:0
 #7 SfxStateCache::GetSlotServer(SfxDispatcher&, com::sun::uno::Reference<com::sun::frame::XDispatchProvider> const&) at core/sfx2/source/control/statcach.cxx:263
 #8 SfxBindings::UpdateSlotServer_Impl() at core/sfx2/source/control/bindings.cxx:1083
 #9 SfxBindings::NextJob_Impl(Timer const*) at core/sfx2/source/control/bindings.cxx:1254
 #10 SfxBindings::NextJob(Timer*) at core/sfx2/source/control/bindings.cxx:1222
 #11 SfxBindings::LinkStubNextJob(void*, Timer*) at core/sfx2/source/control/bindings.cxx:1219
 #12 Link<Timer*, void>::Call(Timer*) const at core/include/tools/link.hxx:111
 #13 Timer::Invoke() at core/vcl/source/app/timer.cxx:76
 #14 Scheduler::CallbackTaskScheduling() at core/vcl/source/app/scheduler.cxx:480
 #15 SalTimer::CallCallback() at core/vcl/inc/saltimer.hxx:55
 #16 SvpSalInstance::CheckTimeout(bool) at core/vcl/headless/svpinst.cxx:161
 #17 SvpSalInstance::ImplYield(bool, bool) at core/vcl/headless/svpinst.cxx:399
 #18 SvpSalInstance::DoYield(bool, bool) at core/vcl/headless/svpinst.cxx:471
 #19 ImplYield(bool, bool) at core/vcl/source/app/svapp.cxx:377
 #20 Scheduler::ProcessEventsToIdle() at core/vcl/source/app/svapp.cxx:407
 #21 unotest::MacrosTest::dispatchCommand(com::sun::uno::Reference<com::sun::lang::XComponent> const&, rtl::OUString const&, com::sun::uno::Sequence<com::sun:🫘:PropertyValue> const&) at core/unotest/source/cpp/macros_test.cxx:95
 #22 (anonymous namespace)::testInterception::TestBody() at core/framework/qa/cppunit/dispatchtest.cxx:172

setting bold/italic will invalidate the slot for the property and timer
starts to update the normal listeners to those properties which might
kick in on a slower run.

Change-Id: Ib37e61c0fbed463f8974d476158e54a0160a2c92
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/157798
Tested-by: Caolán McNamara <caolan.mcnamara@collabora.com>
Reviewed-by: Caolán McNamara <caolan.mcnamara@collabora.com>
2023-10-11 15:20:30 +02:00

204 lines
6.8 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <cppuhelper/implbase.hxx>
#include <test/unoapi_test.hxx>
#include <com/sun/star/frame/XDispatchProviderInterceptor.hpp>
#include <com/sun/star/frame/XInterceptorInfo.hpp>
#include <com/sun/star/util/URLTransformer.hpp>
#include <rtl/ref.hxx>
#include <mutex>
using namespace ::com::sun::star;
namespace
{
/// Sample interception implementation that asserts getInterceptedURLs() and queryDispatch() is in sync.
class MyInterceptor
: public cppu::WeakImplHelper<frame::XDispatchProviderInterceptor, frame::XInterceptorInfo>
{
std::mutex maMutex;
uno::Reference<frame::XDispatchProvider> m_xMaster;
uno::Reference<frame::XDispatchProvider> m_xSlave;
uno::Sequence<OUString> m_aDisabledCommands;
int m_nExpected;
int m_nUnexpected;
public:
MyInterceptor();
/// Number of queryDispatch() calls that operate on a command advertised by getInterceptedURLs().
int getExpected();
/// Number of queryDispatch() calls that operate on a command not advertised by getInterceptedURLs().
int getUnexpected();
// frame::XInterceptorInfo
virtual uno::Sequence<OUString> SAL_CALL getInterceptedURLs() override;
// frame::XDispatchProviderInterceptor
virtual void SAL_CALL setMasterDispatchProvider(
const uno::Reference<frame::XDispatchProvider>& xNewSupplier) override;
virtual uno::Reference<frame::XDispatchProvider> SAL_CALL getMasterDispatchProvider() override;
virtual void SAL_CALL
setSlaveDispatchProvider(const uno::Reference<frame::XDispatchProvider>& xNewSupplier) override;
virtual uno::Reference<frame::XDispatchProvider> SAL_CALL getSlaveDispatchProvider() override;
// frame::XDispatchProvider
virtual uno::Sequence<uno::Reference<frame::XDispatch>> SAL_CALL
queryDispatches(const uno::Sequence<frame::DispatchDescriptor>& rRequests) override;
virtual uno::Reference<frame::XDispatch>
SAL_CALL queryDispatch(const util::URL& rURL, const OUString& rTargetFrameName,
sal_Int32 SearchFlags) override;
};
MyInterceptor::MyInterceptor()
: m_aDisabledCommands{ ".uno:Bold" }
, m_nExpected(0)
, m_nUnexpected(0)
{
}
int MyInterceptor::getExpected()
{
std::unique_lock aGuard(maMutex);
int nRet = m_nExpected;
m_nExpected = 0;
return nRet;
}
int MyInterceptor::getUnexpected()
{
std::unique_lock aGuard(maMutex);
int nRet = m_nUnexpected;
m_nUnexpected = 0;
return nRet;
}
uno::Sequence<OUString> MyInterceptor::getInterceptedURLs() { return m_aDisabledCommands; }
void MyInterceptor::setMasterDispatchProvider(
const uno::Reference<frame::XDispatchProvider>& xNewSupplier)
{
std::unique_lock aGuard(maMutex);
m_xMaster = xNewSupplier;
}
uno::Reference<frame::XDispatchProvider> MyInterceptor::getMasterDispatchProvider()
{
std::unique_lock aGuard(maMutex);
return m_xMaster;
}
void MyInterceptor::setSlaveDispatchProvider(
const uno::Reference<frame::XDispatchProvider>& xNewSupplier)
{
std::unique_lock aGuard(maMutex);
m_xSlave = xNewSupplier;
}
uno::Reference<frame::XDispatchProvider> MyInterceptor::getSlaveDispatchProvider()
{
std::unique_lock aGuard(maMutex);
return m_xSlave;
}
uno::Sequence<uno::Reference<frame::XDispatch>>
MyInterceptor::queryDispatches(const uno::Sequence<frame::DispatchDescriptor>& rRequests)
{
uno::Sequence<uno::Reference<frame::XDispatch>> aResult(rRequests.getLength());
auto aResultRange = asNonConstRange(aResult);
for (sal_Int32 i = 0; i < rRequests.getLength(); ++i)
{
aResultRange[i] = queryDispatch(rRequests[i].FeatureURL, rRequests[i].FrameName,
rRequests[i].SearchFlags);
}
return aResult;
}
uno::Reference<frame::XDispatch> MyInterceptor::queryDispatch(const util::URL& rURL,
const OUString& /*rTargetFrameName*/,
sal_Int32 /*SearchFlags*/)
{
std::unique_lock aGuard(maMutex);
if (std::find(std::cbegin(m_aDisabledCommands), std::cend(m_aDisabledCommands), rURL.Complete)
!= std::cend(m_aDisabledCommands))
++m_nExpected;
else
++m_nUnexpected;
return uno::Reference<frame::XDispatch>();
}
/// Tests how InterceptionHelper invokes a registered interceptor.
class DispatchTest : public UnoApiTest
{
public:
DispatchTest()
: UnoApiTest("/framework/qa/cppunit/data/")
{
}
};
CPPUNIT_TEST_FIXTURE(DispatchTest, testInterception)
{
mxComponent = loadFromDesktop("private:factory/swriter", "com.sun.star.text.TextDocument");
uno::Reference<frame::XModel> xModel(mxComponent, uno::UNO_QUERY);
CPPUNIT_ASSERT(xModel.is());
uno::Reference<frame::XDispatchProviderInterception> xRegistration(
xModel->getCurrentController()->getFrame(), uno::UNO_QUERY);
CPPUNIT_ASSERT(xRegistration.is());
rtl::Reference<MyInterceptor> pInterceptor(new MyInterceptor());
xRegistration->registerDispatchProviderInterceptor(pInterceptor);
dispatchCommand(mxComponent, ".uno:Bold", {});
CPPUNIT_ASSERT_GREATER(0, pInterceptor->getExpected());
CPPUNIT_ASSERT_EQUAL(0, pInterceptor->getUnexpected());
dispatchCommand(mxComponent, ".uno:Italic", {});
// This was 1: MyInterceptor::queryDispatch() was called for .uno:Italic.
CPPUNIT_ASSERT_EQUAL(0, pInterceptor->getUnexpected());
}
CPPUNIT_TEST_FIXTURE(DispatchTest, testSfxOfficeDispatchDispose)
{
// this test doesn't work with a new document because of aURL.Main check in SfxBaseController::dispatch()
loadFromURL(u"empty.fodp");
uno::Reference<frame::XModel> xModel(mxComponent, uno::UNO_QUERY);
CPPUNIT_ASSERT(xModel.is());
uno::Reference<frame::XController> xController(xModel->getCurrentController());
CPPUNIT_ASSERT(xController.is());
uno::Reference<frame::XDispatchProvider> xFrame(xController->getFrame(), uno::UNO_QUERY);
CPPUNIT_ASSERT(xFrame.is());
uno::Reference<util::XURLTransformer> xParser(util::URLTransformer::create(mxComponentContext));
util::URL url;
url.Complete = xModel->getURL() + "#dummy";
xParser->parseStrict(url);
uno::Reference<frame::XDispatch> xDisp(xFrame->queryDispatch(url, "", 0));
CPPUNIT_ASSERT(xDisp.is());
mxComponent->dispose();
util::URL urlSlot;
urlSlot.Complete = "slot:5598";
xParser->parseStrict(urlSlot);
// crashed with UAF
xDisp->dispatch(urlSlot, {});
}
}
CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */