aboutsummaryrefslogtreecommitdiff
path: root/src/burn/test/BurnUnitTest/RelatedBundleTest.cpp
blob: 3d1964c31f050790f7cd27c12c8fde28ecc626c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

#include "precomp.h"


namespace Microsoft
{
namespace Tools
{
namespace WindowsInstallerXml
{
namespace Test
{
namespace Bootstrapper
{
    using namespace System;
    using namespace System::IO;
    using namespace Xunit;
    using namespace WixBuildTools::TestSupport;

    public ref class RelatedBundleTest : BurnUnitTest, IClassFixture<TestRegistryFixture^>
    {
    private:
        TestRegistryFixture^ testRegistry;
    public:
        RelatedBundleTest(BurnTestFixture^ fixture, TestRegistryFixture^ registryFixture) : BurnUnitTest(fixture)
        {
            this->testRegistry = registryFixture;
        }

        [Fact]
        void RelatedBundleDetectPerMachineTest()
        {
            HRESULT hr = S_OK;
            IXMLDOMElement* pixeBundle = NULL;
            BURN_REGISTRATION registration = { };
            BURN_RELATED_BUNDLES relatedBundles = { };
            BURN_CACHE cache = { };
            BURN_ENGINE_COMMAND internalCommand = { };

            try
            {
                this->testRegistry->SetUp();
                this->RegisterFakeBundles();

                LPCWSTR wzDocument =
                    L"<Bundle>"
                    L"    <UX>"
                    L"        <Payload Id='ux.dll' FilePath='ux.dll' Packaging='embedded' SourcePath='ux.dll' />"
                    L"    </UX>"
                    L"    <RelatedBundle Id='{89FDAE1F-8CC1-48B9-B930-3945E0D3E7F0}' Action='Upgrade' />"
                    L"    <Registration Id='{D54F896D-1952-43E6-9C67-B5652240618C}' Tag='foo' ProviderKey='foo' Version='1.0.0.0' ExecutableName='setup.exe' PerMachine='yes'>"
                    L"        <Arp Register='yes' Publisher='WiX Toolset' DisplayName='RegisterBasicTest' DisplayVersion='1.0.0.0' />"
                    L"    </Registration>"
                    L"</Bundle>";

                // load XML document
                LoadBundleXmlHelper(wzDocument, &pixeBundle);

                hr = CacheInitialize(&cache, &internalCommand);
                TestThrowOnFailure(hr, L"Failed initialize cache.");

                hr = RegistrationParseFromXml(&registration, &cache, pixeBundle);
                TestThrowOnFailure(hr, L"Failed to parse registration from XML.");

                RelatedBundlesInitializeForScope(registration.fPerMachine, &registration, &relatedBundles);

                Assert::Equal(1lu, relatedBundles.cRelatedBundles);

                BURN_RELATED_BUNDLE* pRelatedBundle = relatedBundles.rgRelatedBundles + 0;
                NativeAssert::StringEqual(L"{AD75BE46-B5D7-4208-BC8B-918553C72D83}", pRelatedBundle->package.sczId);
                //{E2355133-384C-4332-9B62-1FA950D707B7} should be missing because it causes an error while processing it. It's important that this doesn't cause initialization to fail.
            }
            finally
            {
                ReleaseObject(pixeBundle);
                RegistrationUninitialize(&registration);

                this->testRegistry->TearDown();
            }
        }

        [Fact]
        void RelatedBundleDetectPerUserTest()
        {
            HRESULT hr = S_OK;
            IXMLDOMElement* pixeBundle = NULL;
            BURN_REGISTRATION registration = { };
            BURN_RELATED_BUNDLES relatedBundles = { };
            BURN_CACHE cache = { };
            BURN_ENGINE_COMMAND internalCommand = { };

            try
            {
                this->testRegistry->SetUp();
                this->RegisterFakeBundles();

                LPCWSTR wzDocument =
                    L"<Bundle>"
                    L"    <UX>"
                    L"        <Payload Id='ux.dll' FilePath='ux.dll' Packaging='embedded' SourcePath='ux.dll' />"
                    L"    </UX>"
                    L"    <RelatedBundle Id='{89FDAE1F-8CC1-48B9-B930-3945E0D3E7F0}' Action='Upgrade' />"
                    L"    <Registration Id='{3DB49D3D-1FB8-4147-A465-BBE8BFD0DAD0}' Tag='foo' ProviderKey='foo' Version='4.0.0.0' ExecutableName='setup.exe' PerMachine='no'>"
                    L"        <Arp Register='yes' Publisher='WiX Toolset' DisplayName='RegisterBasicTest' DisplayVersion='4.0.0.0' />"
                    L"    </Registration>"
                    L"</Bundle>";

                // load XML document
                LoadBundleXmlHelper(wzDocument, &pixeBundle);

                hr = CacheInitialize(&cache, &internalCommand);
                TestThrowOnFailure(hr, L"Failed initialize cache.");

                hr = RegistrationParseFromXml(&registration, &cache, pixeBundle);
                TestThrowOnFailure(hr, L"Failed to parse registration from XML.");

                RelatedBundlesInitializeForScope(registration.fPerMachine, &registration, &relatedBundles);

                Assert::Equal(1lu, relatedBundles.cRelatedBundles);

                BURN_RELATED_BUNDLE* pRelatedBundle = relatedBundles.rgRelatedBundles + 0;
                NativeAssert::StringEqual(L"{6DB5D48C-CD7D-40D2-BCBC-AF630E136761}", pRelatedBundle->package.sczId);
                //{42D16EBE-8B6B-4A9A-9AE9-5300F30011AA} should be missing because it causes an error while processing it. It's important that this doesn't cause initialization to fail.
            }
            finally
            {
                ReleaseObject(pixeBundle);
                RegistrationUninitialize(&registration);

                this->testRegistry->TearDown();
            }
        }

        void RegisterFakeBundles()
        {
            this->RegisterFakeBundle(L"{D54F896D-1952-43E6-9C67-B5652240618C}", L"{89FDAE1F-8CC1-48B9-B930-3945E0D3E7F0}", NULL, L"1.0.0.0", TRUE);
            this->RegisterFakeBundle(L"{E2355133-384C-4332-9B62-1FA950D707B7}", L"{89FDAE1F-8CC1-48B9-B930-3945E0D3E7F0}", L"", L"1.1.0.0", TRUE);
            this->RegisterFakeBundle(L"{AD75BE46-B5D7-4208-BC8B-918553C72D83}", L"{89FDAE1F-8CC1-48B9-B930-3945E0D3E7F0}", NULL, L"2.0.0.0", TRUE);
            this->RegisterFakeBundle(L"{6DB5D48C-CD7D-40D2-BCBC-AF630E136761}", L"{89FDAE1F-8CC1-48B9-B930-3945E0D3E7F0}", NULL, L"3.0.0.0", FALSE);
            this->RegisterFakeBundle(L"{42D16EBE-8B6B-4A9A-9AE9-5300F30011AA}", L"{89FDAE1F-8CC1-48B9-B930-3945E0D3E7F0}", L"", L"3.1.0.0", FALSE);
            this->RegisterFakeBundle(L"{3DB49D3D-1FB8-4147-A465-BBE8BFD0DAD0}", L"{89FDAE1F-8CC1-48B9-B930-3945E0D3E7F0}", NULL, L"4.0.0.0", FALSE);
        }

        void RegisterFakeBundle(LPCWSTR wzBundleId, LPCWSTR wzUpgradeCodes, LPCWSTR wzCachePath, LPCWSTR wzVersion, BOOL fPerMachine)
        {
            HRESULT hr = S_OK;
            LPWSTR* rgsczUpgradeCodes = NULL;
            DWORD cUpgradeCodes = 0;
            LPWSTR sczRegistrationKey = NULL;
            LPWSTR sczCachePath = NULL;
            HKEY hkRegistration = NULL;
            HKEY hkRoot = fPerMachine ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;

            try
            {
                hr = StrSplitAllocArray(&rgsczUpgradeCodes, reinterpret_cast<UINT*>(&cUpgradeCodes), wzUpgradeCodes, L";");
                NativeAssert::Succeeded(hr, "Failed to split upgrade codes.");

                hr = StrAllocFormatted(&sczRegistrationKey, L"%s\\%s", BURN_REGISTRATION_REGISTRY_UNINSTALL_KEY, wzBundleId);
                NativeAssert::Succeeded(hr, "Failed to build uninstall registry key path.");

                if (!wzCachePath)
                {
                    hr = StrAllocFormatted(&sczCachePath, L"%ls.exe", wzBundleId);
                    NativeAssert::Succeeded(hr, "Failed to build cache path.");

                    wzCachePath = sczCachePath;
                }

                hr = RegCreate(hkRoot, sczRegistrationKey, KEY_WRITE, &hkRegistration);
                NativeAssert::Succeeded(hr, "Failed to create registration key.");

                hr = RegWriteStringArray(hkRegistration, BURN_REGISTRATION_REGISTRY_BUNDLE_UPGRADE_CODE, rgsczUpgradeCodes, cUpgradeCodes);
                NativeAssert::Succeeded(hr, "Failed to write %ls value.", BURN_REGISTRATION_REGISTRY_BUNDLE_UPGRADE_CODE);

                if (wzCachePath && *wzCachePath)
                {
                    hr = RegWriteString(hkRegistration, BURN_REGISTRATION_REGISTRY_BUNDLE_CACHE_PATH, wzCachePath);
                    NativeAssert::Succeeded(hr, "Failed to write %ls value.", BURN_REGISTRATION_REGISTRY_BUNDLE_CACHE_PATH);
                }

                hr = RegWriteString(hkRegistration, BURN_REGISTRATION_REGISTRY_BUNDLE_VERSION, wzVersion);
                NativeAssert::Succeeded(hr, "Failed to write %ls value.", BURN_REGISTRATION_REGISTRY_BUNDLE_VERSION);
            }
            finally
            {
                ReleaseStrArray(rgsczUpgradeCodes, cUpgradeCodes);
                ReleaseStr(sczRegistrationKey);
                ReleaseStr(sczCachePath);
                ReleaseRegKey(hkRegistration);
            }
        }
    };
}
}
}
}
}