test_helpers: use new ctlsock.CtlSock API
This commit is contained in:
parent
7e51073400
commit
171b1eac91
|
@ -4,17 +4,14 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"syscall"
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/rfjakob/gocryptfs/ctlsock"
|
"github.com/rfjakob/gocryptfs/ctlsock"
|
||||||
"github.com/rfjakob/gocryptfs/internal/nametransform"
|
"github.com/rfjakob/gocryptfs/internal/nametransform"
|
||||||
|
@ -347,29 +344,24 @@ func Du(t *testing.T, fd int) (nBytes int64) {
|
||||||
|
|
||||||
// QueryCtlSock sends a request to the control socket at "socketPath" and
|
// QueryCtlSock sends a request to the control socket at "socketPath" and
|
||||||
// returns the response.
|
// returns the response.
|
||||||
func QueryCtlSock(t *testing.T, socketPath string, req ctlsock.RequestStruct) (response ctlsock.ResponseStruct) {
|
func QueryCtlSock(t *testing.T, socketPath string, req ctlsock.RequestStruct) ctlsock.ResponseStruct {
|
||||||
conn, err := net.DialTimeout("unix", socketPath, 1*time.Second)
|
c, err := ctlsock.New(socketPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// Connecting to the socket failed already. This is fatal.
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer c.Close()
|
||||||
conn.SetDeadline(time.Now().Add(time.Second))
|
resp, err := c.Query(&req)
|
||||||
msg, err := json.Marshal(req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// If we got a response, try to extract it. This is not fatal here
|
||||||
|
// as the tests may expect error responses.
|
||||||
|
if resp2, ok := err.(*ctlsock.ResponseStruct); ok {
|
||||||
|
return *resp2
|
||||||
|
}
|
||||||
|
// Another error means that we did not even get a response. This is fatal.
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
_, err = conn.Write(msg)
|
return *resp
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
buf := make([]byte, 5000)
|
|
||||||
n, err := conn.Read(buf)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
buf = buf[:n]
|
|
||||||
json.Unmarshal(buf, &response)
|
|
||||||
return response
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExtractCmdExitCode extracts the exit code from an error value that was
|
// ExtractCmdExitCode extracts the exit code from an error value that was
|
||||||
|
|
Loading…
Reference in New Issue