Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azfile: Trailing dot support #21355

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/storage/azfile/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* Copy File
* Added `x-ms-file-last-write-time` request header in Put Range and Put Range from URL.
* Updated the SAS Version to `2022-11-02` and added `Encryption Scope` to Account SAS.
* Trailing dot support for files and directories.

### Breaking Changes

Expand Down
2 changes: 1 addition & 1 deletion sdk/storage/azfile/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "go",
"TagPrefix": "go/storage/azfile",
"Tag": "go/storage/azfile_7cda641151"
"Tag": "go/storage/azfile_7a34f14827"
}
295 changes: 295 additions & 0 deletions sdk/storage/azfile/directory/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/internal/testcommon"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/lease"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/sas"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/service"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/share"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -1893,3 +1895,296 @@ func (d *DirectoryUnrecordedTestsSuite) TestDirectoryRenameUsingSAS() {
_, err = destDirCl.GetProperties(context.Background(), nil)
_require.NoError(err)
}

func (d *DirectoryRecordedTestsSuite) TestDirectoryCreateDeleteTrailingDot() {
_require := require.New(d.T())
testName := d.T().Name()

options := &service.ClientOptions{AllowTrailingDot: to.Ptr(true)}
svcClient, err := testcommon.GetServiceClient(d.T(), testcommon.TestAccountDefault, options)
_require.NoError(err)

shareClient := testcommon.CreateNewShare(context.Background(), _require, testcommon.GenerateShareName(testName), svcClient)
defer testcommon.DeleteShare(context.Background(), _require, shareClient)

dirName := "dir...."
dirClient := testcommon.CreateNewDirectory(context.Background(), _require, dirName, shareClient)

_, err = dirClient.GetProperties(context.Background(), nil)
_require.NoError(err)

_, err = dirClient.Delete(context.Background(), nil)
_require.NoError(err)

_, err = dirClient.GetProperties(context.Background(), nil)
_require.Error(err)
testcommon.ValidateFileErrorCode(_require, err, fileerror.ResourceNotFound)
}

func (d *DirectoryRecordedTestsSuite) TestDirectorySetPropertiesTrailingDotAndOAuth() {
_require := require.New(d.T())
testName := d.T().Name()

accountName, _ := testcommon.GetGenericAccountInfo(testcommon.TestAccountDefault)
_require.Greater(len(accountName), 0)

cred, err := testcommon.GetGenericTokenCredential()
_require.NoError(err)

svcClient, err := testcommon.GetServiceClient(d.T(), testcommon.TestAccountDefault, nil)
_require.NoError(err)

shareName := testcommon.GenerateShareName(testName)
shareClient := testcommon.CreateNewShare(context.Background(), _require, shareName, svcClient)
defer testcommon.DeleteShare(context.Background(), _require, shareClient)

dirName := "dir.."
dirURL := "https://" + accountName + ".file.core.windows.net/" + shareName + "/" + dirName

options := &directory.ClientOptions{
FileRequestIntent: to.Ptr(directory.ShareTokenIntentBackup),
AllowTrailingDot: to.Ptr(true),
}
testcommon.SetClientOptions(d.T(), &options.ClientOptions)
dirClient, err := directory.NewClient(dirURL, cred, options)
_require.NoError(err)

cResp, err := dirClient.Create(context.Background(), nil)
_require.NoError(err)
_require.NotNil(cResp.FilePermissionKey)

currTime, err := time.Parse(time.UnixDate, "Fri Mar 31 21:00:00 GMT 2023")
_require.NoError(err)
creationTime := currTime.Add(5 * time.Minute).Round(time.Microsecond)
lastWriteTime := currTime.Add(10 * time.Minute).Round(time.Millisecond)
changeTime := currTime.Add(15 * time.Minute).Round(time.Millisecond)

// Set the custom permissions
sResp, err := dirClient.SetProperties(context.Background(), &directory.SetPropertiesOptions{
FileSMBProperties: &file.SMBProperties{
Attributes: &file.NTFSFileAttributes{
ReadOnly: true,
System: true,
},
CreationTime: &creationTime,
LastWriteTime: &lastWriteTime,
ChangeTime: &changeTime,
},
FilePermissions: &file.Permissions{
Permission: &testcommon.SampleSDDL,
},
})
_require.NoError(err)
_require.NotNil(sResp.FileCreationTime)
_require.NotNil(sResp.FileLastWriteTime)
_require.NotNil(sResp.FileChangeTime)
_require.NotNil(sResp.FilePermissionKey)
_require.NotEqual(*sResp.FilePermissionKey, *cResp.FilePermissionKey)
_require.Equal(*sResp.FileCreationTime, creationTime.UTC())
_require.Equal(*sResp.FileLastWriteTime, lastWriteTime.UTC())
_require.Equal(*sResp.FileChangeTime, changeTime.UTC())

fileAttributes, err := file.ParseNTFSFileAttributes(sResp.FileAttributes)
_require.NoError(err)
_require.NotNil(fileAttributes)
_require.True(fileAttributes.ReadOnly)
_require.True(fileAttributes.System)
_require.True(fileAttributes.Directory)

gResp, err := dirClient.GetProperties(context.Background(), nil)
_require.NoError(err)
_require.NotNil(gResp.FileCreationTime)
_require.NotNil(gResp.FileLastWriteTime)
_require.NotNil(gResp.FileChangeTime)
_require.NotNil(gResp.FilePermissionKey)
_require.Equal(*gResp.FilePermissionKey, *sResp.FilePermissionKey)
_require.Equal(*gResp.FileCreationTime, *sResp.FileCreationTime)
_require.Equal(*gResp.FileLastWriteTime, *sResp.FileLastWriteTime)
_require.Equal(*gResp.FileChangeTime, *sResp.FileChangeTime)
_require.Equal(*gResp.FileAttributes, *sResp.FileAttributes)

fileAttributes2, err := file.ParseNTFSFileAttributes(gResp.FileAttributes)
_require.NoError(err)
_require.NotNil(fileAttributes2)
_require.True(fileAttributes2.ReadOnly)
_require.True(fileAttributes2.System)
_require.True(fileAttributes2.Directory)
_require.EqualValues(fileAttributes2, fileAttributes)
}

