-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test for unix and file operations
- Loading branch information
1 parent
f4e90e4
commit db209b0
Showing
3 changed files
with
160 additions
and
21 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
Snowflake.Data.Tests/UnitTests/Tools/FileOperationsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright (c) 2024 Snowflake Computing Inc. All rights reserved. | ||
*/ | ||
|
||
|
||
namespace Snowflake.Data.Tests.Tools | ||
{ | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
using Mono.Unix; | ||
using Mono.Unix.Native; | ||
using NUnit.Framework; | ||
using Snowflake.Data.Core.Tools; | ||
using static Snowflake.Data.Tests.UnitTests.Configuration.EasyLoggingConfigGenerator; | ||
using System.Security; | ||
|
||
[TestFixture, NonParallelizable] | ||
public class FileOperationsTest | ||
{ | ||
private static FileOperations s_fileOperations; | ||
private static readonly string s_workingDirectory = Path.Combine(Path.GetTempPath(), "file_operations_test_", Path.GetRandomFileName()); | ||
|
||
[OneTimeSetUp] | ||
public static void BeforeAll() | ||
{ | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
return; | ||
if (!Directory.Exists(s_workingDirectory)) | ||
{ | ||
Directory.CreateDirectory(s_workingDirectory); | ||
} | ||
|
||
s_fileOperations = new FileOperations(); | ||
} | ||
|
||
[OneTimeTearDown] | ||
public static void AfterAll() | ||
{ | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
return; | ||
Directory.Delete(s_workingDirectory, true); | ||
} | ||
|
||
[Test] | ||
public void TestReadAllTextOnWindows() | ||
{ | ||
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
Assert.Ignore("skip test only runs on Windows"); | ||
} | ||
|
||
var content = "random text"; | ||
var filePath = CreateConfigTempFile(s_workingDirectory, content); | ||
using (StreamWriter outputFile = new StreamWriter(filePath, true)) | ||
{ | ||
outputFile.WriteLine(content); | ||
} | ||
|
||
// act | ||
var result = s_fileOperations.ReadAllText(filePath); | ||
|
||
// assert | ||
Assert.AreEqual(content, result); | ||
} | ||
|
||
[Test] | ||
public void TestReadAllTextCheckingPermissions() | ||
{ | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
Assert.Ignore("skip test on Windows"); | ||
} | ||
|
||
var content = "random text"; | ||
var filePath = CreateConfigTempFile(s_workingDirectory, content); | ||
var filePermissions = FileAccessPermissions.UserReadWriteExecute; | ||
Syscall.chmod(filePath, (FilePermissions)filePermissions); | ||
|
||
// act | ||
var result = s_fileOperations.ReadAllText(filePath); | ||
|
||
// assert | ||
Assert.AreEqual(content, result); | ||
} | ||
|
||
[Test] | ||
public void TestShouldThrowExceptionIfOtherPermissionsIsSetWhenReadAllText() | ||
{ | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
Assert.Ignore("skip test on Windows"); | ||
} | ||
|
||
var content = "random text"; | ||
var filePath = CreateConfigTempFile(s_workingDirectory, content); | ||
var filePermissions = FileAccessPermissions.UserReadWriteExecute | FileAccessPermissions.OtherReadWriteExecute; | ||
Syscall.chmod(filePath, (FilePermissions)filePermissions); | ||
|
||
// act and assert | ||
Assert.Throws<SecurityException>(() => s_fileOperations.ReadAllText(filePath), | ||
"Attempting to read a file with too broad permissions assigned"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters