| 1 | // 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. |
| 2 | |
| 3 | #include "precomp.h" |
| 4 | |
| 5 | using namespace System; |
| 6 | using namespace System::Collections; |
| 7 | using namespace Xunit; |
| 8 | using namespace WixInternal::TestSupport; |
| 9 | using namespace WixInternal::TestSupport::XunitExtensions; |
| 10 | |
| 11 | namespace DutilTests |
| 12 | { |
| 13 | public ref class EnvUtil |
| 14 | { |
| 15 | public: |
| 16 | [Fact] |
| 17 | void EnvExpandEnvironmentStringsTest() |
| 18 | { |
| 19 | HRESULT hr = S_OK; |
| 20 | LPWSTR sczExpanded = NULL; |
| 21 | SIZE_T cchExpanded = 0; |
| 22 | LPCWSTR wzSimpleString = L"%USERPROFILE%"; |
| 23 | LPCWSTR wzMultipleString = L"%TEMP%;%PATH%"; |
| 24 | LPCWSTR wzLongMultipleString = L"%TEMP%;%PATH%;C:\\abcdefghijklomnopqrstuvwxyz0123456789\\abcdefghijklomnopqrstuvwxyz0123456789\\abcdefghijklomnopqrstuvwxyz0123456789\\abcdefghijklomnopqrstuvwxyz0123456789\\abcdefghijklomnopqrstuvwxyz0123456789"; |
| 25 | String^ expandedSimpleString = Environment::ExpandEnvironmentVariables(gcnew String(wzSimpleString)); |
| 26 | String^ expandedMultipleString = Environment::ExpandEnvironmentVariables(gcnew String(wzMultipleString)); |
| 27 | String^ expandedLongMultipleString = Environment::ExpandEnvironmentVariables(gcnew String(wzLongMultipleString)); |
| 28 | |
| 29 | try |
| 30 | { |
| 31 | hr = EnvExpandEnvironmentStrings(wzSimpleString, &sczExpanded, &cchExpanded); |
| 32 | NativeAssert::Succeeded(hr, "Failed to expand simple string."); |
| 33 | WixAssert::StringEqual(expandedSimpleString, gcnew String(sczExpanded), false); |
| 34 | NativeAssert::Equal<SIZE_T>(expandedSimpleString->Length + 1, cchExpanded); |
| 35 | |
| 36 | hr = EnvExpandEnvironmentStrings(wzMultipleString, &sczExpanded, &cchExpanded); |
| 37 | NativeAssert::Succeeded(hr, "Failed to expand multiple string."); |
| 38 | WixAssert::StringEqual(expandedMultipleString, gcnew String(sczExpanded), false); |
| 39 | NativeAssert::Equal<SIZE_T>(expandedMultipleString->Length + 1, cchExpanded); |
| 40 | |
| 41 | hr = EnvExpandEnvironmentStrings(wzLongMultipleString, &sczExpanded, &cchExpanded); |
| 42 | NativeAssert::Succeeded(hr, "Failed to expand long multiple string."); |
| 43 | WixAssert::StringEqual(expandedLongMultipleString, gcnew String(sczExpanded), false); |
| 44 | NativeAssert::Equal<SIZE_T>(expandedLongMultipleString->Length + 1, cchExpanded); |
| 45 | } |
| 46 | finally |
| 47 | { |
| 48 | ReleaseStr(sczExpanded); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | [SkippableFact] |
| 53 | void EnvExpandEnvironmentStringsForUserTest() |
| 54 | { |
| 55 | HRESULT hr = S_OK; |
| 56 | LPWSTR sczExpanded = NULL; |
| 57 | SIZE_T cchExpanded = 0; |
| 58 | String^ variableName = nullptr; |
| 59 | String^ variableValue = nullptr; |
| 60 | |
| 61 | // Find a system environment variable that doesn't have variables in its value; |
| 62 | for each (DictionaryEntry^ entry in Environment::GetEnvironmentVariables(EnvironmentVariableTarget::Machine)) |
| 63 | { |
| 64 | variableValue = (String^)entry->Value; |
| 65 | if (variableValue->Contains("%")) |
| 66 | { |
| 67 | continue; |
| 68 | } |
| 69 | |
| 70 | variableName = (String^)entry->Key; |
| 71 | break; |
| 72 | } |
| 73 | |
| 74 | if (nullptr == variableName) |
| 75 | { |
| 76 | WixAssert::Skip("No suitable system environment variables"); |
| 77 | } |
| 78 | |
| 79 | pin_ptr<const wchar_t> wzUnexpanded = PtrToStringChars("%" + variableName + "%_%USERNAME%"); |
| 80 | String^ expandedValue = variableValue + "_SYSTEM"; |
| 81 | |
| 82 | try |
| 83 | { |
| 84 | hr = EnvExpandEnvironmentStringsForUser(NULL, wzUnexpanded, &sczExpanded, &cchExpanded); |
| 85 | NativeAssert::Succeeded(hr, "Failed to expand %ls.", wzUnexpanded); |
| 86 | WixAssert::StringEqual(expandedValue, gcnew String(sczExpanded), false); |
| 87 | NativeAssert::Equal<SIZE_T>(expandedValue->Length + 1, cchExpanded); |
| 88 | } |
| 89 | finally |
| 90 | { |
| 91 | ReleaseStr(sczExpanded); |
| 92 | } |
| 93 | } |
| 94 | }; |
| 95 | } |