func (d *DirectoryRecordedTestsSuite) TestDirectorySetMetadataTrailingDot() {
_require := require.New(d.T())
testName := d.T().Name()

options := &service.ClientOptions{AllowTrailingDot: to.Ptr(true)}
svcClient, err := testcommon.GetServiceClient(d.T(), testcommon.TestAccountDefault, options)
_require.NoError(err)

shareClient := testcommon.CreateNewShare(context.Background(), _require, testcommon.GenerateShareName(testName), svcClient)
defer testcommon.DeleteShare(context.Background(), _require, shareClient)

dirName := "dir.."
dirClient := testcommon.CreateNewDirectory(context.Background(), _require, dirName, shareClient)

md := map[string]*string{
"Foo": to.Ptr("FooValuE"),
"Bar": to.Ptr("bArvaLue"),
}

_, err = dirClient.SetMetadata(context.Background(), &directory.SetMetadataOptions{
Metadata: md,
})
_require.NoError(err)

gResp, err := dirClient.GetProperties(context.Background(), nil)
_require.NoError(err)
_require.EqualValues(gResp.Metadata, md)
}

func (d *DirectoryRecordedTestsSuite) TestDirectoryListTrailingDot() {
_require := require.New(d.T())
testName := d.T().Name()

options := &service.ClientOptions{AllowTrailingDot: to.Ptr(true)}
svcClient, err := testcommon.GetServiceClient(d.T(), testcommon.TestAccountDefault, options)
_require.NoError(err)

shareClient := testcommon.CreateNewShare(context.Background(), _require, testcommon.GenerateShareName(testName), svcClient)
defer testcommon.DeleteShare(context.Background(), _require, shareClient)

dirName := testcommon.GenerateDirectoryName(testName) + "."
dirClient := testcommon.CreateNewDirectory(context.Background(), _require, dirName, shareClient)
_require.NoError(err)

// create directories
for i := 0; i < 10; i++ {
subDirName := fmt.Sprintf("dir %v", i)
if i%2 == 0 {
subDirName += ".."
}
subDirClient := dirClient.NewSubdirectoryClient(subDirName)
_, err = subDirClient.Create(context.Background(), nil)
_require.NoError(err)
}

// create files
for i := 0; i < 5; i++ {
fName := fmt.Sprintf("file %v", i)
if i%2 == 0 {
fName += ".."
}
fileClient := dirClient.NewFileClient(fName)
_, err = fileClient.Create(context.Background(), 2048, nil)
_require.NoError(err)
}

subDirCtr, subDirDotCtr, fileCtr, fileDotCtr := 0, 0, 0, 0
pager := dirClient.NewListFilesAndDirectoriesPager(nil)
for pager.More() {
resp, err := pager.NextPage(context.Background())
_require.NoError(err)
subDirCtr += len(resp.Segment.Directories)
fileCtr += len(resp.Segment.Files)
for _, dir := range resp.Segment.Directories {
_require.NotNil(dir.Name)
_require.Greater(len(*dir.Name), 0)
if strings.HasSuffix(*dir.Name, "..") {
subDirDotCtr++
}
}
for _, f := range resp.Segment.Files {
_require.NotNil(f.Name)
_require.Greater(len(*f.Name), 0)
if strings.HasSuffix(*f.Name, "..") {
fileDotCtr++
}
}
}
_require.Equal(subDirCtr, 10)
_require.Equal(subDirDotCtr, 5)
_require.Equal(fileCtr, 5)
_require.Equal(fileDotCtr, 3)
}

