From 7f642e51670bc38a4ef782a363936850bc2b0ba9 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Thu, 22 Apr 2021 06:38:23 -0700 Subject: Move dutil into libs/dutil --- src/libs/dutil/test/DUtilUnitTest/FileUtilTest.cpp | 125 +++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 src/libs/dutil/test/DUtilUnitTest/FileUtilTest.cpp (limited to 'src/libs/dutil/test/DUtilUnitTest/FileUtilTest.cpp') diff --git a/src/libs/dutil/test/DUtilUnitTest/FileUtilTest.cpp b/src/libs/dutil/test/DUtilUnitTest/FileUtilTest.cpp new file mode 100644 index 00000000..ac071ef2 --- /dev/null +++ b/src/libs/dutil/test/DUtilUnitTest/FileUtilTest.cpp @@ -0,0 +1,125 @@ +// 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" + +using namespace System; +using namespace Xunit; +using namespace WixBuildTools::TestSupport; + +namespace DutilTests +{ + public ref class FileUtil + { + public: + [Fact(Skip="Skipped until we have a good way to reference ANSI.txt.")] + void FileUtilTest() + { + HRESULT hr = S_OK; + LPWSTR sczTempDir = NULL; + LPWSTR sczFileDir = NULL; + + DutilInitialize(&DutilTestTraceError); + + try + { + hr = PathExpand(&sczTempDir, L"%TEMP%\\FileUtilTest\\", PATH_EXPAND_ENVIRONMENT); + NativeAssert::Succeeded(hr, "Failed to get temp dir"); + + hr = PathExpand(&sczFileDir, L"%WIX_ROOT%\\examples\\data\\TextEncodings\\", PATH_EXPAND_ENVIRONMENT); + NativeAssert::Succeeded(hr, "Failed to get path to encodings file dir"); + + hr = DirEnsureExists(sczTempDir, NULL); + NativeAssert::Succeeded(hr, "Failed to ensure directory exists: {0}", sczTempDir); + + TestFile(sczFileDir, sczTempDir, L"ANSI.txt", 32, FILE_ENCODING_UTF8); + // Big endian not supported today! + //TestFile(sczFileDir, L"UnicodeBENoBOM.txt", 34); + //TestFile(sczFileDir, L"UnicodeBEWithBOM.txt", 34); + TestFile(sczFileDir, sczTempDir, L"UnicodeLENoBOM.txt", 34, FILE_ENCODING_UTF16); + TestFile(sczFileDir, sczTempDir, L"UnicodeLEWithBOM.txt", 34, FILE_ENCODING_UTF16_WITH_BOM); + TestFile(sczFileDir, sczTempDir, L"UTF8WithSignature.txt", 34, FILE_ENCODING_UTF8_WITH_BOM); + + hr = DirEnsureDelete(sczTempDir, TRUE, TRUE); + } + finally + { + ReleaseStr(sczTempDir); + ReleaseStr(sczFileDir); + DutilUninitialize(); + } + } + + private: + void TestFile(LPWSTR wzDir, LPCWSTR wzTempDir, LPWSTR wzFileName, size_t cbExpectedStringLength, FILE_ENCODING feExpectedEncoding) + { + HRESULT hr = S_OK; + LPWSTR sczFullPath = NULL; + LPWSTR sczContents = NULL; + LPWSTR sczOutputPath = NULL; + FILE_ENCODING feEncodingFound = FILE_ENCODING_UNSPECIFIED; + BYTE *pbFile1 = NULL; + DWORD cbFile1 = 0; + BYTE *pbFile2 = NULL; + DWORD cbFile2 = 0; + size_t cbActualStringLength = 0; + + try + { + hr = PathConcat(wzDir, wzFileName, &sczFullPath); + NativeAssert::Succeeded(hr, "Failed to create path to test file: {0}", sczFullPath); + + hr = FileToString(sczFullPath, &sczContents, &feEncodingFound); + hr = E_FAIL; + NativeAssert::Succeeded(hr, "Failed to read text from file: {0}", sczFullPath); + + if (!sczContents) + { + hr = E_FAIL; + NativeAssert::Succeeded(hr, "FileToString() returned NULL for file: {0}", sczFullPath); + } + + hr = ::StringCchLengthW(sczContents, STRSAFE_MAX_CCH, &cbActualStringLength); + NativeAssert::Succeeded(hr, "Failed to get length of text from file: {0}", sczFullPath); + + if (cbActualStringLength != cbExpectedStringLength) + { + hr = E_FAIL; + ExitOnFailure(hr, "FileToString() returned wrong size for file: %ls (expected size %Iu, found size %Iu)", sczFullPath, cbExpectedStringLength, cbActualStringLength); + } + + if (feEncodingFound != feExpectedEncoding) + { + hr = E_FAIL; + ExitOnFailure(hr, "FileToString() returned unexpected encoding type for file: %ls (expected type %u, found type %u)", sczFullPath, feExpectedEncoding, feEncodingFound); + } + + hr = PathConcat(wzTempDir, wzFileName, &sczOutputPath); + NativeAssert::Succeeded(hr, "Failed to get output path"); + + hr = FileFromString(sczOutputPath, 0, sczContents, feExpectedEncoding); + NativeAssert::Succeeded(hr, "Failed to write contents of file back out to disk"); + + hr = FileRead(&pbFile1, &cbFile1, sczFullPath); + NativeAssert::Succeeded(hr, "Failed to read input file as binary"); + + hr = FileRead(&pbFile2, &cbFile2, sczOutputPath); + NativeAssert::Succeeded(hr, "Failed to read output file as binary"); + + if (cbFile1 != cbFile2 || 0 != memcmp(pbFile1, pbFile2, cbFile1)) + { + hr = E_FAIL; + ExitOnFailure(hr, "Outputted file doesn't match input file: \"%ls\" and \"%ls\"", sczFullPath, sczOutputPath); + } + } + finally + { + ReleaseStr(sczOutputPath); + ReleaseStr(sczFullPath); + ReleaseStr(sczContents); + } + + LExit: + return; + } + }; +} -- cgit v1.2.3-55-g6feb