winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

Filesystem.cpp (4565B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #include "pch.h"
      4 #include "TestCommon.h"
      5 #include <winget/Filesystem.h>
      6 #include <AppInstallerStrings.h>
      7 
      8 using namespace AppInstaller::Utility;
      9 using namespace AppInstaller::Filesystem;
     10 using namespace TestCommon;
     11 
     12 TEST_CASE("PathEscapesDirectory", "[filesystem]")
     13 {
     14     TestCommon::TempDirectory tempDirectory("TempDirectory");
     15     const std::filesystem::path& basePath = tempDirectory.GetPath();
     16 
     17     std::string badRelativePath = "../../target.exe";
     18     std::string badRelativePath2 = "test/../../target.exe";
     19     std::string goodRelativePath = "target.exe";
     20     std::string goodRelativePath2 = "test/../test1/target.exe";
     21 
     22     std::filesystem::path badPath = basePath / badRelativePath;
     23     std::filesystem::path badPath2 = basePath / badRelativePath2;
     24     std::filesystem::path goodPath = basePath / goodRelativePath;
     25     std::filesystem::path goodPath2 = basePath / goodRelativePath2;
     26 
     27     REQUIRE(PathEscapesBaseDirectory(badPath, basePath));
     28     REQUIRE(PathEscapesBaseDirectory(badPath2, basePath));
     29     REQUIRE_FALSE(PathEscapesBaseDirectory(goodPath, basePath));
     30     REQUIRE_FALSE(PathEscapesBaseDirectory(goodPath2, basePath));
     31 }
     32 
     33 TEST_CASE("VerifySymlink", "[filesystem]")
     34 {
     35     TestCommon::TempDirectory tempDirectory("TempDirectory");
     36     const std::filesystem::path& basePath = tempDirectory.GetPath();
     37 
     38     std::filesystem::path testFilePath = basePath / "testFile.txt";
     39     std::filesystem::path symlinkPath = basePath / "symlink.exe";
     40 
     41     TestCommon::TempFile testFile(testFilePath);
     42     std::ofstream file2(testFile, std::ofstream::out);
     43     file2.close();
     44 
     45     std::filesystem::create_symlink(testFile.GetPath(), symlinkPath);
     46 
     47     REQUIRE(SymlinkExists(symlinkPath));
     48     REQUIRE(VerifySymlink(symlinkPath, testFilePath));
     49     REQUIRE_FALSE(VerifySymlink(symlinkPath, "badPath"));
     50 
     51     std::filesystem::remove(testFilePath);
     52 
     53     // Ensure that symlink existence does not check the target
     54     REQUIRE(SymlinkExists(symlinkPath));
     55 
     56     std::filesystem::remove(symlinkPath);
     57 
     58     REQUIRE_FALSE(SymlinkExists(symlinkPath));
     59 }
     60 
     61 TEST_CASE("VerifyIsSameVolume", "[filesystem]")
     62 {
     63     // Note: Pipeline build machine uses 'D:\' as the volume.
     64     std::filesystem::path path1 = L"C:\\Program Files\\WinGet\\Packages";
     65     std::filesystem::path path2 = L"c:\\Users\\testUser\\AppData\\Local\\Microsoft\\WinGet\\Packages";
     66     std::filesystem::path path3 = L"localPath\\test\\folder";
     67     std::filesystem::path path4 = L"test\\folder";
     68     std::filesystem::path path5 = L"D:\\test\\folder";
     69     std::filesystem::path path6 = L"F:\\test\\folder";
     70     std::filesystem::path path7 = L"d:\\randomFolder";
     71     std::filesystem::path path8 = L"f:\\randomFolder";
     72     std::filesystem::path path9 = L"a";
     73     std::filesystem::path path10 = L"b";
     74 
     75     REQUIRE(IsSameVolume(path1, path2));
     76     if (IsSameVolume(path5, path5))
     77     {
     78         REQUIRE(IsSameVolume(path5, path7));
     79     }
     80     REQUIRE(IsSameVolume(path3, path4));
     81     REQUIRE(IsSameVolume(path9, path10));
     82 
     83     REQUIRE_FALSE(IsSameVolume(path1, path5));
     84     REQUIRE_FALSE(IsSameVolume(path1, path6));
     85     REQUIRE_FALSE(IsSameVolume(path2, path5));
     86     REQUIRE_FALSE(IsSameVolume(path2, path6));
     87     REQUIRE_FALSE(IsSameVolume(path3, path6));
     88     REQUIRE_FALSE(IsSameVolume(path5, path6));
     89     REQUIRE_FALSE(IsSameVolume(path4, path6));
     90     REQUIRE_FALSE(IsSameVolume(path6, path8));
     91 }
     92 
     93 TEST_CASE("ReplaceCommonPathPrefix", "[filesystem]")
     94 {
     95     std::filesystem::path prefix = "C:\\test1\\test2";
     96     std::string replacement = "%TEST%";
     97 
     98     std::filesystem::path shouldReplace = "C:\\test1\\test2\\subdir1\\subdir2";
     99     REQUIRE(ReplaceCommonPathPrefix(shouldReplace, prefix, replacement));
    100     REQUIRE(shouldReplace.u8string() == (replacement + "\\subdir1\\subdir2"));
    101 
    102     std::filesystem::path shouldNotReplace = "C:\\test1\\test3\\subdir1\\subdir2";
    103     REQUIRE(!ReplaceCommonPathPrefix(shouldNotReplace, prefix, replacement));
    104     REQUIRE(shouldNotReplace.u8string() == "C:\\test1\\test3\\subdir1\\subdir2");
    105 }
    106 
    107 TEST_CASE("GetExecutablePathForProcess", "[filesystem]")
    108 {
    109     std::filesystem::path thisExecutable = GetExecutablePathForProcess(GetCurrentProcess());
    110     REQUIRE(!thisExecutable.empty());
    111     REQUIRE(thisExecutable.is_absolute());
    112     REQUIRE(thisExecutable.has_filename());
    113     REQUIRE(thisExecutable.has_extension());
    114     REQUIRE(thisExecutable.filename() == L"AppInstallerCLITests.exe");
    115 }