func (d *DirectoryRecordedTestsSuite) TestDirectoryRenameNegativeSourceTrailingDot() {
_require := require.New(d.T())
testName := d.T().Name()

options := &service.ClientOptions{
AllowTrailingDot: to.Ptr(true),
}
svcClient, err := testcommon.GetServiceClient(d.T(), testcommon.TestAccountDefault, options)
_require.NoError(err)

shareName := testcommon.GenerateShareName(testName)
shareClient := testcommon.CreateNewShare(context.Background(), _require, shareName, svcClient)
defer testcommon.DeleteShare(context.Background(), _require, shareClient)

dirName := testcommon.GenerateDirectoryName(testName) + ".."
dirClient := testcommon.CreateNewDirectory(context.Background(), _require, dirName, shareClient)

_, err = dirClient.Rename(context.Background(), "dir1..", nil)
_require.Error(err)
testcommon.ValidateFileErrorCode(_require, err, fileerror.ResourceNotFound)
}

func (d *DirectoryRecordedTestsSuite) TestDirectoryRenameSourceTrailingDotAndOAuth() {
_require := require.New(d.T())
testName := d.T().Name()

accountName, _ := testcommon.GetGenericAccountInfo(testcommon.TestAccountDefault)
_require.Greater(len(accountName), 0)

cred, err := testcommon.GetGenericTokenCredential()
_require.NoError(err)

svcClient, err := testcommon.GetServiceClient(d.T(), testcommon.TestAccountDefault, nil)
_require.NoError(err)

shareName := testcommon.GenerateShareName(testName)
shareClient := testcommon.CreateNewShare(context.Background(), _require, shareName, svcClient)
defer testcommon.DeleteShare(context.Background(), _require, shareClient)

dirName := testcommon.GenerateDirectoryName(testName) + ".."
dirURL := "https://" + accountName + ".file.core.windows.net/" + shareName + "/" + dirName

options := &directory.ClientOptions{
FileRequestIntent: to.Ptr(directory.ShareTokenIntentBackup),
AllowTrailingDot: to.Ptr(true),
AllowSourceTrailingDot: to.Ptr(true),
}
testcommon.SetClientOptions(d.T(), &options.ClientOptions)
dirClient, err := directory.NewClient(dirURL, cred, options)
_require.NoError(err)
_, err = dirClient.Create(context.Background(), nil)
_require.NoError(err)

fileName := "file.."
fClient := dirClient.NewFileClient(fileName)
_, err = fClient.Create(context.Background(), 2048, nil)
_require.NoError(err)

resp, err := dirClient.Rename(context.Background(), "dir1..", nil)
_require.NoError(err)
_require.Equal(resp.LastModified.IsZero(), false)
_require.Equal(resp.FileCreationTime.IsZero(), false)
_require.Equal(resp.FileLastWriteTime.IsZero(), false)
_require.Equal(resp.FileChangeTime.IsZero(), false)
_require.NotNil(resp.Client)

_, err = dirClient.GetProperties(context.Background(), nil)
_require.Error(err)
testcommon.ValidateFileErrorCode(_require, err, fileerror.ResourceNotFound)

_, err = fClient.GetProperties(context.Background(), nil)
_require.Error(err)
testcommon.ValidateFileErrorCode(_require, err, fileerror.ParentNotFound)

newDirClient := resp.Client
_, err = newDirClient.GetProperties(context.Background(), nil)
_require.NoError(err)

newFileClient := newDirClient.NewFileClient(fileName)
_, err = newFileClient.GetProperties(context.Background(), nil)
_require.NoError(err)
}
Loading