func TestCtlSock(t *testing.T) { cDir := test_helpers.InitFS(t) pDir := cDir + ".mnt" sock := cDir + ".sock" test_helpers.MountOrFatal(t, cDir, pDir, "-ctlsock="+sock, "-extpass", "echo test") defer test_helpers.UnmountPanic(pDir) req := ctlsock.RequestStruct{ EncryptPath: "foobar", } response := test_helpers.QueryCtlSock(t, sock, req) if response.Result == "" || response.ErrNo != 0 { t.Errorf("got an error reply: %+v", response) } req.EncryptPath = "not-existing-dir/xyz" response = test_helpers.QueryCtlSock(t, sock, req) if response.ErrNo != int32(syscall.ENOENT) || response.Result != "" { t.Errorf("incorrect error handling: %+v", response) } // Strange paths should not cause a crash crashers := []string{"/foo", "foo/", "/foo/", ".", "/////", "/../../."} for _, c := range crashers { req.EncryptPath = c // QueryCtlSock calls t.Fatal if it gets EOF when gocryptfs panics test_helpers.QueryCtlSock(t, sock, req) } }
// Test DecryptPath and EncryptPath func TestCtlSockPathOps(t *testing.T) { mnt, err := ioutil.TempDir(test_helpers.TmpDir, "reverse_mnt_") if err != nil { t.Fatal(err) } sock := mnt + ".sock" test_helpers.MountOrFatal(t, "ctlsock_reverse_test_fs", mnt, "-reverse", "-extpass", "echo test", "-ctlsock="+sock) defer test_helpers.UnmountPanic(mnt) var req ctlsock.RequestStruct for i, tc := range ctlSockTestCases { // Decrypt req = ctlsock.RequestStruct{DecryptPath: tc[0]} response := test_helpers.QueryCtlSock(t, sock, req) if response.ErrNo != 0 { t.Errorf("Testcase %d Decrypt: %q ErrNo=%d ErrText=%s", i, tc[0], response.ErrNo, response.ErrText) } else if response.Result != tc[1] { t.Errorf("Testcase %d Decrypt: Want %q got %q", i, tc[1], response.Result) } // Encrypt req = ctlsock.RequestStruct{EncryptPath: tc[1]} response = test_helpers.QueryCtlSock(t, sock, req) if response.ErrNo != 0 { t.Errorf("Testcase %d Encrypt: %q ErrNo=%d ErrText=%s", i, tc[0], response.ErrNo, response.ErrText) } else if response.Result != tc[0] { t.Errorf("Testcase %d Encrypt: Want %q got %q", i, tc[1], response.Result) } } }
// We should not panic when somebody feeds requests that make no sense func TestCtlSockCrash(t *testing.T) { mnt, err := ioutil.TempDir(test_helpers.TmpDir, "reverse_mnt_") if err != nil { t.Fatal(err) } sock := mnt + ".sock" test_helpers.MountOrFatal(t, "ctlsock_reverse_test_fs", mnt, "-reverse", "-extpass", "echo test", "-ctlsock="+sock, "-wpanic=0", "-nosyslog=0") defer test_helpers.UnmountPanic(mnt) // Try to crash it req := ctlsock.RequestStruct{DecryptPath: "gocryptfs.longname.XXX_TestCtlSockCrash_XXX.name"} test_helpers.QueryCtlSock(t, sock, req) }