github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/lock/lock_windows_test.go (about)

     1  //go:build windows
     2  // +build windows
     3  
     4  // Copyright (c) 2015-2021 MinIO, Inc.
     5  //
     6  // This file is part of MinIO Object Storage stack
     7  //
     8  // This program is free software: you can redistribute it and/or modify
     9  // it under the terms of the GNU Affero General Public License as published by
    10  // the Free Software Foundation, either version 3 of the License, or
    11  // (at your option) any later version.
    12  //
    13  // This program is distributed in the hope that it will be useful
    14  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  // GNU Affero General Public License for more details.
    17  //
    18  // You should have received a copy of the GNU Affero General Public License
    19  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    20  
    21  package lock
    22  
    23  import (
    24  	"strings"
    25  	"testing"
    26  )
    27  
    28  func TestFixLongPath(t *testing.T) {
    29  	// 248 is long enough to trigger the longer-than-248 checks in
    30  	// fixLongPath, but short enough not to make a path component
    31  	// longer than 255, which is illegal on Windows. (which
    32  	// doesn't really matter anyway, since this is purely a string
    33  	// function we're testing, and it's not actually being used to
    34  	// do a system call)
    35  	veryLong := "l" + strings.Repeat("o", 248) + "ng"
    36  	for _, test := range []struct{ in, want string }{
    37  		// Short; unchanged:
    38  		{`C:\short.txt`, `C:\short.txt`},
    39  		{`C:\`, `C:\`},
    40  		{`C:`, `C:`},
    41  		// The "long" substring is replaced by a looooooong
    42  		// string which triggers the rewriting. Except in the
    43  		// cases below where it doesn't.
    44  		{`C:\long\foo.txt`, `\\?\C:\long\foo.txt`},
    45  		{`C:/long/foo.txt`, `\\?\C:\long\foo.txt`},
    46  		{`C:\long\foo\\bar\.\baz\\`, `\\?\C:\long\foo\bar\baz`},
    47  		{`\\unc\path`, `\\unc\path`},
    48  		{`long.txt`, `long.txt`},
    49  		{`C:long.txt`, `C:long.txt`},
    50  		{`c:\long\..\bar\baz`, `c:\long\..\bar\baz`},
    51  		{`\\?\c:\long\foo.txt`, `\\?\c:\long\foo.txt`},
    52  		{`\\?\c:\long/foo.txt`, `\\?\c:\long/foo.txt`},
    53  	} {
    54  		in := strings.ReplaceAll(test.in, "long", veryLong)
    55  		want := strings.ReplaceAll(test.want, "long", veryLong)
    56  		if got := fixLongPath(in); got != want {
    57  			got = strings.ReplaceAll(got, veryLong, "long")
    58  			t.Errorf("fixLongPath(%q) = %q; want %q", test.in, got, test.want)
    59  		}
    60  	}
    61  }