Skip to content

Commit

Permalink
Added test for unix and file operations
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jmartinezramirez committed Jul 24, 2024
1 parent f4e90e4 commit b5762c5
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 21 deletions.
96 changes: 96 additions & 0 deletions Snowflake.Data.Tests/UnitTests/Tools/FileOperationsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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 (!Directory.Exists(s_workingDirectory))
{
Directory.CreateDirectory(s_workingDirectory);
}

s_fileOperations = new FileOperations();
}

[OneTimeTearDown]
public static void AfterAll()
{
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);

// 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");
}
}
}
58 changes: 47 additions & 11 deletions Snowflake.Data.Tests/UnitTests/Tools/UnixOperationsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

namespace Snowflake.Data.Tests.Tools
{
using System.Security;

[TestFixture, NonParallelizable]
public class UnixOperationsTest
{
private static UnixOperations s_unixOperations;
private static readonly string s_workingDirectory = Path.Combine(Path.GetTempPath(), "easy_logging_test_configs_", Path.GetRandomFileName());

[OneTimeSetUp]
public static void BeforeAll()
{
Expand All @@ -34,7 +36,7 @@ public static void AfterAll()
return;
Directory.Delete(s_workingDirectory, true);
}

[Test]
public void TestDetectGroupOrOthersWritablePermissions(
[ValueSource(nameof(GroupOrOthersWritablePermissions))] FilePermissions groupOrOthersWritablePermissions,
Expand All @@ -45,42 +47,76 @@ public void TestDetectGroupOrOthersWritablePermissions(
{
Assert.Ignore("skip test on Windows");
}

// arrange
var filePath = CreateConfigTempFile(s_workingDirectory, "random text");
var readWriteUserPermissions = FilePermissions.S_IRUSR | FilePermissions.S_IWUSR;
var filePermissions = readWriteUserPermissions | groupOrOthersWritablePermissions | groupNotWritablePermissions | otherNotWritablePermissions;
Syscall.chmod(filePath, filePermissions);

// act
var result = s_unixOperations.CheckFileHasAnyOfPermissions(filePath, FileAccessPermissions.GroupWrite | FileAccessPermissions.OtherWrite);

// assert
Assert.IsTrue(result);
}

[Test]
public void TestDetectGroupOrOthersNotWritablePermissions(
[ValueSource(nameof(UserPermissions))] FilePermissions userPermissions,
[ValueSource(nameof(UserPermissions))] FilePermissions userPermissions,
[ValueSource(nameof(GroupNotWritablePermissions))] FilePermissions groupNotWritablePermissions,
[ValueSource(nameof(OtherNotWritablePermissions))] FilePermissions otherNotWritablePermissions)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Assert.Ignore("skip test on Windows");
}

var filePath = CreateConfigTempFile(s_workingDirectory, "random text");
var filePermissions = userPermissions | groupNotWritablePermissions | otherNotWritablePermissions;
Syscall.chmod(filePath, filePermissions);

// act
var result = s_unixOperations.CheckFileHasAnyOfPermissions(filePath, FileAccessPermissions.GroupWrite | FileAccessPermissions.OtherWrite);

// assert
Assert.IsFalse(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_unixOperations.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_unixOperations.ReadAllText(filePath), "Attempting to read a file with too broad permissions assigned");
}

public static IEnumerable<FilePermissions> UserPermissions()
{
Expand All @@ -89,14 +125,14 @@ public static IEnumerable<FilePermissions> UserPermissions()
yield return FilePermissions.S_IXUSR;
yield return FilePermissions.S_IRUSR | FilePermissions.S_IWUSR | FilePermissions.S_IXUSR;
}

public static IEnumerable<FilePermissions> GroupOrOthersWritablePermissions()
{
yield return FilePermissions.S_IWGRP;
yield return FilePermissions.S_IWOTH;
yield return FilePermissions.S_IWGRP | FilePermissions.S_IWOTH;
}

public static IEnumerable<FilePermissions> GroupNotWritablePermissions()
{
yield return 0;
Expand Down
19 changes: 9 additions & 10 deletions Snowflake.Data/Client/SnowflakeDbConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@
* Copyright (c) 2012-2021 Snowflake Computing Inc. All rights reserved.
*/

using System;
using System.Data.Common;
using Snowflake.Data.Core;
using System.Security;
using System.Threading.Tasks;
using System.Data;
using System.Threading;
using Snowflake.Data.Log;

namespace Snowflake.Data.Client
{
using Core.Tools;
using System;
using System.Data.Common;
using Snowflake.Data.Core;
using System.Security;
using System.Threading.Tasks;
using System.Data;
using System.Threading;
using Snowflake.Data.Log;

[System.ComponentModel.DesignerCategory("Code")]
public class SnowflakeDbConnection : DbConnection
Expand Down Expand Up @@ -320,6 +318,7 @@ public override Task OpenAsync(CancellationToken cancellationToken)
}
registerConnectionCancellationCallback(cancellationToken);
OnSessionConnecting();
FillConnectionStringFromTomlConfigIfNotSet();
return SnowflakeDbConnectionPool
.GetSessionAsync(ConnectionString, Password, cancellationToken)
.ContinueWith(previousTask =>
Expand Down

0 comments on commit b5762c5

Please sign in to comment.