summaryrefslogtreecommitdiff
path: root/cppu/qa
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2020-04-28 13:50:01 +0200
committerStephan Bergmann <sbergman@redhat.com>2020-04-28 15:07:45 +0200
commit679163845bd2f8b6de703f8fe704eb8912bc4ba4 (patch)
tree3bacb2d7758859b5bb052f34dcbaea89c485939d /cppu/qa
parent8e7b61c48544b722b1cea41c9e2e6b1e01191be9 (diff)
tdf#115399: Don't kill pre-existing typelib_TypeDescription members
...in typelib_typedescription_register, in case they are already being referenced from elsewhere. Instead, only move from *ppNewDescription to pTDR->pType those members that were not yet initialized in the latter. Change-Id: I7620219d137f8dd7f24a0f4a04eda30669b6c5a9 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/93062 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'cppu/qa')
-rw-r--r--cppu/qa/typelib.cxx293
1 files changed, 293 insertions, 0 deletions
diff --git a/cppu/qa/typelib.cxx b/cppu/qa/typelib.cxx
new file mode 100644
index 000000000000..227e5731cc4f
--- /dev/null
+++ b/cppu/qa/typelib.cxx
@@ -0,0 +1,293 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
+/*
+ * 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 <sal/config.h>
+
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/plugin/TestPlugIn.h>
+
+#include <com/sun/star/io/XActiveDataSink.hpp>
+#include <com/sun/star/io/XTextInputStream.hpp>
+#include <com/sun/star/lang/EventObject.hpp>
+#include <com/sun/star/script/FinishReason.hpp>
+#include <com/sun/star/uno/Any.hxx>
+#include <com/sun/star/uno/Type.hxx>
+#include <com/sun/star/uno/XInterface.hpp>
+#include <cppu/unotype.hxx>
+#include <rtl/ustring.hxx>
+#include <sal/types.h>
+#include <typelib/typedescription.h>
+#include <typelib/typedescription.hxx>
+
+// Test that typelib_typedescription_register as called from typelib_typedescription_complete
+// returns a correct typelib_TypeDescription and keeps pointers from the original
+// typelib_TypeDescription intact (see tdf#115399 "Data race in typelib_typedescription_register").
+// This code uses sufficiently "obscure" types in typelib_static_*_type_init to make it unlikely
+// that they are already instantiated and registered with the type description manager, which might
+// cause inconsistencies.
+
+namespace
+{
+class Test : public CppUnit::TestFixture
+{
+public:
+ void testEnum()
+ {
+ typelib_TypeDescriptionReference* ref = nullptr;
+ typelib_static_enum_type_init(&ref, "com.sun.star.script.MemberType", 0);
+ CPPUNIT_ASSERT(ref != nullptr);
+ typelib_TypeDescription* td = ref->pType;
+ CPPUNIT_ASSERT(td != nullptr);
+ typelib_typedescription_acquire(td);
+ CPPUNIT_ASSERT(!td->bComplete);
+ auto t = reinterpret_cast<typelib_EnumTypeDescription*>(td);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(0), t->nDefaultEnumValue);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(0), t->nEnumValues);
+ CPPUNIT_ASSERT(t->ppEnumNames == nullptr);
+ CPPUNIT_ASSERT(t->pEnumValues == nullptr);
+ CPPUNIT_ASSERT(typelib_typedescription_complete(&td));
+ CPPUNIT_ASSERT(td != nullptr);
+ CPPUNIT_ASSERT(td->bComplete);
+ t = reinterpret_cast<typelib_EnumTypeDescription*>(td);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(0), t->nDefaultEnumValue);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(3), t->nEnumValues);
+ CPPUNIT_ASSERT(t->ppEnumNames != nullptr);
+ CPPUNIT_ASSERT_EQUAL(OUString("METHOD"), OUString::unacquired(&t->ppEnumNames[0]));
+ CPPUNIT_ASSERT_EQUAL(OUString("PROPERTY"), OUString::unacquired(&t->ppEnumNames[1]));
+ CPPUNIT_ASSERT_EQUAL(OUString("UNKNOWN"), OUString::unacquired(&t->ppEnumNames[2]));
+ CPPUNIT_ASSERT(t->pEnumValues != nullptr);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(0), t->pEnumValues[0]);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(1), t->pEnumValues[1]);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(2), t->pEnumValues[2]);
+ typelib_typedescription_release(td);
+ typelib_typedescriptionreference_release(ref);
+ }
+
+ void testStruct()
+ {
+ auto const t0 = cppu::UnoType<css::lang::EventObject>::get();
+ auto const t1 = cppu::UnoType<css::script::FinishReason>::get();
+ auto const t2 = cppu::UnoType<OUString>::get();
+ auto const t3 = cppu::UnoType<css::uno::Any>::get();
+ typelib_TypeDescriptionReference* ref = nullptr;
+ typelib_TypeDescriptionReference* members[3]
+ = { t1.getTypeLibType(), t2.getTypeLibType(), t3.getTypeLibType() };
+ typelib_static_struct_type_init(&ref, "com.sun.star.script.FinishEngineEvent",
+ t0.getTypeLibType(), 3, members, nullptr);
+ CPPUNIT_ASSERT(ref != nullptr);
+ typelib_TypeDescription* td = ref->pType;
+ CPPUNIT_ASSERT(td != nullptr);
+ typelib_typedescription_acquire(td);
+ CPPUNIT_ASSERT(!td->bComplete);
+ auto t = reinterpret_cast<typelib_StructTypeDescription*>(td);
+ CPPUNIT_ASSERT(css::uno::TypeDescription(&t->aBase.pBaseTypeDescription->aBase).equals(t0));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(3), t->aBase.nMembers);
+ auto const offsets = t->aBase.pMemberOffsets;
+ CPPUNIT_ASSERT(offsets != nullptr);
+ auto const typerefs = t->aBase.ppTypeRefs;
+ CPPUNIT_ASSERT(typerefs != nullptr);
+ CPPUNIT_ASSERT_EQUAL(t1, css::uno::Type(typerefs[0]));
+ CPPUNIT_ASSERT_EQUAL(t2, css::uno::Type(typerefs[1]));
+ CPPUNIT_ASSERT_EQUAL(t3, css::uno::Type(typerefs[2]));
+ CPPUNIT_ASSERT(t->aBase.ppMemberNames == nullptr);
+ CPPUNIT_ASSERT(t->pParameterizedTypes == nullptr);
+ CPPUNIT_ASSERT(typelib_typedescription_complete(&td));
+ CPPUNIT_ASSERT(td != nullptr);
+ CPPUNIT_ASSERT(td->bComplete);
+ t = reinterpret_cast<typelib_StructTypeDescription*>(td);
+ CPPUNIT_ASSERT(css::uno::TypeDescription(&t->aBase.pBaseTypeDescription->aBase).equals(t0));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(3), t->aBase.nMembers);
+ CPPUNIT_ASSERT(t->aBase.pMemberOffsets != nullptr);
+ CPPUNIT_ASSERT(t->aBase.ppTypeRefs != nullptr);
+ CPPUNIT_ASSERT_EQUAL(t1, css::uno::Type(t->aBase.ppTypeRefs[0]));
+ CPPUNIT_ASSERT_EQUAL(t2, css::uno::Type(t->aBase.ppTypeRefs[1]));
+ CPPUNIT_ASSERT_EQUAL(t3, css::uno::Type(t->aBase.ppTypeRefs[2]));
+ CPPUNIT_ASSERT(t->aBase.ppMemberNames != nullptr);
+ CPPUNIT_ASSERT_EQUAL(OUString("Finish"), OUString::unacquired(&t->aBase.ppMemberNames[0]));
+ CPPUNIT_ASSERT_EQUAL(OUString("ErrorMessage"),
+ OUString::unacquired(&t->aBase.ppMemberNames[1]));
+ CPPUNIT_ASSERT_EQUAL(OUString("Return"), OUString::unacquired(&t->aBase.ppMemberNames[2]));
+ CPPUNIT_ASSERT(t->pParameterizedTypes == nullptr);
+ // `offsets` and `typerefs` must still be valid:
+ CPPUNIT_ASSERT_EQUAL(t->aBase.pMemberOffsets[0], offsets[0]);
+ CPPUNIT_ASSERT_EQUAL(t->aBase.pMemberOffsets[1], offsets[1]);
+ CPPUNIT_ASSERT_EQUAL(t->aBase.pMemberOffsets[2], offsets[2]);
+ CPPUNIT_ASSERT_EQUAL(css::uno::Type(t->aBase.ppTypeRefs[0]), css::uno::Type(typerefs[0]));
+ CPPUNIT_ASSERT_EQUAL(css::uno::Type(t->aBase.ppTypeRefs[1]), css::uno::Type(typerefs[1]));
+ CPPUNIT_ASSERT_EQUAL(css::uno::Type(t->aBase.ppTypeRefs[2]), css::uno::Type(typerefs[2]));
+ typelib_typedescription_release(td);
+ typelib_typedescriptionreference_release(ref);
+ }
+
+ void testPolyStruct()
+ {
+ auto const t1 = cppu::UnoType<bool>::get();
+ auto const t2 = cppu::UnoType<sal_Int32>::get();
+ typelib_TypeDescriptionReference* ref = nullptr;
+ typelib_TypeDescriptionReference* members[2] = { t1.getTypeLibType(), t2.getTypeLibType() };
+ sal_Bool const param[2] = { false, true };
+ typelib_static_struct_type_init(&ref, "com.sun.star.beans.Optional<long>", nullptr, 2,
+ members, param);
+ CPPUNIT_ASSERT(ref != nullptr);
+ typelib_TypeDescription* td = ref->pType;
+ CPPUNIT_ASSERT(td != nullptr);
+ typelib_typedescription_acquire(td);
+ CPPUNIT_ASSERT(!td->bComplete);
+ auto t = reinterpret_cast<typelib_StructTypeDescription*>(td);
+ CPPUNIT_ASSERT(t->aBase.pBaseTypeDescription == nullptr);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(2), t->aBase.nMembers);
+ auto const offsets = t->aBase.pMemberOffsets;
+ CPPUNIT_ASSERT(offsets != nullptr);
+ auto const typerefs = t->aBase.ppTypeRefs;
+ CPPUNIT_ASSERT(typerefs != nullptr);
+ CPPUNIT_ASSERT_EQUAL(t1, css::uno::Type(typerefs[0]));
+ CPPUNIT_ASSERT_EQUAL(t2, css::uno::Type(typerefs[1]));
+ CPPUNIT_ASSERT(t->aBase.ppMemberNames == nullptr);
+ CPPUNIT_ASSERT(t->pParameterizedTypes != nullptr);
+ CPPUNIT_ASSERT_EQUAL(param[0], t->pParameterizedTypes[0]);
+ CPPUNIT_ASSERT_EQUAL(param[1], t->pParameterizedTypes[1]);
+ CPPUNIT_ASSERT(typelib_typedescription_complete(&td));
+ CPPUNIT_ASSERT(td != nullptr);
+ CPPUNIT_ASSERT(td->bComplete);
+ t = reinterpret_cast<typelib_StructTypeDescription*>(td);
+ CPPUNIT_ASSERT(t->aBase.pBaseTypeDescription == nullptr);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(2), t->aBase.nMembers);
+ CPPUNIT_ASSERT(t->aBase.pMemberOffsets != nullptr);
+ CPPUNIT_ASSERT(t->aBase.ppTypeRefs != nullptr);
+ CPPUNIT_ASSERT_EQUAL(t1, css::uno::Type(t->aBase.ppTypeRefs[0]));
+ CPPUNIT_ASSERT_EQUAL(t2, css::uno::Type(t->aBase.ppTypeRefs[1]));
+ CPPUNIT_ASSERT(t->aBase.ppMemberNames != nullptr);
+ CPPUNIT_ASSERT_EQUAL(OUString("IsPresent"),
+ OUString::unacquired(&t->aBase.ppMemberNames[0]));
+ CPPUNIT_ASSERT_EQUAL(OUString("Value"), OUString::unacquired(&t->aBase.ppMemberNames[1]));
+ CPPUNIT_ASSERT(t->pParameterizedTypes != nullptr);
+ CPPUNIT_ASSERT_EQUAL(param[0], t->pParameterizedTypes[0]);
+ CPPUNIT_ASSERT_EQUAL(param[1], t->pParameterizedTypes[1]);
+ // `offsets` and `typerefs` must still be valid:
+ CPPUNIT_ASSERT_EQUAL(t->aBase.pMemberOffsets[0], offsets[0]);
+ CPPUNIT_ASSERT_EQUAL(t->aBase.pMemberOffsets[1], offsets[1]);
+ CPPUNIT_ASSERT_EQUAL(css::uno::Type(t->aBase.ppTypeRefs[0]), css::uno::Type(typerefs[0]));
+ CPPUNIT_ASSERT_EQUAL(css::uno::Type(t->aBase.ppTypeRefs[1]), css::uno::Type(typerefs[1]));
+ typelib_typedescription_release(td);
+ typelib_typedescriptionreference_release(ref);
+ }
+
+ void testInterface()
+ {
+ auto const t0 = cppu::UnoType<css::uno::XInterface>::get();
+ typelib_TypeDescriptionReference* ref = nullptr;
+ typelib_TypeDescriptionReference* bases[1] = { t0.getTypeLibType() };
+ typelib_static_mi_interface_type_init(&ref, "com.sun.star.script.XTypeConverter", 1, bases);
+ CPPUNIT_ASSERT(ref != nullptr);
+ typelib_TypeDescription* td = ref->pType;
+ CPPUNIT_ASSERT(td != nullptr);
+ typelib_typedescription_acquire(td);
+ CPPUNIT_ASSERT(!td->bComplete);
+ auto t = reinterpret_cast<typelib_InterfaceTypeDescription*>(td);
+ CPPUNIT_ASSERT(css::uno::TypeDescription(&t->pBaseTypeDescription->aBase).equals(t0));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(0), t->nMembers);
+ CPPUNIT_ASSERT(t->ppMembers == nullptr);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(0), t->nAllMembers);
+ CPPUNIT_ASSERT(t->ppAllMembers == nullptr);
+ CPPUNIT_ASSERT(t->pMapMemberIndexToFunctionIndex == nullptr);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(0), t->nMapFunctionIndexToMemberIndex);
+ CPPUNIT_ASSERT(t->pMapFunctionIndexToMemberIndex == nullptr);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(1), t->nBaseTypes);
+ auto const basetypes = t->ppBaseTypes;
+ CPPUNIT_ASSERT(basetypes != nullptr);
+ CPPUNIT_ASSERT(css::uno::TypeDescription(&basetypes[0]->aBase).equals(t0));
+ CPPUNIT_ASSERT(typelib_typedescription_complete(&td));
+ CPPUNIT_ASSERT(td != nullptr);
+ CPPUNIT_ASSERT(td->bComplete);
+ t = reinterpret_cast<typelib_InterfaceTypeDescription*>(td);
+ CPPUNIT_ASSERT(css::uno::TypeDescription(&t->pBaseTypeDescription->aBase).equals(t0));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(2), t->nMembers);
+ CPPUNIT_ASSERT(t->ppMembers != nullptr);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(5), t->nAllMembers);
+ CPPUNIT_ASSERT(t->ppAllMembers != nullptr);
+ CPPUNIT_ASSERT(t->pMapMemberIndexToFunctionIndex != nullptr);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(5), t->nMapFunctionIndexToMemberIndex);
+ CPPUNIT_ASSERT(t->pMapFunctionIndexToMemberIndex != nullptr);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(1), t->nBaseTypes);
+ CPPUNIT_ASSERT(t->ppBaseTypes != nullptr);
+ CPPUNIT_ASSERT(css::uno::TypeDescription(&t->ppBaseTypes[0]->aBase).equals(t0));
+ // `basetypes` must still be valid:
+ CPPUNIT_ASSERT(
+ css::uno::TypeDescription(&basetypes[0]->aBase).equals(&t->ppBaseTypes[0]->aBase));
+ typelib_typedescription_release(td);
+ typelib_typedescriptionreference_release(ref);
+ }
+
+ void testMultiInterface()
+ {
+ auto const t1 = cppu::UnoType<css::io::XTextInputStream>::get();
+ auto const t2 = cppu::UnoType<css::io::XActiveDataSink>::get();
+ typelib_TypeDescriptionReference* ref = nullptr;
+ typelib_TypeDescriptionReference* bases[2] = { t1.getTypeLibType(), t2.getTypeLibType() };
+ typelib_static_mi_interface_type_init(&ref, "com.sun.star.io.XTextInputStream2", 2, bases);
+ CPPUNIT_ASSERT(ref != nullptr);
+ typelib_TypeDescription* td = ref->pType;
+ CPPUNIT_ASSERT(td != nullptr);
+ typelib_typedescription_acquire(td);
+ CPPUNIT_ASSERT(!td->bComplete);
+ auto t = reinterpret_cast<typelib_InterfaceTypeDescription*>(td);
+ CPPUNIT_ASSERT(css::uno::TypeDescription(&t->pBaseTypeDescription->aBase).equals(t1));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(0), t->nMembers);
+ CPPUNIT_ASSERT(t->ppMembers == nullptr);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(0), t->nAllMembers);
+ CPPUNIT_ASSERT(t->ppAllMembers == nullptr);
+ CPPUNIT_ASSERT(t->pMapMemberIndexToFunctionIndex == nullptr);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(0), t->nMapFunctionIndexToMemberIndex);
+ CPPUNIT_ASSERT(t->pMapFunctionIndexToMemberIndex == nullptr);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(2), t->nBaseTypes);
+ auto const basetypes = t->ppBaseTypes;
+ CPPUNIT_ASSERT(basetypes != nullptr);
+ CPPUNIT_ASSERT(css::uno::TypeDescription(&basetypes[0]->aBase).equals(t1));
+ CPPUNIT_ASSERT(css::uno::TypeDescription(&basetypes[1]->aBase).equals(t2));
+ CPPUNIT_ASSERT(typelib_typedescription_complete(&td));
+ CPPUNIT_ASSERT(td != nullptr);
+ CPPUNIT_ASSERT(td->bComplete);
+ t = reinterpret_cast<typelib_InterfaceTypeDescription*>(td);
+ CPPUNIT_ASSERT(css::uno::TypeDescription(&t->pBaseTypeDescription->aBase).equals(t1));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(0), t->nMembers);
+ CPPUNIT_ASSERT(t->ppMembers == nullptr);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(14), t->nAllMembers);
+ CPPUNIT_ASSERT(t->ppAllMembers != nullptr);
+ CPPUNIT_ASSERT(t->pMapMemberIndexToFunctionIndex != nullptr);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(14), t->nMapFunctionIndexToMemberIndex);
+ CPPUNIT_ASSERT(t->pMapFunctionIndexToMemberIndex != nullptr);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(2), t->nBaseTypes);
+ CPPUNIT_ASSERT(t->ppBaseTypes != nullptr);
+ CPPUNIT_ASSERT(css::uno::TypeDescription(&t->ppBaseTypes[0]->aBase).equals(t1));
+ CPPUNIT_ASSERT(css::uno::TypeDescription(&t->ppBaseTypes[1]->aBase).equals(t2));
+ // `basetypes` must still be valid:
+ CPPUNIT_ASSERT(
+ css::uno::TypeDescription(&basetypes[0]->aBase).equals(&t->ppBaseTypes[0]->aBase));
+ CPPUNIT_ASSERT(
+ css::uno::TypeDescription(&basetypes[1]->aBase).equals(&t->ppBaseTypes[1]->aBase));
+ typelib_typedescription_release(td);
+ typelib_typedescriptionreference_release(ref);
+ }
+
+ CPPUNIT_TEST_SUITE(Test);
+ CPPUNIT_TEST(testEnum);
+ CPPUNIT_TEST(testStruct);
+ CPPUNIT_TEST(testPolyStruct);
+ CPPUNIT_TEST(testInterface);
+ CPPUNIT_TEST(testMultiInterface);
+ CPPUNIT_TEST_SUITE_END();
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(Test);
+}
+
+CPPUNIT_PLUGIN_IMPLEMENT();
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */