From 995e97ff69a30ccdf885b4c25f11bbb368eca936 Mon Sep 17 00:00:00 2001 From: Adnan Gulegulzar Date: Sat, 20 Jul 2024 19:51:52 -0400 Subject: [PATCH] fix: updating error handling for non tar files Signed-off-by: Adnan Gulegulzar --- content/oci/readonlyoci_test.go | 13 +++++++++++++ content/oci/testdata/dummyfile | 0 errdef/errors.go | 1 + internal/fs/tarfs/tarfs.go | 7 +++++++ internal/fs/tarfs/tarfs_test.go | 12 ++++++++++++ internal/fs/tarfs/testdata/emptyfile | 0 6 files changed, 33 insertions(+) create mode 100644 content/oci/testdata/dummyfile create mode 100644 internal/fs/tarfs/testdata/emptyfile diff --git a/content/oci/readonlyoci_test.go b/content/oci/readonlyoci_test.go index d7b4fd0ed..eaa030903 100644 --- a/content/oci/readonlyoci_test.go +++ b/content/oci/readonlyoci_test.go @@ -36,6 +36,7 @@ import ( "oras.land/oras-go/v2" "oras.land/oras-go/v2/content" "oras.land/oras-go/v2/content/memory" + "oras.land/oras-go/v2/errdef" "oras.land/oras-go/v2/internal/docker" "oras.land/oras-go/v2/internal/spec" "oras.land/oras-go/v2/registry" @@ -836,3 +837,15 @@ func Test_deleteAnnotationRefName(t *testing.T) { }) } } + +func TestNewFromTar_NonTarFile(t *testing.T) { + + emptyfile := "testdata/emptyfile" + + _, err := NewFromTar(context.Background(), emptyfile) + + if want := errdef.ErrNotTarFile; !errors.Is(err, want) { + t.Errorf("OCI.NewFromTar(%s) error = %v, wantErr %v", emptyfile, err, want) + } + +} diff --git a/content/oci/testdata/dummyfile b/content/oci/testdata/dummyfile new file mode 100644 index 000000000..e69de29bb diff --git a/errdef/errors.go b/errdef/errors.go index 7adb44b17..78e7f8299 100644 --- a/errdef/errors.go +++ b/errdef/errors.go @@ -28,4 +28,5 @@ var ( ErrSizeExceedsLimit = errors.New("size exceeds limit") ErrUnsupported = errors.New("unsupported") ErrUnsupportedVersion = errors.New("unsupported version") + ErrNotTarFile = errors.New("tar file type expected") ) diff --git a/internal/fs/tarfs/tarfs.go b/internal/fs/tarfs/tarfs.go index a7a0a6c14..6d2fc6862 100644 --- a/internal/fs/tarfs/tarfs.go +++ b/internal/fs/tarfs/tarfs.go @@ -48,6 +48,13 @@ func New(path string) (*TarFS, error) { if err != nil { return nil, fmt.Errorf("failed to resolve absolute path for %s: %w", path, err) } + + extension := filepath.Ext(pathAbs) + + if extension != ".tar" { + return nil, errdef.ErrNotTarFile + } + tarfs := &TarFS{ path: pathAbs, entries: make(map[string]*entry), diff --git a/internal/fs/tarfs/tarfs_test.go b/internal/fs/tarfs/tarfs_test.go index 3a67b88c8..1cab2eb1d 100644 --- a/internal/fs/tarfs/tarfs_test.go +++ b/internal/fs/tarfs/tarfs_test.go @@ -268,3 +268,15 @@ func TestTarFS_Stat_Unsupported(t *testing.T) { } } } + +func TestTarFS_New_NonTarFile(t *testing.T) { + + emptyfile := "testdata/emptyfile" + + _, err := New(emptyfile) + + if want := errdef.ErrNotTarFile; !errors.Is(err, want) { + t.Errorf("TarFS.New(%s) error = %v, wantErr %v", emptyfile, err, want) + } + +} diff --git a/internal/fs/tarfs/testdata/emptyfile b/internal/fs/tarfs/testdata/emptyfile new file mode 100644 index 000000000..e69de29bb