From 185c218affb4b0f70a5d4b8f92bade9225a9df48 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Mon, 31 Jan 2022 15:49:27 +0100 Subject: [PATCH 1/2] add missing test for scenario when destination file is larger than source file (it needs to be truncated) --- .../System.IO.FileSystem/tests/File/Copy.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/libraries/System.IO.FileSystem/tests/File/Copy.cs b/src/libraries/System.IO.FileSystem/tests/File/Copy.cs index 643d2a98cf22a2..d1dfd3fd2b969c 100644 --- a/src/libraries/System.IO.FileSystem/tests/File/Copy.cs +++ b/src/libraries/System.IO.FileSystem/tests/File/Copy.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.Linq; +using System.Security.Cryptography; +using Microsoft.Win32.SafeHandles; using Xunit; namespace System.IO.Tests @@ -350,6 +352,22 @@ public void WindowsAlternateDataStreamOverwrite(string defaultStream, string alt Assert.Throws(() => Copy(testFileAlternateStream, testFile2, overwrite: true)); Assert.Throws(() => Copy(testFileAlternateStream, testFile2 + alternateStream, overwrite: true)); } + + [Fact] + public void DestinationFileIsTruncatedWhenItsLargerThanSourceFile() + { + string sourcePath = GetTestFilePath(); + string destPath = GetTestFilePath(); + + const int SourceFileSize = 1000; + File.WriteAllBytes(sourcePath, RandomNumberGenerator.GetBytes(SourceFileSize)); + File.WriteAllBytes(destPath, RandomNumberGenerator.GetBytes(SourceFileSize * 2)); + + Copy(sourcePath, destPath, overwrite: true); + + using SafeFileHandle destHandle = File.OpenHandle(destPath, FileMode.Open); + Assert.Equal(SourceFileSize, RandomAccess.GetLength(destHandle)); + } } /// From 36c967a02fd6d897d59d147b700453665ba6e702 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Mon, 31 Jan 2022 18:10:04 +0100 Subject: [PATCH 2/2] address code review feedback: check the file content as well --- src/libraries/System.IO.FileSystem/tests/File/Copy.cs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/libraries/System.IO.FileSystem/tests/File/Copy.cs b/src/libraries/System.IO.FileSystem/tests/File/Copy.cs index d1dfd3fd2b969c..8b1f1c2d4e78cd 100644 --- a/src/libraries/System.IO.FileSystem/tests/File/Copy.cs +++ b/src/libraries/System.IO.FileSystem/tests/File/Copy.cs @@ -4,7 +4,6 @@ using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; -using Microsoft.Win32.SafeHandles; using Xunit; namespace System.IO.Tests @@ -359,14 +358,13 @@ public void DestinationFileIsTruncatedWhenItsLargerThanSourceFile() string sourcePath = GetTestFilePath(); string destPath = GetTestFilePath(); - const int SourceFileSize = 1000; - File.WriteAllBytes(sourcePath, RandomNumberGenerator.GetBytes(SourceFileSize)); - File.WriteAllBytes(destPath, RandomNumberGenerator.GetBytes(SourceFileSize * 2)); + byte[] content = RandomNumberGenerator.GetBytes(1000); + File.WriteAllBytes(sourcePath, content); + File.WriteAllBytes(destPath, RandomNumberGenerator.GetBytes(content.Length * 2)); Copy(sourcePath, destPath, overwrite: true); - using SafeFileHandle destHandle = File.OpenHandle(destPath, FileMode.Open); - Assert.Equal(SourceFileSize, RandomAccess.GetLength(destHandle)); + Assert.Equal(content, File.ReadAllBytes(destPath)); } }