...

Source file src/oras.land/oras-go/pkg/oras/opts_test.go

Documentation: oras.land/oras-go/pkg/oras

     1  /*
     2  Copyright The ORAS Authors.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7  http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package oras
    17  
    18  import (
    19  	"testing"
    20  
    21  	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
    22  	"github.com/stretchr/testify/suite"
    23  )
    24  
    25  type PushOptsSuite struct {
    26  	suite.Suite
    27  }
    28  
    29  func (suite *PushOptsSuite) TestValidateNameAsPath() {
    30  	var err error
    31  
    32  	// valid path
    33  	err = ValidateNameAsPath(descFromName("hello.txt"))
    34  	suite.NoError(err, "valid path")
    35  	err = ValidateNameAsPath(descFromName("foo/bar"))
    36  	suite.NoError(err, "valid path with multiple sub-directories")
    37  
    38  	// no empty name
    39  	err = ValidateNameAsPath(descFromName(""))
    40  	suite.Error(err, "empty path")
    41  
    42  	// path should be clean
    43  	err = ValidateNameAsPath(descFromName("./hello.txt"))
    44  	suite.Error(err, "dirty path")
    45  	err = ValidateNameAsPath(descFromName("foo/../bar"))
    46  	suite.Error(err, "dirty path")
    47  
    48  	// path should be slash-separated
    49  	err = ValidateNameAsPath(descFromName("foo\\bar"))
    50  	suite.Error(err, "path not slash separated")
    51  
    52  	// disallow absolute path
    53  	err = ValidateNameAsPath(descFromName("/foo/bar"))
    54  	suite.Error(err, "unix: absolute path disallowed")
    55  	err = ValidateNameAsPath(descFromName("C:\\foo\\bar"))
    56  	suite.Error(err, "windows: absolute path disallowed")
    57  	err = ValidateNameAsPath(descFromName("C:/foo/bar"))
    58  	suite.Error(err, "windows: absolute path disallowed")
    59  
    60  	// disallow path traversal
    61  	err = ValidateNameAsPath(descFromName(".."))
    62  	suite.Error(err, "path traversal disallowed")
    63  	err = ValidateNameAsPath(descFromName("../bar"))
    64  	suite.Error(err, "path traversal disallowed")
    65  	err = ValidateNameAsPath(descFromName("foo/../../bar"))
    66  	suite.Error(err, "path traversal disallowed")
    67  }
    68  
    69  func TestPushOptsSuite(t *testing.T) {
    70  	suite.Run(t, new(PushOptsSuite))
    71  }
    72  
    73  func descFromName(name string) ocispec.Descriptor {
    74  	return ocispec.Descriptor{
    75  		Annotations: map[string]string{
    76  			ocispec.AnnotationTitle: name,
    77  		},
    78  	}
    79  }
    80  

View as plain text