Throw ArgumentException for non-absolute path in GetVersionInfo (#25978)

* Throw ArgumentException for non-absolute path in GetVersionInfo

* Missing semicolon

* Path.IsPathRooted()

* Casing

* Fix Reference Include

* Fix test

* Fix test again

* Undo accidental change to .sln file

* Remove change to .sln entirely

* Update temp file, exception location

* Use FileStream

* Undo .sln change again

* Disable on uapnotuapaot

* Add Console.WriteLine to trigger exception on netfx

* Missing )

* Remove exception/redo test

* Update test case and comments

* Switch to just UAP
This commit is contained in:
William Godbe 2018-01-02 17:38:35 -08:00 коммит произвёл GitHub
Родитель 7c8830daa3
Коммит d7dd6a4b03
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 23 добавлений и 0 удалений

Просмотреть файл

@ -13,6 +13,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\System.Runtime\ref\System.Runtime.csproj" /> <ProjectReference Include="..\..\System.Runtime\ref\System.Runtime.csproj" />
<ProjectReference Include="..\..\System.Runtime.Extensions\ref\System.Runtime.Extensions.csproj" />
</ItemGroup> </ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" /> <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project> </Project>

Просмотреть файл

@ -60,6 +60,7 @@
<Reference Include="System.IO.FileSystem" /> <Reference Include="System.IO.FileSystem" />
<Reference Include="System.Resources.ResourceManager" /> <Reference Include="System.Resources.ResourceManager" />
<Reference Include="System.Runtime" /> <Reference Include="System.Runtime" />
<Reference Include="System.Runtime.Extensions" />
<Reference Include="System.Runtime.InteropServices" /> <Reference Include="System.Runtime.InteropServices" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition="'$(TargetsUnix)' == 'true' OR '$(TargetGroup)' == 'uap'"> <ItemGroup Condition="'$(TargetsUnix)' == 'true' OR '$(TargetGroup)' == 'uap'">

Просмотреть файл

@ -265,6 +265,12 @@ namespace System.Diagnostics
/// </summary> /// </summary>
public static FileVersionInfo GetVersionInfo(string fileName) public static FileVersionInfo GetVersionInfo(string fileName)
{ {
// Check if fileName is a full path. Relative paths can cause confusion if the local file has the .dll extension,
// as .dll search paths can take over & look for system .dll's in that case.
if (!Path.IsPathFullyQualified(fileName))
{
fileName = Path.GetFullPath(fileName);
}
// Check for the existence of the file. File.Exists returns false if Read permission is denied. // Check for the existence of the file. File.Exists returns false if Read permission is denied.
if (!File.Exists(fileName)) if (!File.Exists(fileName))
{ {

Просмотреть файл

@ -102,6 +102,21 @@ namespace System.Diagnostics.Tests
FileVersionInfo.GetVersionInfo(Path.Combine(Directory.GetCurrentDirectory(), TestNotFoundFileName))); FileVersionInfo.GetVersionInfo(Path.Combine(Directory.GetCurrentDirectory(), TestNotFoundFileName)));
} }
[Fact]
[SkipOnTargetFramework(TargetFrameworkMonikers.Uap, "Don't want to create temp file in app container current directory")]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "NetFX throws ArgumentException in this case")]
public void FileVersionInfo_RelativePath_CorrectFilePath()
{
using (new FileStream("kernelbase.dll", FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None, 0x1000, FileOptions.DeleteOnClose))
{
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo("kernelbase.dll");
//File name should be the full path to the local kernelbase.dll, not the relative path or the path to the system .dll
Assert.Equal(Path.GetFullPath("kernelbase.dll"), fvi.FileName);
//FileDescription should be null in the local kernelbase.dll
Assert.Equal(null, fvi.FileDescription);
}
}
// Additional Tests Wanted: // Additional Tests Wanted:
// [] File exists but we don't have permission to read it // [] File exists but we don't have permission to read it
// [] DLL has unknown codepage info // [] DLL has unknown codepage info