storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/lock/lock_windows_test.go (about)

     1  //go:build windows
     2  // +build windows
     3  
     4  /*
     5   * MinIO Cloud Storage, (C) 2017 MinIO, Inc.
     6   *
     7   * Licensed under the Apache License, Version 2.0 (the "License");
     8   * you may not use this file except in compliance with the License.
     9   * You may obtain a copy of the License at
    10   *
    11   *     http://www.apache.org/licenses/LICENSE-2.0
    12   *
    13   * Unless required by applicable law or agreed to in writing, software
    14   * distributed under the License is distributed on an "AS IS" BASIS,
    15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16   * See the License for the specific language governing permissions and
    17   * limitations under the License.
    18   */
    19  
    20  package lock
    21  
    22  import (
    23  	"strings"
    24  	"testing"
    25  )
    26  
    27  func TestFixLongPath(t *testing.T) {
    28  	// 248 is long enough to trigger the longer-than-248 checks in
    29  	// fixLongPath, but short enough not to make a path component
    30  	// longer than 255, which is illegal on Windows. (which
    31  	// doesn't really matter anyway, since this is purely a string
    32  	// function we're testing, and it's not actually being used to
    33  	// do a system call)
    34  	veryLong := "l" + strings.Repeat("o", 248) + "ng"
    35  	for _, test := range []struct{ in, want string }{
    36  		// Short; unchanged:
    37  		{`C:\short.txt`, `C:\short.txt`},
    38  		{`C:\`, `C:\`},
    39  		{`C:`, `C:`},
    40  		// The "long" substring is replaced by a looooooong
    41  		// string which triggers the rewriting. Except in the
    42  		// cases below where it doesn't.
    43  		{`C:\long\foo.txt`, `\\?\C:\long\foo.txt`},
    44  		{`C:/long/foo.txt`, `\\?\C:\long\foo.txt`},
    45  		{`C:\long\foo\\bar\.\baz\\`, `\\?\C:\long\foo\bar\baz`},
    46  		{`\\unc\path`, `\\unc\path`},
    47  		{`long.txt`, `long.txt`},
    48  		{`C:long.txt`, `C:long.txt`},
    49  		{`c:\long\..\bar\baz`, `c:\long\..\bar\baz`},
    50  		{`\\?\c:\long\foo.txt`, `\\?\c:\long\foo.txt`},
    51  		{`\\?\c:\long/foo.txt`, `\\?\c:\long/foo.txt`},
    52  	} {
    53  		in := strings.Replace(test.in, "long", veryLong, -1)
    54  		want := strings.Replace(test.want, "long", veryLong, -1)
    55  		if got := fixLongPath(in); got != want {
    56  			got = strings.Replace(got, veryLong, "long", -1)
    57  			t.Errorf("fixLongPath(%q) = %q; want %q", test.in, got, test.want)
    58  		}
    59  	}
    60  }