Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit de1878e

Browse files
JeremyKuhnedanmoseley
authored andcommitted
Add more alternate data stream tests (#27945)
1 parent ea82f8f commit de1878e

File tree

6 files changed

+168
-64
lines changed

6 files changed

+168
-64
lines changed

src/System.IO.FileSystem/tests/Base/FileGetSetAttributes.cs

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ public abstract class FileGetSetAttributes : BaseGetSetAttributes
1212
[Theory]
1313
[InlineData(FileAttributes.ReadOnly)]
1414
[InlineData(FileAttributes.Normal)]
15-
[PlatformSpecific(TestPlatforms.AnyUnix)] // Unix valid file attributes
16-
public void UnixAttributeSetting(FileAttributes attr)
15+
[PlatformSpecific(TestPlatforms.AnyUnix)]
16+
public void SettingAttributes_Unix(FileAttributes attributes)
1717
{
1818
string path = CreateItem();
19-
SetAttributes(path, attr);
20-
Assert.Equal(attr, GetAttributes(path));
19+
SetAttributes(path, attributes);
20+
Assert.Equal(attributes, GetAttributes(path));
2121
SetAttributes(path, 0);
2222
}
2323

@@ -29,12 +29,12 @@ public void UnixAttributeSetting(FileAttributes attr)
2929
[InlineData(FileAttributes.Normal)]
3030
[InlineData(FileAttributes.Temporary)]
3131
[InlineData(FileAttributes.ReadOnly | FileAttributes.Hidden)]
32-
[PlatformSpecific(TestPlatforms.Windows)] // Valid Windows file attribute
33-
public void WindowsAttributeSetting(FileAttributes attr)
32+
[PlatformSpecific(TestPlatforms.Windows)]
33+
public void SettingAttributes_Windows(FileAttributes attributes)
3434
{
3535
string path = CreateItem();
36-
SetAttributes(path, attr);
37-
Assert.Equal(attr, GetAttributes(path));
36+
SetAttributes(path, attributes);
37+
Assert.Equal(attributes, GetAttributes(path));
3838
SetAttributes(path, 0);
3939
}
4040

@@ -44,11 +44,11 @@ public void WindowsAttributeSetting(FileAttributes attr)
4444
[InlineData(FileAttributes.SparseFile)]
4545
[InlineData(FileAttributes.ReparsePoint)]
4646
[InlineData(FileAttributes.Compressed)]
47-
[PlatformSpecific(TestPlatforms.AnyUnix)] // Unix invalid file attributes
48-
public void UnixInvalidAttributes(FileAttributes attr)
47+
[PlatformSpecific(TestPlatforms.AnyUnix)]
48+
public void SettingInvalidAttributes_Unix(FileAttributes attributes)
4949
{
5050
string path = CreateItem();
51-
SetAttributes(path, attr);
51+
SetAttributes(path, attributes);
5252
Assert.Equal(FileAttributes.Normal, GetAttributes(path));
5353
}
5454

@@ -58,11 +58,36 @@ public void UnixInvalidAttributes(FileAttributes attr)
5858
[InlineData(FileAttributes.SparseFile)]
5959
[InlineData(FileAttributes.ReparsePoint)]
6060
[InlineData(FileAttributes.Compressed)]
61-
[PlatformSpecific(TestPlatforms.Windows)] // Invalid Windows file attributes
62-
public void WindowsInvalidAttributes(FileAttributes attr)
61+
[PlatformSpecific(TestPlatforms.Windows)]
62+
public void SettingInvalidAttributes_Windows(FileAttributes attributes)
6363
{
6464
string path = CreateItem();
65-
SetAttributes(path, attr);
65+
SetAttributes(path, attributes);
66+
Assert.Equal(FileAttributes.Normal, GetAttributes(path));
67+
}
68+
69+
[Theory,
70+
InlineData(":bar"),
71+
InlineData(":bar:$DATA")]
72+
[PlatformSpecific(TestPlatforms.Windows)]
73+
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
74+
public void GettingAndSettingAttributes_AlternateDataStream_Windows(string streamName)
75+
{
76+
string path = CreateItem();
77+
streamName = path + streamName;
78+
File.Create(streamName);
79+
80+
FileAttributes attributes = GetAttributes(streamName);
81+
Assert.NotEqual((FileAttributes)0, attributes);
82+
Assert.NotEqual((FileAttributes)(-1), attributes);
83+
84+
// Attributes are shared for the file and all streams
85+
SetAttributes(streamName, FileAttributes.Hidden);
86+
Assert.Equal(FileAttributes.Hidden, GetAttributes(streamName));
87+
Assert.Equal(FileAttributes.Hidden, GetAttributes(path));
88+
89+
SetAttributes(path, FileAttributes.Normal);
90+
Assert.Equal(FileAttributes.Normal, GetAttributes(streamName));
6691
Assert.Equal(FileAttributes.Normal, GetAttributes(path));
6792
}
6893
}

src/System.IO.FileSystem/tests/File/Delete.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System.Diagnostics;
65
using Xunit;
76
using Xunit.NetCore.Extensions;
87

