summaryrefslogtreecommitdiff
path: root/internal/syscallcompat/open_nofollow_test.go
diff options
context:
space:
mode:
authorJakob Unterwurzacher2017-12-02 20:35:44 +0100
committerJakob Unterwurzacher2017-12-02 20:35:44 +0100
commit91e042e2bafbec3271d8c79005f7dc8229a16a10 (patch)
tree0a49571caf3003ec7c958ed993b96967bb1f0147 /internal/syscallcompat/open_nofollow_test.go
parent1d289736110f99e63a01449312888e5c3f241656 (diff)
syscallcompat: add OpenNofollow helper
OpenNofollow = symlink-race-safe Open Prepares fixing https://github.com/rfjakob/gocryptfs/issues/165
Diffstat (limited to 'internal/syscallcompat/open_nofollow_test.go')
-rw-r--r--internal/syscallcompat/open_nofollow_test.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/internal/syscallcompat/open_nofollow_test.go b/internal/syscallcompat/open_nofollow_test.go
new file mode 100644
index 0000000..e9cdf77
--- /dev/null
+++ b/internal/syscallcompat/open_nofollow_test.go
@@ -0,0 +1,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)
+ }
+}