summaryrefslogtreecommitdiff
path: root/internal/syscallcompat/open_nofollow_test.go
blob: e9cdf77ef85f65d90c45719e6ae38170c7fa1333 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
	fd, err := OpenNofollow(tmpDir, "d1/d2/d3/f1", syscall.O_RDWR|syscall.O_CREAT|syscall.O_EXCL, 0600)
	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")
	fd, err = OpenNofollow(tmpDir, "d1/d2/d3/f1", syscall.O_RDWR|syscall.O_CREAT, 0600)
	if err == nil {
		t.Fatalf("should have failed")
	}
	if err != syscall.ELOOP {
		t.Errorf("expected ELOOP, got %v", err)
	}
}