2016-10-09 20:54:40 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2021-08-10 19:42:33 +02:00
|
|
|
|
2021-08-23 15:05:15 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/stupidgcm"
|
2016-10-09 20:54:40 +02:00
|
|
|
)
|
|
|
|
|
2017-04-29 15:11:17 +02:00
|
|
|
// TestPrefixOArgs checks that the "-o x,y,z" parsing works correctly.
|
2016-10-09 20:54:40 +02:00
|
|
|
func TestPrefixOArgs(t *testing.T) {
|
2021-08-10 09:33:19 +02:00
|
|
|
testcases := []struct {
|
|
|
|
// i is the input
|
|
|
|
i []string
|
|
|
|
// o is the expected output
|
|
|
|
o []string
|
|
|
|
// Do we expect an error?
|
|
|
|
e bool
|
|
|
|
}{
|
2016-10-24 19:36:44 +02:00
|
|
|
{
|
2016-10-09 20:54:40 +02:00
|
|
|
i: nil,
|
|
|
|
o: nil,
|
|
|
|
},
|
2016-10-24 19:36:44 +02:00
|
|
|
{
|
2016-10-09 20:54:40 +02:00
|
|
|
i: []string{"gocryptfs"},
|
|
|
|
o: []string{"gocryptfs"},
|
|
|
|
},
|
2016-10-24 19:36:44 +02:00
|
|
|
{
|
2016-10-09 20:54:40 +02:00
|
|
|
i: []string{"gocryptfs", "-v"},
|
|
|
|
o: []string{"gocryptfs", "-v"},
|
|
|
|
},
|
2016-10-24 19:36:44 +02:00
|
|
|
{
|
2016-10-09 20:54:40 +02:00
|
|
|
i: []string{"gocryptfs", "foo", "bar", "-v"},
|
|
|
|
o: []string{"gocryptfs", "foo", "bar", "-v"},
|
|
|
|
},
|
2016-10-24 19:36:44 +02:00
|
|
|
{
|
2016-10-09 20:54:40 +02:00
|
|
|
i: []string{"gocryptfs", "foo", "bar", "-o", "a"},
|
|
|
|
o: []string{"gocryptfs", "-a", "foo", "bar"},
|
|
|
|
},
|
2016-10-24 19:36:44 +02:00
|
|
|
{
|
2016-10-09 20:54:40 +02:00
|
|
|
i: []string{"gocryptfs", "foo", "bar", "-o", "a,b,xxxxx"},
|
|
|
|
o: []string{"gocryptfs", "-a", "-b", "-xxxxx", "foo", "bar"},
|
|
|
|
},
|
2016-10-24 19:36:44 +02:00
|
|
|
{
|
2016-10-10 19:44:34 +02:00
|
|
|
i: []string{"gocryptfs", "foo", "bar", "-d", "-o=a,b,xxxxx"},
|
|
|
|
o: []string{"gocryptfs", "-a", "-b", "-xxxxx", "foo", "bar", "-d"},
|
|
|
|
},
|
2016-10-24 19:36:44 +02:00
|
|
|
{
|
2016-10-09 20:54:40 +02:00
|
|
|
i: []string{"gocryptfs", "foo", "bar", "-oooo", "a,b,xxxxx"},
|
|
|
|
o: []string{"gocryptfs", "foo", "bar", "-oooo", "a,b,xxxxx"},
|
|
|
|
},
|
2016-10-10 19:44:34 +02:00
|
|
|
// https://github.com/mhogomchungu/sirikali/blob/a36d91d3e39f0c1eb9a79680ed6c28ddb6568fa8/src/siritask.cpp#L192
|
2016-10-24 19:36:44 +02:00
|
|
|
{
|
2016-10-10 19:44:34 +02:00
|
|
|
i: []string{"gocryptfs", "-o", "rw", "--config", "fff", "ccc", "mmm"},
|
|
|
|
o: []string{"gocryptfs", "-rw", "--config", "fff", "ccc", "mmm"},
|
|
|
|
},
|
2017-04-29 15:11:17 +02:00
|
|
|
// "--" should also block "-o" parsing.
|
|
|
|
{
|
|
|
|
i: []string{"gocryptfs", "foo", "bar", "--", "-o", "a"},
|
|
|
|
o: []string{"gocryptfs", "foo", "bar", "--", "-o", "a"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
i: []string{"gocryptfs", "--", "-o", "a"},
|
|
|
|
o: []string{"gocryptfs", "--", "-o", "a"},
|
|
|
|
},
|
2018-06-05 21:02:35 +02:00
|
|
|
// This should error out
|
|
|
|
{
|
|
|
|
i: []string{"gocryptfs", "foo", "bar", "-o"},
|
|
|
|
e: true,
|
|
|
|
},
|
2016-10-09 20:54:40 +02:00
|
|
|
}
|
|
|
|
for _, tc := range testcases {
|
2018-06-05 21:02:35 +02:00
|
|
|
o, err := prefixOArgs(tc.i)
|
|
|
|
e := (err != nil)
|
|
|
|
if !reflect.DeepEqual(o, tc.o) || e != tc.e {
|
|
|
|
t.Errorf("\n in=%q\nwant=%q err=%v\n got=%q err=%v", tc.i, tc.o, tc.e, o, e)
|
2016-10-09 20:54:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-10 19:42:33 +02:00
|
|
|
|
|
|
|
func TestConvertToDoubleDash(t *testing.T) {
|
|
|
|
testcases := []struct {
|
|
|
|
// i is the input
|
|
|
|
i []string
|
|
|
|
// o is the expected output
|
|
|
|
o []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
i: nil,
|
|
|
|
o: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
i: []string{"gocryptfs"},
|
|
|
|
o: []string{"gocryptfs"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
i: []string{"gocryptfs", "foo"},
|
|
|
|
o: []string{"gocryptfs", "foo"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
i: []string{"gocryptfs", "-v", "-quiet"},
|
|
|
|
o: []string{"gocryptfs", "--v", "--quiet"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
i: []string{"gocryptfs", "--", "-foo"},
|
|
|
|
o: []string{"gocryptfs", "--", "-foo"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testcases {
|
|
|
|
o := convertToDoubleDash(tc.i)
|
|
|
|
if !reflect.DeepEqual(o, tc.o) {
|
|
|
|
t.Errorf("in=%q\nwant=%q\nhave=%q", tc.i, tc.o, o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseCliOpts(t *testing.T) {
|
|
|
|
defaultArgs := argContainer{
|
2021-10-21 15:58:19 +02:00
|
|
|
longnames: true,
|
|
|
|
longnamemax: 255,
|
|
|
|
raw64: true,
|
|
|
|
hkdf: true,
|
|
|
|
openssl: stupidgcm.PreferOpenSSLAES256GCM(), // depends on CPU and build flags
|
|
|
|
scryptn: 16,
|
2021-08-10 19:42:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type testcaseContainer struct {
|
|
|
|
// i is the input
|
|
|
|
i []string
|
|
|
|
// o is the expected output
|
|
|
|
o argContainer
|
|
|
|
}
|
|
|
|
|
2023-05-03 20:56:36 +02:00
|
|
|
testcases := []testcaseContainer{
|
|
|
|
{
|
|
|
|
i: []string{"gocryptfs"},
|
|
|
|
o: defaultArgs,
|
|
|
|
},
|
|
|
|
}
|
2021-08-10 19:42:33 +02:00
|
|
|
|
|
|
|
o := defaultArgs
|
|
|
|
o.quiet = true
|
2023-05-03 20:56:36 +02:00
|
|
|
testcases = append(testcases, []testcaseContainer{
|
|
|
|
{
|
|
|
|
i: []string{"gocryptfs", "-q"},
|
|
|
|
o: o,
|
|
|
|
}, {
|
|
|
|
i: []string{"gocryptfs", "--q"},
|
|
|
|
o: o,
|
|
|
|
}, {
|
|
|
|
i: []string{"gocryptfs", "-quiet"},
|
|
|
|
o: o,
|
|
|
|
}, {
|
|
|
|
i: []string{"gocryptfs", "--quiet"},
|
|
|
|
o: o,
|
|
|
|
},
|
|
|
|
}...)
|
2021-08-10 19:42:33 +02:00
|
|
|
|
|
|
|
o = defaultArgs
|
2023-05-03 21:14:53 +02:00
|
|
|
o.exclude = []string{"foo", "bar", "baz,boe"}
|
2023-05-03 20:56:36 +02:00
|
|
|
testcases = append(testcases, []testcaseContainer{
|
|
|
|
{
|
2023-05-03 21:14:53 +02:00
|
|
|
i: []string{"gocryptfs", "-e", "foo", "-e", "bar", "-e", "baz,boe"},
|
2023-05-03 20:56:36 +02:00
|
|
|
o: o,
|
|
|
|
}, {
|
2023-05-03 21:14:53 +02:00
|
|
|
i: []string{"gocryptfs", "--exclude", "foo", "--exclude", "bar", "--exclude", "baz,boe"},
|
2023-05-03 20:56:36 +02:00
|
|
|
o: o,
|
|
|
|
}, /* TODO BROKEN {
|
|
|
|
i: []string{"gocryptfs", "--exclude", "foo", "-e", "bar"},
|
|
|
|
o: o,
|
|
|
|
},*/
|
|
|
|
}...)
|
|
|
|
|
2021-08-10 19:42:33 +02:00
|
|
|
for _, tc := range testcases {
|
|
|
|
o := parseCliOpts(tc.i)
|
|
|
|
if !reflect.DeepEqual(o, tc.o) {
|
|
|
|
t.Errorf("in=%v\nwant=%v\nhave=%v", tc.i, tc.o, o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|