2017-12-02 20:35:44 +01:00
|
|
|
package syscallcompat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestOpenNofollow(t *testing.T) {
|
|
|
|
err := os.MkdirAll(tmpDir+"/d1/d2/d3", 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Create a file
|
2018-09-08 17:41:17 +02:00
|
|
|
dirfd, err := OpenDirNofollow(tmpDir, "d1/d2/d3")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fd, err := Openat(dirfd, "f1", syscall.O_RDWR|syscall.O_CREAT|syscall.O_EXCL, 0600)
|
2017-12-02 20:35:44 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
syscall.Close(fd)
|
|
|
|
_, err = os.Stat(tmpDir + "/d1/d2/d3/f1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Replace "d1" with a symlink - open should fail with ELOOP
|
|
|
|
err = os.Rename(tmpDir+"/d1", tmpDir+"/d1.renamed")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
os.Symlink(tmpDir+"/d1.renamed", tmpDir+"/d1")
|
2018-09-08 17:41:17 +02:00
|
|
|
fd, err = OpenDirNofollow(tmpDir, "d1/d2/d3")
|
2017-12-02 20:35:44 +01:00
|
|
|
if err == nil {
|
2021-08-18 15:47:17 +02:00
|
|
|
syscall.Close(fd)
|
2017-12-02 20:35:44 +01:00
|
|
|
t.Fatalf("should have failed")
|
|
|
|
}
|
2017-12-05 23:31:07 +01:00
|
|
|
if err != syscall.ELOOP && err != syscall.ENOTDIR {
|
|
|
|
t.Errorf("expected ELOOP or ENOTDIR, got %v", err)
|
2017-12-02 20:35:44 +01:00
|
|
|
}
|
2017-12-05 23:08:55 +01:00
|
|
|
// Check to see that the base dir can be opened as well
|
2018-09-08 17:41:17 +02:00
|
|
|
fd, err = OpenDirNofollow(tmpDir, "")
|
2017-12-05 23:08:55 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot open base dir: %v", err)
|
|
|
|
} else {
|
|
|
|
syscall.Close(fd)
|
|
|
|
}
|
2017-12-02 20:35:44 +01:00
|
|
|
}
|