-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathFileVersionInfoTest.cs
More file actions
268 lines (252 loc) · 10.9 KB
/
Copy pathFileVersionInfoTest.cs
File metadata and controls
268 lines (252 loc) · 10.9 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.IO;
using System.Text;
using Xunit;
namespace System.Diagnostics.Tests
{
public partial class FileVersionInfoTest : FileCleanupTestBase
{
// On Unix the internal name's extension is .exe if OutputType is exe even though the TargetExt is .dll.
private readonly string OriginalTestAssemblyInternalName = PlatformDetection.IsWindows ?
"System.Diagnostics.FileVersionInfo.TestAssembly.dll" :
"System.Diagnostics.FileVersionInfo.TestAssembly.exe";
private const string TestCsFileName = "Assembly1.cs";
private const string TestNotFoundFileName = "notfound.dll";
private string WriteTestAssemblyToFile()
{
string path = GetTestFilePath();
using (Stream source = typeof(FileVersionInfoTest).Assembly.GetManifestResourceStream("System.Diagnostics.FileVersionInfo.TestAssembly.dll"))
using (FileStream destination = File.Create(path))
{
source.CopyTo(destination);
}
return path;
}
[Fact]
public void FileVersionInfo_CustomManagedAssembly()
{
string filePath = WriteTestAssemblyToFile();
VerifyVersionInfo(filePath, new MyFVI()
{
Comments = "Have you played a Contoso amusement device today?",
CompanyName = "The name of the company.",
FileBuildPart = 2,
FileDescription = "My File",
FileMajorPart = 4,
FileMinorPart = 3,
FileName = filePath,
FilePrivatePart = 1,
FileVersion = "4.3.2.1",
InternalName = OriginalTestAssemblyInternalName,
IsDebug = false,
IsPatched = false,
IsPrivateBuild = false,
IsPreRelease = false,
IsSpecialBuild = false,
Language = GetFileVersionLanguage(0x0000),
LegalCopyright = "Copyright, you betcha!",
LegalTrademarks = "TM",
OriginalFilename = OriginalTestAssemblyInternalName,
PrivateBuild = "",
ProductBuildPart = 3,
ProductMajorPart = 1,
ProductMinorPart = 2,
ProductName = "The greatest product EVER",
ProductPrivatePart = 0,
ProductVersion = "1.2.3-beta.4",
SpecialBuild = "",
});
}
[Fact]
public void FileVersionInfo_EmptyFVI()
{
// Assembly1.cs
VerifyVersionInfo(Path.Combine(Directory.GetCurrentDirectory(), TestCsFileName), new MyFVI()
{
Comments = null,
CompanyName = null,
FileBuildPart = 0,
FileDescription = null,
FileMajorPart = 0,
FileMinorPart = 0,
FileName = Path.Combine(Directory.GetCurrentDirectory(), TestCsFileName),
FilePrivatePart = 0,
FileVersion = null,
InternalName = null,
IsDebug = false,
IsPatched = false,
IsPrivateBuild = false,
IsPreRelease = false,
IsSpecialBuild = false,
Language = null,
LegalCopyright = null,
LegalTrademarks = null,
OriginalFilename = null,
PrivateBuild = null,
ProductBuildPart = 0,
ProductMajorPart = 0,
ProductMinorPart = 0,
ProductName = null,
ProductPrivatePart = 0,
ProductVersion = null,
SpecialBuild = null,
});
}
[Fact]
public void FileVersionInfo_CurrentDirectory_FileNotFound()
{
Assert.Throws<FileNotFoundException>(() =>
FileVersionInfo.GetVersionInfo(Directory.GetCurrentDirectory()));
}
[Fact]
public void FileVersionInfo_NonExistentFile_FileNotFound()
{
Assert.Throws<FileNotFoundException>(() =>
FileVersionInfo.GetVersionInfo(Path.Combine(Directory.GetCurrentDirectory(), TestNotFoundFileName)));
}
[Fact]
public void FileVersionInfo_RelativePath_CorrectFilePath()
{
string rootPath = (PlatformDetection.IsiOS || PlatformDetection.IstvOS) ? Path.GetTempPath() : string.Empty;
string kernelBasePath = Path.Combine(rootPath, "kernelbase.dll");
try
{
File.WriteAllText(kernelBasePath, "bogus kernelbase.dll");
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(kernelBasePath);
// 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(kernelBasePath), fvi.FileName);
// FileDescription should be null in the local kernelbase.dll
Assert.Null(fvi.FileDescription);
}
finally
{
File.Delete(kernelBasePath);
}
}
// Additional Tests Wanted:
// [] File exists but we don't have permission to read it
// [] DLL has unknown codepage info
// [] DLL language/codepage is 8-hex-digits (locale > 0x999) (different codepath)
private void VerifyVersionInfo(string filePath, MyFVI expected)
{
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(filePath);
Assert.Equal(expected.Comments, fvi.Comments);
Assert.Equal(expected.CompanyName, fvi.CompanyName);
Assert.Equal(expected.FileBuildPart, fvi.FileBuildPart);
Assert.Equal(expected.FileDescription, fvi.FileDescription);
Assert.Equal(expected.FileMajorPart, fvi.FileMajorPart);
Assert.Equal(expected.FileMinorPart, fvi.FileMinorPart);
Assert.Equal(expected.FileName, fvi.FileName);
Assert.Equal(expected.FilePrivatePart, fvi.FilePrivatePart);
Assert.Equal(expected.FileVersion, fvi.FileVersion);
Assert.Equal(expected.InternalName, fvi.InternalName);
Assert.Equal(expected.IsDebug, fvi.IsDebug);
Assert.Equal(expected.IsPatched, fvi.IsPatched);
Assert.Equal(expected.IsPrivateBuild, fvi.IsPrivateBuild);
Assert.Equal(expected.IsPreRelease, fvi.IsPreRelease);
Assert.Equal(expected.IsSpecialBuild, fvi.IsSpecialBuild);
Assert.Contains(fvi.Language, new[] { expected.Language, expected.Language2 });
Assert.Equal(expected.LegalCopyright, fvi.LegalCopyright);
Assert.Equal(expected.LegalTrademarks, fvi.LegalTrademarks);
Assert.Equal(expected.OriginalFilename, fvi.OriginalFilename);
Assert.Equal(expected.PrivateBuild, fvi.PrivateBuild);
Assert.Equal(expected.ProductBuildPart, fvi.ProductBuildPart);
Assert.Equal(expected.ProductMajorPart, fvi.ProductMajorPart);
Assert.Equal(expected.ProductMinorPart, fvi.ProductMinorPart);
Assert.Equal(expected.ProductName, fvi.ProductName);
Assert.Equal(expected.ProductPrivatePart, fvi.ProductPrivatePart);
Assert.Equal(expected.ProductVersion, fvi.ProductVersion);
Assert.Equal(expected.SpecialBuild, fvi.SpecialBuild);
//ToString
string nl = Environment.NewLine;
Assert.Equal("File: " + fvi.FileName + nl +
"InternalName: " + fvi.InternalName + nl +
"OriginalFilename: " + fvi.OriginalFilename + nl +
"FileVersion: " + fvi.FileVersion + nl +
"FileDescription: " + fvi.FileDescription + nl +
"Product: " + fvi.ProductName + nl +
"ProductVersion: " + fvi.ProductVersion + nl +
"Debug: " + fvi.IsDebug.ToString() + nl +
"Patched: " + fvi.IsPatched.ToString() + nl +
"PreRelease: " + fvi.IsPreRelease.ToString() + nl +
"PrivateBuild: " + fvi.IsPrivateBuild.ToString() + nl +
"SpecialBuild: " + fvi.IsSpecialBuild.ToString() + nl +
"Language: " + fvi.Language + nl,
fvi.ToString());
}
internal class MyFVI
{
public string Comments;
public string CompanyName;
public int FileBuildPart;
public string FileDescription;
public int FileMajorPart;
public int FileMinorPart;
public string FileName;
public int FilePrivatePart;
public string FileVersion;
public string InternalName;
public bool IsDebug;
public bool IsPatched;
public bool IsPrivateBuild;
public bool IsPreRelease;
public bool IsSpecialBuild;
public string Language;
public string Language2;
public string LegalCopyright;
public string LegalTrademarks;
public string OriginalFilename;
public string PrivateBuild;
public int ProductBuildPart;
public int ProductMajorPart;
public int ProductMinorPart;
public string ProductName;
public int ProductPrivatePart;
public string ProductVersion;
public string SpecialBuild;
}
static string GetUnicodeString(string str)
{
if (str == null)
return "<null>";
StringBuilder buffer = new StringBuilder();
buffer.Append("\"");
for (int i = 0; i < str.Length; i++)
{
char ch = str[i];
if (ch == '\r')
{
buffer.Append("\\r");
}
else if (ch == '\n')
{
buffer.Append("\\n");
}
else if (ch == '\\')
{
buffer.Append("\\");
}
else if (ch == '\"')
{
buffer.Append("\\\"");
}
else if (ch == '\'')
{
buffer.Append("\\\'");
}
else if (ch < 0x20 || ch >= 0x7f)
{
buffer.Append("\\u");
buffer.Append(((int)ch).ToString("x4"));
}
else
{
buffer.Append(ch);
}
}
buffer.Append("\"");
return (buffer.ToString());
}
}
}