From 0c16616117e9866cc49adc85738e3c9e3adf413a Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Tue, 10 Aug 2021 19:42:33 +0200 Subject: main: add testcases for convertToDoubleDash & parseCliOpts --- cli_args_test.go | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) (limited to 'cli_args_test.go') diff --git a/cli_args_test.go b/cli_args_test.go index 6dff1f1..97f9473 100644 --- a/cli_args_test.go +++ b/cli_args_test.go @@ -3,6 +3,8 @@ package main import ( "reflect" "testing" + + "github.com/rfjakob/gocryptfs/internal/stupidgcm" ) // TestPrefixOArgs checks that the "-o x,y,z" parsing works correctly. @@ -75,3 +77,105 @@ func TestPrefixOArgs(t *testing.T) { } } } + +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{ + longnames: true, + raw64: true, + hkdf: true, + openssl: stupidgcm.PreferOpenSSL(), // depends on CPU and build flags + scryptn: 16, + } + + type testcaseContainer struct { + // i is the input + i []string + // o is the expected output + o argContainer + } + + var testcases []testcaseContainer + + testcases = append(testcases, testcaseContainer{ + i: []string{"gocryptfs"}, + o: defaultArgs, + }) + + o := defaultArgs + o.quiet = true + testcases = append(testcases, testcaseContainer{ + i: []string{"gocryptfs", "-q"}, + o: o, + }) + testcases = append(testcases, testcaseContainer{ + i: []string{"gocryptfs", "--q"}, + o: o, + }) + testcases = append(testcases, testcaseContainer{ + i: []string{"gocryptfs", "-quiet"}, + o: o, + }) + testcases = append(testcases, testcaseContainer{ + i: []string{"gocryptfs", "--quiet"}, + o: o, + }) + + o = defaultArgs + o.exclude = []string{"foo", "bar"} + testcases = append(testcases, testcaseContainer{ + i: []string{"gocryptfs", "-e", "foo", "-e", "bar"}, + o: o, + }) + testcases = append(testcases, testcaseContainer{ + i: []string{"gocryptfs", "--exclude", "foo", "--exclude", "bar"}, + o: o, + }) + /* TODO BROKEN + testcases = append(testcases, testcaseContainer{ + i: []string{"gocryptfs", "--exclude", "foo", "-e", "bar"}, + o: o, + }) + */ + 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) + } + } +} -- cgit v1.2.3