gocryptfs-xray: add -0 flag, add tests

The -0 flags works like xargs -0.
This commit is contained in:
Jakob Unterwurzacher 2020-05-10 00:04:14 +02:00
parent f2e8b776f8
commit a9895b3487
4 changed files with 86 additions and 15 deletions

View File

@ -8,36 +8,55 @@ import (
"github.com/rfjakob/gocryptfs/ctlsock" "github.com/rfjakob/gocryptfs/ctlsock"
) )
func decryptPaths(socketPath string) { func decryptPaths(socketPath string, sep0 bool) {
var req ctlsock.RequestStruct var req ctlsock.RequestStruct
transformPaths(socketPath, &req, &req.DecryptPath) transformPaths(socketPath, &req, &req.DecryptPath, sep0)
} }
func encryptPaths(socketPath string) { func encryptPaths(socketPath string, sep0 bool) {
var req ctlsock.RequestStruct var req ctlsock.RequestStruct
transformPaths(socketPath, &req, &req.EncryptPath) transformPaths(socketPath, &req, &req.EncryptPath, sep0)
} }
func transformPaths(socketPath string, req *ctlsock.RequestStruct, in *string) { func transformPaths(socketPath string, req *ctlsock.RequestStruct, in *string, sep0 bool) {
errorCount := 0
c, err := ctlsock.New(socketPath) c, err := ctlsock.New(socketPath)
if err != nil { if err != nil {
fmt.Printf("fatal: %v\n", err) fmt.Printf("fatal: %v\n", err)
os.Exit(1) os.Exit(1)
} }
line := 0 line := 1
scanner := bufio.NewScanner(os.Stdin) var separator byte = '\n'
for scanner.Scan() { if sep0 {
line++ separator = '\000'
*in = scanner.Text() }
r := bufio.NewReader(os.Stdin)
for eof := false; eof == false; line++ {
val, err := r.ReadBytes(separator)
if len(val) == 0 {
break
}
if err != nil {
// break the loop after we have processed the last val
eof = true
} else {
// drop trailing separator
val = val[:len(val)-1]
}
*in = string(val)
resp, err := c.Query(req) resp, err := c.Query(req)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "error at input line %d %q: %v\n", line, *in, err) fmt.Fprintf(os.Stderr, "error at input line %d %q: %v\n", line, *in, err)
errorCount++
continue continue
} }
if resp.WarnText != "" { if resp.WarnText != "" {
fmt.Fprintf(os.Stderr, "warning at input line %d %q: %v\n", line, *in, resp.WarnText) fmt.Fprintf(os.Stderr, "warning at input line %d %q: %v\n", line, *in, resp.WarnText)
} }
fmt.Println(resp.Result) fmt.Printf("%s%c", resp.Result, separator)
} }
os.Exit(0) if errorCount == 0 {
os.Exit(0)
}
os.Exit(1)
} }

View File

@ -65,10 +65,12 @@ func main() {
decryptPaths *bool decryptPaths *bool
encryptPaths *bool encryptPaths *bool
aessiv *bool aessiv *bool
sep0 *bool
} }
args.dumpmasterkey = flag.Bool("dumpmasterkey", false, "Decrypt and dump the master key") args.dumpmasterkey = flag.Bool("dumpmasterkey", false, "Decrypt and dump the master key")
args.decryptPaths = flag.Bool("decrypt-paths", false, "Decrypt file paths using gocryptfs control socket") args.decryptPaths = flag.Bool("decrypt-paths", false, "Decrypt file paths using gocryptfs control socket")
args.encryptPaths = flag.Bool("encrypt-paths", false, "Encrypt file paths using gocryptfs control socket") args.encryptPaths = flag.Bool("encrypt-paths", false, "Encrypt file paths using gocryptfs control socket")
args.sep0 = flag.Bool("0", false, "Use \\0 instead of \\n as separator")
args.aessiv = flag.Bool("aessiv", false, "Assume AES-SIV mode instead of AES-GCM") args.aessiv = flag.Bool("aessiv", false, "Assume AES-SIV mode instead of AES-GCM")
flag.Usage = usage flag.Usage = usage
flag.Parse() flag.Parse()
@ -83,10 +85,10 @@ func main() {
} }
fn := flag.Arg(0) fn := flag.Arg(0)
if *args.decryptPaths { if *args.decryptPaths {
decryptPaths(fn) decryptPaths(fn, *args.sep0)
} }
if *args.encryptPaths { if *args.encryptPaths {
encryptPaths(fn) encryptPaths(fn, *args.sep0)
} }
fd, err := os.Open(fn) fd, err := os.Open(fn)
if err != nil { if err != nil {

View File

@ -6,6 +6,8 @@ import (
"io/ioutil" "io/ioutil"
"os/exec" "os/exec"
"testing" "testing"
"github.com/rfjakob/gocryptfs/tests/test_helpers"
) )
func TestAesgcmXray(t *testing.T) { func TestAesgcmXray(t *testing.T) {
@ -58,3 +60,51 @@ func TestDumpmasterkey(t *testing.T) {
fmt.Printf("have: %s\n", out) fmt.Printf("have: %s\n", out)
} }
} }
func TestEncryptPaths(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)
testCases := []struct {
in []string
sep0 bool
}{
{
[]string{
"test1",
"test1\n",
"test1\ntest2",
"test1\ntest2\n",
},
false,
},
{
[]string{
"test1",
"test1\000",
"test1\000test2",
"test1\000test2\000",
},
true,
},
}
for _, tc := range testCases {
for _, in := range tc.in {
sepArg := "-0=false"
if tc.sep0 {
sepArg = "-0=true"
}
cmd := exec.Command("../gocryptfs-xray", "-encrypt-paths", sepArg, sock)
cmd.Stdin = bytes.NewBuffer([]byte(in))
out, err := cmd.CombinedOutput()
t.Logf("%q", string(out))
if err != nil {
t.Fatal(err)
}
}
}
}

View File

@ -26,7 +26,7 @@ var testParentDir = ""
const GocryptfsBinary = "../../gocryptfs" const GocryptfsBinary = "../../gocryptfs"
// UnmountScript is the fusermount/umount compatibility wrapper script // UnmountScript is the fusermount/umount compatibility wrapper script
const UnmountScript = "../fuse-unmount.bash" const UnmountScript = "../../tests/fuse-unmount.bash"
// X255 contains 255 uppercase "X". This can be used as a maximum-length filename. // X255 contains 255 uppercase "X". This can be used as a maximum-length filename.
var X255 string var X255 string