Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IsSelfContained() to lookup framework packagefamilyname at runtime #2411

Merged
merged 7 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion dev/Common/Common.vcxitems
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="$(MSBuildThisFileDirectory)WindowsAppRuntime.SelfContained.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)WindowsAppRuntime.VersionInfo.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="$(MSBuildThisFileDirectory)AppModel.Identity.h" />
Expand All @@ -25,5 +26,6 @@
<ClInclude Include="$(MSBuildThisFileDirectory)Microsoft.Utf8.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)Security.IntegrityLevel.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)WindowsAppRuntime.SelfContained.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)WindowsAppRuntime.VersionInfo.h" />
</ItemGroup>
</Project>
</Project>
8 changes: 7 additions & 1 deletion dev/Common/Common.vcxitems.filters
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
<ClInclude Include="$(MSBuildThisFileDirectory)WindowsAppRuntime.SelfContained.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="$(MSBuildThisFileDirectory)WindowsAppRuntime.VersionInfo.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="$(MSBuildThisFileDirectory)Security.IntegrityLevel.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand All @@ -37,5 +40,8 @@
<ClCompile Include="$(MSBuildThisFileDirectory)WindowsAppRuntime.SelfContained.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="$(MSBuildThisFileDirectory)WindowsAppRuntime.VersionInfo.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
</Project>
32 changes: 3 additions & 29 deletions dev/Common/WindowsAppRuntime.SelfContained.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@

#include "pch.h"

#if __has_include(<WindowsAppRuntime-VersionInfo.h>)
# include <WindowsAppRuntime-VersionInfo.h>
# define WINDOWSAPPRUNTIME_SELFCONTAINED_EXPECTED_PACKAGEFAMILYNAME WINDOWSAPPSDK_RUNTIME_PACKAGE_FRAMEWORK_PACKAGEFAMILYNAME_W
#else
# define WINDOWSAPPRUNTIME_SELFCONTAINED_EXPECTED_PACKAGEFAMILYNAME nullptr
#endif
#include <AppModel.PackageGraph.h>

#include "WindowsAppRuntime.VersionInfo.h"
#include "WindowsAppRuntime.SelfContained.h"

static std::wstring g_test_frameworkPackageFamilyName;

