Skip to content

Commit

Permalink
Add integration tests for unsupported directories
Browse files Browse the repository at this point in the history
This adds tests for unsupported directories (
directories which are either named '', or '.'
or '..', or have '//', '/./', '/../' in their
total path in their bucket.
  • Loading branch information
gargnitingoogle committed May 6, 2024
1 parent ef0b1fe commit 02488f1
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
15 changes: 11 additions & 4 deletions tools/integration_tests/implicit_dir/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,16 @@ func TestListImplicitObjectsFromBucket(t *testing.T) {
// testBucket/dirForImplicitDirTests/explicitFile -- File
// testBucket/dirForImplicitDirTests/explicitDirectory/fileInExplicitDir1 -- File
// testBucket/dirForImplicitDirTests/explicitDirectory/fileInExplicitDir2 -- File
// testBucket/dirForImplicitDirTests/.. -- Dir
// testBucket/dirForImplicitDirTests/../fileInUnsupportedImplicitDir1 -- File
// testBucket/dirForImplicitDirTests/. -- Dir
// testBucket/dirForImplicitDirTests/./fileInUnsupportedImplicitDir2 -- File
// testBucket/dirForImplicitDirTests// -- Dir
// testBucket/dirForImplicitDirTests//fileInUnsupportedImplicitDir3 -- File

implicit_and_explicit_dir_setup.CreateImplicitDirectoryStructure(DirForImplicitDirTests)
implicit_and_explicit_dir_setup.CreateExplicitDirectoryStructure(DirForImplicitDirTests, t)
implicit_and_explicit_dir_setup.CreateUnsupportedImplicitDirectoryStructure(DirForImplicitDirTests)

err := filepath.WalkDir(testDir, func(path string, dir fs.DirEntry, err error) error {
if err != nil {
Expand All @@ -61,21 +68,21 @@ func TestListImplicitObjectsFromBucket(t *testing.T) {
if path == testDir {
// numberOfObjects - 3
if len(objs) != implicit_and_explicit_dir_setup.NumberOfTotalObjects {
t.Errorf("Incorrect number of objects in the bucket.")
t.Errorf("Incorrect number of objects in the bucket. Expected: %v, Actual: %v", implicit_and_explicit_dir_setup.NumberOfTotalObjects, len(objs))
}

// testBucket/dirForImplicitDirTests/explicitDir -- Dir
if objs[0].Name() != implicit_and_explicit_dir_setup.ExplicitDirectory || objs[0].IsDir() != true {
t.Errorf("Listed incorrect object")
t.Errorf("Listed incorrect object #0. Expected: %v, Actual: %v", implicit_and_explicit_dir_setup.ExplicitDirectory, objs[0].Name())
}
// testBucket/dirForImplicitDirTests/explicitFile -- File
if objs[1].Name() != implicit_and_explicit_dir_setup.ExplicitFile || objs[1].IsDir() != false {
t.Errorf("Listed incorrect object")
t.Errorf("Listed incorrect object #1. Expected: %v, Actual: %v", implicit_and_explicit_dir_setup.ExplicitFile, objs[1].Name())
}

// testBucket/dirForImplicitDirTests/implicitDir -- Dir
if objs[2].Name() != implicit_and_explicit_dir_setup.ImplicitDirectory || objs[2].IsDir() != true {
t.Errorf("Listed incorrect object")
t.Errorf("Listed incorrect object #3. Expected: %v, Actual: %v", implicit_and_explicit_dir_setup.ImplicitDirectory, objs[2].Name())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ const FirstFileInExplicitDirectory = "fileInExplicitDir1"
const SecondFileInExplicitDirectory = "fileInExplicitDir2"
const FileInImplicitDirectory = "fileInImplicitDir1"
const FileInImplicitSubDirectory = "fileInImplicitDir2"
const UnsupportedImplicitDirectory1 = ".."
const FileInUnsupportedImplicitDirectory1 = "fileInUnsupportedImplicitDir1"
const UnsupportedImplicitDirectory2 = "."
const FileInUnsupportedImplicitDirectory2 = "fileInUnsupportedImplicitDir2"
const UnsupportedImplicitDirectory3 = "/"
const FileInUnsupportedImplicitDirectory3 = "fileInUnsupportedImplicitDir3"

func RunTestsForImplicitDirAndExplicitDir(flags [][]string, m *testing.M) int {
setup.ExitWithFailureIfBothTestBucketAndMountedDirectoryFlagsAreNotSet()
Expand Down Expand Up @@ -83,6 +89,19 @@ func CreateImplicitDirectoryStructure(testDir string) {
setup.RunScriptForTestData("../util/setup/implicit_and_explicit_dir_setup/testdata/create_objects.sh", path.Join(setup.TestBucket(), testDir))
}

func CreateUnsupportedImplicitDirectoryStructure(testDir string) {
// Implicit Directory Structure
// testBucket/testDir/../ -- Dir
// testBucket/testDir/../fileInUnsupportedImplicitDir1 -- File
// testBucket/testDir/. -- Dir
// testBucket/testDir/./fileInUnsupportedImplicitDir2 -- File
// testBucket/testDir// -- Dir
// testBucket/testDir//fileInUnsupportedImplicitDir3 -- File

// Create implicit directory in bucket for testing.
setup.RunScriptForTestData("../util/setup/implicit_and_explicit_dir_setup/testdata/create_objects_in_unsupported_directories.sh", path.Join(setup.TestBucket(), testDir))
}

func CreateExplicitDirectoryStructure(testDir string, t *testing.T) {
// Explicit Directory structure
// testBucket/testDir/explicitDirectory -- Dir
Expand Down Expand Up @@ -116,3 +135,20 @@ func CreateImplicitDirectoryInExplicitDirectoryStructure(testDir string, t *test
dirPathInBucket := path.Join(setup.TestBucket(), testDir, ExplicitDirectory)
setup.RunScriptForTestData("../util/setup/implicit_and_explicit_dir_setup/testdata/create_objects.sh", dirPathInBucket)
}

func CreateUnsupportedImplicitDirectoryInExplicitDirectoryStructure(testDir string, t *testing.T) {
// testBucket/testDir/explicitDirectory -- Dir
// testBucket/testDir/explictFile -- File
// testBucket/testDir/explicitDirectory/fileInExplicitDir1 -- File
// testBucket/testDir/explicitDirectory/fileInExplicitDir2 -- File
// testBucket/testDir/explicitDirectory/.. -- Dir
// testBucket/testDir/explicitDirectory/../fileInUnsupportedImplicitDir1 -- File
// testBucket/testDir/explicitDirectory/. -- Dir
// testBucket/testDir/explicitDirectory/./fileInUnsupportedImplicitDir2 -- File
// testBucket/testDir/explicitDirectory// -- Dir
// testBucket/testDir/explicitDirectory//fileInUnsupportedImplicitDir3 -- File

CreateExplicitDirectoryStructure(testDir, t)
dirPathInBucket := path.Join(setup.TestBucket(), testDir, ExplicitDirectory)
setup.RunScriptForTestData("../util/setup/implicit_and_explicit_dir_setup/testdata/create_objects_in_unsupported_directories.sh", dirPathInBucket)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2023 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Here $1 refers to the testBucket/testdir argument
echo "This is from directory .. file fileInUnsupportedImplicitDir1" > fileInUnsupportedImplicitDir1
# bucket/testdir/../fileInImplicitDir1
gcloud storage cp fileInUnsupportedImplicitDir1 gs://$1/../
echo "This is from directory . file fileInUnsupportedImplicitDir2" > fileInUnsupportedImplicitDir2
# bucket/testdir/./fileInImplicitDir2
gcloud storage cp fileInUnsupportedImplicitDir2 gs://$1/./
echo "This is from directory \"\" file fileInUnsupportedImplicitDir3" > fileInUnsupportedImplicitDir3
# bucket/testdir//fileInImplicitDir3
gcloud storage cp fileInUnsupportedImplicitDir3 gs://$1//fileInUnsupportedImplicitDir3

0 comments on commit 02488f1

Please sign in to comment.