Source file src/os/root_windows_test.go

     1  // Copyright 2024 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build windows
     6  
     7  package os_test
     8  
     9  import (
    10  	"errors"
    11  	"os"
    12  	"path/filepath"
    13  	"testing"
    14  )
    15  
    16  // Verify that Root.Open rejects Windows reserved names.
    17  func TestRootWindowsDeviceNames(t *testing.T) {
    18  	r, err := os.OpenRoot(t.TempDir())
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	defer r.Close()
    23  	if f, err := r.Open("NUL"); err == nil {
    24  		t.Errorf(`r.Open("NUL") succeeded; want error"`)
    25  		f.Close()
    26  	}
    27  }
    28  
    29  // Verify that Root.Open is case-insensitive.
    30  // (The wrong options to NtOpenFile could make operations case-sensitive,
    31  // so this is worth checking.)
    32  func TestRootWindowsCaseInsensitivity(t *testing.T) {
    33  	dir := t.TempDir()
    34  	if err := os.WriteFile(filepath.Join(dir, "file"), nil, 0666); err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	r, err := os.OpenRoot(dir)
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	defer r.Close()
    42  	f, err := r.Open("FILE")
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	f.Close()
    47  	if err := r.Remove("FILE"); err != nil {
    48  		t.Fatal(err)
    49  	}
    50  	if _, err := os.Stat(filepath.Join(dir, "file")); !errors.Is(err, os.ErrNotExist) {
    51  		t.Fatalf("os.Stat(file) after deletion: %v, want ErrNotFound", err)
    52  	}
    53  }
    54  

View as plain text