From d629414e5da96f01cc1d9eb4681b8ef9e2bc8168 Mon Sep 17 00:00:00 2001 From: Scott Jones Date: Wed, 21 Oct 2020 17:20:07 -0700 Subject: [PATCH] ensure robustness against malformed runtime class name --- TestComponentCSharp/Class.cpp | 43 ++++++++++++++------- TestComponentCSharp/Class.h | 2 + TestComponentCSharp/TestComponentCSharp.idl | 2 + UnitTest/TestComponentCSharp_Tests.cs | 5 ++- 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/TestComponentCSharp/Class.cpp b/TestComponentCSharp/Class.cpp index 0d560b829..5cee750b2 100644 --- a/TestComponentCSharp/Class.cpp +++ b/TestComponentCSharp/Class.cpp @@ -1274,16 +1274,16 @@ namespace winrt::TestComponentCSharp::implementation throw hresult_not_implemented(); } - struct native_properties1 : winrt::implements + TestComponentCSharp::IProperties1 Class::NativeProperties1() { - int32_t ReadWriteProperty() + struct native_properties1 : winrt::implements { - return 42; - } - }; + int32_t ReadWriteProperty() + { + return 42; + } + }; - TestComponentCSharp::IProperties1 Class::NativeProperties1() - { return winrt::make(); } @@ -1293,17 +1293,17 @@ namespace winrt::TestComponentCSharp::implementation virtual HRESULT __stdcall GetService(int32_t* type, int32_t* service) noexcept = 0; }; - struct service_provider : winrt::implements + WF::IInspectable Class::ServiceProvider() { - HRESULT __stdcall GetService(int32_t* type, int32_t* service) noexcept override + struct service_provider : winrt::implements { - *service = 42; - return 0; - } - }; + HRESULT __stdcall GetService(int32_t* type, int32_t* service) noexcept override + { + *service = 42; + return 0; + } + }; - WF::IInspectable Class::ServiceProvider() - { return winrt::make(); } @@ -1330,5 +1330,18 @@ namespace winrt::TestComponentCSharp::implementation DataErrorsChangedEventArgs args(detach_abi(mock), take_ownership_from_abi_t()); _dataErrorsChanged(*this, args); } + + WF::IInspectable Class::BadRuntimeClassName() + { + struct bad_runtime_classname : winrt::implements + { + hstring GetRuntimeClassName() + { + return L"BadRuntimeClassName"; + } + }; + + return winrt::make(); + } } diff --git a/TestComponentCSharp/Class.h b/TestComponentCSharp/Class.h index c4eb027e5..7eae0b447 100644 --- a/TestComponentCSharp/Class.h +++ b/TestComponentCSharp/Class.h @@ -336,6 +336,8 @@ namespace winrt::TestComponentCSharp::implementation void ErrorsChanged(winrt::event_token const& token) noexcept; Windows::Foundation::Collections::IIterable GetErrors(hstring const& propertyName); void RaiseDataErrorChanged(); + + static Windows::Foundation::IInspectable BadRuntimeClassName(); }; } diff --git a/TestComponentCSharp/TestComponentCSharp.idl b/TestComponentCSharp/TestComponentCSharp.idl index 7a99e912d..249b68d65 100644 --- a/TestComponentCSharp/TestComponentCSharp.idl +++ b/TestComponentCSharp/TestComponentCSharp.idl @@ -330,6 +330,8 @@ namespace TestComponentCSharp // INotifyDataErrorInfo void RaiseDataErrorChanged(); + + static Object BadRuntimeClassName{ get; }; } [threading(sta), marshaling_behavior(standard)] diff --git a/UnitTest/TestComponentCSharp_Tests.cs b/UnitTest/TestComponentCSharp_Tests.cs index f4abc7cef..865651dcf 100644 --- a/UnitTest/TestComponentCSharp_Tests.cs +++ b/UnitTest/TestComponentCSharp_Tests.cs @@ -478,8 +478,11 @@ public void TestCustomProjections() IntPtr service; serviceProvider.GetService(IntPtr.Zero, out service); Assert.Equal(new IntPtr(42), service); - } + // Ensure robustness with bad runtime class names (parsing errors, type not found, etc) + var badRuntimeClassName = Class.BadRuntimeClassName; + Assert.NotNull(badRuntimeClassName); + } [Fact] public void TestKeyValuePair()