summaryrefslogtreecommitdiff
path: root/src/libs/dutil/test/DUtilUnitTest/PathUtilTest.cpp
blob: 5a1f06fd33fb42a6d80c20a374b1633d63016fc6 (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
// 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 PathUtil
    {
    public:
        [Fact]
        void PathGetHierarchyArrayTest()
        {
            HRESULT hr = S_OK;
            LPWSTR *rgsczPaths = NULL;
            UINT cPaths = 0;

            try
            {
                hr = PathGetHierarchyArray(L"c:\\foo\\bar\\bas\\a.txt", &rgsczPaths, &cPaths);
                NativeAssert::Succeeded(hr, "Failed to get parent directories array for regular file path");
                Assert::Equal<DWORD>(5, cPaths);
                NativeAssert::StringEqual(L"c:\\", rgsczPaths[0]);
                NativeAssert::StringEqual(L"c:\\foo\\", rgsczPaths[1]);
                NativeAssert::StringEqual(L"c:\\foo\\bar\\", rgsczPaths[2]);
                NativeAssert::StringEqual(L"c:\\foo\\bar\\bas\\", rgsczPaths[3]);
                NativeAssert::StringEqual(L"c:\\foo\\bar\\bas\\a.txt", rgsczPaths[4]);
                ReleaseNullStrArray(rgsczPaths, cPaths);

                hr = PathGetHierarchyArray(L"c:\\foo\\bar\\bas\\", &rgsczPaths, &cPaths);
                NativeAssert::Succeeded(hr, "Failed to get parent directories array for regular directory path");
                Assert::Equal<DWORD>(4, cPaths);
                NativeAssert::StringEqual(L"c:\\", rgsczPaths[0]);
                NativeAssert::StringEqual(L"c:\\foo\\", rgsczPaths[1]);
                NativeAssert::StringEqual(L"c:\\foo\\bar\\", rgsczPaths[2]);
                NativeAssert::StringEqual(L"c:\\foo\\bar\\bas\\", rgsczPaths[3]);
                ReleaseNullStrArray(rgsczPaths, cPaths);

                hr = PathGetHierarchyArray(L"\\\\server\\share\\subdir\\file.txt", &rgsczPaths, &cPaths);
                NativeAssert::Succeeded(hr, "Failed to get parent directories array for UNC file path");
                Assert::Equal<DWORD>(3, cPaths);
                NativeAssert::StringEqual(L"\\\\server\\share\\", rgsczPaths[0]);
                NativeAssert::StringEqual(L"\\\\server\\share\\subdir\\", rgsczPaths[1]);
                NativeAssert::StringEqual(L"\\\\server\\share\\subdir\\file.txt", rgsczPaths[2]);
                ReleaseNullStrArray(rgsczPaths, cPaths);

                hr = PathGetHierarchyArray(L"\\\\server\\share\\subdir\\", &rgsczPaths, &cPaths);
                NativeAssert::Succeeded(hr, "Failed to get parent directories array for UNC directory path");
                Assert::Equal<DWORD>(2, cPaths);
                NativeAssert::StringEqual(L"\\\\server\\share\\", rgsczPaths[0]);
                NativeAssert::StringEqual(L"\\\\server\\share\\subdir\\", rgsczPaths[1]);
                ReleaseNullStrArray(rgsczPaths, cPaths);

                hr = PathGetHierarchyArray(L"Software\\Microsoft\\Windows\\ValueName", &rgsczPaths, &cPaths);
                NativeAssert::Succeeded(hr, "Failed to get parent directories array for UNC directory path");
                Assert::Equal<DWORD>(4, cPaths);
                NativeAssert::StringEqual(L"Software\\", rgsczPaths[0]);
                NativeAssert::StringEqual(L"Software\\Microsoft\\", rgsczPaths[1]);
                NativeAssert::StringEqual(L"Software\\Microsoft\\Windows\\", rgsczPaths[2]);
                NativeAssert::StringEqual(L"Software\\Microsoft\\Windows\\ValueName", rgsczPaths[3]);
                ReleaseNullStrArray(rgsczPaths, cPaths);

                hr = PathGetHierarchyArray(L"Software\\Microsoft\\Windows\\", &rgsczPaths, &cPaths);
                NativeAssert::Succeeded(hr, "Failed to get parent directories array for UNC directory path");
                Assert::Equal<DWORD>(3, cPaths);
                NativeAssert::StringEqual(L"Software\\", rgsczPaths[0]);
                NativeAssert::StringEqual(L"Software\\Microsoft\\", rgsczPaths[1]);
                NativeAssert::StringEqual(L"Software\\Microsoft\\Windows\\", rgsczPaths[2]);
                ReleaseNullStrArray(rgsczPaths, cPaths);
            }
            finally
            {
                ReleaseStrArray(rgsczPaths, cPaths);
            }
        }
    };
}