github.com/opencontainers/umoci@v0.4.8-0.20240508124516-656e4836fb0d/oci/casext/refname_test.go (about)

     1  /*
     2   * umoci: Umoci Modifies Open Containers' Images
     3   * Copyright (C) 2016-2020 SUSE LLC
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *    http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package casext
    19  
    20  import (
    21  	"testing"
    22  )
    23  
    24  func TestValidateRefname(t *testing.T) {
    25  	for _, test := range []struct {
    26  		refname string
    27  		valid   bool
    28  	}{
    29  		// No characters.
    30  		{"", false},
    31  		// Component "/" without next component.
    32  		{"somepath392/", false},
    33  		// Duplicate "/".
    34  		{"some//test/hello", false},
    35  		{"some/oth3r//teST", false},
    36  		// Separator without additional alphanum.
    37  		{"deadb33fc4f3+123888+", false},
    38  		// More than one separator.
    39  		{"anot++her", false},
    40  		{"dead-.meme/cafe", false},
    41  		// Leading separator or "/".
    42  		{"/a/b/c123", false},
    43  		{"--21js8CAS", false},
    44  		{".AZ94n18s", false},
    45  		{"@2318s88", false},
    46  		{":29a.158/2131--91ab", false},
    47  		// Plain components.
    48  		{"a", true},
    49  		{"latest", true},
    50  		{"42.03.19", true},
    51  		{"v1.3.1+dev", true},
    52  		{"aBC1958NaK284IT9Q0kX82jnMnis8201j", true},
    53  		{"Aa0Bb1Cc2-Dd3Ee4Ff5.Gg6Hh7Ii8:Jj9KkLl@MmNnOo+QqRrSs--TtUuVv.WwXxYy_Zz", true},
    54  		{"A--2.C+9@e_3", true},
    55  		// Multiple components.
    56  		{"Aa0-Bb1/Cc2-Dd3.Ee4Ff5/Gg6Hh7Ii8:Jj/9KkLl@Mm/NnOo+QqRrS/s--TtUu/Vv.WwXxYy_Z/z", true},
    57  		{"A/1--2.C+9@e_4/3", true},
    58  		{"etc/passwd/123", true},
    59  	} {
    60  		valid := IsValidReferenceName(test.refname)
    61  		if valid != test.valid {
    62  			t.Errorf("incorrectly determined validity of refname '%s': expected %v got %v", test.refname, test.valid, valid)
    63  		}
    64  	}
    65  }