STDAPI WindowsAppRuntime_IsSelfContained(
BOOL* isSelfContained) noexcept try
{
Expand All @@ -25,12 +18,10 @@ STDAPI WindowsAppRuntime_IsSelfContained(
const PACKAGE_INFO* packageInfo{};
wil::unique_cotaskmem_ptr<BYTE[]> buffer;
RETURN_IF_FAILED(::AppModel::PackageGraph::GetCurrentPackageGraph(flags, packageInfoCount, packageInfo, buffer));
PCWSTR c_frameworkPackageFamilyName{ !g_test_frameworkPackageFamilyName.empty() ?
g_test_frameworkPackageFamilyName.c_str() :
WINDOWSAPPRUNTIME_SELFCONTAINED_EXPECTED_PACKAGEFAMILYNAME };
const auto frameworkPackageFamilyName{ ::WindowsAppRuntime::VersionInfo::GetPackageFamilyName() };
for (uint32_t index=0; index < packageInfoCount; ++index)
{
if (CompareStringOrdinal(packageInfo[index].packageFamilyName, -1, c_frameworkPackageFamilyName, -1, TRUE) == CSTR_EQUAL)
if (CompareStringOrdinal(packageInfo[index].packageFamilyName, -1, frameworkPackageFamilyName.c_str(), -1, TRUE) == CSTR_EQUAL)
{
// Found the Windows App SDK framework package in the package graph. Not self-contained!
return S_OK;
Expand All @@ -42,20 +33,3 @@ STDAPI WindowsAppRuntime_IsSelfContained(
return S_OK;
}
CATCH_RETURN();

STDAPI WindowsAppRuntime_SelfContainedTestInitialize(
PCWSTR frameworkPackageFamilyName) noexcept try
{
if (!frameworkPackageFamilyName || (*frameworkPackageFamilyName == L'0'))
{
// Shutdown test support
g_test_frameworkPackageFamilyName.clear();
}
else
{
// Initialize test support
g_test_frameworkPackageFamilyName = frameworkPackageFamilyName;
}
return S_OK;
}
CATCH_RETURN();
32 changes: 1 addition & 31 deletions dev/Common/WindowsAppRuntime.SelfContained.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,16 @@
STDAPI WindowsAppRuntime_IsSelfContained(
BOOL* isSelfContained) noexcept;

/// Initialize SelfContained's test support. This will constrain package enumeration
/// and matching for test purposes.
///
/// @param frameworkPackageFamilyName only match framework packages with this family name.
/// If nullptr test support is disabled.
///
/// @note Not for product use. This is for test purposes only to verify the implementation.
STDAPI WindowsAppRuntime_SelfContainedTestInitialize(
PCWSTR frameworkPackageFamilyName) noexcept;

#if defined(__cplusplus)
/// Return true if Windows App SDK in use by the current process is deployed Self-Contained.
namespace WindowsAppRuntime::SelfContained
{
/// Return true if Windows App SDK in use by the current process is deployed Self-Contained.
inline bool IsSelfContained()
{
BOOL isSelfContained{};
THROW_IF_FAILED(WindowsAppRuntime_IsSelfContained(&isSelfContained));
return !!isSelfContained;
}

/// Initialize SelfContained's test support. This will constrain package enumeration
/// and matching for test purposes.
///
/// @param frameworkPackageFamilyName only match framework packages with this family name
///
/// @note Not for product use. This is for test purposes only to verify the implementation.
inline void TestInitialize(
_In_ PCWSTR frameworkPackageFamilyName)
{
THROW_IF_FAILED(WindowsAppRuntime_SelfContainedTestInitialize(frameworkPackageFamilyName));
}

/// Shutdown SelfContained's test support.
///
/// @note Not for product use. This is for test purposes only to verify the implementation.
inline void TestShutdown()
{
WindowsAppRuntime_SelfContainedTestInitialize(nullptr);
}
}
#endif // defined(__cplusplus)

Expand Down
83 changes: 83 additions & 0 deletions dev/Common/WindowsAppRuntime.VersionInfo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#include "pch.h"

#include <Microsoft.Utf8.h>

#include "WindowsAppRuntime.VersionInfo.h"

static std::wstring g_test_frameworkPackageFamilyName;

namespace Microsoft::WindowsAppRuntime::VersionInfo
{
class RuntimeInformation
{
public:
static const std::wstring& GetFrameworkPackageFamilyName()
{
if (!g_test_frameworkPackageFamilyName.empty())
{
return g_test_frameworkPackageFamilyName;
}

const uint32_t c_frameworkPackageFamilyNameResourceId{ 10002 };
static std::wstring frameworkPackageFamilyName{ LoadStringWFromResource(c_frameworkPackageFamilyNameResourceId) };
return frameworkPackageFamilyName;
}

private:
static std::wstring LoadStringWFromResource(uint32_t id)
{
const auto string{ LoadStringAFromResource(id) };
return ::Microsoft::Utf8::ToUtf16<std::wstring>(string.c_str());
}

static std::string LoadStringAFromResource(uint32_t id)
{
static wil::unique_hmodule module{ LoadResourceModule() };

const uint32_t c_ResourceMaxLength{ 1024 };
char resourceValue[c_ResourceMaxLength]{};
const auto resourceValueLength{ ::LoadStringA(module.get(), id, resourceValue, ARRAYSIZE(resourceValue)) };
THROW_LAST_ERROR_IF_MSG(resourceValueLength == 0, "Failed to load resource string. id: %u", id);
return resourceValue;
}

static wil::unique_hmodule LoadResourceModule()
{
const PCWSTR c_resourceDllName{ L"Microsoft.WindowsAppRuntime.Insights.Resource.dll" };
wil::unique_hmodule resourceDllHandle(::LoadLibraryW(c_resourceDllName));
THROW_LAST_ERROR_IF_NULL_MSG(resourceDllHandle, "Unable to load resource dll. %ls", c_resourceDllName);
return resourceDllHandle;
}
};
}

STDAPI WindowsAppRuntime_VersionInfo_MSIX_Framework_PackageFamilyName_Get(
PCWSTR* packageFamilyName) noexcept try
{
*packageFamilyName = nullptr;
const auto& frameworkPackageFamilyName{ ::Microsoft::WindowsAppRuntime::VersionInfo::RuntimeInformation::GetFrameworkPackageFamilyName() };
*packageFamilyName = frameworkPackageFamilyName.c_str();
RETURN_HR_IF_MSG(E_UNEXPECTED, frameworkPackageFamilyName.empty(), "WindowsAppSDK framework PackageFamilyName resource not valid (\"\")");
return S_OK;
}
CATCH_RETURN();

STDAPI WindowsAppRuntime_VersionInfo_TestInitialize(
PCWSTR frameworkPackageFamilyName) noexcept try
{
if (!frameworkPackageFamilyName || (*frameworkPackageFamilyName == L'0'))
{
// Shutdown test support
g_test_frameworkPackageFamilyName.clear();
}
else
{
// Initialize test support
g_test_frameworkPackageFamilyName = frameworkPackageFamilyName;
}
return S_OK;
}
CATCH_RETURN();
56 changes: 56 additions & 0 deletions dev/Common/WindowsAppRuntime.VersionInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#ifndef __MICROSFT_WINDOWSAPPRUNTIME_VERSIONINFO_H
#define __MICROSFT_WINDOWSAPPRUNTIME_VERSIONINFO_H

/// Determine if Windows App SDK in use by the current process is deployed Self-Contained (vs deployed as MSIX packages)
///
/// @param packageFamilyName framework's package family name.
STDAPI WindowsAppRuntime_VersionInfo_MSIX_Framework_PackageFamilyName_Get(
PCWSTR* packageFamilyName) noexcept;

/// Initialize SelfContained's test support. This will constrain package enumeration
/// and matching for test purposes.
///
/// @param frameworkPackageFamilyName only match framework packages with this family name.
/// If nullptr test support is disabled.
///
/// @note Not for product use. This is for test purposes only to verify the implementation.
STDAPI WindowsAppRuntime_VersionInfo_TestInitialize(
PCWSTR frameworkPackageFamilyName) noexcept;

#if defined(__cplusplus)
namespace WindowsAppRuntime::VersionInfo
{
/// Return the Windows App SDK framework's package family name.
inline std::wstring GetPackageFamilyName()
{
PCWSTR packageFamilyName{};
THROW_IF_FAILED(WindowsAppRuntime_VersionInfo_MSIX_Framework_PackageFamilyName_Get(&packageFamilyName));
return std::wstring{ packageFamilyName };
}

/// Initialize VersionInfo's test support. This will constrain package enumeration
/// and matching for test purposes.
///
/// @param frameworkPackageFamilyName only match framework packages with this family name
///
/// @note Not for product use. This is for test purposes only to verify the implementation.
inline void TestInitialize(
_In_ PCWSTR frameworkPackageFamilyName)
{
THROW_IF_FAILED(WindowsAppRuntime_VersionInfo_TestInitialize(frameworkPackageFamilyName));
}

/// Shutdown VersionInfo's test support.
///
/// @note Not for product use. This is for test purposes only to verify the implementation.
inline void TestShutdown()
{
WindowsAppRuntime_VersionInfo_TestInitialize(nullptr);
}
}
#endif // defined(__cplusplus)

#endif // __MICROSFT_WINDOWSAPPRUNTIME_VERSIONINFO_H
4 changes: 3 additions & 1 deletion dev/WindowsAppRuntime_DLL/WindowsAppRuntime.def
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ EXPORTS
GetSecurityDescriptorForAppContainerNames

WindowsAppRuntime_IsSelfContained
WindowsAppRuntime_SelfContainedTestInitialize

WindowsAppRuntime_VersionInfo_MSIX_Framework_PackageFamilyName_Get
WindowsAppRuntime_VersionInfo_TestInitialize
20 changes: 7 additions & 13 deletions dev/WindowsAppRuntime_Insights/WindowsAppRuntimeInsights.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,15 @@
}

private:
static inline std::string LoadStringFromResource(uint32_t id)
static std::string LoadStringFromResource(uint32_t id)
{
static wil::unique_hmodule module = std::move(LoadResourceModule());
const uint32_t c_ResourceMaxLength{ 100 };
char resourceValue[c_ResourceMaxLength]{};

if(module)
static wil::unique_hmodule module{ LoadResourceModule() };
if (module)
{
if(0 == ::LoadStringA(module.get(), id, resourceValue, ARRAYSIZE(resourceValue)))
{
DWORD error{ GetLastError() };
LOG_HR_IF_MSG(HRESULT_FROM_WIN32(error), ERROR_SUCCESS != error, "Failed to load resource string. id: %d", id);
}
const auto resourceValueLength{ ::LoadStringA(module.get(), id, resourceValue, ARRAYSIZE(resourceValue)) };
LOG_LAST_ERROR_IF_MSG(resourceValueLength == 0, "Failed to load resource string. id: %u", id);
}
return resourceValue;
}
Expand All @@ -54,13 +50,11 @@
{
const PCWSTR c_resourceDllName{ L"Microsoft.WindowsAppRuntime.Insights.Resource.dll" };
wil::unique_hmodule resourceDllHandle(::LoadLibraryW(c_resourceDllName));
LOG_HR_IF_NULL_MSG(HRESULT_FROM_WIN32(GetLastError()), resourceDllHandle, "Unable to load resource dll. %ws", c_resourceDllName);
LOG_LAST_ERROR_IF_NULL_MSG(resourceDllHandle, "Unable to load resource dll. %ls", c_resourceDllName);
return resourceDllHandle;
}
};

} // namespace Microsoft::WindowsAppRuntime::Insights

}

