2012-03-12 19:50:04 -06:00
|
|
|
// Copyright 2012 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 os_test
|
|
|
|
|
|
|
|
import (
|
2012-03-14 09:54:40 -06:00
|
|
|
"fmt"
|
2012-03-12 19:50:04 -06:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2012-03-14 09:54:40 -06:00
|
|
|
"path/filepath"
|
2012-03-12 19:50:04 -06:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestErrIsExist(t *testing.T) {
|
|
|
|
f, err := ioutil.TempFile("", "_Go_ErrIsExist")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("open ErrIsExist tempfile: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.Remove(f.Name())
|
|
|
|
defer f.Close()
|
|
|
|
f2, err := os.OpenFile(f.Name(), os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
|
|
|
|
if err == nil {
|
|
|
|
f2.Close()
|
|
|
|
t.Fatal("Open should have failed")
|
|
|
|
return
|
|
|
|
}
|
2012-03-14 09:54:40 -06:00
|
|
|
if s := checkErrorPredicate("os.IsExist", os.IsExist, err); s != "" {
|
|
|
|
t.Fatal(s)
|
2012-03-12 19:50:04 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2012-03-14 09:54:40 -06:00
|
|
|
|
|
|
|
func testErrNotExist(name string) string {
|
|
|
|
f, err := os.Open(name)
|
|
|
|
if err == nil {
|
|
|
|
f.Close()
|
|
|
|
return "Open should have failed"
|
|
|
|
}
|
|
|
|
if s := checkErrorPredicate("os.IsNotExist", os.IsNotExist, err); s != "" {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Chdir(name)
|
|
|
|
if err == nil {
|
|
|
|
return "Chdir should have failed"
|
|
|
|
}
|
|
|
|
if s := checkErrorPredicate("os.IsNotExist", os.IsNotExist, err); s != "" {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrIsNotExist(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "_Go_ErrIsNotExist")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("create ErrIsNotExist tempdir: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
name := filepath.Join(tmpDir, "NotExists")
|
|
|
|
if s := testErrNotExist(name); s != "" {
|
|
|
|
t.Fatal(s)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
name = filepath.Join(name, "NotExists2")
|
|
|
|
if s := testErrNotExist(name); s != "" {
|
|
|
|
t.Fatal(s)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkErrorPredicate(predName string, pred func(error) bool, err error) string {
|
|
|
|
if !pred(err) {
|
|
|
|
return fmt.Sprintf("%s does not work as expected for %#v", predName, err)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2012-08-02 22:25:35 -06:00
|
|
|
|
2016-04-02 18:44:39 -06:00
|
|
|
type isExistTest struct {
|
2012-08-02 22:25:35 -06:00
|
|
|
err error
|
|
|
|
is bool
|
|
|
|
isnot bool
|
2016-04-02 18:44:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var isExistTests = []isExistTest{
|
2012-08-02 22:25:35 -06:00
|
|
|
{&os.PathError{Err: os.ErrInvalid}, false, false},
|
|
|
|
{&os.PathError{Err: os.ErrPermission}, false, false},
|
|
|
|
{&os.PathError{Err: os.ErrExist}, true, false},
|
|
|
|
{&os.PathError{Err: os.ErrNotExist}, false, true},
|
2016-10-06 22:46:56 -06:00
|
|
|
{&os.PathError{Err: os.ErrClosed}, false, false},
|
2012-08-02 22:25:35 -06:00
|
|
|
{&os.LinkError{Err: os.ErrInvalid}, false, false},
|
|
|
|
{&os.LinkError{Err: os.ErrPermission}, false, false},
|
|
|
|
{&os.LinkError{Err: os.ErrExist}, true, false},
|
|
|
|
{&os.LinkError{Err: os.ErrNotExist}, false, true},
|
2016-10-06 22:46:56 -06:00
|
|
|
{&os.LinkError{Err: os.ErrClosed}, false, false},
|
2015-10-09 22:15:48 -06:00
|
|
|
{&os.SyscallError{Err: os.ErrNotExist}, false, true},
|
|
|
|
{&os.SyscallError{Err: os.ErrExist}, true, false},
|
2012-08-02 22:25:35 -06:00
|
|
|
{nil, false, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsExist(t *testing.T) {
|
|
|
|
for _, tt := range isExistTests {
|
|
|
|
if is := os.IsExist(tt.err); is != tt.is {
|
|
|
|
t.Errorf("os.IsExist(%T %v) = %v, want %v", tt.err, tt.err, is, tt.is)
|
|
|
|
}
|
|
|
|
if isnot := os.IsNotExist(tt.err); isnot != tt.isnot {
|
|
|
|
t.Errorf("os.IsNotExist(%T %v) = %v, want %v", tt.err, tt.err, isnot, tt.isnot)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
syscall: return EINVAL when string arguments have NUL characters
Since NUL usually terminates strings in underlying syscalls, allowing
it when converting string arguments is a security risk, especially
when dealing with filenames. For example, a program might reason that
filename like "/root/..\x00/" is a subdirectory or "/root/" and allow
access to it, while underlying syscall will treat "\x00" as an end of
that string and the actual filename will be "/root/..", which might
be unexpected. Returning EINVAL when string arguments have NUL in
them makes sure this attack vector is unusable.
R=golang-dev, r, bradfitz, fullung, rsc, minux.ma
CC=golang-dev
https://golang.org/cl/6458050
2012-08-05 15:24:32 -06:00
|
|
|
|
2016-04-02 18:44:39 -06:00
|
|
|
type isPermissionTest struct {
|
2015-10-09 22:15:48 -06:00
|
|
|
err error
|
|
|
|
want bool
|
2016-04-02 18:44:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var isPermissionTests = []isPermissionTest{
|
2015-10-09 22:15:48 -06:00
|
|
|
{nil, false},
|
|
|
|
{&os.PathError{Err: os.ErrPermission}, true},
|
|
|
|
{&os.SyscallError{Err: os.ErrPermission}, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsPermission(t *testing.T) {
|
|
|
|
for _, tt := range isPermissionTests {
|
|
|
|
if got := os.IsPermission(tt.err); got != tt.want {
|
|
|
|
t.Errorf("os.IsPermission(%#v) = %v; want %v", tt.err, got, tt.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
syscall: return EINVAL when string arguments have NUL characters
Since NUL usually terminates strings in underlying syscalls, allowing
it when converting string arguments is a security risk, especially
when dealing with filenames. For example, a program might reason that
filename like "/root/..\x00/" is a subdirectory or "/root/" and allow
access to it, while underlying syscall will treat "\x00" as an end of
that string and the actual filename will be "/root/..", which might
be unexpected. Returning EINVAL when string arguments have NUL in
them makes sure this attack vector is unusable.
R=golang-dev, r, bradfitz, fullung, rsc, minux.ma
CC=golang-dev
https://golang.org/cl/6458050
2012-08-05 15:24:32 -06:00
|
|
|
func TestErrPathNUL(t *testing.T) {
|
|
|
|
f, err := ioutil.TempFile("", "_Go_ErrPathNUL\x00")
|
|
|
|
if err == nil {
|
|
|
|
f.Close()
|
|
|
|
t.Fatal("TempFile should have failed")
|
|
|
|
}
|
|
|
|
f, err = ioutil.TempFile("", "_Go_ErrPathNUL")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("open ErrPathNUL tempfile: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(f.Name())
|
|
|
|
defer f.Close()
|
|
|
|
f2, err := os.OpenFile(f.Name(), os.O_RDWR, 0600)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("open ErrPathNUL: %s", err)
|
|
|
|
}
|
|
|
|
f2.Close()
|
|
|
|
f2, err = os.OpenFile(f.Name()+"\x00", os.O_RDWR, 0600)
|
|
|
|
if err == nil {
|
|
|
|
f2.Close()
|
|
|
|
t.Fatal("Open should have failed")
|
|
|
|
}
|
|
|
|
}
|