98
namespace System.IO.Tests
109
{
1110
public class File_Delete : FileSystemTest
1211
{
13-
#region Utilities
14-
1512
public virtual void Delete(string path)
1613
{
1714
File.Delete(path);
@@ -24,8 +21,6 @@ public virtual FileInfo Create(string path)
2421
return ret;
2522
}
2623

27-
#endregion
28-
2924
#region UniversalTests
3025

3126
[Fact]
@@ -199,6 +194,24 @@ public void UnixDeleteReadOnlyFile()
199194
Assert.False(testFile.Exists);
200195
}
201196

197+
[Theory,
198+
InlineData(":bar"),
199+
InlineData(":bar:$DATA")]
200+
[PlatformSpecific(TestPlatforms.Windows)]
201+
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
202+
public void WindowsDeleteAlternateDataStream(string streamName)
203+
{
204+
FileInfo testFile = Create(GetTestFilePath());
205+
testFile.Create().Dispose();
206+
streamName = testFile.FullName + streamName;
207+
File.Create(streamName).Dispose();
208+
Assert.True(File.Exists(streamName));
209+
Delete(streamName);
210+
Assert.False(File.Exists(streamName));
211+
testFile.Refresh();
212+
Assert.True(testFile.Exists);
213+
}
214+
202215
#endregion
203216
}
204217
}

src/System.IO.FileSystem/tests/File/GetSetAttributes.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ public void GetAttributes_MissingFile(char trailingChar)
1818
Assert.Throws<FileNotFoundException>(() => GetAttributes(GetTestFilePath() + trailingChar));
1919
}
2020

21+
// Getting only throws for File, not FileInfo
22+
[Theory,
23+
InlineData(":bar"),
24+
InlineData(":bar:$DATA")]
25+
[PlatformSpecific(TestPlatforms.Windows)]
26+
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
27+
public void GetAttributes_MissingAlternateDataStream_Windows(string streamName)
28+
{
29+
string path = CreateItem();
30+
streamName = path + streamName;
31+
32+
Assert.Throws<FileNotFoundException>(() => GetAttributes(streamName));
33+
}
34+
2135
[Theory, MemberData(nameof(TrailingCharacters))]
2236
public void GetAttributes_MissingDirectory(char trailingChar)
2337
{

src/System.IO.FileSystem/tests/File/Move.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using Xunit;
6+
using System.Linq;
67

78
namespace System.IO.Tests
89
{
@@ -348,6 +349,29 @@ public void UnixWhitespacePath(string whitespace)
348349

349350
}
350351

352+
[Theory,
353+
InlineData("", ":bar"),
354+
InlineData("", ":bar:$DATA"),
355+
InlineData("::$DATA", ":bar"),
356+
InlineData("::$DATA", ":bar:$DATA")]
357+
[PlatformSpecific(TestPlatforms.Windows)]
358+
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
359+
public void WindowsAlternateDataStreamMove(string defaultStream, string alternateStream)
360+
{
361+
DirectoryInfo testDirectory = Directory.CreateDirectory(GetTestFilePath());
362+
string testFile = Path.Combine(testDirectory.FullName, GetTestFileName());
363+
string testFileDefaultStream = testFile + defaultStream;
364+
string testFileAlternateStream = testFile + alternateStream;
365+
366+
// Cannot move into an alternate stream
367+
File.WriteAllText(testFileDefaultStream, "Foo");
368+
Assert.Throws<IOException>(() => Move(testFileDefaultStream, testFileAlternateStream));
369+
370+
// Cannot move out of an alternate stream
371+
File.WriteAllText(testFileAlternateStream, "Bar");
372+
string testFile2 = Path.Combine(testDirectory.FullName, GetTestFileName());
373+
Assert.Throws<IOException>(() => Move(testFileAlternateStream, testFile2));
374+
}
351375
#endregion
352376
}
353377
}

src/System.IO.FileSystem/tests/FileInfo/Open.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ protected override FileStream CreateFileStream(string path, FileMode mode)
1313
return new FileInfo(path).Open(mode);
1414
}
1515

16-
[Fact]
16+
[Theory, MemberData(nameof(StreamSpecifiers))]
1717
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "FileInfo.Open(string, filemode) on netfx always uses FileAccess.ReadWrite instead of choosing a FileAccess based on the FileMode. This bug was fixed in netcoreapp.")]
18-
public override void FileModeAppend()
18+
public override void FileModeAppend(string streamSpecifier)
1919
{
20-
using (FileStream fs = CreateFileStream(GetTestFilePath(), FileMode.Append))
20+
using (FileStream fs = CreateFileStream(GetTestFilePath() + streamSpecifier, FileMode.Append))
2121
{
2222
Assert.Equal(false, fs.CanRead);
2323
Assert.Equal(true, fs.CanWrite);
2424
}
2525
}
2626

27-
[Fact]
27+
[Theory, MemberData(nameof(StreamSpecifiers))]
2828
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "FileInfo.Open(string, filemode) on netfx always uses FileAccess.ReadWrite instead of choosing a FileAccess based on the FileMode. This bug was fixed in netcoreapp.")]
29-
public override void FileModeAppendExisting()
29+
public override void FileModeAppendExisting(string streamSpecifier)
3030
{
31-
string fileName = GetTestFilePath();
31+
string fileName = GetTestFilePath() + streamSpecifier;
3232
using (FileStream fs = CreateFileStream(fileName, FileMode.Create))
3333
{
3434
fs.WriteByte(0);

0 commit comments

Comments
 (0)