test_helpers: use new ctlsock.CtlSock API

This commit is contained in:
Jakob Unterwurzacher 2020-05-09 19:09:33 +02:00
parent 7e51073400
commit 171b1eac91
1 changed files with 12 additions and 20 deletions

View File

@ -4,17 +4,14 @@ import (
"bytes"
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"os/exec"
"path/filepath"
"syscall"
"testing"
"time"
"github.com/rfjakob/gocryptfs/ctlsock"
"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
// returns the response.
func QueryCtlSock(t *testing.T, socketPath string, req ctlsock.RequestStruct) (response ctlsock.ResponseStruct) {
conn, err := net.DialTimeout("unix", socketPath, 1*time.Second)
func QueryCtlSock(t *testing.T, socketPath string, req ctlsock.RequestStruct) ctlsock.ResponseStruct {
c, err := ctlsock.New(socketPath)
if err != nil {
// Connecting to the socket failed already. This is fatal.
t.Fatal(err)
}
defer conn.Close()
conn.SetDeadline(time.Now().Add(time.Second))
msg, err := json.Marshal(req)
defer c.Close()
resp, err := c.Query(&req)
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)
}
_, err = conn.Write(msg)
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
return *resp
}
// ExtractCmdExitCode extracts the exit code from an error value that was