#define _GENERIC_PARTB_FIELDS_ENABLED \
TraceLoggingStruct(4, "COMMON_WINDOWSAPPSDK_PARAMS"), \
Expand Down
16 changes: 8 additions & 8 deletions test/Common/Test_SelfContained.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ namespace Test::Common
{
::TB::SetupBootstrap();
auto cleanup = wil::scope_exit([&] {
::WindowsAppRuntime::SelfContained::TestShutdown();
::WindowsAppRuntime::VersionInfo::TestShutdown();
::TB::CleanupBootstrap();
});
const auto c_doesNotExistPackageFamilyName{ L"Test.PackageFamilyName.DoesNotExist_1234567890abc" };
::WindowsAppRuntime::SelfContained::TestInitialize(c_doesNotExistPackageFamilyName);
::WindowsAppRuntime::VersionInfo::TestInitialize(c_doesNotExistPackageFamilyName);

VERIFY_IS_TRUE(::WindowsAppRuntime::SelfContained::IsSelfContained());
}
Expand All @@ -47,10 +47,10 @@ namespace Test::Common
{
::TB::SetupBootstrap();
auto cleanup = wil::scope_exit([&]{
::WindowsAppRuntime::SelfContained::TestShutdown();
::WindowsAppRuntime::VersionInfo::TestShutdown();
::TB::CleanupBootstrap();
});
::WindowsAppRuntime::SelfContained::TestInitialize(::TP::WindowsAppRuntimeFramework::c_PackageFamilyName);
WindowsAppRuntime::VersionInfo::TestInitialize(::TP::WindowsAppRuntimeFramework::c_PackageFamilyName);

VERIFY_IS_FALSE(::WindowsAppRuntime::SelfContained::IsSelfContained());
}
Expand All @@ -60,16 +60,16 @@ namespace Test::Common
{
::TB::SetupBootstrap();
auto cleanup = wil::scope_exit([&] {
::WindowsAppRuntime::SelfContained::TestShutdown();
::WindowsAppRuntime::VersionInfo::TestShutdown();
::TB::CleanupBootstrap();
});
const auto c_doesNotExistPackageFamilyName{ L"Test.PackageFamilyName.DoesNotExist_1234567890abc" };
::WindowsAppRuntime::SelfContained::TestInitialize(c_doesNotExistPackageFamilyName);
WindowsAppRuntime::VersionInfo::TestInitialize(c_doesNotExistPackageFamilyName);
VERIFY_IS_TRUE(::WindowsAppRuntime::SelfContained::IsSelfContained());

::WindowsAppRuntime::SelfContained::TestInitialize(::TP::WindowsAppRuntimeFramework::c_PackageFamilyName);
WindowsAppRuntime::VersionInfo::TestInitialize(::TP::WindowsAppRuntimeFramework::c_PackageFamilyName);
VERIFY_IS_FALSE(::WindowsAppRuntime::SelfContained::IsSelfContained());
::WindowsAppRuntime::SelfContained::TestInitialize(c_doesNotExistPackageFamilyName);
WindowsAppRuntime::VersionInfo::TestInitialize(c_doesNotExistPackageFamilyName);

VERIFY_IS_TRUE(::WindowsAppRuntime::SelfContained::IsSelfContained());
}
Expand Down
1 change: 1 addition & 0 deletions test/Common/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#include <Microsoft.Utf8.h>
#include <WindowsAppRuntime.SelfContained.h>
#include <WindowsAppRuntime.VersionInfo.h>

#include <WindowsAppRuntime.Test.AppModel.h>
#include <WindowsAppRuntime.Test.Package.h>
Expand Down
Loading