Skip to content

Commit

Permalink
Merge pull request golang#498 from ChrisHines/test-windows-unreadable
Browse files Browse the repository at this point in the history
Add Windows specific function for making a file unreadable during tests.
  • Loading branch information
sdboyer committed May 2, 2017
2 parents 95a347a + 07c0465 commit 6e9f804
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 15 deletions.
24 changes: 24 additions & 0 deletions analyzer_notwindows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build !windows

package dep

import (
"io"
"os"
)

func makeUnreadable(path string) (io.Closer, error) {
err := os.Chmod(path, 0222)
if err != nil {
return nil, err
}
return closer{}, nil
}

type closer struct{}

func (closer) Close() error { return nil }
20 changes: 5 additions & 15 deletions analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
package dep

import (
"os"
"path/filepath"
"runtime"
"testing"

"github.com/golang/dep/test"
Expand Down Expand Up @@ -64,31 +62,23 @@ func TestAnalyzerDeriveManifestAndLockDoesNotExist(t *testing.T) {
}

func TestAnalyzerDeriveManifestAndLockCannotOpen(t *testing.T) {
if runtime.GOOS == "windows" {
// TODO: find an implementation that works on Microsoft
// Windows. Setting permissions works differently there.
// os.Chmod(..., 0222) below is not enough for the file
// to be write-only (unreadable), and os.Chmod(...,
// 0000) returns an invalid argument error.
t.Skip("skipping on windows")
}

h := test.NewHelper(t)
defer h.Cleanup()

h.TempDir("dep")

// Create an empty manifest file
// Simulate an inaccessible manifest file.
h.TempFile(filepath.Join("dep", ManifestName), "")

// Change its mode so that it cannot be read
err := os.Chmod(filepath.Join(h.Path("dep"), ManifestName), 0222)
closer, err := makeUnreadable(filepath.Join(h.Path("dep"), ManifestName))
if err != nil {
t.Fatal(err)
}
defer closer.Close()

a := Analyzer{}

// Verify that the solver rejects the manifest, rather than treating it as
// offering no constraints.
m, l, err := a.DeriveManifestAndLock(h.Path("dep"), "my/fake/project")
if m != nil || l != nil || err == nil {
t.Fatalf("expected manifest & lock to be nil, err to be not nil: m -> %#v l -> %#v err -> %#v", m, l, err)
Expand Down
33 changes: 33 additions & 0 deletions analyzer_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package dep

import (
"io"
"os"
"syscall"
)

// makeUnreadable opens the file at path in exclusive mode. A file opened in
// exclusive mode cannot be opened again until the exclusive mode file handle
// is closed.
func makeUnreadable(path string) (io.Closer, error) {
if len(path) == 0 {
return nil, syscall.ERROR_FILE_NOT_FOUND
}
pathp, err := syscall.UTF16PtrFromString(path)
if err != nil {
return nil, err
}
access := uint32(syscall.GENERIC_READ | syscall.GENERIC_WRITE)
sharemode := uint32(0) // no sharing == exclusive mode
sa := (*syscall.SecurityAttributes)(nil)
createmode := uint32(syscall.OPEN_EXISTING)
h, err := syscall.CreateFile(pathp, access, sharemode, sa, createmode, syscall.FILE_ATTRIBUTE_NORMAL, 0)
if err != nil {
return nil, err
}
return os.NewFile(uintptr(h), path), nil
}

0 comments on commit 6e9f804

Please sign in to comment.