From bd4bc2b03cd3ae7ac06da8ea8de2ab4c68daf069 Mon Sep 17 00:00:00 2001 From: Paul Purifoy Date: Thu, 10 Jun 2021 17:21:56 -0700 Subject: [PATCH] Refactored unregistering tokens --- .../PushNotificationsTestApp/main.cpp | 72 +++++++++++++------ 1 file changed, 49 insertions(+), 23 deletions(-) diff --git a/test/TestApps/PushNotificationsTestApp/main.cpp b/test/TestApps/PushNotificationsTestApp/main.cpp index 63ba192944..d8b98d45e3 100644 --- a/test/TestApps/PushNotificationsTestApp/main.cpp +++ b/test/TestApps/PushNotificationsTestApp/main.cpp @@ -17,8 +17,7 @@ using namespace winrt::Windows::Storage::Streams; winrt::guid remoteId1(L"a2e4a323-b518-4799-9e80-0b37aeb0d225"); // Generated from ms.portal.azure.com winrt::guid remoteId2(L"CA1A4AB2-AC1D-4EFC-A132-E5A191CA285A"); // Dummy guid from visual studio guid tool generator -PushNotificationRegistrationToken appToken = nullptr; -PushNotificationRegistrationToken fakeToken = nullptr; +PushNotificationRegistrationToken g_appToken = nullptr; bool ChannelRequestUsingNullRemoteId() { @@ -60,6 +59,7 @@ bool ChannelRequestUsingRemoteId() return channelOperationResult == S_OK; } +// Verify calling channel close will fail when called twice. bool MultipleChannelClose() { auto channelOperation = PushNotificationManager::CreateChannelAsync(remoteId1); @@ -111,7 +111,7 @@ bool MultipleChannelRequestUsingMultipleRemoteId() bool ActivatorTest() { - PushNotificationManager::UnregisterActivator(std::exchange(appToken, nullptr), PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator); + PushNotificationManager::UnregisterActivator(std::exchange(g_appToken, nullptr), PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator); try { @@ -119,6 +119,14 @@ bool ActivatorTest() PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator, c_fakeComServerId); + PushNotificationRegistrationToken fakeToken = nullptr; + auto scope_exit = wil::scope_exit([&] { + if (fakeToken) + { + PushNotificationManager::UnregisterActivator(fakeToken, PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator); + } + }); + fakeToken = PushNotificationManager::RegisterActivator(info); if (!fakeToken.TaskRegistration()) { @@ -134,11 +142,19 @@ bool ActivatorTest() return true; } +// Verify calling register activator with null PushNotificationActivationInfo is not allowed. bool RegisterActivatorNullDetails() { try { - PushNotificationManager::RegisterActivator(nullptr); + PushNotificationRegistrationToken fakeToken = nullptr; + auto scope_exit = wil::scope_exit([&] { + if (fakeToken) + { + PushNotificationManager::UnregisterActivator(fakeToken, PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator); + } + }); + fakeToken = PushNotificationManager::RegisterActivator(nullptr); } catch (...) { @@ -147,6 +163,7 @@ bool RegisterActivatorNullDetails() return false; } +// Verify calling register activator with null clsid is not allowed. bool RegisterActivatorNullClsid() { winrt::hresult hr = S_OK; @@ -155,7 +172,16 @@ bool RegisterActivatorNullClsid() PushNotificationActivationInfo info( PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator, winrt::guid()); // Null guid - PushNotificationManager::RegisterActivator(info); + + PushNotificationRegistrationToken fakeToken = nullptr; + auto scope_exit = wil::scope_exit([&] { + if (fakeToken) + { + PushNotificationManager::UnregisterActivator(fakeToken, PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator); + } + }); + + fakeToken = PushNotificationManager::RegisterActivator(info); } catch (...) { @@ -164,6 +190,7 @@ bool RegisterActivatorNullClsid() return false; } +// Verify unregistering activator with a null token is not allowed. bool UnregisterActivatorNullToken() { winrt::hresult hr = S_OK; @@ -178,6 +205,8 @@ bool UnregisterActivatorNullToken() return false; } +// Verify unregistering an activator with null background registration is not allowed +// if PushTrigger option is specified. bool UnregisterActivatorNullBackgroundRegistration() { winrt::hresult hr = S_OK; @@ -193,6 +222,7 @@ bool UnregisterActivatorNullBackgroundRegistration() return false; } +// Verify registering multiple activators is not allowed. bool MultipleRegisterActivatorTest() { winrt::hresult hr = S_OK; @@ -202,6 +232,14 @@ bool MultipleRegisterActivatorTest() PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator, c_fakeComServerId); // Fake clsid to test multiple activators + PushNotificationRegistrationToken fakeToken = nullptr; + auto scope_exit = wil::scope_exit([&] { + if (fakeToken) + { + PushNotificationManager::UnregisterActivator(fakeToken, PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator); + } + }); + fakeToken = PushNotificationManager::RegisterActivator(info); } catch (...) @@ -211,7 +249,7 @@ bool MultipleRegisterActivatorTest() return false; } -bool BackgroundActivationTest() // Activating application for background. +bool BackgroundActivationTest() // Activating application for background test. { return true; } @@ -247,33 +285,21 @@ bool runUnitTest(std::string unitTest) return it->second(); } -// Cleanup function for exiting test app. -void UnregisterClsid() -{ - if (appToken) - { - PushNotificationManager::UnregisterActivator(appToken, PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator); - } - - if (fakeToken) - { - PushNotificationManager::UnregisterActivator(fakeToken, PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator); - } - -} - int main() try { bool testResult = false; auto scope_exit = wil::scope_exit([&] { - UnregisterClsid(); + if (g_appToken) + { + PushNotificationManager::UnregisterActivator(g_appToken, PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator); + } }); PushNotificationActivationInfo info( PushNotificationRegistrationOptions::PushTrigger | PushNotificationRegistrationOptions::ComActivator, winrt::guid(c_comServerId)); // same clsid as app manifest - appToken = PushNotificationManager::RegisterActivator(info); + g_appToken = PushNotificationManager::RegisterActivator(info); auto args = AppInstance::GetCurrent().GetActivatedEventArgs(); auto kind = args.Kind();